1 package org.hibernate.impl; 3 4 import java.util.Hashtable ; 5 import java.util.Iterator ; 6 import java.util.Properties ; 7 8 import javax.naming.Context ; 9 import javax.naming.InvalidNameException ; 10 import javax.naming.Name ; 11 import javax.naming.NamingException ; 12 import javax.naming.Reference ; 13 import javax.naming.event.EventContext ; 14 import javax.naming.event.NamespaceChangeListener ; 15 import javax.naming.event.NamingEvent ; 16 import javax.naming.event.NamingExceptionEvent ; 17 import javax.naming.event.NamingListener ; 18 import javax.naming.spi.ObjectFactory ; 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 30 public class SessionFactoryObjectFactory implements ObjectFactory { 31 32 private static final SessionFactoryObjectFactory INSTANCE; 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 LISTENER = new NamespaceChangeListener () { 46 public void objectAdded(NamingEvent evt) { 47 log.debug( "A factory was successfully bound to name: " + evt.getNewBinding().getName() ); 48 } 49 public void objectRemoved(NamingEvent evt) { 50 String name = evt.getOldBinding().getName(); 51 log.info("A factory was unbound from name: " + name); 52 Object instance = NAMED_INSTANCES.remove(name); 53 Iterator iter = INSTANCES.values().iterator(); 54 while ( iter.hasNext() ) { 55 if ( iter.next()==instance ) iter.remove(); 56 } 57 } 58 public void objectRenamed(NamingEvent evt) { 59 String 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 evt) { 64 log.warn( "Naming exception occurred accessing factory: " + evt.getException() ); 65 } 66 }; 67 68 public Object getObjectInstance(Object reference, Name name, Context ctx, Hashtable env) throws Exception { 69 log.debug("JNDI lookup: " + name); 70 String uid = (String ) ( (Reference ) reference ).get(0).getContent(); 71 return getInstance(uid); 72 } 73 74 public static void addInstance(String uid, String name, SessionFactory instance, Properties 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 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 ctx = NamingHelper.getInitialContext(properties); 90 NamingHelper.bind(ctx, name, instance); 91 log.info("Bound factory to JNDI name: " + name); 92 ( (EventContext ) ctx ).addNamingListener(name, EventContext.OBJECT_SCOPE, LISTENER); 93 } 94 catch (InvalidNameException ine) { 95 log.error("Invalid JNDI name: " + name, ine); 96 } 97 catch (NamingException ne) { 98 log.warn("Could not bind factory to JNDI", ne); 99 } 100 catch(ClassCastException cce) { 101 log.warn("InitialContext did not implement EventContext"); 102 } 103 104 } 105 106 } 107 108 public static void removeInstance(String uid, String name, Properties properties) { 109 111 if (name!=null) { 112 log.info("Unbinding factory from JNDI name: " + name); 113 114 try { 115 Context ctx = NamingHelper.getInitialContext(properties); 116 ctx.unbind(name); 117 log.info("Unbound factory from JNDI name: " + name); 118 } 119 catch (InvalidNameException ine) { 120 log.error("Invalid JNDI name: " + name, ine); 121 } 122 catch (NamingException 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 getNamedInstance(String name) { 135 log.debug("lookup: name=" + name); 136 Object 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 getInstance(String uid) { 145 log.debug("lookup: uid=" + uid); 146 Object 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 |