KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > ejbc > AbstractMethodHelper


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 /*
25  * AbstractMethodHelper.java
26  *
27  * Created on December 20, 2001, 5:30 PM
28  */

29
30 package com.sun.jdo.spi.persistence.support.ejb.ejbc;
31
32 import java.util.*;
33 import java.lang.reflect.Method JavaDoc;
34
35 import com.sun.enterprise.deployment.*;
36
37 /** This is a helper class which extracts the information needed for method
38  * code generation of the concrete bean class.
39  *
40  * @author Rochelle Raccah
41  */

42 abstract public class AbstractMethodHelper
43 {
44     /** Constant representing a local interface return type. */
45     public static final int LOCAL_RETURN = 0;
46
47     /** Constant representing a remote interface return type. */
48     public static final int REMOTE_RETURN = 1;
49
50     /** Constant representing no return type. */
51     public static final int NO_RETURN = 2;
52
53     private EjbCMPEntityDescriptor _cmpDescriptor;
54     private List finders = new ArrayList();
55     private List selectors = new ArrayList();
56     //private ArrayList otherMethods = new ArrayList();
57
private List createMethods = new ArrayList();
58     private Map methodNames = new HashMap();
59
60     /** Creates a new instance of AbstractMethodHelper
61      * @param descriptor the EjbCMPEntityDescriptor which defines the
62      * information for this bean.
63      */

64     public AbstractMethodHelper (EjbCMPEntityDescriptor descriptor)
65     {
66         _cmpDescriptor = descriptor;
67         categorizeMethods(); // Separate methods into categories.
68
}
69
70     /** Gets the EjbCMPEntityDescriptor which defines the
71      * information for this bean.
72      * @return the EjbCMPEntityDescriptor for the bean specified in the
73      * constructor.
74      */

75     protected EjbCMPEntityDescriptor getDescriptor() { return _cmpDescriptor; }
76
77     /**
78      * Reads all known methods and sorts them by name into specific
79      * Collections for further processing.
80      */

81     protected void categorizeMethods ()
82     {
83         EjbCMPEntityDescriptor descriptor = getDescriptor();
84         Iterator iterator = descriptor.getMethodDescriptors().iterator();
85
86         while (iterator.hasNext())
87         {
88             MethodDescriptor methodDescriptor =
89                 (MethodDescriptor)iterator.next();
90             Method JavaDoc method = methodDescriptor.getMethod(descriptor);
91             String JavaDoc methodName = methodDescriptor.getName();
92
93             //if (DEBUG)
94
// System.out.println("Method: " + methodName); // NOI18N
95

96             if (methodName.startsWith(CMPTemplateFormatter.find_))
97                 finders.add(method);
98             else if (methodName.startsWith(CMPTemplateFormatter.ejbSelect_))
99                 selectors.add(method);
100             else if (methodName.startsWith(CMPTemplateFormatter.create_))
101                 createMethods.add(method);
102             else if (methodName.startsWith(CMPTemplateFormatter.get_) ||
103                 methodName.startsWith(CMPTemplateFormatter.set_))
104             {
105                 ;// skip
106
}
107             //else
108
// otherMethods.add(method);
109

110             // It is OK to use HashMap here as we won't use it for possible
111
// overloaded methods.
112
methodNames.put(methodName, method);
113         }
114     }
115
116     /** Gets the list of finder methods for this bean.
117      * @return a list of java.lang.reflect.Method objects which represent
118      * the finders for this bean
119      */

120     public List getFinders () { return finders; }
121
122     // give subclasses a chance to replace the list
123
protected void setFinders (List finderList)
124     {
125         finders = finderList;
126     }
127
128     /** Gets the list of selector methods for this bean.
129      * @return a list of java.lang.reflect.Method objects which represent
130      * the selectors for this bean
131      */

132     public List getSelectors () { return selectors; }
133
134     // give subclasses a chance to replace the list
135
protected void setSelectors (List selectorList)
136     {
137         selectors = selectorList;
138     }
139
140     /** Gets the list of ejb create methods for this bean.
141      * @return a list of java.lang.reflect.Method objects which represent
142      * the ejb create methods for this bean
143      */

144     public List getCreateMethods () { return createMethods; }
145
146     // might need this later
147
//public List getOtherMethods () { return otherMethods; }
148

149     /** Gets a map of the method names for this bean. The keys are the
150      * method names and the values are the java.lang.reflect.Method objects.
151      * These should represent all methods of this bean.
152      * @return a map of the method names to java.lang.reflect.Method objects
153      * for this bean
154      */

155     public Map getMethodNames () { return methodNames; }
156
157     /** Gets the name of the local home which corresponds to this bean.
158      * @return the name of the local home class
159      */

160     public String JavaDoc getLocalHome ()
161     {
162         return getDescriptor().getLocalHomeClassName();
163     }
164     
165     /** Gets the name of the remote home which corresponds to this bean.
166      * @return the name of the remote home class
167      */

168     public String JavaDoc getRemoteHome ()
169     {
170         return getDescriptor().getHomeClassName();
171     }
172
173     /** Gets the query descriptor associated with the specified method if it
174      * exists.
175      * @param method the java.lang.reflect.Method object used to find the
176      * query string
177      * @return a query descriptor for the specified method. Returns
178      * <code>null</code> for CMP 1.1 queries.
179      */

180     protected QueryDescriptor getQueryDescriptor (Method JavaDoc method)
181     {
182         PersistenceDescriptor persistenceDescriptor =
183             getDescriptor().getPersistenceDescriptor();
184         return persistenceDescriptor.getQueryFor(method);
185     }
186
187     /** Gets the query string associated with the specified method if it
188      * exists.
189      * @param method the java.lang.reflect.Method object used to find the
190      * query string
191      * @return a query string for the specified method
192      */

193     public String JavaDoc getQueryString (Method JavaDoc method)
194     {
195         QueryDescriptor queryDescriptor = getQueryDescriptor(method);
196
197         return ((queryDescriptor != null) ? queryDescriptor.getQuery() : null);
198     }
199
200     /** Gets the return type associated with the specified method if it
201      * exists. If no corresponding query descriptor is found, the value
202      * <code>NO_RETURN</code> is returned.
203      * @param method the java.lang.reflect.Method object used to find the
204      * query return type
205      * @return the return type for the specified method, one of
206      * {@link #LOCAL_RETURN}, {@link #REMOTE_RETURN}, or {@link #NO_RETURN}
207      */

208     public int getQueryReturnType (Method JavaDoc method)
209     {
210         QueryDescriptor queryDescriptor = getQueryDescriptor(method);
211
212         if (queryDescriptor != null)
213         {
214             if (queryDescriptor.getHasLocalReturnTypeMapping())
215                 return LOCAL_RETURN;
216             if (queryDescriptor.getHasRemoteReturnTypeMapping())
217                 return REMOTE_RETURN;
218         }
219
220         return NO_RETURN;
221     }
222
223     /** Returns <code>true</code> if prefetch is enabled for the specified
224      * method, <code>false</code> otherwise. Prefetch is enabled by default.
225      * @param method the java.lang.reflect.Method object used to find the
226      * prefetch setting.
227      * @return a boolean representing the prefetch setting
228      */

229     abstract public boolean isQueryPrefetchEnabled (Method JavaDoc method);
230
231     /** Gets the jdo filter expression associated with the specified method
232      * if it exists. Note that this method should only be used for CMP 1.1 -
233      * use {@link #getQueryString} for CMP 2.0.
234      * @param method the java.lang.reflect.Method object used to find the
235      * query filter
236      * @return the jdo filter expression
237      */

238     abstract public String JavaDoc getJDOFilterExpression (Method JavaDoc method);
239
240     /** Gets the jdo parameter declaration associated with the specified
241      * method if it exists. Note that this method should only be used for
242      * CMP 1.1 - use {@link #getQueryString} for CMP 2.0.
243      * @param method the java.lang.reflect.Method object used to find the
244      * parameter declaration
245      * @return the jdo parameter declaration
246      */

247     abstract public String JavaDoc getJDOParameterDeclaration (Method JavaDoc method);
248
249     /** Gets the jdo variables declaration associated with the specified
250      * method if it exists. Note that this method should only be used for
251      * CMP 1.1 - use {@link #getQueryString} for CMP 2.0.
252      * @param method the java.lang.reflect.Method object used to find the
253      * parameter declaration
254      * @return the jdo variables declaration
255      */

256     abstract public String JavaDoc getJDOVariableDeclaration (Method JavaDoc method);
257
258     /** Gets the jdo ordering specification associated with the specified
259      * method if it exists. Note that this method should only be used for
260      * CMP 1.1 - use {@link #getQueryString} for CMP 2.0.
261      * @param method the java.lang.reflect.Method object used to find the
262      * parameter declaration
263      * @return the jdo ordering specification
264      */

265     abstract public String JavaDoc getJDOOrderingSpecification (Method JavaDoc method);
266 }
267
Popular Tags