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.plugin.factories;
019    
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.Set;
023    
024    import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
025    import javax.enterprise.deploy.spi.factories.DeploymentFactory;
026    import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
027    
028    import org.apache.geronimo.deployment.ModuleConfigurer;
029    import org.apache.geronimo.deployment.plugin.jmx.RemoteDeploymentManager;
030    import org.apache.geronimo.gbean.AbstractName;
031    import org.apache.geronimo.gbean.AbstractNameQuery;
032    import org.apache.geronimo.gbean.GBeanInfo;
033    import org.apache.geronimo.gbean.GBeanInfoBuilder;
034    import org.apache.geronimo.kernel.GBeanNotFoundException;
035    import org.apache.geronimo.kernel.Kernel;
036    
037    /**
038     *
039     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
040     */
041    public class DeploymentFactoryWithKernel extends BaseDeploymentFactory {
042        private final Kernel kernel;
043        private final RemoteDeploymentManager remoteDeploymentManager;
044        
045        public DeploymentFactoryWithKernel(Kernel kernel) {
046            this(kernel, null);
047        }
048    
049        public DeploymentFactoryWithKernel(Kernel kernel, RemoteDeploymentManager remoteDeploymentManager) {
050            if (null == kernel) {
051                throw new IllegalArgumentException("kernel is required");
052            }
053            this.kernel = kernel;
054            this.remoteDeploymentManager = remoteDeploymentManager;
055            DeploymentFactoryManager.getInstance().registerDeploymentFactory(this);
056        }
057        
058        protected Collection<ModuleConfigurer> getModuleConfigurers() throws DeploymentManagerCreationException {
059            Collection<ModuleConfigurer> moduleConfigurers = new ArrayList<ModuleConfigurer>();
060            Set<AbstractName> configurerNames = kernel.listGBeans(new AbstractNameQuery(ModuleConfigurer.class.getName()));
061            for (AbstractName configurerName : configurerNames) {
062                try {
063                    ModuleConfigurer configurer = (ModuleConfigurer) kernel.getGBean(configurerName);
064                    moduleConfigurers.add(configurer);
065                } catch (GBeanNotFoundException e) {
066                    throw (AssertionError)new AssertionError("No gbean found for name returned in query : " + configurerName).initCause(e);
067                }
068            }
069            return moduleConfigurers;
070        }
071        
072        protected RemoteDeploymentManager getRemoteDeploymentManager() throws DeploymentManagerCreationException {
073            if (remoteDeploymentManager != null) {
074                return remoteDeploymentManager;
075            }
076            try {
077                return (RemoteDeploymentManager) kernel.getGBean(RemoteDeploymentManager.class);
078            } catch (Exception e) {
079                throw (DeploymentManagerCreationException) new DeploymentManagerCreationException("See nested").initCause(e);
080            }
081        }
082    
083        public static final GBeanInfo GBEAN_INFO;
084        
085        static {
086            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DeploymentFactoryWithKernel.class, "DeploymentFactory");
087    
088            infoBuilder.addInterface(DeploymentFactory.class);
089            
090            infoBuilder.setConstructor(new String[] {"kernel"});
091            
092            GBEAN_INFO = infoBuilder.getBeanInfo();
093        }
094        
095        public static GBeanInfo getGBeanInfo() {
096            return GBEAN_INFO;
097        }
098    
099    }