KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > EjbCMPEntityDescriptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment;
25
26 import java.util.*;
27 import java.lang.reflect.*;
28 import java.util.logging.*;
29 import com.sun.logging.*;
30
31 import com.sun.enterprise.util.LocalStringManagerImpl;
32 import com.sun.enterprise.util.TypeUtil;
33
34 import com.sun.enterprise.deployment.util.EjbVisitor;
35 import com.sun.enterprise.deployment.util.LogDomains;
36 import com.sun.enterprise.util.BeanMethodCalculator;
37
38 /**
39  * This class contains information about EJB1.1 and EJB2.0 CMP EntityBeans.
40  * @author Sanjeev Krishnan
41  */

42
43 public class EjbCMPEntityDescriptor extends EjbEntityDescriptor {
44
45     private static final String JavaDoc FIELD_ACCESS_METHOD_PREFIX = "get"; // NOI18N
46

47     // Enum types to be returned from getVersionNumber().
48
public static final int UNDEFINED = -1;
49     public static final int CMP_1_1 = 1;
50     public static final int CMP_2_x = 2;
51
52     private int cmpVersion = UNDEFINED;
53     private PersistenceDescriptor pers;
54     private String JavaDoc abstractSchemaName=null;
55     private FieldDescriptor primaryKeyFieldDesc;
56     private String JavaDoc stateImplClassName=null;
57     private String JavaDoc ejbImplementationImplClassName=null;
58
59     private static LocalStringManagerImpl localStrings =
60         new LocalStringManagerImpl(EjbCMPEntityDescriptor.class);
61
62     static Logger _logger = LogDomains.getLogger(LogDomains.DPL_LOGGER);
63         
64     
65     public EjbCMPEntityDescriptor() {
66     this.setPersistenceType(CONTAINER_PERSISTENCE);
67     }
68     
69     /**
70      * The copy constructor.
71      */

72     public EjbCMPEntityDescriptor(EjbDescriptor other) {
73     super(other);
74
75     this.setPersistenceType(CONTAINER_PERSISTENCE);
76
77     if ( other instanceof EjbCMPEntityDescriptor ) {
78         EjbCMPEntityDescriptor entity = (EjbCMPEntityDescriptor)other;
79         this.pers = entity.pers;
80         this.cmpVersion = entity.cmpVersion;
81         this.abstractSchemaName = entity.abstractSchemaName;
82     }
83     }
84  
85     /**
86      * Sets the State class implementation classname.
87      */

88     public void setStateImplClassName(String JavaDoc name) {
89     this.stateImplClassName = name;
90     }
91     
92     /**
93      * Returns the classname of the State class impl.
94      */

95     public String JavaDoc getStateImplClassName() {
96     return this.stateImplClassName;
97     }
98
99     public Vector getFields() {
100         Vector fields = new Vector();
101         if( isEJB20() ) {
102             // All cmp "fields" are abstract, so we can't construct
103
// java.lang.reflect.Field elements from them. Use
104
// getFieldDescriptors() instead.
105
} else {
106             fields = super.getFields();
107         }
108         return fields;
109     }
110
111     public Vector getFieldDescriptors() {
112         Vector fieldDescriptors = new Vector();
113         if( isEJB20() ) {
114             try {
115                 ClassLoader JavaDoc cl = getEjbBundleDescriptor().getClassLoader();
116                 fieldDescriptors = BeanMethodCalculator.getPossibleCmpCmrFields
117                     (cl, this.getEjbClassName());
118             } catch(Throwable JavaDoc t) {
119                 String JavaDoc errorMsg = localStrings.getLocalString
120                     ("enterprise.deployment.errorloadingejbclass",
121                      "error loading the ejb class {0} in getFields" +
122                      " on EjbDescriptor\n {1}",
123                      new Object JavaDoc[] {this.getEjbClassName(), t.toString() });
124                 _logger.log(Level.FINE,errorMsg);
125            }
126         } else {
127             fieldDescriptors = super.getFieldDescriptors();
128         }
129         return fieldDescriptors;
130     }
131
132     /**
133      * Returns CMP version as an enum type.
134      */

135     public int getCMPVersion() {
136         if (cmpVersion == UNDEFINED) {
137             if (getEjbBundleDescriptor()!=null) {
138                 String JavaDoc bundleVersion = getEjbBundleDescriptor().getSpecVersion();
139                 if (bundleVersion.startsWith("1.")) {
140                     cmpVersion = CMP_1_1;
141                 } else {
142                     cmpVersion = CMP_2_x;
143                 }
144             } else {
145                 // we cannot get the version from the bundle, set it to default...
146
cmpVersion = CMP_2_x;
147             }
148         }
149         return cmpVersion;
150     }
151
152     /**
153      * Set the CMP version
154      */

155     public void setCMPVersion(int version) {
156     if (version==CMP_1_1 || version == CMP_2_x) {
157             cmpVersion = version;
158         } else {
159         throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
160                 "enterprise.deployment.invalidcmpversion", // NOI18N
161
"Invalid CMP version: {0}.", new Object JavaDoc[] {new Integer JavaDoc(version)})); // NOI18N
162
}
163     }
164     
165     /**
166      * return true if this is an EJB2.0 CMP Entitybean
167      * DEPRECATED
168      */

