1 22 package org.jboss.ejb.plugins.cmp.jdbc.keygen; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.InvocationTargetException ; 29 30 import javax.ejb.EJBException ; 31 32 import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand; 33 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager; 34 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData; 35 import org.jboss.ejb.EntityEnterpriseContext; 36 import org.jboss.deployment.DeploymentException; 37 38 47 public class JDBCInformixCreateCommand extends JDBCIdentityColumnCreateCommand 48 { 49 private static final String NAME = "class-name"; 50 private static final String DEFAULT_CLASS = "com.informix.jdbc.IfxStatement"; 51 private static final String METHOD = "method"; 52 private static final String DEFAULT_METHOD = "getSerial"; 53 54 private String className; 55 private String methodName; 56 private Method method; 57 private Method getUnderlyingStatement; 58 59 public void init(JDBCStoreManager manager) throws DeploymentException 60 { 61 super.init(manager); 62 ClassLoader loader = GetTCLAction.getContextClassLoader(); 63 try 64 { 65 Class psClass = loader.loadClass(className); 66 method = psClass.getMethod(methodName, null); 67 } 68 catch(ClassNotFoundException e) 69 { 70 throw new DeploymentException("Could not load driver class: " + className, e); 71 } 72 catch(NoSuchMethodException e) 73 { 74 throw new DeploymentException("Driver does not have method: " + methodName + "()"); 75 } 76 77 try 78 { 79 Class wrapperClass = loader.loadClass("org.jboss.resource.adapter.jdbc.StatementAccess"); 80 getUnderlyingStatement = wrapperClass.getMethod("getUnderlyingStatement", null); 81 } 82 catch (ClassNotFoundException e) 83 { 84 throw new DeploymentException("Could not load org.jboss.resource.adapter.jdbc.StatementAccess", e); 85 } 86 catch (NoSuchMethodException e) 87 { 88 throw new DeploymentException("StatementAccess.getUnderlyingStatement not found", e); 89 } 90 } 91 92 protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException 93 { 94 super.initEntityCommand(entityCommand); 95 className = entityCommand.getAttribute(NAME); 96 if(className == null) 97 { 98 className = DEFAULT_CLASS; 99 } 100 methodName = entityCommand.getAttribute(METHOD); 101 if(methodName == null) 102 { 103 methodName = DEFAULT_METHOD; 104 } 105 } 106 107 protected int executeInsert(int paramIndex, PreparedStatement ps, EntityEnterpriseContext ctx) throws SQLException 108 { 109 int rows = ps.executeUpdate(); 110 111 Statement stmt = ps; 113 do 114 { 115 try 116 { 117 Object [] args = {}; 118 stmt = (Statement ) getUnderlyingStatement.invoke(stmt, args); 119 } 120 catch (IllegalAccessException e) 121 { 122 SQLException ex = new SQLException ("Failed to invoke getUnderlyingStatement"); 123 ex.initCause(e); 124 throw ex; 125 } 126 catch (InvocationTargetException e) 127 { 128 SQLException ex = new SQLException ("Failed to invoke getUnderlyingStatement"); 129 ex.initCause(e); 130 throw ex; 131 } 132 } while (stmt != null && method.getDeclaringClass().isInstance(stmt) == false); 133 134 try 135 { 136 Number pk = (Number )method.invoke(stmt, null); 137 pkField.setInstanceValue(ctx, pk); 138 return rows; 139 } 140 catch(RuntimeException e) 141 { 142 throw e; 143 } 144 catch(Exception e) 145 { 146 throw new EJBException ("Error extracting generated keys", e); 148 } 149 } 150 } 151 | Popular Tags |