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: package org.apache.geronimo.console.car;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Set;
025:
026: import javax.enterprise.deploy.spi.DeploymentManager;
027: import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
028: import javax.enterprise.deploy.spi.factories.DeploymentFactory;
029: import javax.portlet.PortletRequest;
030: import javax.portlet.PortletSession;
031:
032: import org.apache.geronimo.console.util.PortletManager;
033: import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryWithKernel;
034: import org.apache.geronimo.gbean.AbstractName;
035: import org.apache.geronimo.gbean.AbstractNameQuery;
036: import org.apache.geronimo.kernel.GBeanNotFoundException;
037: import org.apache.geronimo.kernel.Kernel;
038: import org.apache.geronimo.system.plugin.PluginInstaller;
039: import org.apache.geronimo.system.plugin.PluginRepositoryList;
040: import org.apache.geronimo.system.plugin.ServerArchiver;
041:
042: /**
043: * @version $Rev: 613462 $ $Date: 2008-01-19 13:46:39 -0800 (Sat, 19 Jan 2008) $
044: */
045: public class ManagementHelper {
046:
047: private final static String PLUGIN_HELPER_KEY = "org.apache.geronimo.console.PluginManagementHelper";
048: private final Kernel kernel;
049: private PluginInstaller pluginInstaller;
050: private ServerArchiver archiver;
051: private List<PluginRepositoryList> pluginRepositoryLists;
052:
053: public static ManagementHelper getManagementHelper(
054: PortletRequest request) {
055: ManagementHelper helper = (ManagementHelper) request
056: .getPortletSession(true).getAttribute(
057: PLUGIN_HELPER_KEY,
058: PortletSession.APPLICATION_SCOPE);
059: if (helper == null) {
060: Kernel kernel = PortletManager.getKernel();
061: helper = new ManagementHelper(kernel);
062: request.getPortletSession().setAttribute(PLUGIN_HELPER_KEY,
063: helper, PortletSession.APPLICATION_SCOPE);
064: }
065: return helper;
066: }
067:
068: public ManagementHelper(Kernel kernel) {
069: this .kernel = kernel;
070: }
071:
072: public PluginInstaller getPluginInstaller() {
073: if (pluginInstaller == null) {
074: Set<AbstractName> pluginInstallers = kernel
075: .listGBeans(new AbstractNameQuery(
076: PluginInstaller.class.getName()));
077: if (pluginInstallers.size() == 0) {
078: throw new IllegalStateException(
079: "No plugin installer registered");
080: }
081: try {
082: pluginInstaller = (PluginInstaller) kernel
083: .getGBean(pluginInstallers.iterator().next());
084: } catch (GBeanNotFoundException e) {
085: throw new IllegalStateException(
086: "Plugin installer cannot be retrieved from kernel");
087: }
088: }
089: return pluginInstaller;
090: }
091:
092: public ServerArchiver getArchiver() {
093: if (archiver == null) {
094: Set<AbstractName> archivers = kernel
095: .listGBeans(new AbstractNameQuery(
096: ServerArchiver.class.getName()));
097: if (archivers.size() == 0) {
098: throw new IllegalStateException(
099: "No plugin installer registered");
100: }
101: try {
102: archiver = (ServerArchiver) kernel.getGBean(archivers
103: .iterator().next());
104: } catch (GBeanNotFoundException e) {
105: throw new IllegalStateException(
106: "Plugin installer cannot be retrieved from kernel");
107: }
108: }
109: return archiver;
110: }
111:
112: public List<PluginRepositoryList> getPluginRepositoryLists() {
113: if (this .pluginRepositoryLists == null) {
114: Set<AbstractName> names = kernel
115: .listGBeans(new AbstractNameQuery(
116: PluginRepositoryList.class.getName()));
117: List<PluginRepositoryList> pluginRepositoryLists = new ArrayList<PluginRepositoryList>(
118: names.size());
119: for (AbstractName name : names) {
120: try {
121: pluginRepositoryLists
122: .add((PluginRepositoryList) kernel
123: .getGBean(name));
124: } catch (GBeanNotFoundException e) {
125: //ignore?
126: }
127: }
128: this .pluginRepositoryLists = pluginRepositoryLists;
129: }
130: return this .pluginRepositoryLists;
131: }
132:
133: public DeploymentManager getDeploymentManager() {
134: DeploymentFactory factory = new DeploymentFactoryWithKernel(
135: kernel);
136: try {
137: return factory.getDeploymentManager(
138: "deployer:geronimo:inVM", null, null);
139: } catch (DeploymentManagerCreationException e) {
140: // log.error(e.getMessage(), e);
141: return null;
142: }
143: }
144:
145: }
|