User: stark   
  Date: 01/01/13 17:28:38

  Added:       src/main/org/jboss/naming ExternalContext.java
                        ExternalContextMBean.java
                        NonSerializableFactory.java
  Log:
  ExternalContext is an mbean implementation that allows one to bind external
  JNDI contexts into the JBoss server JNDI namespace.
  
  Revision  Changes    Path
  1.1                  jboss/src/main/org/jboss/naming/ExternalContext.java
  
  Index: ExternalContext.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.naming;
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.net.URL;
  import java.util.Properties;
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  import javax.naming.CompositeName;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.Name;
  import javax.naming.NamingException;
  import javax.naming.Reference;
  import javax.naming.StringRefAddr;
  
  import org.jnp.server.Main;
  
  import org.jboss.logging.Log;
  import org.jboss.util.ServiceMBeanSupport;
  
  /** A MBean that binds an arbitrary InitialContext into the JBoss default
  InitialContext as a Reference to a nonserializable object.
  
  @see org.jboss.naming.NonSerializableFactory
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
   */
  public class ExternalContext extends ServiceMBeanSupport implements 
ExternalContextMBean
  {
      // Constants -----------------------------------------------------
  
      // Attributes ----------------------------------------------------
      private String jndiName;
      private Properties contextProps;
  
      // Static --------------------------------------------------------
  
      // Constructors --------------------------------------------------
      public ExternalContext()
      {
      }
      public ExternalContext(String jndiName, String contextPropsURL) throws 
IOException
      {
          setJndiName(jndiName);
          setProperties(contextPropsURL);
      }
  
      // Public --------------------------------------------------------
      /** Set the jndi name under which the external context is bound.
      */
      public String getJndiName()
      {
         return jndiName;
      }
      /** Set the jndi name under which the external context is bound.
       */
      public void setJndiName(String jndiName)
      {
         this.jndiName = jndiName;
      }
  
      public void setProperties(String contextPropsURL) throws IOException
      {
          InputStream is = null;
          IOException ex = null;
          contextProps = new Properties();
  
          // See if this is a URL we can load
          try
          {
              URL url = new URL(contextPropsURL);
              is = url.openStream();
              contextProps.load(is);
              return;
          }
          catch(IOException e)
          {   // Failed, try to locate a classpath resource below
              is = null;
              ex = e;
          }
  
          is = 
Thread.currentThread().getContextClassLoader().getResourceAsStream(contextPropsURL);
          if( is == null )
          {
              if( ex != null )
                  throw ex;
              throw new IOException("Failed to locate context props as URL or 
resource:"+contextPropsURL);
          }
          contextProps.load(is);
          if( log != null )
              log.debug("ContextProps: "+contextProps);
      }
  
      public ObjectName getObjectName(MBeanServer server, ObjectName name)
        throws javax.management.MalformedObjectNameException
      {
          return new ObjectName(OBJECT_NAME);
      }
  
      public String getName()
      {
          return "ExternalContext(" + jndiName + ")";
      }
  
      public void initService()
        throws Exception
      {
      }
  
      public void startService()
        throws Exception
      {
          Context rootCtx = (Context) new InitialContext();
          InitialContext ctx = new InitialContext(contextProps);
          log.debug("ctx="+ctx+", env="+ctx.getEnvironment());
          // Get the parent context into which we are to bind
          Name fullName = rootCtx.getNameParser("").parse(jndiName);
          log.debug("fullName="+fullName);
          Name parentName = fullName;
          if( fullName.size() > 1 )
              parentName = fullName.getPrefix(fullName.size()-1);
          else
              parentName = new CompositeName();
          log.debug("parentName="+parentName);
          Context parentCtx = createContext(rootCtx, parentName);
          log.debug("parentCtx="+parentCtx);
          // Place the external context into the NonSerializableFactory hashmap
          NonSerializableFactory.rebind(jndiName, ctx);
  
          // Bind a reference to the extern context using NonSerializableFactory as 
the ObjectFactory
          String className = "javax.naming.Context";
          String factory = NonSerializableFactory.class.getName();
          StringRefAddr addr = new StringRefAddr("nns", jndiName);
          Reference memoryRef = new Reference(className, addr, factory, null);
          Name atom = fullName.getSuffix(fullName.size()-1);
          parentCtx.rebind(atom, memoryRef);
      }
  
      public void stopService()
      {
          try
          {
              Context rootCtx = (Context) new InitialContext();
              Context ctx = (Context) rootCtx.lookup(jndiName);
              if( ctx != null )
                  ctx.close();
              rootCtx.unbind(jndiName);
              NonSerializableFactory.unbind(jndiName);
          }
          catch(NamingException e)
          {
              log.exception(e);
          }
      }
  
      // Protected -----------------------------------------------------
      private 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;
      }
  }
  
  
  
  1.1                  jboss/src/main/org/jboss/naming/ExternalContextMBean.java
  
  Index: ExternalContextMBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.naming;
  
  import java.io.IOException;
  import javax.naming.NamingException;
  
  /**
   *   
   *      
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public interface ExternalContextMBean extends org.jboss.util.ServiceMBean
  {
      // Constants -----------------------------------------------------
      public static final String OBJECT_NAME = ":service=ExternalContext";
      
      // Public --------------------------------------------------------
  
      /** Set the jndi name under which the external context is bound.
      */
      public String getJndiName();
      /** Set the jndi name under which the external context is bound.
      */
      public void setJndiName(String jndiName);
  
      /** Set the jndi.properties information for the external InitialContext.
      This is either a URL string or a classpath resource name. Examples:
          file:///config/myldap.properties
          http://config.mycompany.com/myldap.properties
          /conf/myldap.properties
          myldap.properties
  
      @param contextPropsURL, either a URL string to a jndi.properties type of
          content or a name of a resource to locate via the current thread
          context classpath.
      @throws IOException, thrown if the url/resource cannot be loaded.
      */
      public void setProperties(String contextPropsURL) throws IOException;
  }
  
  
  
  1.1                  jboss/src/main/org/jboss/naming/NonSerializableFactory.java
  
  Index: NonSerializableFactory.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.naming;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Hashtable;
  import java.util.Map;
  import javax.naming.Context;
  import javax.naming.Name;
  import javax.naming.NameAlreadyBoundException;
  import javax.naming.NameNotFoundException;
  import javax.naming.Reference;
  import javax.naming.RefAddr;
  import javax.naming.spi.ObjectFactory;
  
  /** A utility class that allows one to bind a non-serializable object into a
  local JNDI context. The binding will only be valid for the lifetime of the
  VM in which the JNDI InitialContext lives. An example usage code snipet is:
  
  <code>
      // The non-Serializable object to bind
      Object nonserializable = ...;
      // An arbitrary key to use in the StringRefAddr. The best key is the jndi
      // name that the object will be bound under.
      String key = ...;
      // This places nonserializable into the NonSerializableFactory hashmap under key
      NonSerializableFactory.rebind(key, nonserializable);
  
      Context ctx = new InitialContext();
      // Bind a reference to nonserializable using NonSerializableFactory as the 
ObjectFactory
      String className = nonserializable.getClass().getName();
      String factory = NonSerializableFactory.class.getName();
      StringRefAddr addr = new StringRefAddr("nns", key);
      Reference memoryRef = new Reference(className, addr, factory, null);
      ctx.rebind(key, memoryRef);
  </code>
  
  @see javax.naming.spi.ObjectFactory
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class NonSerializableFactory implements ObjectFactory
  {
        private static Map wrapperMap = Collections.synchronizedMap(new HashMap());
  
      /** Place an object into the NonSerializableFactory namespce for subsequent
      access by getObject. There cannot be an existing binding for key.
  
      @param key, the name to bind target under. This should typically be the
      name that will be used to bind target in the JNDI namespace, but it does
      not have to be.
      @param target, the non-Serializable object to bind.
      @throws NameAlreadyBoundException, thrown if key already exists in the
       NonSerializableFactory map
      */
        public static synchronized void bind(String key, Object target) throws 
