KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCCustomFinderQuery


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.ejb.FinderException JavaDoc;
34
35 import org.jboss.ejb.EntityEnterpriseContext;
36 import org.jboss.ejb.GenericEntityObjectFactory;
37 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
38 import org.jboss.logging.Logger;
39
40 /**
41  * CMPStoreManager CustomFindByEntitiesCommand.
42  * Implements bridge for custom implemented finders in container managed entity
43  * beans. These methods are called ejbFindX in the EJB implementation class,
44  * where X can be anything. Such methods are called findX in the Home and/or
45  * the LocalHome interface.
46  *
47  * @see org.jboss.ejb.plugins.cmp.jdbc.JDBCFindEntitiesCommand
48  * @author <a HREF="mailto:michel.anke@wolmail.nl">Michel de Groot</a>
49  * @author <a HREF="mailto:john-jboss@freeborg.com">John Freeborg</a>
50  * @version $Revision: 37459 $
51  */

52 public final class JDBCCustomFinderQuery implements JDBCQueryCommand
53 {
54    private final Logger log;
55    private final Method JavaDoc finderMethod;
56    private final JDBCReadAheadMetaData readAheadMetaData;
57    private final ReadAheadCache readAheadCache;
58    private final JDBCStoreManager manager;
59    /**
60     * Constructs a command which can handle multiple entity finders
61     * that are BMP implemented.
62     * @param finderMethod the EJB finder method implementation
63     */

64    public JDBCCustomFinderQuery(JDBCStoreManager manager, Method JavaDoc finderMethod)
65    {
66       this.finderMethod = finderMethod;
67       this.manager = manager;
68
69       JDBCReadAheadMetaData readAheadMetaData = manager.getMetaData().getReadAhead();
70       if((readAheadMetaData != null) && readAheadMetaData.isOnLoad())
71       {
72          this.readAheadCache = manager.getReadAheadCache();
73          this.readAheadMetaData = readAheadMetaData;
74       }
75       else
76       {
77          this.readAheadCache = null;
78          this.readAheadMetaData = null;
79       }
80
81       this.log = Logger.getLogger(
82          this.getClass().getName() +
83          "." +
84          manager.getMetaData().getName() +
85          "." +
86          finderMethod.getName());
87
88       if(log.isDebugEnabled())
89          log.debug("Finder: Custom finder " + finderMethod.getName());
90    }
91
92    public JDBCStoreManager getSelectManager()
93    {
94       return manager;
95    }
96
97    public Collection JavaDoc execute(Method JavaDoc unused,
98                              Object JavaDoc[] args,
99                              EntityEnterpriseContext ctx,
100                              GenericEntityObjectFactory factory)
101       throws FinderException JavaDoc
102    {
103       try
104       {
105          // invoke implementation method on ejb instance
106
Object JavaDoc value = finderMethod.invoke(ctx.getInstance(), args);
107
108          // if expected return type is Collection, return as is
109
// if expected return type is not Collection, wrap value in Collection
110
if(value instanceof Enumeration JavaDoc)
111          {
112             Enumeration JavaDoc enumeration = (Enumeration JavaDoc)value;
113             List JavaDoc result = new ArrayList JavaDoc();
114             while(enumeration.hasMoreElements())
115             {
116                result.add(enumeration.nextElement());
117             }
118             cacheResults(result);
119             return GenericEntityObjectFactory.UTIL.getEntityCollection(factory, result);
120          }
121          else if(value instanceof Collection JavaDoc)
122          {
123             List JavaDoc result;
124             if (value instanceof List JavaDoc)
125                result = (List JavaDoc)value;
126             else
127                result = new ArrayList JavaDoc((Collection JavaDoc)value);
128             cacheResults(result);
129             return GenericEntityObjectFactory.UTIL.getEntityCollection(factory, result);
130          }
131          else
132          {
133             // Don't bother trying to cache this
134
return Collections.singleton(factory.getEntityEJBObject(value));
135          }
136       }
137       catch(IllegalAccessException JavaDoc e)
138       {
139          log.error("Error invoking custom finder " + finderMethod.getName(), e);
140          throw new FinderException JavaDoc("Unable to access finder implementation: " +
141             finderMethod.getName());
142       }
143       catch(IllegalArgumentException JavaDoc e)
144       {
145          log.error("Error invoking custom finder " + finderMethod.getName(), e);
146          throw new FinderException JavaDoc("Illegal arguments for finder " +
147             "implementation: " + finderMethod.getName());
148       }
149       catch(InvocationTargetException JavaDoc e)
150       {
151          // Throw the exception if its a FinderException
152
Throwable JavaDoc ex = e.getTargetException();
153          if( ex instanceof FinderException JavaDoc )
154          {
155             throw (FinderException JavaDoc) ex;
156          }
157          else
158          {
159             throw new FinderException JavaDoc("Errror invoking cutom finder " +
160                finderMethod.getName() + ": " + ex);
161          }
162       }
163    }
164
165    private void cacheResults(List JavaDoc listOfPKs)
166    {
167       // the on-load read ahead cache strategy is the only one that makes
168
// sense to support for custom finders since all we have is a list of
169
// primary keys.
170
if(readAheadCache != null)
171       {
172          readAheadCache.addFinderResults(listOfPKs, readAheadMetaData);
173       }
174    }
175 }
176
Popular Tags