KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > persistence > CMPEntityManagerExtended


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

17
18 package org.apache.geronimo.persistence;
19
20 import java.util.Map JavaDoc;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.EntityManagerFactory;
24 import javax.persistence.EntityTransaction;
25 import javax.persistence.FlushModeType;
26 import javax.persistence.LockModeType;
27 import javax.persistence.Query;
28
29 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
30
31 /**
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

34 public class CMPEntityManagerExtended implements EntityManager {
35
36     private final TransactionManagerImpl transactionManager;
37     private final String JavaDoc persistenceUnit;
38     private final EntityManagerFactory entityManagerFactory;
39     private final Map JavaDoc entityManagerProperties;
40     private final InternalCMPEntityManagerExtended entityManager;
41
42     public CMPEntityManagerExtended(TransactionManagerImpl transactionManager, String JavaDoc persistenceUnit, EntityManagerFactory entityManagerFactory, Map JavaDoc entityManagerProperties) {
43         this.transactionManager = transactionManager;
44         this.persistenceUnit = persistenceUnit;
45         this.entityManagerFactory = entityManagerFactory;
46         this.entityManagerProperties = entityManagerProperties;
47         entityManager = getEntityManager();
48     }
49
50     private InternalCMPEntityManagerExtended getEntityManager() {
51         InternalCMPEntityManagerExtended entityManager = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit);
52         if (entityManager == null) {
53             entityManager = createEntityManager();
54             EntityManagerExtendedRegistry.putEntityManager(persistenceUnit, entityManager);
55         }
56         entityManager.registerBean();
57         return entityManager;
58     }
59
60     private InternalCMPEntityManagerExtended createEntityManager() {
61         EntityManager entityManager;
62         if (entityManagerProperties == null) {
63             entityManager = entityManagerFactory.createEntityManager();
64         } else {
65             entityManager = entityManagerFactory.createEntityManager(entityManagerProperties);
66         }
67         return new InternalCMPEntityManagerExtended(entityManager, persistenceUnit, transactionManager);
68     }
69
70     public void beanRemoved() {
71         entityManager.beanRemoved();
72     }
73
74
75     public void persist(Object JavaDoc o) {
76         entityManager.persist(o);
77     }
78
79     public <T>T merge(T t) {
80         return entityManager.merge(t);
81     }
82
83     public void remove(Object JavaDoc o) {
84         entityManager.remove(o);
85     }
86
87     public <T>T find(Class JavaDoc<T> aClass, Object JavaDoc o) {
88         return entityManager.find(aClass, o);
89     }
90
91     public <T>T getReference(Class JavaDoc<T> aClass, Object JavaDoc o) {
92         return entityManager.getReference(aClass, o);
93     }
94
95     public void flush() {
96         entityManager.flush();
97     }
98
99     public void setFlushMode(FlushModeType flushModeType) {
100         entityManager.setFlushMode(flushModeType);
101     }
102
103     public FlushModeType getFlushMode() {
104         return entityManager.getFlushMode();
105     }
106
107     public void lock(Object JavaDoc o, LockModeType lockModeType) {
108         entityManager.lock(o, lockModeType);
109     }
110
111     public void refresh(Object JavaDoc o) {
112         entityManager.refresh(o);
113     }
114
115     public void clear() {
116         entityManager.clear();
117     }
118
119     public boolean contains(Object JavaDoc o) {
120         return entityManager.contains(o);
121     }
122
123     public Query createQuery(String JavaDoc s) {
124         return entityManager.createQuery(s);
125     }
126
127     public Query createNamedQuery(String JavaDoc s) {
128         return entityManager.createNamedQuery(s);
129     }
130
131     public Query createNativeQuery(String JavaDoc s) {
132         return entityManager.createNativeQuery(s);
133     }
134
135     public Query createNativeQuery(String JavaDoc s, Class JavaDoc aClass) {
136         return entityManager.createNativeQuery(s, aClass);
137     }
138
139     public Query createNativeQuery(String JavaDoc s, String JavaDoc s1) {
140         return entityManager.createNativeQuery(s, s1);
141     }
142
143     public void close() {
144         throw new IllegalStateException JavaDoc("You cannot call close on a Container Managed Entity Manager");
145     }
146
147     public boolean isOpen() {
148         return true;
149     }
150
151     public EntityTransaction getTransaction() {
152         throw new IllegalStateException JavaDoc("You cannot call getTransaction on a container managed EntityManager");
153     }
154
155     public void joinTransaction() {
156         throw new IllegalStateException JavaDoc("You cannot call joinTransaction on a container managed EntityManager");
157     }
158
159     public Object JavaDoc getDelegate() {
160         return entityManager.getDelegate();
161     }
162
163 }
164
Popular Tags