001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.console.car;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    import java.util.Set;
026    
027    import javax.enterprise.deploy.spi.DeploymentManager;
028    import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
029    import javax.enterprise.deploy.spi.factories.DeploymentFactory;
030    import javax.portlet.PortletRequest;
031    import javax.portlet.PortletSession;
032    
033    import org.apache.geronimo.console.util.PortletManager;
034    import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryWithKernel;
035    import org.apache.geronimo.gbean.AbstractName;
036    import org.apache.geronimo.gbean.AbstractNameQuery;
037    import org.apache.geronimo.kernel.GBeanNotFoundException;
038    import org.apache.geronimo.kernel.Kernel;
039    import org.apache.geronimo.system.plugin.PluginInstaller;
040    import org.apache.geronimo.system.plugin.PluginRepositoryList;
041    import org.apache.geronimo.system.plugin.ServerArchiver;
042    
043    /**
044     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
045     */
046    public class ManagementHelper {
047    
048        private final static String PLUGIN_HELPER_KEY = "org.apache.geronimo.console.PluginManagementHelper";
049        private final Kernel kernel;
050        private PluginInstaller pluginInstaller;
051        private ServerArchiver archiver;
052        private List<PluginRepositoryList> pluginRepositoryLists;
053    
054        public static ManagementHelper getManagementHelper(PortletRequest request) {
055            ManagementHelper helper = (ManagementHelper) request.getPortletSession(true).getAttribute(PLUGIN_HELPER_KEY, PortletSession.APPLICATION_SCOPE);
056            if (helper == null) {
057                Kernel kernel = PortletManager.getKernel();
058                helper = new ManagementHelper(kernel);
059                request.getPortletSession().setAttribute(PLUGIN_HELPER_KEY, helper, PortletSession.APPLICATION_SCOPE);
060            }
061            return helper;
062        }
063    
064        public ManagementHelper(Kernel kernel) {
065            this.kernel = kernel;
066        }
067    
068        public PluginInstaller getPluginInstaller() {
069            if (pluginInstaller == null) {
070                Set<AbstractName> pluginInstallers = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
071                if (pluginInstallers.size() == 0) {
072                    throw new IllegalStateException("No plugin installer registered");
073                }
074                try {
075                    pluginInstaller = (PluginInstaller) kernel.getGBean(pluginInstallers.iterator().next());
076                } catch (GBeanNotFoundException e) {
077                    throw new IllegalStateException("Plugin installer cannot be retrieved from kernel");
078                }
079            }
080            return pluginInstaller;
081        }
082    
083        public ServerArchiver getArchiver() {
084            if (archiver == null) {
085                Set<AbstractName> archivers = kernel.listGBeans(new AbstractNameQuery(ServerArchiver.class.getName()));
086                if (archivers.size() == 0) {
087                    throw new IllegalStateException("No plugin installer registered");
088                }
089                try {
090                    archiver = (ServerArchiver) kernel.getGBean(archivers.iterator().next());
091                } catch (GBeanNotFoundException e) {
092                    throw new IllegalStateException("Plugin installer cannot be retrieved from kernel");
093                }
094            }
095            return archiver;
096        }
097    
098        public List<PluginRepositoryList> getPluginRepositoryLists() {
099            if (this.pluginRepositoryLists == null) {
100                Set<AbstractName> names = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName()));
101                List<PluginRepositoryList> pluginRepositoryLists = new ArrayList<PluginRepositoryList>(names.size());
102                for (AbstractName name : names) {
103                    try {
104                        pluginRepositoryLists.add((PluginRepositoryList) kernel.getGBean(name));
105                    } catch (GBeanNotFoundException e) {
106                        //ignore?
107                    }
108                }
109                this.pluginRepositoryLists = pluginRepositoryLists;
110            }
111            return this.pluginRepositoryLists;
112        }
113    
114        public DeploymentManager getDeploymentManager() {
115            DeploymentFactory factory = new DeploymentFactoryWithKernel(kernel);
116            try {
117                return factory.getDeploymentManager("deployer:geronimo:inVM", null, null);
118            } catch (DeploymentManagerCreationException e) {
119                //            log.error(e.getMessage(), e);
120                return null;
121            }
122        }
123    
124    }