| 1 17 package org.apache.geronimo.kernel.config; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.LinkedHashMap ; 25 import java.util.LinkedHashSet ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.HashSet ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.geronimo.gbean.AbstractName; 35 import org.apache.geronimo.kernel.management.State; 36 import org.apache.geronimo.kernel.repository.Artifact; 37 import org.apache.geronimo.kernel.repository.ArtifactResolver; 38 import org.apache.geronimo.kernel.repository.Dependency; 39 import org.apache.geronimo.kernel.repository.Environment; 40 import org.apache.geronimo.kernel.repository.ImportType; 41 import org.apache.geronimo.kernel.repository.MissingDependencyException; 42 import org.apache.geronimo.kernel.repository.Version; 43 44 47 public class SimpleConfigurationManager implements ConfigurationManager { 48 protected static final Log log = LogFactory.getLog(SimpleConfigurationManager.class); 49 protected final Collection stores; 50 private final ArtifactResolver artifactResolver; 51 protected final Map configurations = new LinkedHashMap (); 52 protected final ConfigurationModel configurationModel = new ConfigurationModel(); 53 protected final Collection repositories; 54 protected final Collection watchers; 55 56 62 private Configuration reloadingConfiguration; 63 64 65 public SimpleConfigurationManager(Collection stores, ArtifactResolver artifactResolver, Collection repositories) { 66 this(stores, artifactResolver, repositories, Collections.EMPTY_SET); 67 } 68 69 public SimpleConfigurationManager(Collection stores, ArtifactResolver artifactResolver, Collection repositories, Collection watchers) { 70 if (stores == null) stores = Collections.EMPTY_SET; 71 if (repositories == null) repositories = Collections.EMPTY_SET; 72 if (watchers == null) watchers = Collections.EMPTY_SET; 73 74 this.stores = stores; 75 this.artifactResolver = artifactResolver; 76 this.repositories = repositories; 77 this.watchers = watchers; 78 } 79 80 public synchronized boolean isInstalled(Artifact configId) { 81 if(!configId.isResolved()) { 82 throw new IllegalArgumentException ("Artifact "+configId+" is not fully resolved"); 83 } 84 List storeSnapshot = getStoreList(); 85 for (int i = 0; i < storeSnapshot.size(); i++) { 86 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 87 if(store.containsConfiguration(configId)) { 88 return true; 89 } 90 } 91 return false; 92 } 93 94 public synchronized boolean isLoaded(Artifact configId) { 95 if(!configId.isResolved()) { 96 throw new IllegalArgumentException ("Artifact "+configId+" is not fully resolved"); 97 } 98 if(reloadingConfiguration != null && reloadingConfiguration.getId().equals(configId)) { 99 return true; 100 } 101 return configurationModel.isLoaded(configId); 102 } 103 104 public synchronized boolean isRunning(Artifact configId) { 105 if(!configId.isResolved()) { 106 throw new IllegalArgumentException ("Artifact "+configId+" is not fully resolved"); 107 } 108 return configurationModel.isStarted(configId); 109 } 110 111 public Artifact[] getInstalled(Artifact query) { 112 Artifact[] all = artifactResolver.queryArtifacts(query); 113 List configs = new ArrayList (); 114 for (int i = 0; i < all.length; i++) { 115 Artifact artifact = all[i]; 116 if(isConfiguration(artifact)) { 117 configs.add(artifact); 118 } 119 } 120 if(configs.size() == all.length) { 121 return all; 122 } 123 return (Artifact[]) configs.toArray(new Artifact[configs.size()]); 124 } 125 126 public Artifact[] getLoaded(Artifact query) { 127 return configurationModel.getLoaded(query); 128 } 129 130 public Artifact[] getRunning(Artifact query) { 131 return configurationModel.getStarted(query); 132 } 133 134 135 public List listStores() { 136 List storeSnapshot = getStoreList(); 137 List result = new ArrayList (storeSnapshot.size()); 138 for (int i = 0; i < storeSnapshot.size(); i++) { 139 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 140 result.add(store.getAbstractName()); 141 } 142 return result; 143 } 144 145 public ConfigurationStore[] getStores() { 146 List storeSnapshot = getStoreList(); 147 return (ConfigurationStore[]) storeSnapshot.toArray(new ConfigurationStore[storeSnapshot.size()]); 148 } 149 150 public Collection getRepositories() { 151 return repositories; 152 } 153 154 public List listConfigurations() { 155 List storeSnapshot = getStoreList(); 156 List list = new ArrayList (); 157 for (int i = 0; i < storeSnapshot.size(); i++) { 158 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 159 list.addAll(listConfigurations(store)); 160 } 161 return list; 162 } 163 164 public ConfigurationStore getStoreForConfiguration(Artifact configId) { 165 if(!configId.isResolved()) { 166 throw new IllegalArgumentException ("Artifact "+configId+" is not fully resolved"); 167 } 168 List storeSnapshot = getStoreList(); 169 for (int i = 0; i < storeSnapshot.size(); i++) { 170 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 171 if(store.containsConfiguration(configId)) { 172 return store; 173 } 174 } 175 return null; 176 } 177 178 public List listConfigurations(AbstractName storeName) throws NoSuchStoreException { 179 List storeSnapshot = getStoreList(); 180 for (int i = 0; i < storeSnapshot.size(); i++) { 181 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 182 if (storeName.equals(store.getAbstractName())) { 183 return listConfigurations(store); 184 } 185 } 186 throw new NoSuchStoreException("No such store: " + storeName); 187 } 188 189 private List listConfigurations(ConfigurationStore store) { 190 List list = store.listConfigurations(); 191 for (ListIterator iterator = list.listIterator(); iterator.hasNext();) { 192 ConfigurationInfo configurationInfo = (ConfigurationInfo) iterator.next(); 193 if (isRunning(configurationInfo.getConfigID())) { 194 configurationInfo = new ConfigurationInfo(store.getAbstractName(), 195 configurationInfo.getConfigID(), 196 configurationInfo.getType(), 197 configurationInfo.getCreated(), 198 configurationInfo.getOwnedConfigurations(), 199 configurationInfo.getChildConfigurations(), 200 configurationInfo.getInPlaceLocation(), 201 State.RUNNING); 202 } else { 203 configurationInfo = new ConfigurationInfo(store.getAbstractName(), 204 configurationInfo.getConfigID(), 205 configurationInfo.getType(), 206 configurationInfo.getCreated(), 207 configurationInfo.getOwnedConfigurations(), 208 configurationInfo.getChildConfigurations(), 209 configurationInfo.getInPlaceLocation(), 210 State.STOPPED); 211 } 212 iterator.set(configurationInfo); 213 } 214 return list; 215 } 216 217 public boolean isConfiguration(Artifact artifact) { 218 if(!artifact.isResolved()) { 219 throw new IllegalArgumentException ("Artifact "+artifact+" is not fully resolved"); 220 } 221 synchronized (this) { 222 if (configurations.containsKey(artifact)) { 224 return true; 225 } 226 } 227 228 List storeSnapshot = getStoreList(); 230 for (int i = 0; i < storeSnapshot.size(); i++) { 231 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 232 if (store.containsConfiguration(artifact)) { 233 return true; 234 } 235 } 236 return false; 237 } 238 239 public synchronized Configuration getConfiguration(Artifact configurationId) { 240 if(!configurationId.isResolved()) { 241 throw new IllegalArgumentException ("Artifact "+configurationId+" is not fully resolved"); 242 } 243 if(reloadingConfiguration != null && reloadingConfiguration.getId().equals(configurationId)) { 244 return reloadingConfiguration; 245 } 246 return (Configuration) configurations.get(configurationId); 247 } 248 249 public synchronized LifecycleResults loadConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException { 250 return loadConfiguration(configurationId, NullLifecycleMonitor.INSTANCE); 251 } 252 253 public synchronized LifecycleResults loadConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 254 if(!configurationId.isResolved()) { 255 throw new IllegalArgumentException ("Artifact "+configurationId+" is not fully resolved"); 256 } 257 if (isLoaded(configurationId)) { 258 load(configurationId); 260 261 monitor.finished(); 262 return new LifecycleResults(); 263 } 264 265 ConfigurationData configurationData = null; 267 try { 268 configurationData = loadConfigurationData(configurationId, monitor); 269 } catch (Exception e) { 270 monitor.finished(); 271 throw new LifecycleException("load", configurationId, e); 272 } 273 274 LifecycleResults results = loadConfiguration(configurationData, monitor); 276 277 return results; 278 } 279 280 public synchronized LifecycleResults loadConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, LifecycleException { 281 return loadConfiguration(configurationData, NullLifecycleMonitor.INSTANCE); 282 } 283 284 public synchronized LifecycleResults loadConfiguration(ConfigurationData configurationData, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 285 Artifact id = configurationData.getId(); 286 LifecycleResults results = new LifecycleResults(); 287 if (!isLoaded(id)) { 288 LinkedHashMap configurationsToLoad = new LinkedHashMap (); 290 try { 291 loadDepthFirst(configurationData, configurationsToLoad, monitor); 292 } catch (Exception e) { 293 monitor.finished(); 294 throw new LifecycleException("load", id, e); 295 } 296 297 Map actuallyLoaded = new LinkedHashMap (configurationsToLoad.size()); 299 Artifact configurationId = null; 300 try { 301 for (Iterator iterator = configurationsToLoad.entrySet().iterator(); iterator.hasNext();) { 302 Map.Entry entry = (Map.Entry ) iterator.next(); 303 configurationId = (Artifact) entry.getKey(); 304 UnloadedConfiguration unloadedConfiguration = (UnloadedConfiguration) entry.getValue(); 305 306 monitor.loading(configurationId); 307 Configuration configuration = load(unloadedConfiguration.getConfigurationData(), unloadedConfiguration.getResolvedParentIds(), actuallyLoaded); 308 monitor.succeeded(configurationId); 309 310 actuallyLoaded.put(configurationId, configuration); 311 } 312 } catch (Exception e) { 313 monitor.failed(configurationId, e); 314 315 for (Iterator iterator = actuallyLoaded.values().iterator(); iterator.hasNext();) { 317 Configuration configuration = (Configuration) iterator.next(); 318 unload(configuration); 319 } 320 321 monitor.finished(); 322 throw new LifecycleException("load", id, e); 323 } 324 325 addNewConfigurationsToModel(actuallyLoaded); 327 results.setLoaded(actuallyLoaded.keySet()); 328 } 329 load(id); 330 monitor.finished(); 331 return results; 332 } 333 334 protected void load(Artifact configurationId) throws NoSuchConfigException { 335 configurationModel.load(configurationId); 336 } 337 338 protected Configuration load(ConfigurationData configurationData, LinkedHashSet resolvedParentIds, Map loadedConfigurations) throws InvalidConfigException { 339 Artifact configurationId = configurationData.getId(); 340 try { 341 Collection parents = findParentConfigurations(resolvedParentIds, loadedConfigurations); 342 343 Configuration configuration = new Configuration(parents, configurationData, new ConfigurationResolver(configurationData, repositories, artifactResolver), null); 344 configuration.doStart(); 345 return configuration; 346 } catch (Exception e) { 347 throw new InvalidConfigException("Error starting configuration gbean " + configurationId, e); 348 } 349 } 350 351 private Collection findParentConfigurations(LinkedHashSet resolvedParentIds, Map loadedConfigurations) throws InvalidConfigException { 352 LinkedHashMap parents = new LinkedHashMap (); 353 for (Iterator iterator = resolvedParentIds.iterator(); iterator.hasNext();) { 354 Artifact resolvedArtifact = (Artifact) iterator.next(); 355 356 Configuration parent = null; 357 if (loadedConfigurations.containsKey(resolvedArtifact)) { 358 parent = (Configuration) loadedConfigurations.get(resolvedArtifact); 359 } else if (isLoaded(resolvedArtifact)) { 360 parent = getConfiguration(resolvedArtifact); 361 } else { 362 throw new InvalidConfigException("Cound not find parent configuration: " + resolvedArtifact); 363 } 364 365 parents.put(resolvedArtifact, parent); 366 } 367 return parents.values(); 368 } 369 370 private void addNewConfigurationsToModel(Map loadedConfigurations) throws NoSuchConfigException { 371 for (Iterator iterator = loadedConfigurations.values().iterator(); iterator.hasNext();) { 372 Configuration configuration = (Configuration) iterator.next(); 373 addNewConfigurationToModel(configuration); 374 } 375 } 376 377 protected void addNewConfigurationToModel(Configuration configuration) throws NoSuchConfigException { 378 configurationModel.addConfiguation(configuration.getId(), 379 getConfigurationIds(getLoadParents(configuration)), 380 getConfigurationIds(getStartParents(configuration))); 381 configurations.put(configuration.getId(), configuration); 382 } 383 384 protected LinkedHashSet getLoadParents(Configuration configuration) { 385 LinkedHashSet loadParent = new LinkedHashSet (configuration.getClassParents()); 386 for (Iterator iterator = configuration.getChildren().iterator(); iterator.hasNext();) { 387 Configuration childConfiguration = (Configuration) iterator.next(); 388 LinkedHashSet childLoadParent = getLoadParents(childConfiguration); 389 390 childLoadParent.remove(configuration); 392 393 loadParent.addAll(childLoadParent); 394 } 395 return loadParent; 396 } 397 398 protected LinkedHashSet getStartParents(Configuration configuration) { 399 LinkedHashSet startParent = new LinkedHashSet (configuration.getServiceParents()); 400 for (Iterator iterator = configuration.getChildren().iterator(); iterator.hasNext();) { 401 Configuration childConfiguration = (Configuration) iterator.next(); 402 LinkedHashSet childStartParent = getStartParents(childConfiguration); 403 404 childStartParent.remove(configuration); 406 407 startParent.addAll(childStartParent); 408 } 409 return startParent; 410 } 411 412 private static LinkedHashSet getConfigurationIds(Collection configurations) { 413 LinkedHashSet configurationIds = new LinkedHashSet (configurations.size()); 414 for (Iterator iterator = configurations.iterator(); iterator.hasNext();) { 415 Configuration configuration = (Configuration) iterator.next(); 416 configurationIds.add(configuration.getId()); 417 } 418 return configurationIds; 419 } 420 421 private synchronized void loadDepthFirst(ConfigurationData configurationData, LinkedHashMap configurationsToLoad, LifecycleMonitor monitor) throws NoSuchConfigException, IOException , InvalidConfigException, MissingDependencyException { 422 Artifact configurationId = configurationData.getId(); 424 if (!configurationsToLoad.containsKey(configurationId)) { 425 LinkedHashSet resolvedParentIds = resolveParentIds(configurationData); 426 427 for (Iterator iterator = resolvedParentIds.iterator(); iterator.hasNext();) { 428 Artifact parentId = (Artifact) iterator.next(); 429 if (!isLoaded(parentId) && isConfiguration(parentId)) { 431 ConfigurationData parentConfigurationData = loadConfigurationData(parentId, monitor); 432 loadDepthFirst(parentConfigurationData, configurationsToLoad, monitor); 433 } 434 } 435 436 configurationsToLoad.put(configurationId, new UnloadedConfiguration(configurationData, resolvedParentIds)); 438 } 439 } 440 441 private ConfigurationData loadConfigurationData(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, IOException , InvalidConfigException { 442 List storeSnapshot = getStoreList(); 443 444 monitor.addConfiguration(configurationId); 445 monitor.reading(configurationId); 446 for (int i = 0; i < storeSnapshot.size(); i++) { 447 ConfigurationStore store = (ConfigurationStore) storeSnapshot.get(i); 448 if (store.containsConfiguration(configurationId)) { 449 ConfigurationData configurationData = store.loadConfiguration(configurationId); 450 monitor.succeeded(configurationId); 451 return configurationData; 452 } 453 } 454 NoSuchConfigException exception = new NoSuchConfigException(configurationId); 455 monitor.failed(configurationId, exception); 456 throw exception; 457 } 458 459 private LinkedHashSet resolveParentIds(ConfigurationData configurationData) throws MissingDependencyException, InvalidConfigException { 460 Environment environment = configurationData.getEnvironment(); 461 462 LinkedHashSet parentIds = new LinkedHashSet (); 463 List dependencies = new ArrayList (environment.getDependencies()); 464 for (ListIterator iterator = dependencies.listIterator(); iterator.hasNext();) { 465 Dependency dependency = (Dependency) iterator.next(); 466 Artifact resolvedArtifact = artifactResolver.resolveInClassLoader(dependency.getArtifact()); 467 if (isConfiguration(resolvedArtifact)) { 468 parentIds.add(resolvedArtifact); 469 470 dependency = new Dependency(resolvedArtifact, dependency.getImportType()); 472 iterator.set(dependency); 473 } else if (dependency.getImportType() == ImportType.SERVICES) { 474 throw new InvalidConfigException("Dependency does not have services: " + resolvedArtifact); 476 } 477 } 478 479 for (Iterator iterator = configurationData.getChildConfigurations().values().iterator(); iterator.hasNext();) { 480 ConfigurationData childConfigurationData = (ConfigurationData) iterator.next(); 481 LinkedHashSet childParentIds = resolveParentIds(childConfigurationData); 482 childParentIds.remove(configurationData.getId()); 484 parentIds.addAll(childParentIds); 485 } 486 return parentIds; 487 } 488 489 private static class UnloadedConfiguration { 490 private final ConfigurationData configurationData; 491 private final LinkedHashSet resolvedParentIds; 492 493 public UnloadedConfiguration(ConfigurationData configurationData, LinkedHashSet resolvedParentIds) { 494 this.configurationData = configurationData; 495 this.resolvedParentIds = resolvedParentIds; 496 } 497 498 public ConfigurationData getConfigurationData() { 499 return configurationData; 500 } 501 502 public LinkedHashSet getResolvedParentIds() { 503 return resolvedParentIds; 504 } 505 } 506 507 public synchronized LifecycleResults startConfiguration(Artifact id) throws NoSuchConfigException, LifecycleException { 508 return startConfiguration(id, NullLifecycleMonitor.INSTANCE); 509 } 510 511 public synchronized LifecycleResults startConfiguration(Artifact id, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 512 if(!id.isResolved()) { 513 throw new IllegalArgumentException ("Artifact "+id+" is not fully resolved"); 514 } 515 LinkedHashSet unstartedConfigurations = configurationModel.start(id); 516 517 addConfigurationsToMonitor(monitor, unstartedConfigurations); 518 519 LifecycleResults results = new LifecycleResults(); 520 Artifact configurationId = null; 521 try { 522 for (Iterator iterator = unstartedConfigurations.iterator(); iterator.hasNext();) { 523 configurationId = (Artifact) iterator.next(); 524 Configuration configuration = getConfiguration(configurationId); 525 526 monitor.starting(configurationId); 527 start(configuration); 528 monitor.succeeded(configurationId); 529 530 results.addStarted(configurationId); 531 } 532 } catch (Exception e) { 533 monitor.failed(configurationId, e); 534 configurationModel.stop(id); 535 536 for (Iterator iterator = results.getStarted().iterator(); iterator.hasNext();) { 537 configurationId = (Artifact) iterator.next(); 538 Configuration configuration = getConfiguration(configurationId); 539 monitor.stopping(configurationId); 540 stop(configuration); 541 monitor.succeeded(configurationId); 542 } 543 monitor.finished(); 544 throw new LifecycleException("start", id, e); 545 } 546 monitor.finished(); 547 return results; 548 } 549 550 protected void start(Configuration configuration) throws Exception { 551 throw new UnsupportedOperationException (); 552 } 553 554 public synchronized LifecycleResults stopConfiguration(Artifact id) throws NoSuchConfigException { 555 return stopConfiguration(id, NullLifecycleMonitor.INSTANCE); 556 } 557 558 public synchronized LifecycleResults stopConfiguration(Artifact id, LifecycleMonitor monitor) throws NoSuchConfigException { 559 if(!id.isResolved()) { 560 throw new IllegalArgumentException ("Artifact "+id+" is not fully resolved"); 561 } 562 LinkedHashSet stopList = configurationModel.stop(id); 563 564 addConfigurationsToMonitor(monitor, stopList); 565 566 LifecycleResults results = new LifecycleResults(); 567 for (Iterator iterator = stopList.iterator(); iterator.hasNext();) { 568 Artifact configurationId = (Artifact) iterator.next(); 569 Configuration configuration = getConfiguration(configurationId); 570 571 monitor.stopping(configurationId); 572 stop(configuration); 573 monitor.succeeded(configurationId); 574 575 results.addStopped(configurationId); 576 } 577 578 monitor.finished(); 579 return results; 580 } 581 582 protected void stop(Configuration configuration) { 583 } 586 587 public synchronized LifecycleResults restartConfiguration(Artifact id) throws NoSuchConfigException, LifecycleException { 588 return restartConfiguration(id, NullLifecycleMonitor.INSTANCE); 589 } 590 591 public synchronized LifecycleResults restartConfiguration(Artifact id, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 592 if(!id.isResolved()) { 593 throw new IllegalArgumentException ("Artifact "+id+" is not fully resolved"); 594 } 595 LinkedHashSet restartList = configurationModel.restart(id); 597 598 addConfigurationsToMonitor(monitor, restartList); 599 600 LifecycleResults results = new LifecycleResults(); 602 for (Iterator iterator = restartList.iterator(); iterator.hasNext();) { 603 Artifact configurationId = (Artifact) iterator.next(); 604 Configuration configuration = getConfiguration(configurationId); 605 monitor.stopping(configurationId); 606 stop(configuration); 607 monitor.succeeded(configurationId); 608 results.addStopped(configurationId); 609 } 610 611 restartList = reverse(restartList); 613 614 Set skip = new HashSet (); 616 for (Iterator iterator = restartList.iterator(); iterator.hasNext();) { 617 Artifact configurationId = (Artifact) iterator.next(); 618 619 &nbs
|