KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > connection > CacheConnectionImpl


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.connection;
30
31 import java.util.Iterator;
32 import java.util.List;
33
34 import java.util.logging.Logger;
35 import java.util.logging.Level;
36
37 import java.sql.Connection;
38 import java.sql.ResultSet;
39 import java.sql.SQLException;
40
41 import javax.sql.DataSource;
42
43 import com.caucho.log.Log;
44
45 import com.caucho.util.L10N;
46
47 import com.caucho.config.ConfigException;
48
49 import com.caucho.amber.AmberManager;
50 import com.caucho.amber.AmberQuery;
51 import com.caucho.amber.AmberException;
52 import com.caucho.amber.AmberRuntimeException;
53
54 import com.caucho.amber.entity.Entity;
55 import com.caucho.amber.entity.AmberEntityHome;
56 import com.caucho.amber.entity.EntityItem;
57 import com.caucho.amber.entity.EntityFactory;
58
59 import com.caucho.amber.query.UserQuery;
60 import com.caucho.amber.query.AbstractQuery;
61 import com.caucho.amber.query.QueryParser;
62 import com.caucho.amber.query.CachedQueryKey;
63
64 import com.caucho.amber.collection.AmberCollection;
65
66 /**
67  * Similar to the AmberConnection bug with no entities, or transactions
68  */

