KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > bridge > EJBSelectBridge


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.jdbc2.bridge;
23
24 import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge;
25 import org.jboss.ejb.plugins.cmp.jdbc2.QueryCommand;
26 import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2;
27 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema;
28 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
29 import org.jboss.ejb.EntityEnterpriseContext;
30 import org.jboss.ejb.EntityContainer;
31 import org.jboss.ejb.GenericEntityObjectFactory;
32
33 import javax.ejb.FinderException JavaDoc;
34 import javax.transaction.Transaction JavaDoc;
35 import javax.transaction.TransactionManager JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.Collection JavaDoc;
38
39 /**
40  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
41  * @version <tt>$Revision: 43446 $</tt>
42  */

43 public class EJBSelectBridge
44    implements SelectorBridge
45 {
46    private final static byte SINGLE = 0;
47    private final static byte COLLECTION = 2;
48
49    private final JDBCQueryMetaData metadata;
50    private final QueryCommand command;
51    private final byte returnType;
52    private final Schema schema;
53    private boolean syncBeforeSelect;
54    private TransactionManager JavaDoc tm;
55
56    public EJBSelectBridge(EntityContainer container, Schema schema, JDBCQueryMetaData metadata, QueryCommand command)
57    {
58       this.schema = schema;
59       this.metadata = metadata;
60       this.command = command;
61
62       Class JavaDoc type = metadata.getMethod().getReturnType();
63       if(Collection JavaDoc.class.isAssignableFrom(type))
64       {
65          returnType = COLLECTION;
66       }
67       else
68       {
69          returnType = SINGLE;
70       }
71
72       tm = container.getTransactionManager();
73       syncBeforeSelect = !container.getBeanMetaData().getContainerConfiguration().getSyncOnCommitOnly();
74    }
75
76    // BridgeInvoker implementation
77

78    public Object JavaDoc invoke(EntityEnterpriseContext instance, Method JavaDoc method, Object JavaDoc[] args)
79       throws Exception JavaDoc
80    {
81       Transaction JavaDoc tx = (instance != null ? instance.getTransaction() : tm.getTransaction());
82
83       if(syncBeforeSelect)
84       {
85          EntityContainer.synchronizeEntitiesWithinTransaction(tx);
86       }
87
88       return execute(args);
89    }
90
91    // SelectorBridge implementation
92

93    public String JavaDoc getSelectorName()
94    {
95       return metadata.getMethod().getName();
96    }
97
98    public Method JavaDoc getMethod()
99    {
100       return metadata.getMethod();
101    }
102
103    public Object JavaDoc execute(Object JavaDoc[] args) throws FinderException JavaDoc
104    {
105       JDBCStoreManager2 manager = command.getStoreManager();
106       GenericEntityObjectFactory factory = (metadata.isResultTypeMappingLocal() ?
107          (GenericEntityObjectFactory)manager.getContainer().getLocalProxyFactory() : manager.getContainer().getProxyFactory());
108
109       Object JavaDoc result;
110       switch(returnType)
111       {
112          case SINGLE:
113             result = command.fetchOne(schema, factory, args);
114             if(result == null && getMethod().getReturnType().isPrimitive())
115             {
116                throw new FinderException JavaDoc(
117                   "Cannot return null as a value of primitive type " + getMethod().getReturnType().getName()
118                );
119             }
120             break;
121          case COLLECTION:
122             result = command.fetchCollection(schema, factory, args);
123             break;
124          default:
125             throw new IllegalStateException JavaDoc("Unexpected return type: " + returnType);
126       }
127       return result;
128    }
129 }
130
Popular Tags