jboynes 2004/01/21 14:53:42
Modified: modules/kernel/src/java/org/apache/geronimo/kernel Kernel.java Added: modules/kernel/src/test/org/apache/geronimo/kernel GBeanTest.java Log: Add helper methods for appplications embedding a kernel Revision Changes Path 1.1 incubator-geronimo/modules/kernel/src/test/org/apache/geronimo/kernel/GBeanTest.java Index: GBeanTest.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Geronimo" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * "Apache Geronimo", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * ==================================================================== */ package org.apache.geronimo.kernel; import javax.management.ObjectName; import org.apache.geronimo.gbean.jmx.GBeanMBean; import org.apache.geronimo.kernel.management.State; import junit.framework.TestCase; /** * * * @version $Revision: 1.1 $ $Date: 2004/01/21 22:53:42 $ */ public class GBeanTest extends TestCase { private ObjectName name; private Kernel kernel; public void testLoad() throws Exception { GBeanMBean gbean = new GBeanMBean(MockGBean.getGBeanInfo()); gbean.setAttribute("Name", "Test"); gbean.setAttribute("MutableInt", new Integer(123)); gbean.setAttribute("FinalInt", new Integer(123)); kernel.loadGBean(name, gbean); kernel.startGBean(name); assertEquals(new Integer(State.RUNNING_INDEX), kernel.getMBeanServer().getAttribute(name, "state")); assertEquals("Hello", kernel.getMBeanServer().invoke(name, "doSomething", new Object[]{"Hello"}, new String[] {String.class.getName()})); kernel.stopGBean(name); kernel.unloadGBean(name); } protected void setUp() throws Exception { name = new ObjectName("test:name=MyMockGBean"); kernel = new Kernel("test"); kernel.boot(); } protected void tearDown() throws Exception { kernel.shutdown(); } } 1.6 +69 -1 incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/Kernel.java Index: Kernel.java =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/Kernel.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Kernel.java 19 Jan 2004 06:34:21 -0000 1.5 +++ Kernel.java 21 Jan 2004 22:53:42 -0000 1.6 @@ -73,6 +73,10 @@ import javax.management.NotificationFilter; import javax.management.NotificationListener; import javax.management.ObjectName; +import javax.management.InstanceAlreadyExistsException; +import javax.management.NotCompliantMBeanException; +import javax.management.MBeanException; +import javax.management.ReflectionException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -269,6 +273,70 @@ throw (IllegalStateException) new IllegalStateException("Error deregistering configuration " + configName).initCause(e); } log.info("Unloaded Configuration " + configName); + } + + /** + * Load a specific GBean into this kernel. + * This is intended for applications that are embedding the kernel. + * @param name the name to register the GBean under + * @param gbean the GBean to register + * @throws InstanceAlreadyExistsException if the name is already used + * @throws InvalidConfigException if there is a problem during registration + */ + public void loadGBean(ObjectName name, GBeanMBean gbean) throws InstanceAlreadyExistsException, InvalidConfigException { + try { + mbServer.registerMBean(gbean, name); + } catch (MBeanRegistrationException e) { + throw new InvalidConfigException("Invalid GBean configuration for " + name, e); + } catch (NotCompliantMBeanException e) { + throw new InvalidConfigException("Invalid GBean configuration for " + name, e); + } + } + + /** + * Start a specific GBean. + * @param name the GBean to start + * @throws InstanceNotFoundException if the GBean could not be found + */ + public void startGBean(ObjectName name) throws InstanceNotFoundException, InvalidConfigException { + try { + mbServer.invoke(name, "start", null, null); + } catch (MBeanException e) { + // start is not supposed to throw anything + throw new InvalidConfigException("Invalid GBean configuration for " + name, e); + } catch (ReflectionException e) { + throw new InvalidConfigException("Invalid GBean configuration for " + name, e); + } + } + + /** + * Stop a specific GBean. + * @param name the GBean to stop + * @throws InstanceNotFoundException if the GBean could not be found + */ + public void stopGBean(ObjectName name) throws InstanceNotFoundException, InvalidConfigException { + try { + mbServer.invoke(name, "stop", null, null); + } catch (MBeanException e) { + // stop is not supposed to throw anything + throw new InvalidConfigException("Invalid GBean configuration for " + name, e); + } catch (ReflectionException e) { + throw new InvalidConfigException("Invalid GBean configuration for " + name, e); + } + } + + /** + * Unload a specific GBean. + * This is intended for applications that are embedding the kernel. + * @param name the name of the GBean to unregister + * @throws InstanceNotFoundException if the GBean could not be found + */ + public void unloadGBean(ObjectName name) throws InstanceNotFoundException { + try { + mbServer.unregisterMBean(name); + } catch (MBeanRegistrationException e) { + throw (IllegalStateException) new IllegalStateException("Error unloading GBean " + name).initCause(e); + } } /**