KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > ResourceLocalEntityManagerFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.persistence.EntityManager;
26 import javax.persistence.EntityManagerFactory;
27 import javax.persistence.spi.PersistenceUnitInfo;
28
29 import org.apache.cayenne.ObjectContext;
30 import org.apache.cayenne.access.DataContext;
31 import org.apache.cayenne.access.DataDomain;
32 import org.apache.cayenne.access.ObjectStore;
33 import org.apache.cayenne.intercept.DataChannelCallbackInterceptor;
34 import org.apache.cayenne.intercept.ObjectContextCallbackInterceptor;
35
36 /**
37  * A Cayenne EntityManagerFactory that supports resource-local transactions.
38  *
39  * @author Andrus Adamchik
40  */

41 public class ResourceLocalEntityManagerFactory implements EntityManagerFactory {
42
43     protected DataDomain domain;
44     protected boolean open;
45     protected PersistenceUnitInfo unitInfo;
46     protected Provider provider;
47
48     /**
49      * Non-public constructor used mostly for unit testing.
50      */

51     ResourceLocalEntityManagerFactory(PersistenceUnitInfo unitInfo) {
52         this(null, null, unitInfo);
53     }
54
55     /**
56      * Creates a new JpaEntityManagerFactory.
57      */

58     public ResourceLocalEntityManagerFactory(Provider provider, DataDomain domain,
59             PersistenceUnitInfo unitInfo) {
60         this.unitInfo = unitInfo;
61         this.open = true;
62         this.domain = domain;
63         this.provider = provider;
64     }
65
66     /**
67      * Returns wrapped unit.
68      */

69     protected PersistenceUnitInfo getPersistenceUnitInfo() {
70         return unitInfo;
71     }
72
73     /**
74      * Indicates whether the factory is open. Returns true until the factory has been
75      * closed.
76      */

77     public boolean isOpen() {
78         return open;
79     }
80
81     /**
82      * Close the factory, releasing any resources that it holds. After a factory instance
83      * is closed, all methods invoked on it will throw an IllegalStateException, except
84      * for isOpen, which will return false.
85      */

86     public void close() {
87         checkClosed();
88         this.open = false;
89     }
90
91     /**
92      * Create a new EntityManager. Returns a new EntityManager instance every time it is
93      * invoked. The {@link EntityManager#isOpen()} method will return true of the returned
94      * instance.
95      *
96      * @return a new EntityManager instance.
97      */

98     public EntityManager createEntityManager() {
99         return createEntityManager(Collections.EMPTY_MAP);
100     }
101
102     /**
103      * Creates a new resource-local EntityManager with the specified map of properties.
104      * Returns a new EntityManager instance every time it is invoked. The
105      * {@link EntityManager#isOpen()} method will return true of the returned instance.
106      * Parameter map is ignored as Cayenne provider defines no properties for
107      * EntityManager as of now.
108      *
109      * @return a new EntityManager instance.
110      */

111     public EntityManager createEntityManager(Map JavaDoc map) {
112         checkClosed();
113         CayenneEntityManager em = new ResourceLocalEntityManager(createObjectContext(), this);
114         return new TypeCheckingEntityManager(em);
115     }
116
117     /**
118      * Creates a new Cayenne {@link ObjectContext} based on this factory DataDomain.
119      * Returned context has lifecycle callbacks enabled, as expected in the JPA
120      * environment.
121      */

122     protected ObjectContext createObjectContext() {
123         DataChannelCallbackInterceptor postInterceptor = new DataChannelCallbackInterceptor();
124         postInterceptor.setChannel(domain);
125         ObjectStore objectStore = new ObjectStore(domain.getSharedSnapshotCache());
126
127         ObjectContextCallbackInterceptor preInterceptor = new ObjectContextCallbackInterceptor();
128         preInterceptor.setContext(new DataContext(postInterceptor, objectStore));
129
130         return preInterceptor;
131     }
132
133     /**
134      * A convenience method that throws an exception if called on closed factory.
135      */

136     void checkClosed() throws IllegalStateException JavaDoc {
137         if (!isOpen()) {
138             throw new IllegalStateException JavaDoc(
139                     "An attempt to access closed EntityManagerFactory.");
140         }
141     }
142
143     /**
144      * Returns a parent persistence provider.
145      */

146     public Provider getProvider() {
147         return provider;
148     }
149
150     /**
151      * Returns PersistenceUnitInfo used by this factory.
152      */

153     PersistenceUnitInfo getUnitInfo() {
154         return unitInfo;
155     }
156 }
157
Popular Tags