NameAlreadyBoundException
        {
          if( wrapperMap.containsKey(key) == true )
              throw new NameAlreadyBoundException(key+" already exists in the 
NonSerializableFactory map");
                wrapperMap.put(key, target);
        }
      /** Place or replace an object in the NonSerializableFactory namespce
       for subsequent access by getObject. Any existing binding for key will be
       replaced by target.
  
      @param key, the name to bind target under. This should typically be the
      name that will be used to bind target in the JNDI namespace, but it does
      not have to be.
      @param target, the non-Serializable object to bind.
      */
        public static void rebind(String key, Object target)
        {
                wrapperMap.put(key, target);
        }
  
      /** Remove a binding from the NonSerializableFactory map.
  
      @param key, the key into the NonSerializableFactory map to remove.
      @param target, the non-Serializable object to bind.
      @throws NameNotFoundException, thrown if key does not exist in the
       NonSerializableFactory map
      */
      public static void unbind(String key) throws NameNotFoundException
      {
          if( wrapperMap.remove(key) == null )
              throw new NameNotFoundException(key+" was not found in the 
NonSerializableFactory map");
      }
  
  // --- Begin ObjectFactory interface methods
      /** Transform the obj Reference bound into the JNDI namespace into the
      actual non-Serializable object.
  
      @param obj, the object bound in the JNDI namespace. This must be an 
implementation
      of javax.naming.Reference with a javax.naming.RefAddr of type "nns" whose
      content is the String key used to location the non-Serializable object in the 
      NonSerializableFactory map.
      @param name, ignored.
      @param nameCtx, ignored.
      @param env, ignored.
  
      @return the non-Serializable object associated with the obj Reference if one
      exists, null if one does not.
      */
      public Object getObjectInstance(Object obj, Name name, Context nameCtx, 
Hashtable env)
          throws Exception
      { // Get the nns value from the Reference obj and use it as the map key
          Reference ref = (Reference) obj;
          RefAddr addr = ref.get("nns");
          String key = (String) addr.getContent();
          Object target = wrapperMap.get(key);
          return target;
      }
  // --- End ObjectFactory interface methods
  }
  
  
  

Reply via email to