KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > bridge > JDBCSelectorBridge


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.bridge;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.FinderException JavaDoc;
30 import javax.ejb.ObjectNotFoundException JavaDoc;
31 import javax.transaction.Transaction JavaDoc;
32 import javax.transaction.TransactionManager JavaDoc;
33
34 import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge;
35 import org.jboss.ejb.plugins.cmp.jdbc.JDBCQueryCommand;
36 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
37 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
38 import org.jboss.ejb.EntityEnterpriseContext;
39 import org.jboss.ejb.EntityContainer;
40 import org.jboss.ejb.GenericEntityObjectFactory;
41
42 /**
43  * JDBCSelectorBridge represents one ejbSelect method.
44  * <p/>
45  * Life-cycle:
46  * Tied to the EntityBridge.
47  * <p/>
48  * Multiplicity:
49  * One for each entity bean ejbSelect method.
50  *
51  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
52  * @version $Revision: 43446 $
53  */

54 public class JDBCSelectorBridge implements SelectorBridge
55 {
56    private final JDBCQueryMetaData queryMetaData;
57    private final JDBCStoreManager manager;
58    private TransactionManager JavaDoc tm;
59    private boolean syncBeforeSelect;
60
61    public JDBCSelectorBridge(JDBCStoreManager manager, JDBCQueryMetaData queryMetaData)
62    {
63       this.queryMetaData = queryMetaData;
64       this.manager = manager;
65
66       EntityContainer container = manager.getContainer();
67       tm = container.getTransactionManager();
68       syncBeforeSelect = !container.getBeanMetaData().getContainerConfiguration().getSyncOnCommitOnly();
69    }
70
71    // BridgeInvoker implementation
72

73    public Object JavaDoc invoke(EntityEnterpriseContext ctx, Method JavaDoc method, Object JavaDoc[] args)
74       throws Exception JavaDoc
75    {
76       Transaction JavaDoc tx = (ctx != null ? ctx.getTransaction() : tm.getTransaction());
77
78       if(syncBeforeSelect)
79       {
80          EntityContainer.synchronizeEntitiesWithinTransaction(tx);
81       }
82
83       return execute(args);
84    }
85
86    // SelectorBridge implementation
87

88    public String JavaDoc getSelectorName()
89    {
90       return queryMetaData.getMethod().getName();
91    }
92
93    public Method JavaDoc getMethod()
94    {
95       return queryMetaData.getMethod();
96    }
97
98    private Class JavaDoc getReturnType()
99    {
100       return queryMetaData.getMethod().getReturnType();
101    }
102
103    public Object JavaDoc execute(Object JavaDoc[] args) throws FinderException JavaDoc
104    {
105       Collection JavaDoc retVal;
106       Method JavaDoc method = getMethod();
107       try
108       {
109          JDBCQueryCommand query = manager.getQueryManager().getQueryCommand(method);
110          GenericEntityObjectFactory factory = (queryMetaData.isResultTypeMappingLocal() ?
111             (GenericEntityObjectFactory)query.getSelectManager().getContainer().getLocalProxyFactory() :
112             query.getSelectManager().getContainer().getProxyFactory());
113          retVal = query.execute(method, args, null, factory);
114       }
115       catch(FinderException JavaDoc e)
116       {
117          throw e;
118       }
119       catch(EJBException JavaDoc e)
120       {
121          throw e;
122       }
123       catch(Exception JavaDoc e)
124       {
125          throw new EJBException JavaDoc("Error in " + getSelectorName(), e);
126       }
127
128       if(!Collection JavaDoc.class.isAssignableFrom(getReturnType()))
129       {
130          // single object
131
if(retVal.size() == 0)
132          {
133             throw new ObjectNotFoundException JavaDoc();
134          }
135          if(retVal.size() > 1)
136          {
137             throw new FinderException JavaDoc(getSelectorName() +
138                " returned " + retVal.size() + " objects");
139          }
140
141          Object JavaDoc o = retVal.iterator().next();
142          if(o == null && method.getReturnType().isPrimitive())
143          {
144             throw new FinderException JavaDoc(
145                "Cannot return null as a value of primitive type " + method.getReturnType().getName()
146             );
147          }
148
149          return o;
150       }
151       else
152       {
153          // collection or set
154
if(Set JavaDoc.class.isAssignableFrom(getReturnType()))
155          {
156             return new HashSet JavaDoc(retVal);
157          }
158          else
159          {
160             return retVal;
161          }
162       }
163    }
164 }
165
Popular Tags