KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > entity > InjectedSessionFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.entity;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.persistence.EntityManagerFactory;
35 import org.hibernate.HibernateException;
36 import org.hibernate.Interceptor;
37 import org.hibernate.SessionFactory;
38 import org.hibernate.StatelessSession;
39 import org.hibernate.classic.Session;
40 import org.hibernate.ejb.HibernateEntityManagerFactory;
41 import org.hibernate.engine.FilterDefinition;
42 import org.hibernate.metadata.ClassMetadata;
43 import org.hibernate.metadata.CollectionMetadata;
44 import org.hibernate.stat.Statistics;
45 import org.jboss.ejb3.PersistenceUnitRegistry;
46
47 /**
48  * Comment
49  *
50  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
51  * @version $Revision: 39709 $
52  */

53 public class InjectedSessionFactory implements SessionFactory, Externalizable JavaDoc
54 {
55    private transient EntityManagerFactory delegate;
56    private transient ManagedEntityManagerFactory managedFactory;
57
58    public InjectedSessionFactory(ManagedEntityManagerFactory factory)
59    {
60       this.managedFactory = factory;
61       this.delegate = factory.getEntityManagerFactory();
62    }
63
64    public InjectedSessionFactory() {}
65
66    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
67    {
68       out.writeUTF(managedFactory.getKernelName());
69    }
70
71    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
72    {
73       String JavaDoc kernelName = in.readUTF();
74       PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
75       if (deployment == null) throw new IOException JavaDoc("Unable to find persistence unit in registry: " + kernelName);
76       managedFactory = deployment.getManagedFactory();
77       delegate = managedFactory.getEntityManagerFactory();
78    }
79
80    private EntityManagerFactory getDelegate()
81    {
82       return delegate;
83    }
84
85    private SessionFactory getSessionFactory()
86    {
87       return ((HibernateEntityManagerFactory)getDelegate()).getSessionFactory();
88    }
89
90    public Set JavaDoc getDefinedFilterNames()
91    {
92       return getSessionFactory().getDefinedFilterNames();
93    }
94
95    public FilterDefinition getFilterDefinition(String JavaDoc filterName) throws HibernateException
96    {
97       return getSessionFactory().getFilterDefinition(filterName);
98    }
99
100    public Session openSession(Connection JavaDoc connection)
101    {
102       return getSessionFactory().openSession(connection);
103    }
104
105    public Session openSession(Interceptor interceptor)
106    throws HibernateException
107    {
108       return getSessionFactory().openSession(interceptor);
109    }
110
111    public Session openSession(Connection JavaDoc connection, Interceptor interceptor)
112    {
113       return getSessionFactory().openSession(connection, interceptor);
114    }
115
116    public Session openSession()
117    throws HibernateException
118    {
119       return getSessionFactory().openSession();
120    }
121
122    public Session getCurrentSession()
123    throws HibernateException
124    {
125       return getSessionFactory().getCurrentSession();
126    }
127
128    public ClassMetadata getClassMetadata(Class JavaDoc persistentClass)
129    throws HibernateException
130    {
131       return getSessionFactory().getClassMetadata(persistentClass);
132    }
133
134    public ClassMetadata getClassMetadata(String JavaDoc entityName)
135    throws HibernateException
136    {
137       return getSessionFactory().getClassMetadata(entityName);
138    }
139
140    public CollectionMetadata getCollectionMetadata(String JavaDoc roleName)
141    throws HibernateException
142    {
143       return getSessionFactory().getCollectionMetadata(roleName);
144    }
145
146    public Map JavaDoc getAllClassMetadata()
147    throws HibernateException
148    {
149       return getSessionFactory().getAllClassMetadata();
150    }
151
152    public Map JavaDoc getAllCollectionMetadata()
153    throws HibernateException
154    {
155       return getSessionFactory().getAllCollectionMetadata();
156    }
157
158    public Statistics getStatistics()
159    {
160       return getSessionFactory().getStatistics();
161    }
162
163    public void close()
164    throws HibernateException
165    {
166       throw new IllegalStateException JavaDoc("It is illegal to close an injected SessionFactory");
167    }
168
169    public boolean isClosed()
170    {
171       return getSessionFactory().isClosed();
172    }
173
174    public void evict(Class JavaDoc persistentClass)
175    throws HibernateException
176    {
177       getSessionFactory().evict(persistentClass);
178    }
179
180    public void evict(Class JavaDoc persistentClass, Serializable JavaDoc id)
181    throws HibernateException
182    {
183       getSessionFactory().evict(persistentClass, id);
184    }
185
186    public void evictEntity(String JavaDoc entityName)
187    throws HibernateException
188    {
189       getSessionFactory().evictEntity(entityName);
190    }
191
192    public void evictEntity(String JavaDoc entityName, Serializable JavaDoc id)
193    throws HibernateException
194    {
195       getSessionFactory().evictEntity(entityName, id);
196    }
197
198    public void evictCollection(String JavaDoc roleName)
199    throws HibernateException
200    {
201       getSessionFactory().evictCollection(roleName);
202    }
203
204    public void evictCollection(String JavaDoc roleName, Serializable JavaDoc id)
205    throws HibernateException
206    {
207       getSessionFactory().evictCollection(roleName, id);
208    }
209
210    public void evictQueries()
211    throws HibernateException
212    {
213       getSessionFactory().evictQueries();
214    }
215
216    public void evictQueries(String JavaDoc cacheRegion)
217    throws HibernateException
218    {
219       getSessionFactory().evictQueries(cacheRegion);
220    }
221
222    public StatelessSession openStatelessSession()
223    {
224       return getSessionFactory().openStatelessSession();
225    }
226
227    public StatelessSession openStatelessSession(Connection JavaDoc connection)
228    {
229       return getSessionFactory().openStatelessSession(connection);
230    }
231
232    public Reference JavaDoc getReference()
233    throws NamingException JavaDoc
234    {
235       return getSessionFactory().getReference();
236    }
237
238 }
239
Popular Tags