KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > keygen > JDBCInformixCreateCommand


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.keygen;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29
30 import javax.ejb.EJBException JavaDoc;
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 /**
39  * Create command for Informix that uses the driver's getSerial method
40  * to retrieve SERIAL values. Also supports SERIAL8 columns if method
41  * attribute of entity-command is set to "getSerial8"
42  *
43  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 37459 $
46  */

47 public class JDBCInformixCreateCommand extends JDBCIdentityColumnCreateCommand
48 {
49    private static final String JavaDoc NAME = "class-name";
50    private static final String JavaDoc DEFAULT_CLASS = "com.informix.jdbc.IfxStatement";
51    private static final String JavaDoc METHOD = "method";
52    private static final String JavaDoc DEFAULT_METHOD = "getSerial";
53
54    private String JavaDoc className;
55    private String JavaDoc methodName;
56    private Method JavaDoc method;
57    private Method JavaDoc getUnderlyingStatement;
58
59    public void init(JDBCStoreManager manager) throws DeploymentException
60    {
61       super.init(manager);
62       ClassLoader JavaDoc loader = GetTCLAction.getContextClassLoader();
63       try
64       {
65          Class JavaDoc psClass = loader.loadClass(className);
66          method = psClass.getMethod(methodName, null);
67       }
68       catch(ClassNotFoundException JavaDoc e)
69       {
70          throw new DeploymentException("Could not load driver class: " + className, e);
71       }
72       catch(NoSuchMethodException JavaDoc e)
73       {
74          throw new DeploymentException("Driver does not have method: " + methodName + "()");
75       }
76
77       try
78       {
79          Class JavaDoc wrapperClass = loader.loadClass("org.jboss.resource.adapter.jdbc.StatementAccess");
80          getUnderlyingStatement = wrapperClass.getMethod("getUnderlyingStatement", null);
81       }
82       catch (ClassNotFoundException JavaDoc e)
83       {
84          throw new DeploymentException("Could not load org.jboss.resource.adapter.jdbc.StatementAccess", e);
85       }
86       catch (NoSuchMethodException JavaDoc 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 JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
108    {
109       int rows = ps.executeUpdate();
110
111       // remove any JCA wrappers
112
Statement JavaDoc stmt = ps;
113       do
114       {
115          try
116          {
117             Object JavaDoc[] args = {};
118             stmt = (Statement JavaDoc) getUnderlyingStatement.invoke(stmt, args);
119          }
120          catch (IllegalAccessException JavaDoc e)
121          {
122             SQLException JavaDoc ex = new SQLException JavaDoc("Failed to invoke getUnderlyingStatement");
123             ex.initCause(e);
124             throw ex;
125          }
126          catch (InvocationTargetException JavaDoc e)
127          {
128             SQLException JavaDoc ex = new SQLException JavaDoc("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 JavaDoc pk = (Number JavaDoc)method.invoke(stmt, null);
137          pkField.setInstanceValue(ctx, pk);
138          return rows;
139       }
140       catch(RuntimeException JavaDoc e)
141       {
142          throw e;
143       }
144       catch(Exception JavaDoc e)
145       {
146          // throw EJBException to force a rollback as the row has been inserted
147
throw new EJBException JavaDoc("Error extracting generated keys", e);
148       }
149    }
150 }
151
Popular Tags