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