FileDocCategorySizeDatePackage
SessionFactoryObjectFactory.javaAPI DocHibernate 3.2.54815Thu Mar 22 16:43:14 GMT 2007org.hibernate.impl

SessionFactoryObjectFactory

public class SessionFactoryObjectFactory extends Object implements ObjectFactory
Resolves SessionFactory JNDI lookups and deserialization

Fields Summary
private static final SessionFactoryObjectFactory
INSTANCE
private static final Log
log
private static final org.hibernate.util.FastHashMap
INSTANCES
private static final org.hibernate.util.FastHashMap
NAMED_INSTANCES
private static final NamingListener
LISTENER
Constructors Summary
Methods Summary
public static voidaddInstance(java.lang.String uid, java.lang.String name, org.hibernate.SessionFactory instance, java.util.Properties properties)


		log.debug("registered: " + uid + " (" + ( (name==null) ? "unnamed" : name ) + ')");
		INSTANCES.put(uid, instance);
		if (name!=null) NAMED_INSTANCES.put(name, instance);

		//must add to JNDI _after_ adding to HashMaps, because some JNDI servers use serialization
		if (name==null) {
			log.info("Not binding factory to JNDI, no JNDI name configured");
		}
		else {

			log.info("Factory name: " + name);

			try {
				Context ctx = NamingHelper.getInitialContext(properties);
				NamingHelper.bind(ctx, name, instance);
				log.info("Bound factory to JNDI name: " + name);
				( (EventContext) ctx ).addNamingListener(name, EventContext.OBJECT_SCOPE, LISTENER);
			}
			catch (InvalidNameException ine) {
				log.error("Invalid JNDI name: " + name, ine);
			}
			catch (NamingException ne) {
				log.warn("Could not bind factory to JNDI", ne);
			}
			catch(ClassCastException cce) {
				log.warn("InitialContext did not implement EventContext");
			}

		}

	
public static java.lang.ObjectgetInstance(java.lang.String uid)

		log.debug("lookup: uid=" + uid);
		Object result = INSTANCES.get(uid);
		if (result==null) {
			log.debug("Not found: " + uid);
			log.debug(INSTANCES);
		}
		return result;
	
public static java.lang.ObjectgetNamedInstance(java.lang.String name)

		log.debug("lookup: name=" + name);
		Object result = NAMED_INSTANCES.get(name);
		if (result==null) {
			log.debug("Not found: " + name);
			log.debug(NAMED_INSTANCES);
		}
		return result;
	
public java.lang.ObjectgetObjectInstance(java.lang.Object reference, javax.naming.Name name, javax.naming.Context ctx, java.util.Hashtable env)


	            
		log.debug("JNDI lookup: " + name);
		String uid = (String) ( (Reference) reference ).get(0).getContent();
		return getInstance(uid);
	
public static voidremoveInstance(java.lang.String uid, java.lang.String name, java.util.Properties properties)

		//TODO: theoretically non-threadsafe...

		if (name!=null) {
			log.info("Unbinding factory from JNDI name: " + name);

			try {
				Context ctx = NamingHelper.getInitialContext(properties);
				ctx.unbind(name);
				log.info("Unbound factory from JNDI name: " + name);
			}
			catch (InvalidNameException ine) {
				log.error("Invalid JNDI name: " + name, ine);
			}
			catch (NamingException ne) {
				log.warn("Could not unbind factory from JNDI", ne);
			}

			NAMED_INSTANCES.remove(name);

		}

		INSTANCES.remove(uid);