001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc2.bridge;
023:
024: import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge;
025: import org.jboss.ejb.plugins.cmp.jdbc2.QueryCommand;
026: import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2;
027: import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema;
028: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
029: import org.jboss.ejb.EntityEnterpriseContext;
030: import org.jboss.ejb.EntityContainer;
031: import org.jboss.ejb.GenericEntityObjectFactory;
032:
033: import javax.ejb.FinderException;
034: import javax.transaction.Transaction;
035: import javax.transaction.TransactionManager;
036: import java.lang.reflect.Method;
037: import java.util.Collection;
038:
039: /**
040: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
041: * @version <tt>$Revision: 57209 $</tt>
042: */
043: public class EJBSelectBridge implements SelectorBridge {
044: private final static byte SINGLE = 0;
045: private final static byte COLLECTION = 2;
046:
047: private final JDBCQueryMetaData metadata;
048: private final QueryCommand command;
049: private final byte returnType;
050: private final Schema schema;
051: private boolean syncBeforeSelect;
052: private TransactionManager tm;
053:
054: public EJBSelectBridge(EntityContainer container, Schema schema,
055: JDBCQueryMetaData metadata, QueryCommand command) {
056: this .schema = schema;
057: this .metadata = metadata;
058: this .command = command;
059:
060: Class type = metadata.getMethod().getReturnType();
061: if (Collection.class.isAssignableFrom(type)) {
062: returnType = COLLECTION;
063: } else {
064: returnType = SINGLE;
065: }
066:
067: tm = container.getTransactionManager();
068: syncBeforeSelect = !container.getBeanMetaData()
069: .getContainerConfiguration().getSyncOnCommitOnly();
070: }
071:
072: // BridgeInvoker implementation
073:
074: public Object invoke(EntityEnterpriseContext instance,
075: Method method, Object[] args) throws Exception {
076: Transaction tx = (instance != null ? instance.getTransaction()
077: : tm.getTransaction());
078:
079: if (syncBeforeSelect) {
080: EntityContainer.synchronizeEntitiesWithinTransaction(tx);
081: }
082:
083: return execute(args);
084: }
085:
086: // SelectorBridge implementation
087:
088: public String getSelectorName() {
089: return metadata.getMethod().getName();
090: }
091:
092: public Method getMethod() {
093: return metadata.getMethod();
094: }
095:
096: public Object execute(Object[] args) throws FinderException {
097: JDBCStoreManager2 manager = command.getStoreManager();
098: GenericEntityObjectFactory factory = (metadata
099: .isResultTypeMappingLocal() ? (GenericEntityObjectFactory) manager
100: .getContainer().getLocalProxyFactory()
101: : manager.getContainer().getProxyFactory());
102:
103: Object result;
104: switch (returnType) {
105: case SINGLE:
106: result = command.fetchOne(schema, factory, args);
107: if (result == null
108: && getMethod().getReturnType().isPrimitive()) {
109: throw new FinderException(
110: "Cannot return null as a value of primitive type "
111: + getMethod().getReturnType().getName());
112: }
113: break;
114: case COLLECTION:
115: result = command.fetchCollection(schema, factory, args);
116: break;
117: default:
118: throw new IllegalStateException("Unexpected return type: "
119: + returnType);
120: }
121: return result;
122: }
123: }
|