1 package org.hibernate.jmx; 3 4 import java.io.InvalidObjectException ; 5 import java.io.ObjectStreamException ; 6 import java.io.Serializable ; 7 import java.sql.Connection ; 8 import java.util.Map ; 9 10 import javax.naming.NamingException ; 11 import javax.naming.Reference ; 12 import javax.naming.StringRefAddr ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.hibernate.AssertionFailure; 17 import org.hibernate.HibernateException; 18 import org.hibernate.Interceptor; 19 import org.hibernate.SessionFactory; 20 import org.hibernate.StatelessSession; 21 import org.hibernate.id.IdentifierGenerator; 22 import org.hibernate.id.UUIDHexGenerator; 23 import org.hibernate.impl.SessionFactoryObjectFactory; 24 import org.hibernate.metadata.ClassMetadata; 25 import org.hibernate.metadata.CollectionMetadata; 26 import org.hibernate.stat.Statistics; 27 28 35 public class SessionFactoryStub implements SessionFactory { 36 37 private static final Log log = LogFactory.getLog(SessionFactoryStub.class); 38 39 private static final IdentifierGenerator UUID_GENERATOR = new UUIDHexGenerator(); 40 41 private transient SessionFactory impl; 42 private transient HibernateService service; 43 private String uuid; 44 private String name; 45 46 SessionFactoryStub(HibernateService service) { 47 this.service = service; 48 this.name = service.getJndiName(); 49 try { 50 uuid = (String ) UUID_GENERATOR.generate(null, null); 51 } 52 catch (Exception e) { 53 throw new AssertionFailure("Could not generate UUID"); 54 } 55 56 SessionFactoryObjectFactory.addInstance( uuid, name, this, service.getProperties() ); 57 } 58 59 public org.hibernate.classic.Session openSession(Connection connection, Interceptor interceptor) { 60 return getImpl().openSession(connection, interceptor); 61 } 62 63 public org.hibernate.classic.Session openSession(Interceptor interceptor) throws HibernateException { 64 return getImpl().openSession(interceptor); 65 } 66 67 public org.hibernate.classic.Session openSession() throws HibernateException { 68 return getImpl().openSession(); 69 } 70 71 public org.hibernate.classic.Session openSession(Connection conn) { 72 return getImpl().openSession(conn); 73 } 74 75 public org.hibernate.classic.Session getCurrentSession() { 76 return getImpl().getCurrentSession(); 77 } 78 79 private synchronized SessionFactory getImpl() { 80 if (impl==null) impl = service.buildSessionFactory(); 81 return impl; 82 } 83 84 private Object readResolve() throws ObjectStreamException { 86 Object result = SessionFactoryObjectFactory.getInstance(uuid); 88 if (result==null) { 89 result = SessionFactoryObjectFactory.getNamedInstance(name); 92 if (result==null) { 93 throw new InvalidObjectException ("Could not find a stub SessionFactory named: " + name); 94 } 95 else { 96 log.debug("resolved stub SessionFactory by name"); 97 } 98 } 99 else { 100 log.debug("resolved stub SessionFactory by uid"); 101 } 102 return result; 103 } 104 105 108 public Reference getReference() throws NamingException { 109 return new Reference ( 110 SessionFactoryStub.class.getName(), 111 new StringRefAddr ("uuid", uuid), 112 SessionFactoryObjectFactory.class.getName(), 113 null 114 ); 115 } 116 117 public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException { 118 return getImpl().getClassMetadata(persistentClass); 119 } 120 121 public ClassMetadata getClassMetadata(String entityName) 122 throws HibernateException { 123 return getImpl().getClassMetadata(entityName); 124 } 125 126 public CollectionMetadata getCollectionMetadata(String roleName) throws HibernateException { 127 return getImpl().getCollectionMetadata(roleName); 128 } 129 130 public Map getAllClassMetadata() throws HibernateException { 131 return getImpl().getAllClassMetadata(); 132 } 133 134 public Map getAllCollectionMetadata() throws HibernateException { 135 return getImpl().getAllCollectionMetadata(); 136 } 137 138 public void close() throws HibernateException { 139 } 140 141 public boolean isClosed() { 142 return false; 143 } 144 145 public void evict(Class persistentClass, Serializable id) 146 throws HibernateException { 147 getImpl().evict(persistentClass, id); 148 } 149 150 public void evict(Class persistentClass) throws HibernateException { 151 getImpl().evict(persistentClass); 152 } 153 154 public void evictEntity(String entityName, Serializable id) 155 throws HibernateException { 156 getImpl().evictEntity(entityName, id); 157 } 158 159 public void evictEntity(String entityName) throws HibernateException { 160 getImpl().evictEntity(entityName); 161 } 162 163 public void evictCollection(String roleName, Serializable id) 164 throws HibernateException { 165 getImpl().evictCollection(roleName, id); 166 } 167 168 public void evictCollection(String roleName) throws HibernateException { 169 getImpl().evictCollection(roleName); 170 } 171 172 public void evictQueries() throws HibernateException { 173 getImpl().evictQueries(); 174 } 175 176 public void evictQueries(String cacheRegion) throws HibernateException { 177 getImpl().evictQueries(cacheRegion); 178 } 179 180 public Statistics getStatistics() { 181 return getImpl().getStatistics(); 182 } 183 184 public StatelessSession openStatelessSession() { 185 return getImpl().openStatelessSession(); 186 } 187 188 public StatelessSession openStatelessSession(Connection conn) { 189 return getImpl().openStatelessSession(conn); 190 } 191 } 192 | Popular Tags |