KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Map JavaDoc;
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.PersistenceContextType;
32 import org.jboss.ejb3.PersistenceUnitRegistry;
33
34 /**
35  * Comment
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 42263 $
39  */

40 public class InjectedEntityManagerFactory implements EntityManagerFactory, Externalizable JavaDoc
41 {
42    private transient EntityManagerFactory delegate;
43    private transient ManagedEntityManagerFactory managedFactory;
44    private String JavaDoc kernelName;
45
46    public InjectedEntityManagerFactory() {}
47
48    public InjectedEntityManagerFactory(ManagedEntityManagerFactory managedFactory)
49    {
50       this.delegate = managedFactory.getEntityManagerFactory();
51       this.managedFactory = managedFactory;
52    }
53
54
55    public EntityManagerFactory getDelegate()
56    {
57       return delegate;
58    }
59
60    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
61    {
62       out.writeUTF(managedFactory.getKernelName());
63    }
64
65    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
66    {
67       String JavaDoc kernelName = in.readUTF();
68       PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
69       if (deployment == null) throw new IOException JavaDoc("Unable to find persistence unit in registry: " + kernelName);
70       managedFactory = deployment.getManagedFactory();
71       delegate = managedFactory.getEntityManagerFactory();
72    }
73
74    public EntityManager createEntityManager()
75    {
76       return getDelegate().createEntityManager();
77    }
78
79    public EntityManager createEntityManager(Map JavaDoc map)
80    {
81       return delegate.createEntityManager(map);
82    }
83
84
85    public EntityManager getEntityManager()
86    {
87       return new TransactionScopedEntityManager(managedFactory);
88    }
89
90    public void close()
91    {
92       throw new IllegalStateException JavaDoc("It is illegal to close an injected EntityManagerFactory");
93    }
94
95    public boolean isOpen()
96    {
97       return getDelegate().isOpen();
98    }
99 }
100
Popular Tags