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     */
017    
018    package org.apache.geronimo.deployment.cli;
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Set;
025    
026    import org.apache.geronimo.common.DeploymentException;
027    import org.apache.geronimo.gbean.AbstractName;
028    import org.apache.geronimo.gbean.AbstractNameQuery;
029    import org.apache.geronimo.kernel.GBeanNotFoundException;
030    import org.apache.geronimo.kernel.Kernel;
031    import org.apache.geronimo.kernel.config.ConfigurationManager;
032    import org.apache.geronimo.kernel.config.ConfigurationStore;
033    import org.apache.geronimo.kernel.config.ConfigurationUtil;
034    import org.apache.geronimo.kernel.config.LifecycleException;
035    import org.apache.geronimo.kernel.config.NoSuchConfigException;
036    import org.apache.geronimo.kernel.config.PersistentConfigurationList;
037    import org.apache.geronimo.kernel.config.SwitchablePersistentConfigurationList;
038    import org.apache.geronimo.kernel.repository.Artifact;
039    import org.apache.geronimo.kernel.repository.ArtifactResolver;
040    import org.apache.geronimo.kernel.repository.MissingDependencyException;
041    
042    /**
043     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
044     */
045    public class OfflineDeployerStarter {
046        private static final Artifact OFFLINE_DEPLOYER_ARTIFACT = new Artifact("org.apache.geronimo.configs",
047                "offline-deployer",
048                (String) null,
049                "car");
050        
051        private final Kernel kernel;
052        private final AbstractName onlineDeployerConfigurationManagerName;
053        private final ConfigurationManager onlineDeployerConfigurationManager;
054        private final Set<AbstractName> onlineDeployerConfigStores;
055    
056        public OfflineDeployerStarter(Kernel kernel) {
057            if (null == kernel) {
058                throw new IllegalArgumentException("kernel is required");
059            }
060            this.kernel = kernel;
061            
062            onlineDeployerConfigurationManagerName = ConfigurationUtil.getConfigurationManagerName(kernel);
063            onlineDeployerConfigurationManager = ConfigurationUtil.getConfigurationManager(kernel);
064            onlineDeployerConfigStores = kernel.listGBeans(new AbstractNameQuery(ConfigurationStore.class.getName()));
065        }
066        
067        public void start() throws DeploymentException {
068            try {
069                Artifact offlineDeployerArtifact = resolveOfflineDeployer();
070                startOfflineConfiguration(offlineDeployerArtifact);
071                startPersistentOfflineConfigurations(offlineDeployerArtifact);
072                stopOfflineConfigurationManager();
073                stopOnlineConfigStores();
074                enablePersistentConfigurationTracking();
075            } catch (Exception e) {
076                throw new DeploymentException("Unexpected error. Cannot start offline-deployer", e);
077            }
078            onlineDeployerConfigurationManager.setOnline(false);
079        }
080    
081        protected Artifact resolveOfflineDeployer() throws MissingDependencyException {
082            ArtifactResolver artifactResolver = onlineDeployerConfigurationManager.getArtifactResolver();
083            return artifactResolver.resolveInClassLoader(OFFLINE_DEPLOYER_ARTIFACT);
084        }
085        
086        protected void enablePersistentConfigurationTracking() throws GBeanNotFoundException {
087            SwitchablePersistentConfigurationList switchableList = (SwitchablePersistentConfigurationList) kernel.getGBean(SwitchablePersistentConfigurationList.class);
088            switchableList.setOnline(true);
089        }
090    
091        protected void stopOnlineConfigStores() throws GBeanNotFoundException {
092            for (AbstractName configStoreName : onlineDeployerConfigStores) {
093                kernel.stopGBean(configStoreName);
094            }
095        }
096    
097        protected void stopOfflineConfigurationManager() throws GBeanNotFoundException {
098            Set names = kernel.listGBeans(new AbstractNameQuery(ConfigurationManager.class.getName()));
099            for (Iterator iterator = names.iterator(); iterator.hasNext();) {
100                AbstractName abstractName = (AbstractName) iterator.next();
101                if (!onlineDeployerConfigurationManagerName.equals(abstractName)) {
102                    kernel.stopGBean(abstractName);
103                }
104            }
105        }
106    
107        protected void startPersistentOfflineConfigurations(Artifact offlineDeployerArtifact) throws Exception {
108            AbstractNameQuery query = new AbstractNameQuery(offlineDeployerArtifact, 
109                    Collections.EMPTY_MAP, 
110                    Collections.singleton(PersistentConfigurationList.class.getName()));
111            Set configLists = kernel.listGBeans(query);
112    
113            List<Artifact> configs = new ArrayList<Artifact>();
114            for (Iterator i = configLists.iterator(); i.hasNext();) {
115                AbstractName configListName = (AbstractName) i.next();
116                configs.addAll((List<Artifact>) kernel.invoke(configListName, "restore"));
117            }
118            
119            for (Artifact config : configs) {
120                onlineDeployerConfigurationManager.loadConfiguration(config);
121                onlineDeployerConfigurationManager.startConfiguration(config);
122            }
123        }
124    
125        protected void startOfflineConfiguration(Artifact offlineDeployerArtifact) throws NoSuchConfigException, LifecycleException {
126            onlineDeployerConfigurationManager.loadConfiguration(offlineDeployerArtifact);
127            onlineDeployerConfigurationManager.startConfiguration(offlineDeployerArtifact);
128        }
129    
130    }