1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.InvocationTargetException ; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.Enumeration ; 31 import java.util.List ; 32 33 import javax.ejb.FinderException ; 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 52 public final class JDBCCustomFinderQuery implements JDBCQueryCommand 53 { 54 private final Logger log; 55 private final Method finderMethod; 56 private final JDBCReadAheadMetaData readAheadMetaData; 57 private final ReadAheadCache readAheadCache; 58 private final JDBCStoreManager manager; 59 64 public JDBCCustomFinderQuery(JDBCStoreManager manager, Method 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 execute(Method unused, 98 Object [] args, 99 EntityEnterpriseContext ctx, 100 GenericEntityObjectFactory factory) 101 throws FinderException 102 { 103 try 104 { 105 Object value = finderMethod.invoke(ctx.getInstance(), args); 107 108 if(value instanceof Enumeration ) 111 { 112 Enumeration enumeration = (Enumeration )value; 113 List result = new ArrayList (); 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 ) 122 { 123 List result; 124 if (value instanceof List ) 125 result = (List )value; 126 else 127 result = new ArrayList ((Collection )value); 128 cacheResults(result); 129 return GenericEntityObjectFactory.UTIL.getEntityCollection(factory, result); 130 } 131 else 132 { 133 return Collections.singleton(factory.getEntityEJBObject(value)); 135 } 136 } 137 catch(IllegalAccessException e) 138 { 139 log.error("Error invoking custom finder " + finderMethod.getName(), e); 140 throw new FinderException ("Unable to access finder implementation: " + 141 finderMethod.getName()); 142 } 143 catch(IllegalArgumentException e) 144 { 145 log.error("Error invoking custom finder " + finderMethod.getName(), e); 146 throw new FinderException ("Illegal arguments for finder " + 147 "implementation: " + finderMethod.getName()); 148 } 149 catch(InvocationTargetException e) 150 { 151 Throwable ex = e.getTargetException(); 153 if( ex instanceof FinderException ) 154 { 155 throw (FinderException ) ex; 156 } 157 else 158 { 159 throw new FinderException ("Errror invoking cutom finder " + 160 finderMethod.getName() + ": " + ex); 161 } 162 } 163 } 164 165 private void cacheResults(List listOfPKs) 166 { 167 if(readAheadCache != null) 171 { 172 readAheadCache.addFinderResults(listOfPKs, readAheadMetaData); 173 } 174 } 175 } 176 | Popular Tags |