001: package org.apache.ojb.otm.connector;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.Identity;
019: import org.apache.ojb.broker.cache.ObjectCache;
020: import org.apache.ojb.broker.metadata.ClassDescriptor;
021: import org.apache.ojb.broker.query.Query;
022: import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
023: import org.apache.ojb.otm.EditingContext;
024: import org.apache.ojb.otm.OTMConnection;
025: import org.apache.ojb.otm.core.Transaction;
026: import org.apache.ojb.otm.lock.LockingException;
027: import org.odmg.OQLQuery;
028:
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: /**
033: *
034: * Wraps the OTMConnection and associates/disassociates the connection
035: * handle.
036: *
037: * kudos to David Jencks for inspiration, and pointers.
038: *
039: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird<a>
040: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
041: */
042:
043: public class OTMJCAConnection implements OTMConnection {
044: private OTMJCAManagedConnection m_managedConnection;
045: private boolean m_closed = false;
046:
047: public OTMJCAConnection(OTMJCAManagedConnection mc) {
048: Util.log("In OTMJCAConnection");
049: this .m_managedConnection = mc;
050: }
051:
052: void setManagedConnection(OTMJCAManagedConnection managedConnection) {
053: m_managedConnection = managedConnection;
054: }
055:
056: public void makePersistent(Object o) throws LockingException {
057: isValidUnderlyingConnection();
058: m_managedConnection.getConnection().makePersistent(o);
059: }
060:
061: public void deletePersistent(Object o) throws LockingException {
062: isValidUnderlyingConnection();
063: m_managedConnection.getConnection().deletePersistent(o);
064: }
065:
066: public void lockForWrite(Object o) throws LockingException {
067: isValidUnderlyingConnection();
068: m_managedConnection.getConnection().lockForWrite(o);
069: }
070:
071: public Object getObjectByIdentity(Identity identity)
072: throws LockingException {
073: isValidUnderlyingConnection();
074: return m_managedConnection.getConnection().getObjectByIdentity(
075: identity);
076: }
077:
078: public Object getObjectByIdentity(Identity identity, int i)
079: throws LockingException {
080: isValidUnderlyingConnection();
081: return m_managedConnection.getConnection().getObjectByIdentity(
082: identity, i);
083: }
084:
085: public Iterator getIteratorByQuery(Query query) {
086: isValidUnderlyingConnection();
087: return m_managedConnection.getConnection().getIteratorByQuery(
088: query);
089: }
090:
091: public Iterator getIteratorByQuery(Query query, int i) {
092: isValidUnderlyingConnection();
093: return m_managedConnection.getConnection().getIteratorByQuery(
094: query, i);
095: }
096:
097: public Iterator getIteratorByOQLQuery(OQLQuery query) {
098: isValidUnderlyingConnection();
099: return m_managedConnection.getConnection()
100: .getIteratorByOQLQuery(query);
101: }
102:
103: public Iterator getIteratorByOQLQuery(OQLQuery query, int lock) {
104: isValidUnderlyingConnection();
105: return m_managedConnection.getConnection()
106: .getIteratorByOQLQuery(query, lock);
107: }
108:
109: public Collection getCollectionByQuery(Query query, int lock) {
110: isValidUnderlyingConnection();
111: return m_managedConnection.getConnection()
112: .getCollectionByQuery(query, lock);
113: }
114:
115: public Collection getCollectionByQuery(Query query) {
116: isValidUnderlyingConnection();
117: return m_managedConnection.getConnection()
118: .getCollectionByQuery(query);
119: }
120:
121: public Identity getIdentity(Object o) {
122: isValidUnderlyingConnection();
123: return m_managedConnection.getConnection().getIdentity(o);
124: }
125:
126: public ClassDescriptor getDescriptorFor(Class aClass) {
127: isValidUnderlyingConnection();
128: return m_managedConnection.getConnection().getDescriptorFor(
129: aClass);
130: }
131:
132: public EditingContext getEditingContext() {
133: isValidUnderlyingConnection();
134: return m_managedConnection.getConnection().getEditingContext();
135: }
136:
137: public void invalidate(Identity identity) throws LockingException {
138: isValidUnderlyingConnection();
139: m_managedConnection.getConnection().invalidate(identity);
140: }
141:
142: public void invalidateAll() throws LockingException {
143: isValidUnderlyingConnection();
144: m_managedConnection.getConnection().invalidateAll();
145: }
146:
147: public EnhancedOQLQuery newOQLQuery() {
148: isValidUnderlyingConnection();
149: return m_managedConnection.getConnection().newOQLQuery();
150: }
151:
152: public EnhancedOQLQuery newOQLQuery(int lock) {
153: isValidUnderlyingConnection();
154: return m_managedConnection.getConnection().newOQLQuery(lock);
155: }
156:
157: public int getCount(Query query) {
158: isValidUnderlyingConnection();
159: return m_managedConnection.getConnection().getCount(query);
160: }
161:
162: public void refresh(Object object) {
163: isValidUnderlyingConnection();
164: m_managedConnection.getConnection().refresh(object);
165: }
166:
167: public void close() {
168: m_closed = true;
169: if (m_managedConnection != null) {
170: m_managedConnection.closeHandle(this );
171: }
172: m_managedConnection = null;
173: }
174:
175: public boolean isClosed() {
176: isValidUnderlyingConnection();
177: return m_managedConnection.getConnection().isClosed();
178: }
179:
180: public ObjectCache serviceObjectCache() {
181: isValidUnderlyingConnection();
182: return m_managedConnection.getConnection().serviceObjectCache();
183: }
184:
185: private void isValidUnderlyingConnection()
186: throws OTMConnectionRuntimeException {
187: if (m_closed) {
188: throw new OTMConnectionRuntimeException(
189: "OTMConnection handle is closed and unusable.");
190: }
191: if (m_managedConnection == null) {
192: throw new OTMConnectionRuntimeException(
193: "Connection handle is not currently associated with a ManagedConnection");
194: }
195: }
196:
197: OTMConnection getConnection() {
198: isValidUnderlyingConnection();
199: return m_managedConnection.getConnection();
200: }
201:
202: public Transaction getTransaction() {
203: return this .m_managedConnection.getTransaction();
204: }
205:
206: public void setTransaction(Transaction t) {
207: this.m_managedConnection.setTransaction(t);
208: }
209: }
|