KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > base > EntityManagerFactoryImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.base;
23
24 import java.util.Map JavaDoc;
25 import oracle.toplink.essentials.threetier.ServerSession;
26 import oracle.toplink.essentials.internal.localization.ExceptionLocalization;
27 import oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl;
28
29 /**
30 * <p>
31 * <b>Purpose</b>: Provides the implementation for the EntityManager Factory.
32 * <p>
33 * <b>Description</b>: This class will store a reference to the active ServerSession. When a request
34 * is made for an EntityManager an new EntityManager is created with the ServerSession and returned.
35 * The primary consumer of these EntityManager is assumed to be either the Container. There is
36 * one EntityManagerFactory per deployment.
37 * @see javax.persistence.EntityManager
38 * @see oracle.toplink.essentials.ejb.cmp3.EntityManager
39 * @see javax.persistence.EntityManagerFactory
40 */

41
42 /* @author gyorke
43  * @since TopLink 10.1.3 EJB 3.0 Preview
44  */

45
46 public abstract class EntityManagerFactoryImpl {
47     // This stores a reference to the ServerSession for this deployement.
48
protected ServerSession serverSession;
49     protected EntityManagerSetupImpl setupImpl;
50     protected boolean isOpen = true;
51     protected Map JavaDoc properties;
52
53     protected abstract EntityManagerImpl createEntityManagerImplInternal(Map JavaDoc properties, boolean extended);
54
55     /**
56      * Will return an instance of the Factory. Should only be called by TopLink.
57      * @param serverSession
58      */

59     public EntityManagerFactoryImpl(ServerSession serverSession){
60         this.serverSession = serverSession;
61     }
62     
63     public EntityManagerFactoryImpl(EntityManagerSetupImpl setupImpl, Map JavaDoc properties){
64         this.setupImpl = setupImpl;
65         this.properties = properties;
66     }
67     
68     /**
69      * INTERNAL:
70      * Returns the ServerSession that the Factory will be using and initializes it if it is not available.
71      * This method makes use of the partially constructed session stored in our setupImpl and
72      * completes its construction
73      */

74     public synchronized ServerSession getServerSession(){
75         if (serverSession == null){
76             ClassLoader JavaDoc realLoader = setupImpl.getPersistenceUnitInfo().getClassLoader();
77             // the call top setupImpl.deploy() finishes the session creation
78
serverSession = setupImpl.deploy(realLoader, properties);
79         }
80         return this.serverSession;
81     }
82     
83     /**
84      * Closes this factory, releasing any resources that might be held by this factory. After
85      * invoking this method, all methods on the instance will throw an
86      * {@link IllegalStateException}, except for {@link #isOpen}, which will return
87      * <code>false</code>.
88      */

89     public synchronized void close(){
90         verifyOpen();
91         isOpen = false;
92         if(serverSession != null) {
93             setupImpl.undeploy();
94         }
95     }
96
97
98     /**
99      * Indicates whether or not this factory is open. Returns <code>true</code> until a call
100      * to {@link #close} is made.
101      */

102     public boolean isOpen(){
103        return isOpen;
104     }
105
106     protected EntityManagerImpl createEntityManagerImpl(boolean extended) {
107         return createEntityManagerImpl(null, extended);
108     }
109
110     protected synchronized EntityManagerImpl createEntityManagerImpl(Map JavaDoc properties, boolean extended) {
111         verifyOpen();
112
113         if (!getServerSession().isConnected()) {
114             getServerSession().login();
115         }
116         return createEntityManagerImplInternal(properties, extended);
117     }
118
119     protected void verifyOpen(){
120         if (!isOpen){
121             throw new IllegalStateException JavaDoc(ExceptionLocalization.buildMessage("operation_on_closed_entity_manager_factory"));
122         }
123     }
124
125     protected void finalize() throws Throwable JavaDoc {
126         if(isOpen()) {
127             close();
128         }
129     }
130 }
131
Popular Tags