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    package org.apache.geronimo.console.util;
018    
019    import java.io.File;
020    import java.util.Map;
021    
022    import javax.naming.InitialContext;
023    import javax.naming.NamingException;
024    import javax.portlet.PortletRequest;
025    import javax.portlet.PortletSession;
026    import javax.security.auth.Subject;
027    import javax.security.auth.login.LoginException;
028    import javax.security.auth.spi.LoginModule;
029    import javax.servlet.http.HttpSession;
030    
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.geronimo.gbean.AbstractName;
034    import org.apache.geronimo.gbean.GBeanData;
035    import org.apache.geronimo.kernel.Kernel;
036    import org.apache.geronimo.kernel.KernelRegistry;
037    import org.apache.geronimo.kernel.config.ConfigurationManager;
038    import org.apache.geronimo.kernel.config.ConfigurationModuleType;
039    import org.apache.geronimo.kernel.config.ConfigurationUtil;
040    import org.apache.geronimo.kernel.proxy.GeronimoManagedBean;
041    import org.apache.geronimo.kernel.repository.Artifact;
042    import org.apache.geronimo.kernel.repository.Repository;
043    import org.apache.geronimo.management.J2EEDeployedObject;
044    import org.apache.geronimo.management.geronimo.J2EEDomain;
045    import org.apache.geronimo.management.geronimo.J2EEServer;
046    import org.apache.geronimo.management.geronimo.JCAAdminObject;
047    import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
048    import org.apache.geronimo.management.geronimo.JCAResource;
049    import org.apache.geronimo.management.geronimo.JMSBroker;
050    import org.apache.geronimo.management.geronimo.JMSConnector;
051    import org.apache.geronimo.management.geronimo.JMSManager;
052    import org.apache.geronimo.management.geronimo.JVM;
053    import org.apache.geronimo.management.geronimo.NetworkConnector;
054    import org.apache.geronimo.management.geronimo.ResourceAdapter;
055    import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
056    import org.apache.geronimo.management.geronimo.WebAccessLog;
057    import org.apache.geronimo.management.geronimo.WebContainer;
058    import org.apache.geronimo.management.geronimo.WebManager;
059    import org.apache.geronimo.system.logging.SystemLog;
060    
061    /**
062     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
063     */
064    public class PortletManager {
065        private final static Log log = LogFactory.getLog(PortletManager.class);
066        // The following are currently static due to having only one server/JVM/etc. per Geronimo
067        private final static String HELPER_KEY = "org.apache.geronimo.console.ManagementHelper";
068        private final static String DOMAIN_KEY = "org.apache.geronimo.console.J2EEDomain";
069        private final static String SERVER_KEY = "org.apache.geronimo.console.J2EEServer";
070        private final static String JVM_KEY = "org.apache.geronimo.console.JVM";
071        private final static String SYSTEM_LOG_KEY = "org.apache.geronimo.console.SystemLog";
072        // The following may change based on the user's selections
073        // nothing yet
074    
075        private static ManagementHelper createHelper() {
076            Kernel kernel = getKernel();
077            return new KernelManagementHelper(kernel);
078        }
079    
080        public static Kernel getKernel() {
081            //todo: consider making this configurable; we could easily connect to a remote kernel if we wanted to
082            Kernel kernel = null;
083            try {
084                kernel = (Kernel) new InitialContext().lookup("java:comp/GeronimoKernel");
085            } catch (NamingException e) {
086    //            log.error("Unable to look up kernel in JNDI", e);
087            }
088            if (kernel == null) {
089                log.debug("Unable to find kernel in JNDI; using KernelRegistry instead");
090                kernel = KernelRegistry.getSingleKernel();
091            }
092            return kernel;
093        }
094    
095        public static ConfigurationManager getConfigurationManager() {
096            return ConfigurationUtil.getConfigurationManager(getKernel());
097        }
098    
099        public static ManagementHelper getManagementHelper(PortletRequest request) {
100            ManagementHelper helper = (ManagementHelper) request.getPortletSession(true).getAttribute(HELPER_KEY, PortletSession.APPLICATION_SCOPE);
101            if (helper == null) {
102                helper = createHelper();
103                request.getPortletSession().setAttribute(HELPER_KEY, helper, PortletSession.APPLICATION_SCOPE);
104            }
105            return helper;
106        }
107    
108        public static ManagementHelper getManagementHelper(HttpSession session) {
109            ManagementHelper helper = (ManagementHelper) session.getAttribute(HELPER_KEY);
110            if (helper == null) {
111                helper = createHelper();
112                session.setAttribute(HELPER_KEY, helper);
113            }
114            return helper;
115        }
116    
117        public static J2EEDomain getCurrentDomain(PortletRequest request) {
118            J2EEDomain domain = (J2EEDomain) request.getPortletSession(true).getAttribute(DOMAIN_KEY, PortletSession.APPLICATION_SCOPE);
119            if (domain == null) {
120                domain = getManagementHelper(request).getDomains()[0]; //todo: some day, select a domain
121                request.getPortletSession().setAttribute(DOMAIN_KEY, domain, PortletSession.APPLICATION_SCOPE);
122            }
123            return domain;
124    
125        }
126    
127        public static J2EEServer getCurrentServer(PortletRequest request) {
128            J2EEServer server = (J2EEServer) request.getPortletSession(true).getAttribute(SERVER_KEY, PortletSession.APPLICATION_SCOPE);
129            if (server == null) {
130                server = getCurrentDomain(request).getServerInstances()[0]; //todo: some day, select a server from the domain
131                request.getPortletSession().setAttribute(SERVER_KEY, server, PortletSession.APPLICATION_SCOPE);
132            } else {
133                // to do     handle "should not occur" error   - message?
134            }
135            return server;
136        }
137    
138        public static JVM getCurrentJVM(PortletRequest request) {
139            JVM jvm = (JVM) request.getPortletSession(true).getAttribute(JVM_KEY, PortletSession.APPLICATION_SCOPE);
140            if (jvm == null) {
141                ManagementHelper helper = getManagementHelper(request);
142                jvm = helper.getJavaVMs(getCurrentServer(request))[0]; //todo: some day, select a JVM from the server
143                request.getPortletSession().setAttribute(JVM_KEY, jvm, PortletSession.APPLICATION_SCOPE);
144            }
145            return jvm;
146        }
147    
148        public static void testLoginModule(PortletRequest request, LoginModule module, Map options) {
149            ManagementHelper helper = getManagementHelper(request);
150            helper.testLoginModule(getCurrentServer(request), module, options);
151        }
152    
153        public static Subject testLoginModule(PortletRequest request, LoginModule module, Map options, String username, String password) throws LoginException {
154            ManagementHelper helper = getManagementHelper(request);
155            return helper.testLoginModule(getCurrentServer(request), module, options, username, password);
156        }
157    
158        public static ResourceAdapterModule[] getOutboundRAModules(PortletRequest request, String iface) {
159            ManagementHelper helper = getManagementHelper(request);
160            return helper.getOutboundRAModules(getCurrentServer(request), iface);
161        }
162    
163        public static ResourceAdapterModule[] getOutboundRAModules(PortletRequest request, String[] iface) {
164            ManagementHelper helper = getManagementHelper(request);
165            return helper.getOutboundRAModules(getCurrentServer(request), iface);
166        }
167    
168        public static ResourceAdapterModule[] getAdminObjectModules(PortletRequest request, String[] ifaces) {
169            ManagementHelper helper = getManagementHelper(request);
170            return helper.getAdminObjectModules(getCurrentServer(request), ifaces);
171        }
172    
173        public static JCAManagedConnectionFactory[] getOutboundFactoriesOfType(PortletRequest request, String iface) {
174            ManagementHelper helper = getManagementHelper(request);
175            return helper.getOutboundFactories(getCurrentServer(request), iface);
176        }
177    
178        public static JCAManagedConnectionFactory[] getOutboundFactoriesForRA(PortletRequest request, AbstractName resourceAdapterModuleName) {
179            ManagementHelper helper = getManagementHelper(request);
180            return helper.getOutboundFactories((ResourceAdapterModule) helper.getObject(resourceAdapterModuleName));
181        }
182    
183        public static JCAManagedConnectionFactory[] getOutboundFactoriesForRA(PortletRequest request, AbstractName resourceAdapterModuleName, String iface) {
184            ManagementHelper helper = getManagementHelper(request);
185            return helper.getOutboundFactories((ResourceAdapterModule) helper.getObject(resourceAdapterModuleName), iface);
186        }
187    
188        public static JCAManagedConnectionFactory[] getOutboundFactoriesForRA(PortletRequest request, ResourceAdapterModule module) {
189            ManagementHelper helper = getManagementHelper(request);
190            return helper.getOutboundFactories(module);
191        }
192    
193        public static JCAManagedConnectionFactory[] getOutboundFactoriesForRA(PortletRequest request, ResourceAdapterModule module, String iface) {
194            ManagementHelper helper = getManagementHelper(request);
195            return helper.getOutboundFactories(module, iface);
196        }
197    
198        public static JCAManagedConnectionFactory[] getOutboundFactoriesForRA(PortletRequest request, ResourceAdapterModule module, String[] iface) {
199            ManagementHelper helper = getManagementHelper(request);
200            return helper.getOutboundFactories(module, iface);
201        }
202    
203        //todo: Create an interface for admin objects
204        public static JCAAdminObject[] getAdminObjectsForRA(PortletRequest request, ResourceAdapterModule module, String[] ifaces) {
205            ManagementHelper helper = getManagementHelper(request);
206            return helper.getAdminObjects(module, ifaces);
207        }
208    
209        public static WebManager[] getWebManagers(PortletRequest request) {
210            return getCurrentServer(request).getWebManagers();
211        }
212    
213        public static WebManager getWebManager(PortletRequest request, AbstractName managerName) {
214            ManagementHelper helper = getManagementHelper(request);
215            return (WebManager) helper.getObject(managerName);
216        }
217    
218    //    private static String[] namesToStrings(AbstractName[] names) {
219    //        String[] result = new String[names.length];
220    //        for (int i = 0; i < names.length; i++) {
221    //            AbstractName name = names[i];
222    //            result[i] = name.toURI().toString();
223    //        }
224    //        return result;
225    //    }
226    //
227    
228        public static WebAccessLog getWebAccessLog(PortletRequest request, AbstractName managerName, AbstractName containerName) {
229            ManagementHelper helper = getManagementHelper(request);
230            WebManager manager = (WebManager) helper.getObject(managerName);
231            return manager.getAccessLog((WebContainer) helper.getObject(containerName));
232        }
233    
234        public static WebContainer getWebContainer(PortletRequest request, AbstractName containerName) {
235            ManagementHelper helper = getManagementHelper(request);
236            return (WebContainer) helper.getObject(containerName);
237        }
238    
239        public static NetworkConnector[] getNetworkConnectors(PortletRequest request, AbstractName managerName) {
240            ManagementHelper helper = getManagementHelper(request);
241            WebManager manager = (WebManager) helper.getObject(managerName);
242            return manager.getConnectors();
243        }
244    
245        public static NetworkConnector[] getNetworkConnectors(PortletRequest request, AbstractName managerName, String protocol) {
246            ManagementHelper helper = getManagementHelper(request);
247            WebManager manager = (WebManager) helper.getObject(managerName);
248            return manager.getConnectors(protocol);
249        }
250    
251        public static NetworkConnector getNetworkConnector(PortletRequest request, AbstractName connectorName) {
252            ManagementHelper helper = getManagementHelper(request);
253            return (NetworkConnector) helper.getObject(connectorName);
254        }
255    
256        public static NetworkConnector[] getNetworkConnectorsForContainer(PortletRequest request, AbstractName managerName, AbstractName containerName, String protocol) {
257            ManagementHelper helper = getManagementHelper(request);
258            WebManager manager = (WebManager) helper.getObject(managerName);
259            return manager.getConnectorsForContainer(containerName, protocol);
260        }
261    
262        public static JMSBroker getJMSBroker(PortletRequest request, AbstractName brokerName) {
263            ManagementHelper helper = getManagementHelper(request);
264            return (JMSBroker) helper.getObject(brokerName);
265        }
266    
267        public static JMSConnector createJMSConnector(PortletRequest request, JMSManager manager, AbstractName containerName, String name, String protocol, String host, int port) {
268            return manager.addConnector(getJMSBroker(request, containerName), name, protocol, host, port);
269        }
270    
271        public static JMSConnector[] getJMSConnectors(PortletRequest request, AbstractName managerName) {
272            ManagementHelper helper = getManagementHelper(request);
273            JMSManager manager = (JMSManager) helper.getObject(managerName);
274            return (JMSConnector[]) manager.getConnectors();
275        }
276    
277        public static JMSConnector[] getJMSConnectors(PortletRequest request, AbstractName managerName, String protocol) {
278            ManagementHelper helper = getManagementHelper(request);
279            JMSManager manager = (JMSManager) helper.getObject(managerName);
280            return (JMSConnector[]) manager.getConnectors(protocol);
281        }
282    
283        public static JMSConnector[] getJMSConnectorsForContainer(PortletRequest request, AbstractName managerName, AbstractName brokerName) {
284            ManagementHelper helper = getManagementHelper(request);
285            JMSManager manager = (JMSManager) helper.getObject(managerName);
286            return (JMSConnector[]) manager.getConnectorsForContainer(brokerName);
287        }
288    
289        public static JMSConnector[] getJMSConnectorsForContainer(PortletRequest request, AbstractName managerName, AbstractName brokerName, String protocol) {
290            ManagementHelper helper = getManagementHelper(request);
291            JMSManager manager = (JMSManager) helper.getObject(managerName);
292            return (JMSConnector[]) manager.getConnectorsForContainer(brokerName, protocol);
293        }
294    
295        public static ResourceAdapter[] getResourceAdapters(PortletRequest request, ResourceAdapterModule module) {
296            ManagementHelper helper = getManagementHelper(request);
297            return helper.getResourceAdapters(module);
298        }
299    
300        public static JCAResource[] getJCAResources(PortletRequest request, ResourceAdapter adapter) {
301            ManagementHelper helper = getManagementHelper(request);
302            return helper.getRAResources(adapter);
303        }
304    
305        public static String getGBeanDescription(PortletRequest request, AbstractName objectName) {
306            ManagementHelper helper = getManagementHelper(request);
307            return helper.getGBeanDescription(objectName);
308        }
309    
310        public static SystemLog getCurrentSystemLog(PortletRequest request) {
311            SystemLog log = (SystemLog) request.getPortletSession(true).getAttribute(SYSTEM_LOG_KEY, PortletSession.APPLICATION_SCOPE);
312            if (log == null) {
313                ManagementHelper helper = getManagementHelper(request);
314                log = helper.getSystemLog(getCurrentJVM(request));
315                request.getPortletSession().setAttribute(SYSTEM_LOG_KEY, log, PortletSession.APPLICATION_SCOPE);
316            }
317            return log;
318        }
319    
320        public static GeronimoManagedBean[] getManagedBeans(PortletRequest request, Class intrface) {
321            ManagementHelper helper = getManagementHelper(request);
322            Object[] obs = helper.findByInterface(intrface);
323            GeronimoManagedBean[] results = new GeronimoManagedBean[obs.length];
324            for (int i = 0; i < results.length; i++) {
325                results[i] = (GeronimoManagedBean) obs[i];
326            }
327            return results;
328        }
329    
330        public static GeronimoManagedBean getManagedBean(PortletRequest request, AbstractName name) {
331            ManagementHelper helper = getManagementHelper(request);
332            return (GeronimoManagedBean) helper.getObject(name);
333        }
334    
335        public static Artifact getConfigurationFor(PortletRequest request, AbstractName objectName) {
336            ManagementHelper helper = getManagementHelper(request);
337            return helper.getConfigurationNameFor(objectName);
338        }
339    
340        public static AbstractName getNameFor(PortletRequest request, Object component) {
341            ManagementHelper helper = getManagementHelper(request);
342            return helper.getNameFor(component);
343        }
344    
345        public static File getRepositoryEntry(PortletRequest request, String repositoryURI) {
346            J2EEServer server = getCurrentServer(request);
347            Repository[] repos = server.getRepositories();
348            Artifact uri = Artifact.create(repositoryURI);
349            if (!uri.isResolved()) {
350                Artifact[] all = server.getConfigurationManager().getArtifactResolver().queryArtifacts(uri);
351                if (all.length == 0) {
352                    return null;
353                } else {
354                    uri = all[all.length - 1];
355                }
356            }
357            for (int i = 0; i < repos.length; i++) {
358                Repository repo = repos[i];
359                if (repo.contains(uri)) {
360                    return repo.getLocation(uri);
361                }
362            }
363            return null;
364        }
365    
366        public static J2EEDeployedObject getModule(PortletRequest request, Artifact configuration) {
367            ManagementHelper helper = getManagementHelper(request);
368            return helper.getModuleForConfiguration(configuration);
369        }
370    
371        public static ConfigurationData[] getConfigurations(PortletRequest request, ConfigurationModuleType type, boolean includeChildModules) {
372            ManagementHelper helper = getManagementHelper(request);
373            return helper.getConfigurations(type, includeChildModules);
374        }
375        
376        public static Object[] getGBeansImplementing(PortletRequest request, Class iface) {
377            ManagementHelper helper = getManagementHelper(request);
378            return helper.getGBeansImplementing(iface);
379        }    
380    
381        /**
382         * This methods adds a GBean to an existing configuration.
383         * @param request PortletRequest object to get hold of ManagementHelper
384         * @param configID  The configuration to add the GBean to.
385         * @param gbean     The data representing the GBean to add.
386         * @param start     If true, the GBean should be started as part of this call.
387         */
388        public static void addGBeanToConfiguration(PortletRequest request, Artifact configID, GBeanData gbean, boolean start) {
389            ManagementHelper helper = getManagementHelper(request);
390            helper.addGBeanToConfiguration(configID, gbean, start);
391        }
392    
393    }