Re: [appfuse-user] Setting appContext in jndi

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [appfuse-user] Setting appContext in jndi

Matt Raible-3
Here's the code that a colleague of mine wrote to do this.  I changed
some names in the code to protect the innocent.

<snip>
Its the AppContextJMXBean and the BaseMBean.  This is made to startup on
JBoss as an MBean.  To make it used on other appservers, you need to
make a Serializable wrapper around the AppContext and bind that to JNDI.
 If you are interested I can explain how to do that.

Here is the code (2 classes):

package org.appfuse.jmx;

import  org.appfuse.jmx.AppContextJMXMBean;

import org.jboss.naming.NonSerializableFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.access.ContextSingletonBeanFactoryLocator;

import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;

/**
 * AppContextJMX.java
 *
 * @author Jeff Genender
 * @version $Id: AppContextJMX.java,v 1.13 2004/10/19 22:07:51
jgenender Exp $
 *
 * @jmx.mbean name="AppFuse:name=appContextJMX"
 * description="Container for the AppFuseSpring Application Context"
 * display-name="AppFuse-Spring Application Context"
 *
 * @jboss.service servicefile="jboss"
 */
public class AppContextJMX extends BaseMBean implements AppContextJMXMBean
{

    /* Globals */
    public final String MAIN_APP_CONTEXT_KEY = "appfuse.main";
    public final String MAIN_APP_BEAN_REF =
"conf/spring/beanRefFactory-root.xml";

    /* members */
    private ApplicationContext _context = null;
    private BeanFactoryReference _beanFactoryRef = null;
    private String _jndiName = "java:AppFuseAppContext";
    private boolean _started = false;


    /**
     * Default constructor
     *
     * @jmx:managed-constructor description="Default constructor"
     */
    public AppContextJMX()
    {
    }

    /**
     * Get thread count
     * @jmx:managed-operation description="Bound JNDI application name"
     */
    public String getStatus()
    {
        if (_context != null)
            return "<font color='green'>Context loaded</font>";

        return "<font color='red'>No context loaded</font>";
    }

    /**
     * Get thread count
     * @return String the number of threads running
     * @jmx:managed-operation description="Bound JNDI application name"
     */
    public String getJndiName()
    {
       return _jndiName;
    }

    /**
     * Starting service
     * @jmx:managed-operation description="Starts the service"
     */
    public void start() throws Exception
    {
        if (!_started){
            _log.info("Starting service");

            BeanFactoryLocator bfr =
                    ContextSingletonBeanFactoryLocator.getInstance(
MAIN_APP_BEAN_REF );
            _beanFactoryRef = bfr.useBeanFactory( MAIN_APP_CONTEXT_KEY );
            _context = ( ApplicationContext ) _beanFactoryRef.getFactory();
            _started = true;

            if ( _context != null )
            {
                _log.info( MAIN_APP_BEAN_REF + " Spring context
successfully loaded from " + MAIN_APP_BEAN_REF );
            }
            else
            {
                _log.info( MAIN_APP_CONTEXT_KEY + " Spring context
FAILED to be loaded!");
            }

            rebind(_context, _jndiName);
        }
    }

    /**
     * Stopping service
     * @jmx:managed-operation description="Stops the service"
     */
    public void stop()
    {
        if (_started){
            _log.info("Stopping service");
            _started = false;
            unbind(_jndiName);
            if ( _beanFactoryRef != null )
                _beanFactoryRef.release();

            _context = null;
            _beanFactoryRef = null;
        }
     }

}


package org.appfuse.jmx;

import org.jboss.naming.NonSerializableFactory;

import javax.naming.*;

/**
 * User: Jeff Genender
 * Date: Oct 19, 2004
 * Time: 3:11:38 PM
 */
public class BaseMBean {
    protected static Context createContext(Context rootContext, Name
name) throws NamingException
    {
        Context subctx = rootContext;
        for(int n = 0; n < name.size(); n ++)
        {
            String atom = name.get(n);
            try
            {
                Object obj = subctx.lookup(atom);
                subctx = (Context) obj;
            }
            catch(NamingException e)
            {  // No binding exists, create a subcontext
                subctx = subctx.createSubcontext(atom);
            }
        }

        return subctx;
    }

    protected void rebind(Object obj, String jndiName) throws
NamingException
    {
        InitialContext rootCtx = new InitialContext();
        // Get the parent context into which we are to bind
        Name fullName = rootCtx.getNameParser("").parse(jndiName);
        Name parentName = fullName;
        if( fullName.size() > 1 )
            parentName = fullName.getPrefix(fullName.size()-1);
        else
            parentName = new CompositeName();
        Context parentCtx = createContext(rootCtx, parentName);
        Name atomName = fullName.getSuffix(fullName.size()-1);
        String atom = atomName.get(0);
        NonSerializableFactory.rebind(parentCtx, atom, obj);
    }

    protected void unbind(String jndiName)
    {
        try
        {
            Context rootCtx = (Context) new InitialContext();
            rootCtx.unbind(jndiName);
            NonSerializableFactory.unbind(jndiName);
        }
        catch(NamingException e)
        {
            e.printStackTrace();
        }
    }

}

</snip>

On 6/29/05, Matt Raible <[hidden email]> wrote:

> I have colleagues that did this on a recent project.  I'll try to get
> some code from them later today.
>
> Matt
>
> On 6/27/05, Abdullah Jibaly <[hidden email]> wrote:
> > Hi fusers,
> >
> > I want to put the spring ApplicationContext into JNDI
> > and am not quite sure where to start. I setup a bean
> > like:
> >
> > <bean id="jndiAppContext"
> > class="org.springframework.jndi.JndiObjectFactoryBean">
> >         <property
> > name="jndiName"><value>myApp/appCtxt</value></property>
> >     </bean>
> >
> > but how do I set it from the StartupListener? Am I
> > going about this right? The reason I want to put the
> > applicationContext into JNDI scope is to access the
> > beans from another application in the same container.
> >
> > Thanks!
> > Abdullah
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...