169     public boolean isEJB20() {
170     return getCMPVersion()==CMP_2_x;
171     }
172     
173     public void setEjbBundleDescriptor(EjbBundleDescriptor bundleDescriptor) {
174         super.setEjbBundleDescriptor(bundleDescriptor);
175     }
176
177     public Vector getPossibleTransactionAttributes() {
178         Vector txAttributes = null;
179         if( isEJB20() ) {
180             txAttributes = new Vector();
181             txAttributes.add(new ContainerTransaction
182                 (ContainerTransaction.REQUIRED, ""));
183             txAttributes.add(new ContainerTransaction
184                 (ContainerTransaction.REQUIRES_NEW, ""));
185             txAttributes.add(new ContainerTransaction
186                 (ContainerTransaction.MANDATORY, ""));
187             if( isTimedObject() ) {
188                 txAttributes.add(new ContainerTransaction
189                     (ContainerTransaction.NOT_SUPPORTED, ""));
190             }
191         } else {
192             txAttributes = super.getPossibleTransactionAttributes();
193         }
194         return txAttributes;
195     }
196  
197     public void setPersistenceDescriptor(PersistenceDescriptor pd)
198     {
199     this.pers = pd;
200     pd.setParentDescriptor(this);
201     }
202
203     public PersistenceDescriptor getPersistenceDescriptor()
204     {
205     if (pers==null) {
206         pers = new PersistenceDescriptor();
207         this.setPersistenceDescriptor(pers);
208         }
209     return pers;
210     }
211
212     private void invalidatePersistenceInfo() {
213         if( pers != null ) {
214             pers.invalidate();
215         }
216     }
217
218     public void setPrimaryKeyFieldDesc(FieldDescriptor pkf)
219     {
220     this.primaryKeyFieldDesc = pkf;
221         invalidatePersistenceInfo();
222     }
223
224     public FieldDescriptor getPrimaryKeyFieldDesc()
225     {
226     return primaryKeyFieldDesc;
227     }
228
229
230
231
232     public void setAbstractSchemaName(String JavaDoc abstractSchemaName)
233     {
234     this.abstractSchemaName = abstractSchemaName;
235     }
236
237     public String JavaDoc getAbstractSchemaName()
238     {
239     return abstractSchemaName;
240     }
241
242     /**
243      * set the generated implementation class for a CMP 2.0 Ejb object
244      * @param className the generated implementation
245      */

246     public void setEjbImplementationImplClassName(String JavaDoc className) {
247         ejbImplementationImplClassName = className;
248     }
249     
250     /**
251      * @return the generated implementation class
252      */

253     public String JavaDoc getEjbImplementationImplClassName() {
254         return ejbImplementationImplClassName;
255     }
256     
257     public static Vector getPossibleCmpCmrFields(ClassLoader JavaDoc cl,
258                                                  String JavaDoc className)
259         throws Exception JavaDoc {
260
261         Vector fieldDescriptors = new Vector();
262         Class JavaDoc theClass = cl.loadClass(className);
263
264         // Start with all *public* methods
265
Method[] methods = theClass.getMethods();
266
267         // Find all accessors that could be cmp fields. This list
268
// will contain all cmr field accessors as well, since there
269
// is no good way to distinguish between the two purely based
270
// on method signature.
271
for(int mIndex = 0; mIndex < methods.length; mIndex++) {
272             Method next = methods[mIndex];
273             String JavaDoc nextName = next.getName();
274             int nextModifiers = next.getModifiers();
275             if( Modifier.isAbstract(nextModifiers) ) {
276                 if( nextName.startsWith(FIELD_ACCESS_METHOD_PREFIX) &&
277                     nextName.length() > 3 ) {
278                     String JavaDoc field =
279                         nextName.substring(3,4).toLowerCase() +
280                         nextName.substring(4);
281                     fieldDescriptors.add(new FieldDescriptor(field));
282                 }
283             }
284         }
285         return fieldDescriptors;
286     }
287   
288     /**
289     * Return my formatted string representation.
290     */

291     public void print(StringBuffer JavaDoc toStringBuffer) {
292     super.print(toStringBuffer);
293     toStringBuffer.append("\n cmpVersion ").append(cmpVersion).
294             append("\n primKeyField ");
295
296         if(getPrimaryKeyFieldDesc() != null)
297             ((Descriptor)getPrimaryKeyFieldDesc()).print(toStringBuffer);
298
299         if(pers != null)
300             ((Descriptor)pers).print(toStringBuffer);
301     }
302     
303     /**
304      * visit the descriptor and all sub descriptors with a DOL visitor implementation
305      *
306      * @param a visitor to traverse the descriptors
307      */

308     public void visit(EjbVisitor aVisitor) {
309         super.visit(aVisitor);
310         PersistenceDescriptor persistenceDesc = getPersistenceDescriptor();
311         for (Iterator e=persistenceDesc.getCMPFields().iterator();e.hasNext();) {
312             FieldDescriptor fd = (FieldDescriptor) e.next();
313             aVisitor.accept(fd);
314         }
315         for (Iterator e=persistenceDesc.getQueriedMethods().iterator();e.hasNext();) {
316             Object JavaDoc method = e.next();
317             if (method instanceof MethodDescriptor) {
318                 QueryDescriptor qd = persistenceDesc.getQueryFor((MethodDescriptor) method);
319                 aVisitor.accept((MethodDescriptor) method, qd);
320             }
321         }
322     }
323     
324 }
325
Popular Tags