KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jndi > JNDIReferenceFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.jndi;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.naming.spi.ObjectFactory JavaDoc;
24 import javax.naming.Name JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.Reference JavaDoc;
27 import javax.naming.StringRefAddr JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Enumeration JavaDoc;
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 JavaDoc {
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 class
51      * not found)
52      */

53     public Object JavaDoc getObjectInstance(Object JavaDoc object, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc environment) throws Exception JavaDoc {
54         Object JavaDoc result = null;
55         if (object instanceof Reference JavaDoc) {
56             Reference JavaDoc reference = (Reference JavaDoc) object;
57
58             if (log.isTraceEnabled()) {
59                 log.trace("Getting instance of " + reference.getClassName());
60             }
61
62             Class JavaDoc theClass = loadClass(this, reference.getClassName());
63             if (JNDIStorableInterface.class.isAssignableFrom(theClass)) {
64
65                 JNDIStorableInterface store = (JNDIStorableInterface) theClass.newInstance();
66                 Properties JavaDoc properties = new Properties JavaDoc();
67                 for (Enumeration JavaDoc iter = reference.getAll(); iter.hasMoreElements();) {
68
69                     StringRefAddr JavaDoc addr = (StringRefAddr JavaDoc) iter.nextElement();
70                     properties.put(addr.getType(), (addr.getContent() == null) ? "" : addr.getContent());
71
72                 }
73                 store.setProperties(properties);
74                 result = store;
75             }
76         }
77         else {
78             log.error("Object " + object + " is not a reference - cannot load");
79             throw new RuntimeException JavaDoc("Object " + object + " is not a reference");
80         }
81         return result;
82     }
83
84     /**
85      * Create a Reference instance from a JNDIStorable object
86      *
87      * @param instanceClassName
88      * @param po
89      * @return @throws
90      * NamingException
91      */

92
93     public static Reference JavaDoc createReference(String JavaDoc instanceClassName, JNDIStorableInterface po) throws NamingException JavaDoc {
94         if (log.isTraceEnabled()) {
95             log.trace("Creating reference: " + instanceClassName + "," + po);
96         }
97         Reference JavaDoc result = new Reference JavaDoc(instanceClassName, JNDIReferenceFactory.class.getName(), null);
98         try {
99             Properties JavaDoc props = po.getProperties();
100             for (Enumeration JavaDoc iter = props.propertyNames(); iter.hasMoreElements();) {
101                 String JavaDoc key = (String JavaDoc) iter.nextElement();
102                 String JavaDoc value = props.getProperty(key);
103                 javax.naming.StringRefAddr JavaDoc addr = new javax.naming.StringRefAddr JavaDoc(key, value);
104                 result.add(addr);
105             }
106         }
107         catch (Exception JavaDoc e) {
108             log.error(e.getMessage(), e);
109             throw new NamingException JavaDoc(e.getMessage());
110         }
111         return result;
112     }
113
114     /**
115      * Retrieve the class loader for a named class
116      *
117      * @param thisObj
118      * @param className
119      * @return @throws
120      * ClassNotFoundException
121      */

122
123     public static Class JavaDoc loadClass(Object JavaDoc thisObj, String JavaDoc className) throws ClassNotFoundException JavaDoc {
124         // tryu local ClassLoader first.
125
ClassLoader JavaDoc loader = thisObj.getClass().getClassLoader();
126         Class JavaDoc theClass;
127         if (loader != null) {
128             theClass = loader.loadClass(className);
129         }
130         else {
131             // Will be null in jdk1.1.8
132
// use default classLoader
133
theClass = Class.forName(className);
134         }
135         return theClass;
136     }
137
138 }
139
Popular Tags