KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > connector > OTMJCAConnection


1 package org.apache.ojb.otm.connector;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 import org.apache.ojb.broker.Identity;
19 import org.apache.ojb.broker.cache.ObjectCache;
20 import org.apache.ojb.broker.metadata.ClassDescriptor;
21 import org.apache.ojb.broker.query.Query;
22 import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
23 import org.apache.ojb.otm.EditingContext;
24 import org.apache.ojb.otm.OTMConnection;
25 import org.apache.ojb.otm.core.Transaction;
26 import org.apache.ojb.otm.lock.LockingException;
27 import org.odmg.OQLQuery;
28
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  *
34  * Wraps the OTMConnection and associates/disassociates the connection
35  * handle.
36  *
37  * kudos to David Jencks for inspiration, and pointers.
38  *
39  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird<a>
40  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
41  */

42
43 public class OTMJCAConnection implements OTMConnection
44 {
45     private OTMJCAManagedConnection m_managedConnection;
46     private boolean m_closed = false;
47
48     public OTMJCAConnection(OTMJCAManagedConnection mc)
49     {
50         Util.log("In OTMJCAConnection");
51         this.m_managedConnection = mc;
52     }
53
54     void setManagedConnection(OTMJCAManagedConnection managedConnection)
55     {
56         m_managedConnection = managedConnection;
57     }
58
59     public void makePersistent(Object JavaDoc o) throws LockingException
60     {
61         isValidUnderlyingConnection();
62         m_managedConnection.getConnection().makePersistent(o);
63     }
64
65     public void deletePersistent(Object JavaDoc o) throws LockingException
66     {
67         isValidUnderlyingConnection();
68         m_managedConnection.getConnection().deletePersistent(o);
69     }
70
71     public void lockForWrite(Object JavaDoc o) throws LockingException
72     {
73         isValidUnderlyingConnection();
74         m_managedConnection.getConnection().lockForWrite(o);
75     }
76
77     public Object JavaDoc getObjectByIdentity(Identity identity) throws LockingException
78     {
79         isValidUnderlyingConnection();
80         return m_managedConnection.getConnection().getObjectByIdentity(identity);
81     }
82
83     public Object JavaDoc getObjectByIdentity(Identity identity, int i) throws LockingException
84     {
85         isValidUnderlyingConnection();
86         return m_managedConnection.getConnection().getObjectByIdentity(identity, i);
87     }
88
89     public Iterator JavaDoc getIteratorByQuery(Query query)
90     {
91         isValidUnderlyingConnection();
92         return m_managedConnection.getConnection().getIteratorByQuery(query);
93     }
94
95     public Iterator JavaDoc getIteratorByQuery(Query query, int i)
96     {
97         isValidUnderlyingConnection();
98         return m_managedConnection.getConnection().getIteratorByQuery(query, i);
99     }
100
101     public Iterator JavaDoc getIteratorByOQLQuery(OQLQuery query)
102     {
103         isValidUnderlyingConnection();
104         return m_managedConnection.getConnection().getIteratorByOQLQuery(query);
105     }
106
107     public Iterator JavaDoc getIteratorByOQLQuery(OQLQuery query, int lock)
108     {
109         isValidUnderlyingConnection();
110         return m_managedConnection.getConnection().getIteratorByOQLQuery(query, lock);
111     }
112
113     public Collection JavaDoc getCollectionByQuery(Query query, int lock)
114     {
115         isValidUnderlyingConnection();
116         return m_managedConnection.getConnection().getCollectionByQuery(query, lock);
117     }
118
119     public Collection JavaDoc getCollectionByQuery(Query query)
120     {
121         isValidUnderlyingConnection();
122         return m_managedConnection.getConnection().getCollectionByQuery(query);
123     }
124
125     public Identity getIdentity(Object JavaDoc o)
126     {
127         isValidUnderlyingConnection();
128         return m_managedConnection.getConnection().getIdentity(o);
129     }
130
131     public ClassDescriptor getDescriptorFor(Class JavaDoc aClass)
132     {
133         isValidUnderlyingConnection();
134         return m_managedConnection.getConnection().getDescriptorFor(aClass);
135     }
136
137     public EditingContext getEditingContext()
138     {
139         isValidUnderlyingConnection();
140         return m_managedConnection.getConnection().getEditingContext();
141     }
142
143     public void invalidate(Identity identity) throws LockingException
144     {
145         isValidUnderlyingConnection();
146         m_managedConnection.getConnection().invalidate(identity);
147     }
148
149     public void invalidateAll() throws LockingException
150     {
151         isValidUnderlyingConnection();
152         m_managedConnection.getConnection().invalidateAll();
153     }
154
155     public EnhancedOQLQuery newOQLQuery()
156     {
157         isValidUnderlyingConnection();
158         return m_managedConnection.getConnection().newOQLQuery();
159     }
160
161     public EnhancedOQLQuery newOQLQuery(int lock)
162     {
163         isValidUnderlyingConnection();
164         return m_managedConnection.getConnection().newOQLQuery(lock);
165     }
166
167     public int getCount(Query query)
168     {
169         isValidUnderlyingConnection();
170         return m_managedConnection.getConnection().getCount(query);
171     }
172
173     public void refresh(Object JavaDoc object)
174     {
175         isValidUnderlyingConnection();
176         m_managedConnection.getConnection().refresh(object);
177     }
178
179     public void close()
180     {
181         m_closed = true;
182         if (m_managedConnection != null)
183         {
184             m_managedConnection.closeHandle(this);
185         }
186         m_managedConnection = null;
187     }
188
189     public boolean isClosed()
190     {
191         isValidUnderlyingConnection();
192         return m_managedConnection.getConnection().isClosed();
193     }
194
195     public ObjectCache serviceObjectCache()
196     {
197         isValidUnderlyingConnection();
198         return m_managedConnection.getConnection().serviceObjectCache();
199     }
200
201     private void isValidUnderlyingConnection() throws OTMConnectionRuntimeException
202     {
203         if (m_closed)
204         {
205             throw new OTMConnectionRuntimeException("OTMConnection handle is closed and unusable.");
206         }
207         if (m_managedConnection == null)
208         {
209             throw new OTMConnectionRuntimeException("Connection handle is not currently associated with a ManagedConnection");
210         }
211     }
212
213     OTMConnection getConnection()
214     {
215         isValidUnderlyingConnection();
216         return m_managedConnection.getConnection();
217     }
218
219     public Transaction getTransaction()
220     {
221         return this.m_managedConnection.getTransaction();
222     }
223
224     public void setTransaction(Transaction t)
225     {
226         this.m_managedConnection.setTransaction(t);
227     }
228 }
229
Popular Tags