001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.manager;
031:
032: import com.caucho.util.L10N;
033: import com.caucho.webbeans.component.HandleAware;
034:
035: import javax.persistence.EntityManager;
036: import javax.persistence.EntityTransaction;
037: import javax.persistence.FlushModeType;
038: import javax.persistence.LockModeType;
039: import javax.persistence.Query;
040: import java.util.logging.Logger;
041:
042: /**
043: * The Entity manager
044: */
045: public class EntityManagerProxy implements EntityManager,
046: java.io.Serializable, HandleAware {
047: private static final L10N L = new L10N(EntityManagerProxy.class);
048: private static final Logger log = Logger
049: .getLogger(EntityManagerProxy.class.getName());
050:
051: private AmberPersistenceUnit _persistenceUnit;
052:
053: private boolean _isExtended;
054: private Object _serializationHandle;
055:
056: public EntityManagerProxy(AmberPersistenceUnit persistenceUnit) {
057: _persistenceUnit = persistenceUnit;
058: }
059:
060: /**
061: * Makes the instance managed.
062: */
063: public void persist(Object entity) {
064: getCurrent().persist(entity);
065: }
066:
067: /**
068: * Merges the state of the entity into the current context.
069: */
070: public <T> T merge(T entity) {
071: return getCurrent().merge(entity);
072: }
073:
074: /**
075: * Remove the instance.
076: */
077: public void remove(Object entity) {
078: getCurrent().remove(entity);
079: }
080:
081: /**
082: * Find by the primary key.
083: */
084: /*
085: public Object find(String entityName, Object primaryKey)
086: {
087: return getCurrent().find(entityName, primaryKey);
088: }
089: */
090:
091: /**
092: * Find by the primary key.
093: */
094: public <T> T find(Class<T> entityClass, Object primaryKey) {
095: return getCurrent().find(entityClass, primaryKey);
096: }
097:
098: /**
099: * Find by the primary key.
100: */
101: public <T> T getReference(Class<T> entityClass, Object primaryKey) {
102: return getCurrent().getReference(entityClass, primaryKey);
103: }
104:
105: /**
106: * Returns the flush mode.
107: */
108: public FlushModeType getFlushMode() {
109: return getCurrent().getFlushMode();
110: }
111:
112: /**
113: * Sets the flush mode.
114: */
115: public void setFlushMode(FlushModeType mode) {
116: getCurrent().setFlushMode(mode);
117: }
118:
119: /**
120: * Sets the extended type.
121: */
122: public void setExtended(boolean isExtended) {
123: _isExtended = isExtended;
124:
125: getCurrent().setExtended(isExtended);
126: }
127:
128: /**
129: * Locks the object.
130: */
131: public void lock(Object entity, LockModeType lockMode) {
132: getCurrent().lock(entity, lockMode);
133: }
134:
135: /**
136: * Clears the manager.
137: */
138: public void clear() {
139: getCurrent().clear();
140: }
141:
142: /**
143: * Synchronize with the database.
144: */
145: public void flush() {
146: getCurrent().flush();
147: }
148:
149: /**
150: * Joins the transaction.
151: */
152: public void joinTransaction() {
153: throw new UnsupportedOperationException();
154: }
155:
156: /**
157: * Gets the delegate.
158: */
159: public Object getDelegate() {
160: throw new UnsupportedOperationException();
161: }
162:
163: /**
164: * Clears the manager.
165: */
166: public void close() {
167: throw new IllegalStateException(
168: L
169: .l("Container-manager persistence context may not be closed."));
170: }
171:
172: /**
173: * Creates a query.
174: */
175: public Query createQuery(String sql) {
176: return getCurrent().createQuery(sql);
177: }
178:
179: /**
180: * Creates an instance of the named query
181: */
182: public Query createNamedQuery(String sql) {
183: return getCurrent().createNamedQuery(sql);
184: }
185:
186: /**
187: * Creates a query.
188: */
189: public Query createNativeQuery(String sql) {
190: return getCurrent().createNativeQuery(sql);
191: }
192:
193: /**
194: * Creates a query.
195: */
196: public Query createNativeQuery(String sql, String map) {
197: return getCurrent().createNativeQuery(sql, map);
198: }
199:
200: /**
201: * Creates a query.
202: */
203: public Query createNativeQuery(String sql, Class retType) {
204: return getCurrent().createNativeQuery(sql, retType);
205: }
206:
207: /**
208: * Refresh the state of the instance from the database.
209: */
210: public void refresh(Object entity) {
211: getCurrent().refresh(entity);
212: }
213:
214: /**
215: * Returns true if the entity belongs to the current context.
216: */
217: public boolean contains(Object entity) {
218: return getCurrent().contains(entity);
219: }
220:
221: /**
222: * Returns the entity manager transaction.
223: */
224: public EntityTransaction getTransaction() {
225: return getCurrent().getTransaction();
226: }
227:
228: /**
229: * Returns true if open.
230: */
231: public boolean isOpen() {
232: return true;
233: }
234:
235: /**
236: * Returns the current entity manager.
237: */
238: private AmberConnection getCurrent() {
239: return _persistenceUnit.getThreadConnection(_isExtended);
240: }
241:
242: /**
243: * Serialization handle
244: */
245: public void setSerializationHandle(Object handle) {
246: _serializationHandle = handle;
247: }
248:
249: /**
250: * Serialize to the handle.
251: */
252: private Object writeReplace() {
253: return _serializationHandle;
254: }
255:
256: public String toString() {
257: AmberConnection aConn = getCurrent();
258:
259: if (aConn != null)
260: return "EntityManagerProxy["
261: + aConn.getPersistenceUnit().getName() + "]";
262: else
263: return "EntityManagerProxy[closed]";
264: }
265: }
|