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