KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > impl > SessionFactoryObjectFactory


1 //$Id: SessionFactoryObjectFactory.java,v 1.9 2004/10/06 23:28:00 epbernard Exp $
2
package org.hibernate.impl;
3
4 import java.util.Hashtable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InvalidNameException JavaDoc;
10 import javax.naming.Name JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12 import javax.naming.Reference JavaDoc;
13 import javax.naming.event.EventContext JavaDoc;
14 import javax.naming.event.NamespaceChangeListener JavaDoc;
15 import javax.naming.event.NamingEvent JavaDoc;
16 import javax.naming.event.NamingExceptionEvent JavaDoc;
17 import javax.naming.event.NamingListener JavaDoc;
18 import javax.naming.spi.ObjectFactory JavaDoc;
19
20 import org.hibernate.SessionFactory;
21 import org.hibernate.util.FastHashMap;
22 import org.hibernate.util.NamingHelper;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * Resolves <tt>SessionFactory</tt> JNDI lookups and deserialization
29  */

30 public class SessionFactoryObjectFactory implements ObjectFactory JavaDoc {
31
32     private static final SessionFactoryObjectFactory INSTANCE; //to stop the class from being unloaded
33

34     private static final Log log;
35
36     static {
37         log = LogFactory.getLog(SessionFactoryObjectFactory.class);
38         INSTANCE = new SessionFactoryObjectFactory();
39         log.debug("initializing class SessionFactoryObjectFactory");
40     }
41
42     private static final FastHashMap INSTANCES = new FastHashMap();
43     private static final FastHashMap NAMED_INSTANCES = new FastHashMap();
44
45     private static final NamingListener JavaDoc LISTENER = new NamespaceChangeListener JavaDoc() {
46         public void objectAdded(NamingEvent JavaDoc evt) {
47             log.debug( "A factory was successfully bound to name: " + evt.getNewBinding().getName() );
48         }
49         public void objectRemoved(NamingEvent JavaDoc evt) {
50             String JavaDoc name = evt.getOldBinding().getName();
51             log.info("A factory was unbound from name: " + name);
52             Object JavaDoc instance = NAMED_INSTANCES.remove(name);
53             Iterator JavaDoc iter = INSTANCES.values().iterator();
54             while ( iter.hasNext() ) {
55                 if ( iter.next()==instance ) iter.remove();
56             }
57         }
58         public void objectRenamed(NamingEvent JavaDoc evt) {
59             String JavaDoc name = evt.getOldBinding().getName();
60             log.info("A factory was renamed from name: " + name);
61             NAMED_INSTANCES.put( evt.getNewBinding().getName(), NAMED_INSTANCES.remove(name) );
62         }
63         public void namingExceptionThrown(NamingExceptionEvent JavaDoc evt) {
64             log.warn( "Naming exception occurred accessing factory: " + evt.getException() );
65         }
66     };
67
68     public Object JavaDoc getObjectInstance(Object JavaDoc reference, Name JavaDoc name, Context JavaDoc ctx, Hashtable JavaDoc env) throws Exception JavaDoc {
69         log.debug("JNDI lookup: " + name);
70         String JavaDoc uid = (String JavaDoc) ( (Reference JavaDoc) reference ).get(0).getContent();
71         return getInstance(uid);
72     }
73
74     public static void addInstance(String JavaDoc uid, String JavaDoc name, SessionFactory instance, Properties JavaDoc properties) {
75
76         log.debug("registered: " + uid + " (" + ( (name==null) ? "unnamed" : name ) + ')');
77         INSTANCES.put(uid, instance);
78         if (name!=null) NAMED_INSTANCES.put(name, instance);
79
80         //must add to JNDI _after_ adding to HashMaps, because some JNDI servers use serialization
81
if (name==null) {
82             log.info("Not binding factory to JNDI, no JNDI name configured");
83         }
84         else {
85
86             log.info("Factory name: " + name);
87
88             try {
89                 Context JavaDoc ctx = NamingHelper.getInitialContext(properties);
90                 NamingHelper.bind(ctx, name, instance);
91                 log.info("Bound factory to JNDI name: " + name);
92                 ( (EventContext JavaDoc) ctx ).addNamingListener(name, EventContext.OBJECT_SCOPE, LISTENER);
93             }
94             catch (InvalidNameException JavaDoc ine) {
95                 log.error("Invalid JNDI name: " + name, ine);
96             }
97             catch (NamingException JavaDoc ne) {
98                 log.warn("Could not bind factory to JNDI", ne);
99             }
100             catch(ClassCastException JavaDoc cce) {
101                 log.warn("InitialContext did not implement EventContext");
102             }
103
104         }
105
106     }
107
108     public static void removeInstance(String JavaDoc uid, String JavaDoc name, Properties JavaDoc properties) {
109         //TODO: theoretically non-threadsafe...
110

111         if (name!=null) {
112             log.info("Unbinding factory from JNDI name: " + name);
113
114             try {
115                 Context JavaDoc ctx = NamingHelper.getInitialContext(properties);
116                 ctx.unbind(name);
117                 log.info("Unbound factory from JNDI name: " + name);
118             }
119             catch (InvalidNameException JavaDoc ine) {
120                 log.error("Invalid JNDI name: " + name, ine);
121             }
122             catch (NamingException JavaDoc ne) {
123                 log.warn("Could not unbind factory from JNDI", ne);
124             }
125
126             NAMED_INSTANCES.remove(name);
127
128         }
129
130         INSTANCES.remove(uid);
131
132     }
133
134     public static Object JavaDoc getNamedInstance(String JavaDoc name) {
135         log.debug("lookup: name=" + name);
136         Object JavaDoc result = NAMED_INSTANCES.get(name);
137         if (result==null) {
138             log.warn("Not found: " + name);
139             log.debug(NAMED_INSTANCES);
140         }
141         return result;
142     }
143
144     public static Object JavaDoc getInstance(String JavaDoc uid) {
145         log.debug("lookup: uid=" + uid);
146         Object JavaDoc result = INSTANCES.get(uid);
147         if (result==null) {
148             log.warn("Not found: " + uid);
149             log.debug(INSTANCES);
150         }
151         return result;
152     }
153
154 }
155
156
157
158
159
160
161
162
Popular Tags