001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment.cli;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Set;
023:
024: import org.apache.geronimo.common.DeploymentException;
025: import org.apache.geronimo.gbean.AbstractName;
026: import org.apache.geronimo.gbean.AbstractNameQuery;
027: import org.apache.geronimo.kernel.GBeanNotFoundException;
028: import org.apache.geronimo.kernel.Kernel;
029: import org.apache.geronimo.kernel.config.ConfigurationManager;
030: import org.apache.geronimo.kernel.config.ConfigurationStore;
031: import org.apache.geronimo.kernel.config.ConfigurationUtil;
032: import org.apache.geronimo.kernel.config.LifecycleException;
033: import org.apache.geronimo.kernel.config.NoSuchConfigException;
034: import org.apache.geronimo.kernel.config.PersistentConfigurationList;
035: import org.apache.geronimo.kernel.config.SwitchablePersistentConfigurationList;
036: import org.apache.geronimo.kernel.repository.Artifact;
037: import org.apache.geronimo.kernel.repository.ArtifactResolver;
038: import org.apache.geronimo.kernel.repository.MissingDependencyException;
039:
040: /**
041: * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
042: */
043: public class OfflineDeployerStarter {
044: private static final Artifact OFFLINE_DEPLOYER_ARTIFACT = new Artifact(
045: "org.apache.geronimo.framework", "offline-deployer",
046: (String) null, "car");
047:
048: private final Kernel kernel;
049: private final AbstractName onlineDeployerConfigurationManagerName;
050: private final ConfigurationManager onlineDeployerConfigurationManager;
051: private final Set<AbstractName> onlineDeployerConfigStores;
052:
053: public OfflineDeployerStarter(Kernel kernel) {
054: if (null == kernel) {
055: throw new IllegalArgumentException("kernel is required");
056: }
057: this .kernel = kernel;
058:
059: onlineDeployerConfigurationManagerName = ConfigurationUtil
060: .getConfigurationManagerName(kernel);
061: onlineDeployerConfigurationManager = ConfigurationUtil
062: .getConfigurationManager(kernel);
063: onlineDeployerConfigStores = kernel
064: .listGBeans(new AbstractNameQuery(
065: ConfigurationStore.class.getName()));
066: }
067:
068: public void start() throws DeploymentException {
069: try {
070: Artifact offlineDeployerArtifact = resolveOfflineDeployer();
071: startOfflineConfiguration(offlineDeployerArtifact);
072: startPersistentOfflineConfigurations(offlineDeployerArtifact);
073: stopOfflineConfigurationManager();
074: //stopOnlineConfigStores(); // If we stop the stores here, we are left with no stores to work with!!
075: enablePersistentConfigurationTracking();
076: } catch (Exception e) {
077: throw new DeploymentException(
078: "Unexpected error. Cannot start offline-deployer",
079: e);
080: }
081: onlineDeployerConfigurationManager.setOnline(false);
082: }
083:
084: protected Artifact resolveOfflineDeployer()
085: throws MissingDependencyException {
086: ArtifactResolver artifactResolver = onlineDeployerConfigurationManager
087: .getArtifactResolver();
088: return artifactResolver
089: .resolveInClassLoader(OFFLINE_DEPLOYER_ARTIFACT);
090: }
091:
092: protected void enablePersistentConfigurationTracking()
093: throws GBeanNotFoundException {
094: SwitchablePersistentConfigurationList switchableList = (SwitchablePersistentConfigurationList) kernel
095: .getGBean(SwitchablePersistentConfigurationList.class);
096: switchableList.setOnline(true);
097: }
098:
099: protected void stopOnlineConfigStores()
100: throws GBeanNotFoundException {
101: for (AbstractName configStoreName : onlineDeployerConfigStores) {
102: kernel.stopGBean(configStoreName);
103: }
104: }
105:
106: protected void stopOfflineConfigurationManager()
107: throws GBeanNotFoundException {
108: Set names = kernel.listGBeans(new AbstractNameQuery(
109: ConfigurationManager.class.getName()));
110: for (Iterator iterator = names.iterator(); iterator.hasNext();) {
111: AbstractName abstractName = (AbstractName) iterator.next();
112: if (!onlineDeployerConfigurationManagerName
113: .equals(abstractName)) {
114: kernel.stopGBean(abstractName);
115: }
116: }
117: }
118:
119: protected void startPersistentOfflineConfigurations(
120: Artifact offlineDeployerArtifact) throws Exception {
121: AbstractNameQuery query = new AbstractNameQuery(
122: offlineDeployerArtifact, Collections.EMPTY_MAP,
123: Collections.singleton(PersistentConfigurationList.class
124: .getName()));
125: Set configLists = kernel.listGBeans(query);
126:
127: List<Artifact> configs = new ArrayList<Artifact>();
128: for (Iterator i = configLists.iterator(); i.hasNext();) {
129: AbstractName configListName = (AbstractName) i.next();
130: configs.addAll((List<Artifact>) kernel.invoke(
131: configListName, "restore"));
132: }
133:
134: for (Artifact config : configs) {
135: onlineDeployerConfigurationManager
136: .loadConfiguration(config);
137: onlineDeployerConfigurationManager
138: .startConfiguration(config);
139: }
140: }
141:
142: protected void startOfflineConfiguration(
143: Artifact offlineDeployerArtifact)
144: throws NoSuchConfigException, LifecycleException {
145: onlineDeployerConfigurationManager
146: .loadConfiguration(offlineDeployerArtifact);
147: onlineDeployerConfigurationManager
148: .startConfiguration(offlineDeployerArtifact);
149: }
150:
151: }
|