Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » jndi » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.jndi;
   18   
   19   import java.util.Enumeration;
   20   import java.util.Hashtable;
   21   import java.util.Properties;
   22   
   23   import javax.naming.Context;
   24   import javax.naming.Name;
   25   import javax.naming.NamingException;
   26   import javax.naming.Reference;
   27   import javax.naming.StringRefAddr;
   28   import javax.naming.spi.ObjectFactory;
   29   
   30   import org.apache.commons.logging.Log;
   31   import org.apache.commons.logging.LogFactory;
   32   
   33   /**
   34    * Converts objects implementing JNDIStorable into a property fields so they can
   35    * be stored and regenerated from JNDI
   36    */
   37   public class JNDIReferenceFactory implements ObjectFactory {
   38   
   39       static Log log = LogFactory.getLog(JNDIReferenceFactory.class);
   40   
   41       /**
   42        * This will be called by a JNDIprovider when a Reference is retrieved from
   43        * a JNDI store - and generates the orignal instance
   44        * 
   45        * @param object the Reference object
   46        * @param name the JNDI name
   47        * @param nameCtx the context
   48        * @param environment the environment settings used by JNDI
   49        * @return the instance built from the Reference object
   50        * @throws Exception if building the instance from Reference fails (usually
   51        *                 class not found)
   52        */
   53       public Object getObjectInstance(Object object, Name name, Context nameCtx, Hashtable environment) throws Exception {
   54           Object result = null;
   55           if (object instanceof Reference) {
   56               Reference reference = (Reference)object;
   57   
   58               if (log.isTraceEnabled()) {
   59                   log.trace("Getting instance of " + reference.getClassName());
   60               }
   61   
   62               Class theClass = loadClass(this, reference.getClassName());
   63               if (JNDIStorableInterface.class.isAssignableFrom(theClass)) {
   64   
   65                   JNDIStorableInterface store = (JNDIStorableInterface)theClass.newInstance();
   66                   Properties properties = new Properties();
   67                   for (Enumeration iter = reference.getAll(); iter.hasMoreElements();) {
   68   
   69                       StringRefAddr addr = (StringRefAddr)iter.nextElement();
   70                       properties.put(addr.getType(), (addr.getContent() == null) ? "" : addr.getContent());
   71   
   72                   }
   73                   store.setProperties(properties);
   74                   result = store;
   75               }
   76           } else {
   77               log.error("Object " + object + " is not a reference - cannot load");
   78               throw new RuntimeException("Object " + object + " is not a reference");
   79           }
   80           return result;
   81       }
   82   
   83       /**
   84        * Create a Reference instance from a JNDIStorable object
   85        * 
   86        * @param instanceClassName
   87        * @param po
   88        * @return
   89        * @throws NamingException
   90        */
   91   
   92       public static Reference createReference(String instanceClassName, JNDIStorableInterface po) throws NamingException {
   93           if (log.isTraceEnabled()) {
   94               log.trace("Creating reference: " + instanceClassName + "," + po);
   95           }
   96           Reference result = new Reference(instanceClassName, JNDIReferenceFactory.class.getName(), null);
   97           try {
   98               Properties props = po.getProperties();
   99               for (Enumeration iter = props.propertyNames(); iter.hasMoreElements();) {
  100                   String key = (String)iter.nextElement();
  101                   String value = props.getProperty(key);
  102                   javax.naming.StringRefAddr addr = new javax.naming.StringRefAddr(key, value);
  103                   result.add(addr);
  104               }
  105           } catch (Exception e) {
  106               log.error(e.getMessage(), e);
  107               throw new NamingException(e.getMessage());
  108           }
  109           return result;
  110       }
  111   
  112       /**
  113        * Retrieve the class loader for a named class
  114        * 
  115        * @param thisObj
  116        * @param className
  117        * @return
  118        * @throws ClassNotFoundException
  119        */
  120   
  121       public static Class loadClass(Object thisObj, String className) throws ClassNotFoundException {
  122           // tryu local ClassLoader first.
  123           ClassLoader loader = thisObj.getClass().getClassLoader();
  124           Class theClass;
  125           if (loader != null) {
  126               theClass = loader.loadClass(className);
  127           } else {
  128               // Will be null in jdk1.1.8
  129               // use default classLoader
  130               theClass = Class.forName(className);
  131           }
  132           return theClass;
  133       }
  134   
  135   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » jndi » [javadoc | source]