View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * 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   **/
    18
    19  package org.codehaus.activemq.jndi;
    20
    21  import org.apache.commons.logging.Log;
    22  import org.apache.commons.logging.LogFactory;
    23
    24  import javax.naming.Context;
    25  import javax.naming.Name;
    26  import javax.naming.NamingException;
    27  import javax.naming.Reference;
    28  import javax.naming.StringRefAddr;
    29  import javax.naming.spi.ObjectFactory;
    30  import java.util.Enumeration;
    31  import java.util.Hashtable;
    32  import java.util.Properties;
    33
    34  /***
    35   * Converts objects implementing JNDIStorable into a property fields so they can
    36   * be stored and regenerated from JNDI
    37   */
    38  public class JNDIReferenceFactory implements ObjectFactory {
    39
    40      static Log log = LogFactory.getLog(JNDIReferenceFactory.class);
    41
    42      /***
    43       * This will be called by a JNDIprovider when a Reference is retrieved from
    44       * a JNDI store - and generates the orignal instance
    45       *
    46       * @param object      the Reference object
    47       * @param name        the JNDI name
    48       * @param nameCtx     the context
    49       * @param environment the environment settings used by JNDI
    50       * @return the instance built from the Reference object
    51       * @throws Exception if building the instance from Reference fails (usually class
    52       *                   not found)
    53       */
    54      public Object getObjectInstance(Object object, Name name, Context nameCtx, Hashtable environment) throws Exception {
    55          Object result = null;
    56          if (object instanceof Reference) {
    57              Reference reference = (Reference) object;
    58
    59              if (log.isTraceEnabled()) {
    60                  log.trace("Getting instance of " + reference.getClassName());
    61              }
    62
    63              Class theClass = loadClass(this, reference.getClassName());
    64              if (JNDIStorableInterface.class.isAssignableFrom(theClass)) {
    65
    66                  JNDIStorableInterface store = (JNDIStorableInterface) theClass.newInstance();
    67                  Properties properties = new Properties();
    68                  for (Enumeration iter = reference.getAll(); iter.hasMoreElements();) {
    69
    70                      StringRefAddr addr = (StringRefAddr) iter.nextElement();
    71                      properties.put(addr.getType(), (addr.getContent() == null) ? "" : addr.getContent());
    72
    73                  }
    74                  store.setProperties(properties);
    75                  result = store;
    76              }
    77          }
    78          else {
    79              log.error("Object " + object + " is not a reference - cannot load");
    80              throw new RuntimeException("Object " + object + " is not a reference");
    81          }
    82          return result;
    83      }
    84
    85      /***
    86       * Create a Reference instance from a JNDIStorable object
    87       *
    88       * @param instanceClassName
    89       * @param po
    90       * @return @throws
    91       *         NamingException
    92       */
    93
    94      public static Reference createReference(String instanceClassName, JNDIStorableInterface po) throws NamingException {
    95          if (log.isTraceEnabled()) {
    96              log.trace("Creating reference: " + instanceClassName + "," + po);
    97          }
    98          Reference result = new Reference(instanceClassName, JNDIReferenceFactory.class.getName(), null);
    99          try {
    100             Properties props = po.getProperties();
    101             for (Enumeration iter = props.propertyNames(); iter.hasMoreElements();) {
    102                 String key = (String) iter.nextElement();
    103                 String value = props.getProperty(key);
    104                 javax.naming.StringRefAddr addr = new javax.naming.StringRefAddr(key, value);
    105                 result.add(addr);
    106             }
    107         }
    108         catch (Exception e) {
    109             log.error(e.getMessage(), e);
    110             throw new NamingException(e.getMessage());
    111         }
    112         return result;
    113     }
    114
    115     /***
    116      * Retrieve the class loader for a named class
    117      *
    118      * @param thisObj
    119      * @param className
    120      * @return @throws
    121      *         ClassNotFoundException
    122      */
    123
    124     public static Class loadClass(Object thisObj, String className) throws ClassNotFoundException {
    125         // tryu local ClassLoader first.
    126         ClassLoader loader = thisObj.getClass().getClassLoader();
    127         Class theClass;
    128         if (loader != null) {
    129             theClass = loader.loadClass(className);
    130         }
    131         else {
    132             // Will be null in jdk1.1.8
    133             // use default classLoader
    134             theClass = Class.forName(className);
    135         }
    136         return theClass;
    137     }
    138
    139 }
    140