Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.app.impl; |
| 17 | |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 20 | import com.google.common.collect.HashMultimap; |
| 21 | import com.google.common.collect.Multimap; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 22 | import com.google.common.util.concurrent.Uninterruptibles; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 23 | import org.apache.karaf.features.Feature; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 24 | import org.apache.karaf.features.FeaturesService; |
| 25 | import org.onosproject.app.ApplicationAdminService; |
| 26 | import org.onosproject.app.ApplicationEvent; |
| 27 | import org.onosproject.app.ApplicationListener; |
| 28 | import org.onosproject.app.ApplicationService; |
| 29 | import org.onosproject.app.ApplicationState; |
| 30 | import org.onosproject.app.ApplicationStore; |
| 31 | import org.onosproject.app.ApplicationStoreDelegate; |
| 32 | import org.onosproject.core.Application; |
| 33 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 34 | import org.onosproject.event.AbstractListenerManager; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 35 | import org.onosproject.security.Permission; |
| 36 | import org.onosproject.security.SecurityUtil; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | import org.osgi.service.component.annotations.Activate; |
| 38 | import org.osgi.service.component.annotations.Component; |
| 39 | import org.osgi.service.component.annotations.Deactivate; |
| 40 | import org.osgi.service.component.annotations.Reference; |
| 41 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 44 | import java.io.InputStream; |
| 45 | import java.util.Set; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 46 | import java.util.concurrent.CountDownLatch; |
| 47 | import java.util.concurrent.TimeUnit; |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 48 | import java.util.function.Consumer; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 51 | import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED; |
| 52 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
| 53 | import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED; |
| 54 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 55 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 56 | import static org.onosproject.security.AppPermission.Type.APP_READ; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 57 | import static org.slf4j.LoggerFactory.getLogger; |
| 58 | |
| 59 | /** |
| 60 | * Implementation of the application management service. |
| 61 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Component(immediate = true, service = {ApplicationService.class, ApplicationAdminService.class}) |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 63 | public class ApplicationManager |
| 64 | extends AbstractListenerManager<ApplicationEvent, ApplicationListener> |
| 65 | implements ApplicationService, ApplicationAdminService { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
| 69 | private static final String APP_ID_NULL = "Application ID cannot be null"; |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 70 | private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 2000; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 71 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 72 | private final ApplicationStoreDelegate delegate = new InternalStoreDelegate(); |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 75 | protected ApplicationStore store; |
| 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | protected FeaturesService featuresService; |
| 79 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 80 | // Application supplied hooks for pre-activation processing. |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 81 | private final Multimap<String, Runnable> deactivateHooks = HashMultimap.create(); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 82 | private final Cache<ApplicationId, CountDownLatch> pendingOperations = |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 83 | CacheBuilder.newBuilder() |
| 84 | .expireAfterWrite(DEFAULT_OPERATION_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS) |
| 85 | .build(); |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 86 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 89 | eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 90 | store.setDelegate(delegate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 91 | log.info("Started"); |
| 92 | } |
| 93 | |
| 94 | @Deactivate |
| 95 | public void deactivate() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 96 | eventDispatcher.removeSink(ApplicationEvent.class); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 97 | store.unsetDelegate(delegate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 98 | log.info("Stopped"); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Set<Application> getApplications() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 103 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 104 | return store.getApplications(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public ApplicationId getId(String name) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 109 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 110 | checkNotNull(name, "Name cannot be null"); |
| 111 | return store.getId(name); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Application getApplication(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 116 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 117 | checkNotNull(appId, APP_ID_NULL); |
| 118 | return store.getApplication(appId); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public ApplicationState getState(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 123 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 124 | checkNotNull(appId, APP_ID_NULL); |
| 125 | return store.getState(appId); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Set<Permission> getPermissions(ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 130 | checkPermission(APP_READ); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 131 | checkNotNull(appId, APP_ID_NULL); |
| 132 | return store.getPermissions(appId); |
| 133 | } |
| 134 | |
| 135 | @Override |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 136 | public void registerDeactivateHook(ApplicationId appId, Runnable hook) { |
| 137 | checkPermission(APP_READ); |
| 138 | checkNotNull(appId, APP_ID_NULL); |
| 139 | checkNotNull(hook, "Hook cannot be null"); |
| 140 | deactivateHooks.put(appId.name(), hook); |
| 141 | } |
| 142 | |
| 143 | @Override |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 144 | public Application install(InputStream appDescStream) { |
| 145 | checkNotNull(appDescStream, "Application archive stream cannot be null"); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 146 | Application app = store.create(appDescStream); |
| 147 | SecurityUtil.register(app.id()); |
| 148 | return app; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void uninstall(ApplicationId appId) { |
| 153 | checkNotNull(appId, APP_ID_NULL); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 154 | updateStoreAndWaitForNotificationHandling(appId, store::remove); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void activate(ApplicationId appId) { |
| 159 | checkNotNull(appId, APP_ID_NULL); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 160 | if (!SecurityUtil.isAppSecured(appId)) { |
| 161 | return; |
| 162 | } |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 163 | updateStoreAndWaitForNotificationHandling(appId, store::activate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void deactivate(ApplicationId appId) { |
| 168 | checkNotNull(appId, APP_ID_NULL); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 169 | updateStoreAndWaitForNotificationHandling(appId, store::deactivate); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 174 | checkNotNull(appId, APP_ID_NULL); |
| 175 | checkNotNull(permissions, "Permissions cannot be null"); |
| 176 | store.setPermissions(appId, permissions); |
| 177 | } |
| 178 | |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public InputStream getApplicationArchive(ApplicationId appId) { |
| 181 | checkNotNull(appId, APP_ID_NULL); |
| 182 | return store.getApplicationArchive(appId); |
| 183 | } |
| 184 | |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 185 | private void updateStoreAndWaitForNotificationHandling(ApplicationId appId, |
| 186 | Consumer<ApplicationId> storeUpdateTask) { |
| 187 | CountDownLatch latch = new CountDownLatch(1); |
| 188 | try { |
| 189 | pendingOperations.put(appId, latch); |
| 190 | storeUpdateTask.accept(appId); |
| 191 | } catch (Exception e) { |
| 192 | pendingOperations.invalidate(appId); |
| 193 | latch.countDown(); |
| 194 | log.warn("Failed to update store for {}", appId.name(), e); |
| 195 | } |
| 196 | Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 197 | } |
| 198 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 199 | private class InternalStoreDelegate implements ApplicationStoreDelegate { |
| 200 | @Override |
| 201 | public void notify(ApplicationEvent event) { |
| 202 | ApplicationEvent.Type type = event.type(); |
| 203 | Application app = event.subject(); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 204 | CountDownLatch latch = pendingOperations.getIfPresent(app.id()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 205 | try { |
| 206 | if (type == APP_ACTIVATED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 207 | if (installAppFeatures(app)) { |
| 208 | log.info("Application {} has been activated", app.id().name()); |
| 209 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 210 | |
| 211 | } else if (type == APP_DEACTIVATED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 212 | if (uninstallAppFeatures(app)) { |
| 213 | log.info("Application {} has been deactivated", app.id().name()); |
| 214 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 215 | |
| 216 | } else if (type == APP_INSTALLED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 217 | if (installAppArtifacts(app)) { |
| 218 | log.info("Application {} has been installed", app.id().name()); |
| 219 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 220 | |
| 221 | } else if (type == APP_UNINSTALLED) { |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 222 | if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) { |
| 223 | log.info("Application {} has been uninstalled", app.id().name()); |
| 224 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 225 | |
| 226 | } |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 227 | post(event); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 228 | } catch (Exception e) { |
| 229 | log.warn("Unable to perform operation on application " + app.id().name(), e); |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 230 | } finally { |
| 231 | if (latch != null) { |
| 232 | latch.countDown(); |
Madan Jampani | a0c4bdb | 2016-06-17 11:02:38 -0700 | [diff] [blame] | 233 | pendingOperations.invalidate(app.id()); |
Madan Jampani | 015d3a3 | 2016-06-16 18:25:11 -0700 | [diff] [blame] | 234 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Thomas Vachuska | 0d6af8d | 2015-03-23 15:54:47 -0700 | [diff] [blame] | 239 | // The following methods are fully synchronized to guard against remote vs. |
| 240 | // locally induced feature service interactions. |
| 241 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 242 | // Installs all feature repositories required by the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 243 | private synchronized boolean installAppArtifacts(Application app) throws Exception { |
| 244 | if (app.featuresRepo().isPresent() && |
| 245 | featuresService.getRepository(app.featuresRepo().get()) == null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 246 | featuresService.addRepository(app.featuresRepo().get()); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 247 | return true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 248 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 249 | return false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 252 | // Uninstalls all the feature repositories required by the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 253 | private synchronized boolean uninstallAppArtifacts(Application app) throws Exception { |
| 254 | if (app.featuresRepo().isPresent() && |
| 255 | featuresService.getRepository(app.featuresRepo().get()) != null) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 256 | featuresService.removeRepository(app.featuresRepo().get()); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 257 | return true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 258 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 259 | return false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 262 | // Installs all features that define the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 263 | private synchronized boolean installAppFeatures(Application app) throws Exception { |
| 264 | boolean changed = false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 265 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 266 | Feature feature = featuresService.getFeature(name); |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 267 | |
| 268 | // If we see an attempt at activation of a non-existent feature |
| 269 | // attempt to install the app artifacts first and then retry. |
| 270 | // This can be triggered by a race condition between different ONOS |
| 271 | // instances "installing" the apps from disk at their own pace. |
| 272 | // Perhaps there is a more elegant solution to be explored in the |
| 273 | // future. |
| 274 | if (feature == null) { |
| 275 | installAppArtifacts(app); |
| 276 | feature = featuresService.getFeature(name); |
| 277 | } |
| 278 | |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 279 | if (feature != null && !featuresService.isInstalled(feature)) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 280 | featuresService.installFeature(name); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 281 | changed = true; |
Thomas Vachuska | b56b917 | 2015-11-24 11:24:23 -0800 | [diff] [blame] | 282 | } else if (feature == null) { |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 283 | log.warn("Feature {} not found", name); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 284 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 285 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 286 | return changed; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 289 | // Uninstalls all features that define the specified app. |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 290 | private synchronized boolean uninstallAppFeatures(Application app) throws Exception { |
| 291 | boolean changed = false; |
Jonathan Hart | c32585f | 2016-06-16 14:01:27 -0700 | [diff] [blame] | 292 | deactivateHooks.removeAll(app.id().name()).forEach(hook -> invokeHook(hook, app.id())); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 293 | for (String name : app.features()) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 294 | Feature feature = featuresService.getFeature(name); |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 295 | if (feature != null && featuresService.isInstalled(feature)) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 296 | featuresService.uninstallFeature(name); |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 297 | changed = true; |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 298 | } else if (feature == null) { |
| 299 | log.warn("Feature {} not found", name); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 300 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 301 | } |
Thomas Vachuska | 9ff88a9 | 2015-05-13 13:57:18 -0700 | [diff] [blame] | 302 | return changed; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 305 | // Invokes the specified function, if not null. |
Ray Milkey | aef4585 | 2016-01-11 17:13:19 -0800 | [diff] [blame] | 306 | @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run() |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 307 | private void invokeHook(Runnable hook, ApplicationId appId) { |
| 308 | if (hook != null) { |
| 309 | try { |
| 310 | hook.run(); |
| 311 | } catch (Exception e) { |
| 312 | log.warn("Deactivate hook for application {} encountered an error", |
| 313 | appId.name(), e); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 318 | } |