KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCCreateBeanClassInstanceCommand


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;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.util.*;
28
29 import org.jboss.ejb.EntityContainer;
30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
31 import org.jboss.ejb.plugins.cmp.bridge.EntityBridgeInvocationHandler;
32 import org.jboss.ejb.plugins.cmp.bridge.FieldBridge;
33 import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge;
34 import org.jboss.proxy.compiler.Proxy;
35 import org.jboss.proxy.compiler.InvocationHandler;
36 import org.jboss.deployment.DeploymentException;
37
38 /**
39  * JDBCBeanClassInstanceCommand creates instance of the bean class. For
40  * CMP 2.0 it creates an instance of a subclass of the bean class, as the
41  * bean class is abstract.
42  * <p/>
43  * <FIX-ME>should not generat a subclass for ejb 1.1</FIX-ME>
44  *
45  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
46  * @version $Revision: 37459 $
47  */

48
49 public final class JDBCCreateBeanClassInstanceCommand
50 {
51    private final JDBCEntityBridge entityBridge;
52    private final Class JavaDoc beanClass;
53    private final Constructor JavaDoc beanProxyConstructor;
54    private final Map fieldMap;
55    private final Map selectorMap;
56
57    public JDBCCreateBeanClassInstanceCommand(JDBCStoreManager manager)
58       throws Exception JavaDoc
59    {
60       EntityContainer theContainer = manager.getContainer();
61       entityBridge = (JDBCEntityBridge) manager.getEntityBridge();
62       beanClass = theContainer.getBeanClass();
63       fieldMap = createFieldMap();
64       selectorMap = createSelectorMap();
65       // use proxy generator to create one implementation
66
EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(fieldMap, selectorMap, beanClass);
67       Class JavaDoc[] classes = new Class JavaDoc[]{beanClass};
68       ClassLoader JavaDoc classLoader = beanClass.getClassLoader();
69
70       Object JavaDoc o = Proxy.newProxyInstance(classLoader, classes, handler);
71
72       // steal the constructor from the object
73
beanProxyConstructor = o.getClass().getConstructor(new Class JavaDoc[]{InvocationHandler.class});
74
75       // now create one to make sure everything is cool
76
execute();
77    }
78
79    public void destroy()
80    {
81       Proxy.forgetProxyForClass(beanClass);
82    }
83
84    public Object JavaDoc execute() throws Exception JavaDoc
85    {
86       EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(fieldMap, selectorMap, beanClass);
87       return beanProxyConstructor.newInstance(new Object JavaDoc[]{handler});
88    }
89
90    private Map getAbstractAccessors()
91    {
92       Method JavaDoc[] methods = beanClass.getMethods();
93       Map abstractAccessors = new HashMap(methods.length);
94
95       for(int i = 0; i < methods.length; i++)
96       {
97          if(Modifier.isAbstract(methods[i].getModifiers()))
98          {
99             String JavaDoc methodName = methods[i].getName();
100             if(methodName.startsWith("get") || methodName.startsWith("set"))
101             {
102                abstractAccessors.put(methodName, methods[i]);
103             }
104          }
105       }
106       return abstractAccessors;
107    }
108
109    private Map createFieldMap() throws DeploymentException
110    {
111       Map abstractAccessors = getAbstractAccessors();
112
113       List fields = entityBridge.getFields();
114       Map map = new HashMap(fields.size() * 2);
115       for(int i = 0; i < fields.size(); i++)
116       {
117          FieldBridge field = (FieldBridge) fields.get(i);
118
119          // get the names
120
String JavaDoc fieldName = field.getFieldName();
121          String JavaDoc fieldBaseName = Character.toUpperCase(fieldName.charAt(0)) +
122             fieldName.substring(1);
123          String JavaDoc getterName = "get" + fieldBaseName;
124          String JavaDoc setterName = "set" + fieldBaseName;
125
126          // get the accessor methods
127
Method JavaDoc getterMethod = (Method JavaDoc) abstractAccessors.get(getterName);
128          Method JavaDoc setterMethod = (Method JavaDoc) abstractAccessors.get(setterName);
129
130          // getters and setters must come in pairs
131
if(getterMethod != null && setterMethod == null)
132          {
133             throw new DeploymentException("Getter was found but no setter was found for field " + fieldName
134                + " in entity " + entityBridge.getEntityName());
135          }
136          else if(getterMethod == null && setterMethod != null)
137          {
138             throw new DeploymentException("Setter was found but no getter was found for field " + fieldName
139                + " in entity " + entityBridge.getEntityName());
140          }
141          else if(getterMethod != null && setterMethod != null)
142          {
143             // add methods
144
map.put(getterMethod.getName(), new EntityBridgeInvocationHandler.FieldGetInvoker(field));
145             map.put(setterMethod.getName(), new EntityBridgeInvocationHandler.FieldSetInvoker(field));
146
147             // remove the accessors (they have been used)
148
abstractAccessors.remove(getterName);
149             abstractAccessors.remove(setterName);
150          }
151       }
152       return Collections.unmodifiableMap(map);
153    }
154
155    private Map createSelectorMap()
156    {
157       Collection selectors = entityBridge.getSelectors();
158       Map map = new HashMap(selectors.size());
159       for(Iterator iter = selectors.iterator(); iter.hasNext();)
160       {
161          SelectorBridge selector = (SelectorBridge) iter.next();
162          //map.put(selector.getMethod().getName(), selector);
163
map.put(selector.getMethod(), selector);
164       }
165       return Collections.unmodifiableMap(map);
166    }
167 }
168
Popular Tags