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