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.lang.reflect.Array;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.security.auth.Subject;
029: import javax.security.auth.callback.Callback;
030: import javax.security.auth.callback.CallbackHandler;
031: import javax.security.auth.callback.NameCallback;
032: import javax.security.auth.callback.PasswordCallback;
033: import javax.security.auth.callback.UnsupportedCallbackException;
034: import javax.security.auth.login.LoginException;
035: import javax.security.auth.spi.LoginModule;
036:
037: import org.apache.geronimo.gbean.AbstractName;
038: import org.apache.geronimo.gbean.AbstractNameQuery;
039: import org.apache.geronimo.gbean.GBeanData;
040: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
041: import org.apache.geronimo.kernel.GBeanNotFoundException;
042: import org.apache.geronimo.kernel.Kernel;
043: import org.apache.geronimo.kernel.Naming;
044: import org.apache.geronimo.kernel.config.Configuration;
045: import org.apache.geronimo.kernel.config.ConfigurationInfo;
046: import org.apache.geronimo.kernel.config.ConfigurationManager;
047: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
048: import org.apache.geronimo.kernel.config.ConfigurationUtil;
049: import org.apache.geronimo.kernel.config.EditableConfigurationManager;
050: import org.apache.geronimo.kernel.config.InvalidConfigException;
051: import org.apache.geronimo.kernel.config.NoSuchStoreException;
052: import org.apache.geronimo.kernel.management.State;
053: import org.apache.geronimo.kernel.proxy.GeronimoManagedBean;
054: import org.apache.geronimo.kernel.proxy.ProxyManager;
055: import org.apache.geronimo.kernel.repository.Artifact;
056: import org.apache.geronimo.management.AppClientModule;
057: import org.apache.geronimo.management.EJB;
058: import org.apache.geronimo.management.EJBModule;
059: import org.apache.geronimo.management.J2EEDeployedObject;
060: import org.apache.geronimo.management.J2EEModule;
061: import org.apache.geronimo.management.J2EEResource;
062: import org.apache.geronimo.management.JDBCDataSource;
063: import org.apache.geronimo.management.JDBCDriver;
064: import org.apache.geronimo.management.JDBCResource;
065: import org.apache.geronimo.management.JMSResource;
066: import org.apache.geronimo.management.Servlet;
067: import org.apache.geronimo.management.geronimo.J2EEApplication;
068: import org.apache.geronimo.management.geronimo.J2EEDomain;
069: import org.apache.geronimo.management.geronimo.J2EEServer;
070: import org.apache.geronimo.management.geronimo.JCAAdminObject;
071: import org.apache.geronimo.management.geronimo.JCAConnectionFactory;
072: import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
073: import org.apache.geronimo.management.geronimo.JCAResource;
074: import org.apache.geronimo.management.geronimo.JVM;
075: import org.apache.geronimo.management.geronimo.ResourceAdapter;
076: import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
077: import org.apache.geronimo.management.geronimo.WebModule;
078: import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
079: import org.apache.geronimo.system.logging.SystemLog;
080:
081: /**
082: * An implementation of the ManagementHelper interface that uses a Geronimo
083: * kernel. That must be an in-VM kernel.
084: *
085: * @version $Rev:386276 $ $Date: 2007-12-15 10:24:52 -0800 (Sat, 15 Dec 2007) $
086: */
087: public class KernelManagementHelper implements ManagementHelper {
088: private final Kernel kernel;
089:
090: public KernelManagementHelper(Kernel kernel) {
091: this .kernel = kernel;
092: }
093:
094: public J2EEDomain[] getDomains() {
095: Set domainNames = kernel.listGBeans(new AbstractNameQuery(
096: J2EEDomain.class.getName()));
097: J2EEDomain[] result = new J2EEDomain[domainNames.size()];
098: int i = 0;
099: for (Iterator iterator = domainNames.iterator(); iterator
100: .hasNext();) {
101: AbstractName domainName = (AbstractName) iterator.next();
102: result[i++] = (J2EEDomain) kernel.getProxyManager()
103: .createProxy(domainName, J2EEDomain.class);
104: }
105: return result;
106: }
107:
108: public J2EEServer[] getServers(J2EEDomain domain) {
109: return proxify(domain.getServerInstances(), J2EEServer.class);
110: }
111:
112: public J2EEDeployedObject[] getDeployedObjects(J2EEServer server) {
113: return proxify(server.getDeployedObjectInstances(),
114: J2EEDeployedObject.class);
115: }
116:
117: public J2EEApplication[] getApplications(J2EEServer server) {
118: return proxify(server.getApplications(), J2EEApplication.class);
119: }
120:
121: public AppClientModule[] getAppClients(J2EEServer server) {
122: return proxify(server.getAppClients(), AppClientModule.class);
123: }
124:
125: public WebModule[] getWebModules(J2EEServer server) {
126: return proxify(server.getWebModules(), WebModule.class);
127: }
128:
129: public EJBModule[] getEJBModules(J2EEServer server) {
130: return proxify(server.getEJBModules(), EJBModule.class);
131: }
132:
133: public ResourceAdapterModule[] getRAModules(J2EEServer server) {
134: return proxify(server.getResourceAdapterModules(),
135: ResourceAdapterModule.class);
136: }
137:
138: public JCAManagedConnectionFactory[] getOutboundFactories(
139: J2EEServer server, String connectionFactoryInterface) {
140: List<JCAManagedConnectionFactory> list = new ArrayList<JCAManagedConnectionFactory>();
141: ResourceAdapterModule[] modules = server
142: .getResourceAdapterModules();
143: for (ResourceAdapterModule module : modules) {
144: ResourceAdapter[] adapters = module
145: .getResourceAdapterInstances();
146: for (ResourceAdapter adapter : adapters) {
147: JCAResource[] resources = adapter
148: .getJCAResourceImplementations();
149: for (JCAResource resource : resources) {
150: JCAManagedConnectionFactory[] outboundFactories = resource
151: .getOutboundFactories();
152: list.addAll(Arrays.asList(outboundFactories));
153: }
154: }
155:
156: }
157: return proxify(list
158: .toArray(new JCAManagedConnectionFactory[list.size()]),
159: JCAManagedConnectionFactory.class);
160: }
161:
162: public ResourceAdapterModule[] getOutboundRAModules(
163: J2EEServer server, String connectionFactoryInterface) {
164: return getOutboundRAModules(server,
165: new String[] { connectionFactoryInterface });
166: }
167:
168: public ResourceAdapterModule[] getOutboundRAModules(
169: J2EEServer server, String[] connectionFactoryInterfaces) {
170: List<ResourceAdapterModule> list = new ArrayList<ResourceAdapterModule>();
171:
172: ResourceAdapterModule[] modules = server
173: .getResourceAdapterModules();
174:
175: outer: for (ResourceAdapterModule module : modules) {
176: ResourceAdapter[] adapters = module
177: .getResourceAdapterInstances();
178: for (ResourceAdapter adapter : adapters) {
179: JCAResource[] resources = adapter
180: .getJCAResourceImplementations();
181: for (JCAResource resource : resources) {
182: JCAManagedConnectionFactory[] outboundFactories = resource
183: .getOutboundFactories(connectionFactoryInterfaces);
184: if (outboundFactories.length > 0) {
185: list.add(module);
186: continue outer;
187: }
188: }
189: }
190:
191: }
192: return proxify(list.toArray(new ResourceAdapterModule[list
193: .size()]), ResourceAdapterModule.class);
194: }
195:
196: public ResourceAdapterModule[] getAdminObjectModules(
197: J2EEServer server, String[] adminObjectInterfaces) {
198: List<ResourceAdapterModule> list = new ArrayList<ResourceAdapterModule>();
199:
200: ResourceAdapterModule[] modules = server
201: .getResourceAdapterModules();
202:
203: outer: for (ResourceAdapterModule module : modules) {
204: ResourceAdapter[] adapters = module
205: .getResourceAdapterInstances();
206: for (ResourceAdapter adapter : adapters) {
207: JCAResource[] resources = adapter
208: .getJCAResourceImplementations();
209: for (JCAResource resource : resources) {
210: JCAAdminObject[] adminObjects = resource
211: .getAdminObjectInstances(adminObjectInterfaces);
212: if (adminObjects.length > 0) {
213: list.add(module);
214: continue outer;
215: }
216: }
217: }
218:
219: }
220: return proxify(list.toArray(new ResourceAdapterModule[list
221: .size()]), ResourceAdapterModule.class);
222: }
223:
224: public JCAManagedConnectionFactory[] getOutboundFactories(
225: ResourceAdapterModule module) {
226: return getOutboundFactories(module, (String[]) null);
227: }
228:
229: public JCAManagedConnectionFactory[] getOutboundFactories(
230: ResourceAdapterModule module,
231: String connectionFactoryInterface) {
232: return getOutboundFactories(module,
233: new String[] { connectionFactoryInterface });
234: }
235:
236: public JCAManagedConnectionFactory[] getOutboundFactories(
237: ResourceAdapterModule module,
238: String[] connectionFactoryInterfaces) {
239: List<JCAManagedConnectionFactory> list = new ArrayList<JCAManagedConnectionFactory>();
240:
241: ResourceAdapter[] resourceAdapters = module
242: .getResourceAdapterInstances();
243: for (ResourceAdapter resourceAdapter : resourceAdapters) {
244: JCAResource[] jcaResources = resourceAdapter
245: .getJCAResourceImplementations();
246: for (JCAResource jcaResource : jcaResources) {
247: JCAManagedConnectionFactory[] outboundFactories = jcaResource
248: .getOutboundFactories(connectionFactoryInterfaces);
249: list.addAll(Arrays.asList(outboundFactories));
250: }
251: }
252:
253: return proxify(list
254: .toArray(new JCAManagedConnectionFactory[list.size()]),
255: JCAManagedConnectionFactory.class);
256: }
257:
258: public JCAAdminObject[] getAdminObjects(
259: ResourceAdapterModule module, String[] adminObjectInterfaces) {
260: List<JCAAdminObject> list = new ArrayList<JCAAdminObject>();
261: ResourceAdapter[] resourceAdapters = module
262: .getResourceAdapterInstances();
263: for (ResourceAdapter resourceAdapter : resourceAdapters) {
264: JCAResource[] jcaResources = resourceAdapter
265: .getJCAResourceImplementations();
266: for (JCAResource jcaResource : jcaResources) {
267: JCAAdminObject[] adminObjects = jcaResource
268: .getAdminObjectInstances(adminObjectInterfaces);
269: list.addAll(Arrays.asList(adminObjects));
270: }
271: }
272:
273: return proxify(list.toArray(new JCAAdminObject[list.size()]),
274: JCAAdminObject.class);
275: }
276:
277: public J2EEResource[] getResources(J2EEServer server) {
278: return proxify(server.getResourceInstances(),
279: J2EEResource.class);
280: }
281:
282: public JCAResource[] getJCAResources(J2EEServer server) {
283: List<JCAResource> list = new ArrayList<JCAResource>();
284: ResourceAdapterModule[] modules = server
285: .getResourceAdapterModules();
286: for (ResourceAdapterModule module : modules) {
287: ResourceAdapter[] adapters = module
288: .getResourceAdapterInstances();
289: for (ResourceAdapter adapter : adapters) {
290: JCAResource[] resources = adapter
291: .getJCAResourceImplementations();
292: list.addAll(Arrays.asList(resources));
293: }
294:
295: }
296: return proxify(list.toArray(new JCAResource[list.size()]),
297: JCAResource.class);
298: }
299:
300: public JDBCResource[] getJDBCResources(J2EEServer server) {
301: return new JDBCResource[0]; // Geronimo uses JCA resources for this
302: }
303:
304: public JMSResource[] getJMSResources(J2EEServer server) {
305: return new JMSResource[0]; // Geronimo uses JCA resources for this
306: }
307:
308: public JVM[] getJavaVMs(J2EEServer server) {
309: return proxify(server.getJavaVMInstances(), JVM.class);
310: }
311:
312: public SystemLog getSystemLog(JVM jvm) {
313: return proxify(jvm.getSystemLog(), SystemLog.class);
314: }
315:
316: // application properties
317: public J2EEModule[] getModules(J2EEApplication application) {
318: return proxify(application.getModulesInstances(),
319: J2EEModule.class);
320: }
321:
322: public AppClientModule[] getAppClients(J2EEApplication application) {
323: return proxify(application.getClientModules(),
324: AppClientModule.class);
325: }
326:
327: public WebModule[] getWebModules(J2EEApplication application) {
328: return proxify(application.getWebModules(), WebModule.class);
329: }
330:
331: public EJBModule[] getEJBModules(J2EEApplication application) {
332: return proxify(application.getEJBModules(), EJBModule.class);
333: }
334:
335: public ResourceAdapterModule[] getRAModules(
336: J2EEApplication application) {
337: return proxify(application.getRAModules(),
338: ResourceAdapterModule.class);
339: }
340:
341: public JCAResource[] getJCAResources(J2EEApplication application) {
342: List<JCAResource> list = new ArrayList<JCAResource>();
343: ResourceAdapterModule[] modules = application.getRAModules();
344: for (ResourceAdapterModule module : modules) {
345: ResourceAdapter[] adapters = module
346: .getResourceAdapterInstances();
347: for (ResourceAdapter adapter : adapters) {
348: JCAResource[] resources = adapter
349: .getJCAResourceImplementations();
350: list.addAll(Arrays.asList(resources));
351: }
352:
353: }
354: return proxify(list.toArray(new JCAResource[list.size()]),
355: JCAResource.class);
356: }
357:
358: public JDBCResource[] getJDBCResources(J2EEApplication application) {
359: return new JDBCResource[0]; // Geronimo uses JCAResources for this
360: }
361:
362: public JMSResource[] getJMSResources(J2EEApplication application) {
363: return new JMSResource[0]; // Geronimo uses JCAResources for this
364: }
365:
366: // module properties
367: public EJB[] getEJBs(EJBModule module) {
368: return new EJB[0]; //todo
369: }
370:
371: public Servlet[] getServlets(WebModule module) {
372: return new Servlet[0]; //todo
373: }
374:
375: public ResourceAdapter[] getResourceAdapters(
376: ResourceAdapterModule module) {
377: return proxify(module.getResourceAdapterInstances(),
378: ResourceAdapter.class);
379: }
380:
381: // resource adapter properties
382: public JCAResource[] getRAResources(ResourceAdapter adapter) {
383: return proxify(adapter.getJCAResourceImplementations(),
384: JCAResource.class);
385: }
386:
387: // resource properties
388: public JDBCDataSource[] getDataSource(JDBCResource resource) {
389: return new JDBCDataSource[0]; //todo
390: }
391:
392: public JDBCDriver[] getDriver(JDBCDataSource dataSource) {
393: return new JDBCDriver[0]; //todo
394: }
395:
396: public JCAConnectionFactory[] getConnectionFactories(
397: JCAResource resource) {
398: return proxify(resource.getConnectionFactoryInstances(),
399: JCAConnectionFactory.class);
400: }
401:
402: public JCAAdminObject[] getAdminObjects(JCAResource resource) {
403: return proxify(resource.getAdminObjectInstances(),
404: JCAAdminObject.class);
405: }
406:
407: public JCAManagedConnectionFactory getManagedConnectionFactory(
408: JCAConnectionFactory factory) {
409: return proxify(factory.getManagedConnectionFactoryInstance(),
410: JCAManagedConnectionFactory.class);
411: }
412:
413: public Object getObject(AbstractName objectName) {
414: ClassLoader cl = null;
415: try {
416: cl = kernel.getClassLoaderFor(objectName);
417: } catch (GBeanNotFoundException e) {
418: cl = KernelManagementHelper.class.getClassLoader();
419: }
420: return kernel.getProxyManager().createProxy(objectName, cl);
421: }
422:
423: public Artifact getConfigurationNameFor(AbstractName abstractName) {
424: return abstractName.getArtifact();
425: }
426:
427: public String getGBeanDescription(AbstractName abstractName) {
428: try {
429: return kernel.getGBeanInfo(abstractName).getName();
430: } catch (GBeanNotFoundException e) {
431: return null;
432: }
433: }
434:
435: public void testLoginModule(J2EEServer server, LoginModule module,
436: Map options) {
437: options.put(JaasLoginModuleUse.KERNEL_NAME_LM_OPTION, kernel
438: .getKernelName());
439: options.put(JaasLoginModuleUse.SERVERINFO_LM_OPTION, server
440: .getServerInfo());
441: if (!options
442: .containsKey(JaasLoginModuleUse.CLASSLOADER_LM_OPTION)) {
443: options.put(JaasLoginModuleUse.CLASSLOADER_LM_OPTION,
444: module.getClass().getClassLoader());
445: }
446: module.initialize(null, null, new HashMap(), options);
447: }
448:
449: public Subject testLoginModule(final J2EEServer server,
450: final LoginModule module, final Map options,
451: final String username, final String password)
452: throws LoginException {
453: options.put(JaasLoginModuleUse.KERNEL_NAME_LM_OPTION, kernel
454: .getKernelName());
455: if (!options
456: .containsKey(JaasLoginModuleUse.CLASSLOADER_LM_OPTION)) {
457: options.put(JaasLoginModuleUse.CLASSLOADER_LM_OPTION,
458: module.getClass().getClassLoader());
459: }
460: options.put(JaasLoginModuleUse.SERVERINFO_LM_OPTION, server
461: .getServerInfo());
462: Subject sub = new Subject();
463: CallbackHandler handler = new CallbackHandler() {
464: public void handle(Callback[] callbacks)
465: throws UnsupportedCallbackException {
466: for (int i = 0; i < callbacks.length; i++) {
467: Callback callback = callbacks[i];
468: if (callback instanceof PasswordCallback) {
469: ((PasswordCallback) callback)
470: .setPassword(password.toCharArray());
471: } else if (callback instanceof NameCallback) {
472: ((NameCallback) callback).setName(username);
473: } else {
474: throw new UnsupportedCallbackException(callback);
475: }
476: }
477: }
478: };
479: module.initialize(sub, handler, new HashMap(), options);
480: if (module.login() && module.commit()) {
481: return sub;
482: } else {
483: module.abort();
484: }
485: return null;
486: }
487:
488: public Object[] findByInterface(Class iface) {
489: Set set = kernel.listGBeans(new AbstractNameQuery(iface
490: .getName()));
491: Object[] result = new Object[set.size()];
492: int i = 0;
493: for (Iterator it = set.iterator(); it.hasNext();) {
494: AbstractName name = (AbstractName) it.next();
495: result[i++] = kernel.getProxyManager().createProxy(name,
496: iface.getClassLoader());
497: }
498: return result;
499: }
500:
501: public AbstractName getNameFor(Object component) {
502: return kernel.getAbstractNameFor(component);
503: }
504:
505: public ConfigurationData[] getConfigurations(
506: ConfigurationModuleType type, boolean includeChildModules) {
507: ConfigurationManager mgr = ConfigurationUtil
508: .getConfigurationManager(kernel);
509: List<AbstractName> stores = mgr.listStores();
510: List<ConfigurationData> results = new ArrayList<ConfigurationData>();
511: for (AbstractName storeName : stores) {
512: try {
513: List<ConfigurationInfo> infos = mgr
514: .listConfigurations(storeName);
515: for (ConfigurationInfo info : infos) {
516: AbstractName configuration = Configuration
517: .getConfigurationAbstractName(info
518: .getConfigID());
519: if (type == null
520: || type.getValue() == info.getType()
521: .getValue()) {
522: J2EEDeployedObject module = getModuleForConfiguration(info
523: .getConfigID());
524: results.add(new ConfigurationData(info
525: .getConfigID(), configuration, null,
526: info.getState(), info.getType(),
527: module == null ? null : kernel
528: .getAbstractNameFor(module)));
529: }
530: if (includeChildModules
531: && info.getType().getValue() == ConfigurationModuleType.EAR
532: .getValue()
533: && info.getState().toInt() == State.RUNNING_INDEX) {
534: J2EEApplication app = (J2EEApplication) getModuleForConfiguration(info
535: .getConfigID());
536: if (app == null) {
537: throw new IllegalStateException(
538: "Unable to load children for J2EE Application '"
539: + info.getConfigID()
540: + "' (no J2EEApplication found)");
541: }
542: Object[] modules = null;
543: if (type == null) {
544: modules = app.getModulesInstances();
545: } else if (type
546: .equals(ConfigurationModuleType.CAR)) {
547: modules = app.getClientModules();
548: } else if (type
549: .equals(ConfigurationModuleType.EJB)) {
550: modules = app.getEJBModules();
551: } else if (type
552: .equals(ConfigurationModuleType.RAR)) {
553: modules = app.getRAModules();
554: } else if (type
555: .equals(ConfigurationModuleType.WAR)) {
556: modules = app.getWebModules();
557: } //todo: handle dynamically registered module types, etc.
558: if (modules == null)
559: continue;
560: for (Object module : modules) {
561: ConfigurationModuleType moduleType = type;
562: if (moduleType == null) {
563: if (module instanceof WebModule) {
564: moduleType = ConfigurationModuleType.WAR;
565: } else if (module instanceof EJBModule) {
566: moduleType = ConfigurationModuleType.EJB;
567: } else if (module instanceof ResourceAdapterModule) {
568: moduleType = ConfigurationModuleType.RAR;
569: } else if (module instanceof AppClientModule)
570: moduleType = ConfigurationModuleType.CAR;
571: }
572: String moduleName;
573: if (type != null
574: && type
575: .equals(ConfigurationModuleType.WAR)) {
576: moduleName = ((WebModule) module)
577: .getWARName();
578: } else {
579: //todo: solutions for other module types
580: moduleName = (String) kernel
581: .getAbstractNameFor(module)
582: .getName().get(
583: NameFactory.J2EE_NAME);
584: }
585: results
586: .add(new ConfigurationData(
587: info.getConfigID(),
588: configuration,
589: moduleName,
590: info.getState(),
591: moduleType,
592: kernel
593: .getAbstractNameFor(module)));
594: }
595: }
596: }
597: } catch (NoSuchStoreException e) {
598: // we just got this list so this should not happen
599: // in the unlikely event it does, just continue
600: } catch (InvalidConfigException e) {
601: throw new RuntimeException(
602: "Bad configID; should never happen", e);
603: }
604: }
605: Collections.sort(results);
606: return results.toArray(new ConfigurationData[results.size()]);
607: }
608:
609: /**
610: * Gets a JSR-77 Module (WebModule, EJBModule, etc.) for the specified configuration.
611: * Note: this only works if the configuration is running at the time you ask.
612: *
613: * @return The Module, or null if the configuration is not running.
614: */
615: public J2EEDeployedObject getModuleForConfiguration(
616: Artifact configuration) {
617: ConfigurationManager manager = ConfigurationUtil
618: .getConfigurationManager(kernel);
619: Configuration config = manager.getConfiguration(configuration);
620: if (config == null || !manager.isRunning(configuration)) {
621: return null; // The configuration is not running, so we can't get its contents
622: }
623: ConfigurationModuleType type = config.getModuleType();
624: AbstractName result;
625: try {
626: if (type.equals(ConfigurationModuleType.CAR)) {
627: result = config.findGBean(new AbstractNameQuery(
628: AppClientModule.class.getName()));
629: } else if (type.equals(ConfigurationModuleType.EAR)) {
630: result = config.findGBean(new AbstractNameQuery(
631: J2EEApplication.class.getName()));
632: } else if (type.equals(ConfigurationModuleType.EJB)) {
633: result = config.findGBean(new AbstractNameQuery(
634: EJBModule.class.getName()));
635: } else if (type.equals(ConfigurationModuleType.RAR)) {
636: result = config.findGBean(new AbstractNameQuery(
637: ResourceAdapterModule.class.getName()));
638: } else if (type.equals(ConfigurationModuleType.WAR)) {
639: result = config.findGBean(new AbstractNameQuery(
640: WebModule.class.getName()));
641: } else {
642: return null;
643: }
644: ClassLoader classLoader = kernel.getClassLoaderFor(result);
645: return (J2EEDeployedObject) kernel.getProxyManager()
646: .createProxy(result, classLoader);
647: } catch (GBeanNotFoundException e) {
648: throw new IllegalStateException("Bad config ID: "
649: + e.getMessage(), e);
650: }
651: }
652:
653: public Object[] getGBeansImplementing(Class iface) {
654: Set set = kernel.listGBeans(new AbstractNameQuery(iface
655: .getName()));
656: Object[] result = (Object[]) Array.newInstance(iface, set
657: .size());
658: int index = 0;
659: ProxyManager mgr = kernel.getProxyManager();
660: for (Iterator it = set.iterator(); it.hasNext();) {
661: AbstractName name = (AbstractName) it.next();
662: result[index++] = mgr.createProxy(name, iface);
663: }
664: return result;
665: }
666:
667: /**
668: * Adds a new GBean to an existing Configuration.
669: * @param configID The configuration to add the GBean to.
670: * @param gbean The data representing the GBean to add.
671: * @param start If true, the GBean should be started as part of this call.
672: */
673: public void addGBeanToConfiguration(Artifact configID,
674: GBeanData gbean, boolean start) {
675: EditableConfigurationManager mgr = ConfigurationUtil
676: .getEditableConfigurationManager(kernel);
677: try {
678: mgr.addGBeanToConfiguration(configID, gbean, start);
679: } catch (InvalidConfigException e) {
680: throw new RuntimeException("Bad configID. configID = "
681: + configID, e);
682: } finally {
683: ConfigurationUtil.releaseConfigurationManager(kernel, mgr);
684: }
685: }
686:
687: /**
688: * This method returns the Naming object of the kernel.
689: */
690: public Naming getNaming() {
691: return kernel.getNaming();
692: }
693:
694: /**
695: * Helper method to connect to a remote kernel.
696: */
697: public static KernelManagementHelper getRemoteKernelManager(
698: String host, String user, String password)
699: throws java.io.IOException {
700: String uri = "jmx:rmi://" + host + "/jndi/rmi:/JMXConnector";
701: java.util.Map environment = new java.util.HashMap();
702: String[] credentials = new String[] { user, password };
703: environment.put(
704: javax.management.remote.JMXConnector.CREDENTIALS,
705: credentials);
706: javax.management.remote.JMXServiceURL address = new javax.management.remote.JMXServiceURL(
707: "service:" + uri);
708: javax.management.remote.JMXConnector jmxConnector = javax.management.remote.JMXConnectorFactory
709: .connect(address, environment);
710: javax.management.MBeanServerConnection mbServerConnection = jmxConnector
711: .getMBeanServerConnection();
712: Kernel kernel = new org.apache.geronimo.system.jmx.KernelDelegate(
713: mbServerConnection);
714: return new KernelManagementHelper(kernel);
715: }
716:
717: private <T> T[] proxify(T[] array, Class<T> clazz) {
718: for (int i = 0; i < array.length; i++) {
719: array[i] = proxify(array[i], clazz);
720: }
721: return array;
722: }
723:
724: private <T> T proxify(T t, Class<T> clazz) {
725: if (!(t instanceof GeronimoManagedBean)) {
726: AbstractName name = kernel.getAbstractNameFor(t);
727: t = (T) kernel.getProxyManager().createProxy(name, clazz);
728: }
729: return t;
730: }
731:
732: }
|