69 public class CacheConnectionImpl extends AmberConnectionImpl {
70   private static final L10N L = new L10N(CacheConnectionImpl.class);
71   private static final Logger log = Log.open(CacheConnectionImpl.class);
72
73   private Connection _conn;
74
75   private int _depth;
76
77   private CachedQueryKey _queryKey = new CachedQueryKey();
78
79   public CacheConnectionImpl(AmberManager amberManager)
80   {
81     super(amberManager);
82   }
83
84   /**
85    * Registers a collection.
86    */

87   public void register(AmberCollection query)
88   {
89   }
90
91   /**
92    * Returns true if a transaction is active.
93    */

94   public boolean isInTransaction()
95   {
96     return false;
97   }
98
99   /**
100    * Loads the object based on the class and primary key.
101    */

102   public Object load(Class cl, Object key)
103     throws AmberException
104   {
105     AmberEntityHome entityHome = _amberManager.getEntityHome(cl.getName());
106
107     if (entityHome == null)
108       return null;
109     else {
110       try {
111     entityHome.init();
112       } catch (ConfigException e) {
113     throw new AmberException(e);
114       }
115       
116       Entity entity = entityHome.load(this, key);
117
118       return entity;
119     }
120   }
121
122
123   /**
124    * Returns the entity for the connection.
125    */

126   public Entity getEntity(EntityItem item)
127   {
128     return item.copy(this);
129   }
130
131   /**
132    * Loads the object based on itself.
133    */

134   public Object makePersistent(Object obj)
135     throws SQLException
136   {
137     Entity entity = (Entity) obj;
138
139     // check to see if exists
140

141     if (entity == null)
142       throw new NullPointerException();
143
144     Class cl = entity.getClass();
145     
146     AmberEntityHome entityHome;
147     entityHome = _amberManager.getEntityHome(entity.getClass().getName());
148
149     if (entityHome == null)
150       throw new AmberException(L.l("entity has no matching home"));
151     
152     entityHome.makePersistent(entity, this, false);
153
154     return entity;
155   }
156
157   /**
158    * Loads the object with the given class.
159    */

160   public Entity loadLazy(Class cl, String name, Object key)
161   {
162     if (key == null)
163       return null;
164     
165     try {
166       AmberEntityHome home = _amberManager.getEntityHome(name);
167       
168       if (home == null)
169     throw new RuntimeException(L.l("no matching home for {0}", cl.getName()));
170
171       home.init();
172
173       Object obj = home.loadLazy(this, key);
174
175       Entity entity = (Entity) obj;
176     
177       return entity;
178     } catch (SQLException e) {
179       log.log(Level.WARNING, e.toString(), e);
180
181       return null;
182     } catch (ConfigException e) {
183       throw new AmberRuntimeException(e);
184     }
185   }
186
187   /**
188    * Loads the object with the given class.
189    */

190   public Object loadProxy(String name, Object key)
191   {
192     if (key == null)
193       return null;
194     
195     try {
196       AmberEntityHome home = _amberManager.getEntityHome(name);
197       
198       if (home == null)
199     throw new RuntimeException(L.l("no matching home for {0}", name));
200
201       home.init();
202
203       EntityItem item = home.findEntityItem(this, key, false);
204
205       if (item == null)
206     return null;
207
208       EntityFactory factory = home.getEntityFactory();
209
210       Object entity = factory.getEntity(this, item);
211
212       return entity;
213     } catch (SQLException e) {
214       log.log(Level.WARNING, e.toString(), e);
215
216       return null;
217     } catch (ConfigException e) {
218       throw new AmberRuntimeException(e);
219     }
220   }
221
222   /**
223    * Loads the object based on the class and primary key.
224    */

225   public Object load(Class cl, long intKey)
226     throws AmberException
227   {
228     AmberEntityHome entityHome = _amberManager.getEntityHome(cl.getName());
229     
230     if (entityHome == null)
231       return null;
232
233     Object key = entityHome.toObjectKey(intKey);
234
235     return load(cl, key);
236   }
237
238   /**
239    * Loads the object based on the class and primary key.
240    */

241   public Object loadLazy(Class cl, long intKey)
242     throws AmberException
243   {
244     AmberEntityHome entityHome = _amberManager.getEntityHome(cl.getName());
245     
246     if (entityHome == null)
247       return null;
248
249     Object key = entityHome.toObjectKey(intKey);
250
251     return loadLazy(cl, cl.getName(), key);
252   }
253
254   /**
255    * Matches the entity.
256    */

257   public Entity getEntity(Class cl, Object key)
258   {
259     return null;
260   }
261
262   /**
263    * Starts a transaction.
264    */

265   public void beginTransaction()
266     throws SQLException
267   {
268   }
269
270   /**
271    * Commits a transaction.
272    */

273   public void commit()
274     throws SQLException
275   {
276   }
277
278   /**
279    * Commits a transaction.
280    */

281   public void rollback()
282     throws SQLException
283   {
284   }
285
286   /**
287    * Commits a transaction.
288    */

289   public void flush()
290     throws SQLException
291   {
292   }
293
294   /**
295    * Returns the connection.
296    */

297   public Connection getConnection()
298     throws SQLException
299   {
300     DataSource dataSource = _amberManager.getReadDataSource();
301
302     if (dataSource == null)
303       dataSource = _amberManager.getDataSource();
304       
305     if (_conn == null) {
306       _conn = dataSource.getConnection();
307     }
308     else if (_conn.isClosed()) {
309       closeConnection();
310       _conn = dataSource.getConnection();
311     }
312
313     return _conn;
314   }
315   
316   /**
317    * Makes the object transactional.
318    *
319    * @param obj the object to save
320    *
321    * @return the proxy for the saved object
322    */

323   public void makeTransactional(Entity entity)
324   {
325     throw new UnsupportedOperationException();
326   }
327   
328   /**
329    * Updates the database with the values in object. If the object does
330    * not exist, throws an exception.
331    *
332    * @param obj the object to update
333    */

334   public void update(Object obj)
335   {
336   }
337   
338   /**
339    * Saves the object.
340    *
341    * @param obj the object to create
342    */

343   public void create(Object obj)
344     throws SQLException
345   {
346     throw new UnsupportedOperationException();
347   }
348
349   /**
350    * Loads the object with the given class.
351    */

352   public void update(Entity entity)
353   {
354   }
355   
356   /**
357    * Deletes the object.
358    *
359    * @param obj the object to delete
360    */

361   public void delete(Object obj)
362     throws SQLException
363   {
364     throw new UnsupportedOperationException();
365   }
366
367   /**
368    * Returns the query key.
369    */

370   public CachedQueryKey getQueryKey()
371   {
372     if (_queryKey == null)
373       _queryKey = new CachedQueryKey();
374     
375     return _queryKey;
376   }
377   
378   /**
379    * Creates a query object from a query string.
380    *
381    * @param query a Hibernate query
382    */

383   public AmberQuery prepareQuery(String queryString)
384     throws AmberException
385   {
386     return prepareQuery(queryString, false);
387   }
388   
389   /**
390    * Creates a query object from a query string.
391    *
392    * @param query a Hibernate query
393    */

394   public AmberQuery prepareLazyQuery(String queryString)
395     throws AmberException
396   {
397     return prepareQuery(queryString, true);
398   }
399   
400   /**
401    * Creates a query object from a query string.
402    *
403    * @param query a Hibernate query
404    */

405   public AmberQuery prepareUpdate(String queryString)
406     throws AmberException
407   {
408     return prepareQuery(queryString, true);
409   }
410   
411   /**
412    * Creates a query object from a query string.
413    *
414    * @param query a Hibernate query
415    */

416   private AmberQuery prepareQuery(String queryString, boolean isLazy)
417     throws AmberException
418   {
419     try {
420       _amberManager.initEntityHomes();
421     } catch (Exception e) {
422       throw AmberRuntimeException.create(e);
423     }
424     
425     QueryParser parser = new QueryParser(queryString);
426
427     parser.setAmberManager(_amberManager);
428     parser.setLazyResult(isLazy);
429
430     AbstractQuery queryProgram = parser.parse();
431     UserQuery query = new UserQuery(queryProgram);
432     
433     query.setSession(this);
434     
435     return query;
436   }
437
438   /**
439    * Select a list of objects with a Hibernate query.
440    *
441    * @param query the hibernate query
442    *
443    * @return the query results.
444    */

445   public ResultSet query(String hsql)
446     throws SQLException
447   {
448     AmberQuery query = prepareQuery(hsql);
449
450     return query.executeQuery();
451   }
452
453   /**
454    * Updates the database with a query
455    *
456    * @param query the hibernate query
457    *
458    * @return the query results.
459    */

460   public int update(String hsql)
461     throws SQLException
462   {
463     throw new UnsupportedOperationException();
464   }
465
466   /**
467    * Select a list of objects with a Hibernate query.
468    *
469    * @param query the hibernate query
470    *
471    * @return the query results.
472    */

473   public List find(String hsql)
474     throws SQLException
475   {
476     AmberQuery query = prepareQuery(hsql);
477
478     return query.list();
479   }
480             
481   /**
482    * Select a list of objects with a Hibernate query, using a single
483    * JDBC "?" parameter.
484    *
485    * @param query the hibernate query
486    * @param value the parameter value
487    *
488    * @return the query results.
489    */

490   public List find(String query, Object value)
491     throws SQLException
492   {
493     return null;
494   }
495
496   /**
497    * Select a list of objects with a Hibernate query, with a list of
498    * JDBC "?" parameters.
499    *
500    * @param query the hibernate query
501    * @param values the parameter values
502    *
503    * @return the query results.
504    */

505   public List find(String query, Object[] values)
506     throws SQLException
507   {
508     return null;
509   }
510
511   /**
512    * Returns the results of a query in an iterator.
513    *
514    * @param query the query string.
515    */

516   public Iterator iterate(String query)
517     throws SQLException
518   {
519     return null;
520   }
521             
522   /**
523    * Select a list of objects with a Hibernate query, using a single
524    * JDBC "?" parameter.
525    *
526    * @param query the hibernate query
527    * @param value the parameter value
528    *
529    * @return the query results.
530    */

531   public Iterator iterate(String query, Object value)
532     throws SQLException
533   {
534     return null;
535   }
536
537   /**
538    * Select a list of objects with a Hibernate query, with a list of
539    * JDBC "?" parameters.
540    *
541    * @param query the hibernate query
542    * @param values the parameter values
543    * @param types the parameter types
544    *
545    * @return the query results.
546    */

547   public Iterator iterate(String query, Object[] values)
548     throws SQLException
549   {
550     return null;
551   }
552   /**
553    * Cleans up the connection.
554    */

555   public void cleanup()
556   {
557     freeConnection();
558   }
559
560   /**
561    * Pushes the allocation depth.
562    */

563   public void pushDepth()
564   {
565     _depth++;
566   }
567
568   /**
569    * Pops the allocation depth.
570    */

571   public void popDepth()
572   {
573     _depth--;
574
575     if (_depth == 0)
576       closeConnection();
577   }
578
579   /**
580    * Frees the connection.
581    */

582   public void freeConnection()
583   {
584     if (_depth != 0)
585       return;
586
587     closeConnection();
588       
589     _amberManager.freeCacheConnection(this);
590   }
591
592   /**
593    * Closes the connection.
594    */

595   protected void closeConnection()
596   {
597     super.freeConnection();
598
599     Connection conn = _conn;
600     _conn = null;
601     
602     try {
603       if (conn != null)
604     conn.close();
605     } catch (Exception e) {
606       log.log(Level.WARNING, e.toString(), e);
607     }
608   }
609
610   /**
611    * Displays the connection as a string.
612    */

613   public String toString()
614   {
615     return "CacheConnectionImpl[]";
616   }
617
618   /**
619    * Finalizer.
620    */

621   public void finalize()
622   {
623   }
624 }
625
Popular Tags