KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.deployment;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.*;
27 import com.sun.enterprise.util.LocalStringManagerImpl;
28 import com.sun.enterprise.util.TypeUtil;
29 import java.util.logging.*;
30 import com.sun.enterprise.deployment.util.LogDomains;
31
32     /** I am a deployment object representing a single method or a collection
33     * of methods on Enterprise Bean classes.
34     * @author Danny Coward
35     */

36
37 public class MethodDescriptor extends Descriptor {
38     /** Represents the bean home interface ejbClassSymbol.*/
39     public static String JavaDoc EJB_HOME = "Home";
40     /** Represents the bean local home interface ejbClassSymbol.*/
41     public static String JavaDoc EJB_LOCALHOME = "LocalHome";
42     /** Represents the bean remote interface ejbClassSymbol.*/
43     public static String JavaDoc EJB_REMOTE = "Remote";
44     /** Represents the bean local interface ejbClassSymbol.*/
45     public static String JavaDoc EJB_LOCAL = "Local";
46     /** Represents the web service interface ejbClassSymbol.*/
47     public static String JavaDoc EJB_WEB_SERVICE = "ServiceEndpoint";
48     /** Represents the bean class ejbClassSymbol.*/
49     public static String JavaDoc EJB_BEAN = "Bean";
50     /** Unused.*/
51     public static String JavaDoc ALL_OF_NAME = "AllOfName";
52     /** The method descriptor name representing all methods.*/
53     public static String JavaDoc ALL_EJB_METHODS = "*";
54     public static String JavaDoc ALL_METHODS = "*";
55     
56     private String JavaDoc[] parameterClassNames = null;
57     private String JavaDoc[] javaParameterClassNames = null;
58     private String JavaDoc className = ""; // cache this
59
private String JavaDoc ejbClassSymbol;
60     private String JavaDoc ejbName;
61     private static LocalStringManagerImpl localStrings =
62         new LocalStringManagerImpl(MethodDescriptor.class);
63         
64     static Logger _logger = LogDomains.getLogger(LogDomains.DPL_LOGGER);
65         
66     private int JAVA_FORMAT = 1;
67     private int XML_FORMAT = -1;
68     private int XML_JAVA_FORMAT = 0;
69     private boolean isExact = false;
70     
71     /**
72     * Constructs a method descriptor corresponding to methods on the ejb class defined by the ejbClassSymbol (or home
73     * and remote if null) with the same name (or all if ALL_EJB_METHODS) and paramater list (or just all by name of this is null).
74     * (Styles 1 2 and 3 in the ejb specification)
75     */

76     public MethodDescriptor(String JavaDoc name, String JavaDoc description, String JavaDoc[] parameterClassNames, String JavaDoc ejbClassSymbol) {
77     super(name, description);
78     if (name == null) {
79         super.setName("");
80     }
81         if (parameterClassNames != null)
82             convertToAppropriateFormat (parameterClassNames);
83     this.setEjbClassSymbol(ejbClassSymbol);
84     }
85            
86     // converts an XML style parameter class name to java style and vice versa
87
private void convertToAppropriateFormat (String JavaDoc[] parameterClassNames){
88     int format = isJavaFormat (parameterClassNames);
89     // not java format so fix the java string
90
if (format == JAVA_FORMAT) {
91         this.javaParameterClassNames = parameterClassNames;
92         this.parameterClassNames =
93         fixParamClassNames (parameterClassNames);
94     } else if (format == XML_FORMAT) { // fix the non java string
95
this.javaParameterClassNames =
96         xmlFormat2JavaClassNames (parameterClassNames);
97         this.parameterClassNames = parameterClassNames;
98     } else if (format == XML_JAVA_FORMAT){
99         // let them be as it is makes no difference
100
this.javaParameterClassNames = parameterClassNames;
101         this.parameterClassNames = parameterClassNames;
102     }
103     }
104     /** Constructor for styles 2 and 1.
105     ** Style 1 iff ALL_METHODS is used
106     */

107     
108     public MethodDescriptor(String JavaDoc name, String JavaDoc description, String JavaDoc ejbClassSymbol) {
109     super(name, description);
110     this.parameterClassNames = null;
111     this.setEjbClassSymbol(ejbClassSymbol);
112     }
113     
114     /** Construct an exact method descriptor from the given method object, classloader and ejb descriptor.
115     */

116     public MethodDescriptor(Method JavaDoc method, String JavaDoc methodIntf) {
117         this(method);
118         isExact=true;
119     this.setEjbClassSymbol(methodIntf);
120     }
121
122     /** Construct an method descriptor from the given method object.
123      */

124     public MethodDescriptor(Method JavaDoc method) {
125         super(method.getName(), "");
126         Class JavaDoc methodClass = method.getClass();
127         Method JavaDoc[] methods = methodClass.getMethods();
128         this.parameterClassNames = getParameterClassNamesFor(method);
129         this.javaParameterClassNames = getJavaFormatClassNamesFor(method);
130         this.className = method.getDeclaringClass().getName();
131     }
132     
133     public MethodDescriptor() {
134     }
135     
136     // XXX JD fix this
137
public void addParameterClass(String JavaDoc parameter) {
138         if (parameterClassNames==null) {
139             parameterClassNames = new String JavaDoc[1];
140         } else {
141             String JavaDoc [] newParameterClassNames = new String JavaDoc[parameterClassNames.length + 1];
142             for (int i=0;i<parameterClassNames.length;i++) {
143                 newParameterClassNames[i] = parameterClassNames[i];
144             }
145             parameterClassNames = newParameterClassNames;
146         }
147         parameterClassNames[parameterClassNames.length-1]=parameter;
148     javaParameterClassNames = xmlFormat2JavaClassNames (parameterClassNames);
149     }
150     
151     public void setEjbName(String JavaDoc ejbName) {
152         this.ejbName = ejbName;
153     }
154     
155     public String JavaDoc getEjbName() {
156         return ejbName;
157     }
158     
159     
160     /** Returns true if I have enough information to specifiy a unique method
161     * on an ejb's home or remote interface unambiguously.
162     */

163     public boolean isExact() {
164         if (isExact) {
165             return true;
166         }
167     boolean isExactName = !this.getName().equals(ALL_EJB_METHODS);
168     boolean hasMethodIntf = getEjbClassSymbol()!=null;
169     boolean hasParamsListed = (this.getParameterClassNames() != null);
170     return isExactName && hasMethodIntf && hasParamsListed;
171     }
172     
173     /**
174      * <p>
175      * @return the style level of this method descriptors. According to the J2EE spec, methods
176      * can be described byt using style 1, style 2 or style 3 xml tags.
177      * </p>
178      */

179     public int getStyle() {
180         if (getName().equals(ALL_EJB_METHODS))
181             return 1;
182         if (getParameterClassNames()==null)
183             return 2;
184         return 3;
185     }
186
187     public Method JavaDoc getMethod(EjbDescriptor ejbDescriptor) {
188         Method JavaDoc method = null;
189         try {
190             ClassLoader JavaDoc classloader = ejbDescriptor.getEjbBundleDescriptor().getClassLoader();
191             String JavaDoc[] javaParamClassNames = getJavaParameterClassNames();
192
193         if ( ejbClassSymbol == null || ejbClassSymbol.equals("")
194          || ejbClassSymbol.equals(EJB_BEAN) ) {
195         try {
196                     if ( !(className.equals("")) ) {
197                         // If declaring class is known, use it. Since method
198
// can have any access type and there is no need
199
// to search super-classes, use
200
// Class.getDeclaredMethod() lookup behavior.
201
Class JavaDoc declaringClass =classloader.loadClass(className);
202                         return TypeUtil.getDeclaredMethod
203                             (declaringClass, classloader, getName(),
204                              javaParamClassNames);
205                     } else {
206                         // Method is public but can be anywhere in class
207
// hierarchy.
208
Class JavaDoc ejbClass = classloader.loadClass
209                             (ejbDescriptor.getEjbClassName());
210                         return TypeUtil.getMethod(ejbClass, classloader,
211                           getName(), javaParamClassNames);
212                     }
213         } catch(NoSuchMethodException JavaDoc nsme) {}
214         try {
215                     if( ejbDescriptor.isRemoteInterfacesSupported() ) {
216                         Class JavaDoc homeClass = classloader.loadClass
217                             (ejbDescriptor.getHomeClassName());
218                         return TypeUtil.getMethod(homeClass, classloader,
219                                                   getName(), javaParamClassNames);
220                     }
221         } catch(NoSuchMethodException JavaDoc nsme) {}
222         try {
223                     if( ejbDescriptor.isLocalInterfacesSupported() ) {
224                         Class JavaDoc cl = classloader.loadClass
225                             (ejbDescriptor.getLocalHomeClassName());
226                         return TypeUtil.getMethod(cl, classloader,
227                         getName(), javaParamClassNames);
228                     }
229         } catch(NoSuchMethodException JavaDoc nsme) {}
230                 try {
231                     if( ejbDescriptor.hasWebServiceEndpointInterface() ) {
232                         Class JavaDoc cl = classloader.loadClass
233                            (ejbDescriptor.getWebServiceEndpointInterfaceName());
234                         return TypeUtil.getMethod(cl, classloader,
235                         getName(), javaParamClassNames);
236                     }
237         } catch(NoSuchMethodException JavaDoc nsme) {}
238         }
239         else if ( ejbClassSymbol.equals(EJB_HOME) ) {
240         try {
241             Class JavaDoc homeClass =
242             classloader.loadClass(ejbDescriptor.getHomeClassName());
243             method = TypeUtil.getMethod(homeClass, classloader,
244                         getName(), javaParamClassNames);
245         } catch(NoSuchMethodException JavaDoc nsme) {}
246             }
247         else if ( ejbClassSymbol.equals(EJB_LOCALHOME) ) {
248         try {
249             Class JavaDoc cl = classloader.loadClass(
250                     ejbDescriptor.getLocalHomeClassName());
251             method = TypeUtil.getMethod(cl, classloader,
252                         getName(), javaParamClassNames);
253         } catch(NoSuchMethodException JavaDoc nsme) {}
254             }
255         else if ( ejbClassSymbol.equals(EJB_REMOTE) ) {
256                 if( ejbDescriptor.isRemoteInterfacesSupported() ) {
257                     try {
258                         Class JavaDoc cl = classloader.loadClass(
259                                        ejbDescriptor.getRemoteClassName());
260                         method = TypeUtil.getMethod(cl, classloader,
261                                        getName(), javaParamClassNames);
262                     } catch(NoSuchMethodException JavaDoc nsme) {}
263                 }
264                 if( (method == null) &&
265                     ejbDescriptor.isRemoteBusinessInterfacesSupported() ) {
266
267                     for(String JavaDoc intf :
268                             ejbDescriptor.getRemoteBusinessClassNames() ) {
269                         try {
270                             Class JavaDoc cl = classloader.loadClass(intf);
271                             method = TypeUtil.getMethod(cl, classloader,
272                                          getName(), javaParamClassNames);
273                         } catch(NoSuchMethodException JavaDoc nsme) {}
274                         
275                         if( method != null ) {
276                             break;
277                         }
278                     }
279                 }
280             }
281         else if ( ejbClassSymbol.equals(EJB_LOCAL) ) {
282                 if( ejbDescriptor.isLocalInterfacesSupported() ) {
283                     try {
284                         Class JavaDoc cl = classloader.loadClass(
285                       ejbDescriptor.getLocalClassName());
286                         method = TypeUtil.getMethod(cl, classloader,
287                       getName(), javaParamClassNames);
288                     } catch(NoSuchMethodException JavaDoc nsme) {}
289                 }
290                 if( (method == null) &&
291                     ejbDescriptor.isLocalBusinessInterfacesSupported() ) {
292
293                     for(String JavaDoc intf :
294                             ejbDescriptor.getLocalBusinessClassNames() ) {
295                         try {
296                             Class JavaDoc cl = classloader.loadClass(intf);
297                             method = TypeUtil.getMethod(cl, classloader,
298                                          getName(), javaParamClassNames);
299                         } catch(NoSuchMethodException JavaDoc nsme) {}
300                         
301                         if( method != null ) {
302                             break;
303                         }
304                     }
305                 }
306             }
307             else if ( ejbClassSymbol.equals(EJB_WEB_SERVICE) ) {
308         try {
309             Class JavaDoc cl = classloader.loadClass
310                         (ejbDescriptor.getWebServiceEndpointInterfaceName());
311             method = TypeUtil.getMethod(cl, classloader,
312                         getName(), javaParamClassNames);
313         } catch(NoSuchMethodException JavaDoc nsme) {}
314             }
315         } catch(Exception JavaDoc e) {
316             _logger.log(Level.SEVERE,"enterprise.deployment.backend.methodClassLoadFailure",new Object JavaDoc[]{ejbDescriptor});
317         }
318         return method;
319     }
320
321
322     public Method JavaDoc getMethod(Class JavaDoc declaringClass)
323     {
324     try {
325         return TypeUtil.getMethod(declaringClass,
326                       declaringClass.getClassLoader(),
327                                       getName(), getJavaParameterClassNames());
328         } catch(Exception JavaDoc e) {
329             _logger.log(Level.SEVERE,"enterprise.deployment.backend.methodClassLoadFailure",new Object JavaDoc[]{declaringClass});
330         return null;
331         }
332     }
333
334     public Method JavaDoc getDeclaredMethod(Class JavaDoc declaringClass)
335     {
336     try {
337         return TypeUtil.getDeclaredMethod(declaringClass,
338                       declaringClass.getClassLoader(),
339                                       getName(), getJavaParameterClassNames());
340         } catch(Exception JavaDoc e) {
341             _logger.log(Level.SEVERE,"enterprise.deployment.backend.methodClassLoadFailure",new Object JavaDoc[]{declaringClass});
342         return null;
343         }
344     }
345     
346     /**
347     * Performs a conversion from the style1 style2 and style3 (no interface symbol) to
348     * method descriptors of style3 with an interface symbol.
349     */

350     public Vector doStyleConversion(EjbDescriptor ejbDescriptor, Collection allMethods) { // must be exact methods
351
Vector v = new Vector();
352     if (this.getName().equals(ALL_EJB_METHODS)) { // STYLE 1
353
for (Iterator itr = allMethods.iterator(); itr.hasNext();) {
354         MethodDescriptor next = (MethodDescriptor) itr.next();
355                 // when ejb-name is present
356
// since it is an optional element in some case
357
if (this.getEjbName() != null
358                     && this.getEjbName().length() > 0) {
359                     next.setEjbName(ejbDescriptor.getName());
360                 }
361         if (!next.isExact()) {
362             //throw new RuntimeException("Conversion failed: " + next);
363
}
364                 if (this.getDescription() != null
365                     && this.getDescription().length() > 0) {
366             next.setDescription(this.getDescription());
367                 }
368         if (getEjbClassSymbol()==null) {
369             v.addElement(next);
370         } else if (this.getEjbClassSymbol().equals(next.getEjbClassSymbol())) {
371             v.addElement(next);
372         }
373         
374         }
375     } else if (this.getParameterClassNames() == null) { // STYLE 2
376
v.addAll(this.getMethodDescriptorsOfName(this.getName(), allMethods));
377     } else { // STYLE 3, but maybe not exact
378
if (getEjbClassSymbol()==null) {
379         v.addAll(this.getMethodDescriptorsOfNameAndParameters(this.getName(), this.getParameterClassNames(), allMethods));
380         } else {
381         v.addElement(this); // this must be exact
382
}
383     }
384     return v;
385     }
386     
387     private Set getMethodDescriptorsOfNameAndParameters(String JavaDoc name, String JavaDoc[] parameterArray, Collection methodDescriptors) {
388     Set methods = new HashSet();
389     for (Iterator itr = getMethodDescriptorsOfName(name, methodDescriptors).iterator(); itr.hasNext();) {
390         MethodDescriptor next = (MethodDescriptor) itr.next();
391             next.setEjbName(getEjbName());
392         if (stringArrayEquals(parameterArray, next.getParameterClassNames())) {
393         methods.add(next);
394         }
395     }
396     return methods;
397     }
398     
399     private Set getMethodDescriptorsOfName(String JavaDoc name, Collection methodDescriptors) {
400     Set set = new HashSet();
401     for (Iterator itr = methodDescriptors.iterator(); itr.hasNext();) {
402         MethodDescriptor next = (MethodDescriptor) itr.next();
403             next.setEjbName(getEjbName());
404         if (name.equals(next.getName())) {
405         if (getEjbClassSymbol()==null) {
406             set.add(next);
407         } else if (getEjbClassSymbol().equals(next.getEjbClassSymbol())) {
408             set.add(next);
409         }
410         }
411     }
412     return set;
413     }
414     
415     /** Returns the ejb class sybol for this method descriptor. */
416     public String JavaDoc getEjbClassSymbol() {
417     return this.ejbClassSymbol;
418     }
419      /** Sets the ejb class sybol for this method descriptor. */
420     public void setEjbClassSymbol(String JavaDoc ejbClassSymbol) {
421     this.ejbClassSymbol = ejbClassSymbol;
422     }
423        
424
425     private void processEjbSymbol(Method JavaDoc method, EjbDescriptor ejbDescriptor, ClassLoader JavaDoc classloader) {
426     try {
427         Class JavaDoc bean, declaringClass = null;
428         String JavaDoc declaringClassname = method.getDeclaringClass().getName();
429         
430             
431             ClassLoader JavaDoc cl = (classloader != null) ? classloader :
432                 ejbDescriptor.getEjbBundleDescriptor().getClassLoader();
433             bean = cl.loadClass(ejbDescriptor.getEjbClassName());
434             declaringClass = classloader.loadClass(declaringClassname);
435
436             // Message-driven beans don't have a home or remote interface
437
if( ejbDescriptor.getType().equals(EjbMessageBeanDescriptor.TYPE) ) {
438                 if (declaringClass.isAssignableFrom(bean)) {
439                     this.ejbClassSymbol = EJB_BEAN;
440                 }
441                 else {
442                     _logger.log(Level.FINE,"declaring class = " + declaringClass);
443                     _logger.log(Level.FINE,"method = " + this);
444                     _logger.log(Level.FINE,"bean class = " + bean);
445                     throw new IllegalArgumentException JavaDoc();
446                 }
447             }
448             else {
449         boolean foundremote = ejbDescriptor.isRemoteInterfacesSupported();
450         if ( ejbDescriptor.isRemoteInterfacesSupported() ) {
451             Class JavaDoc home = cl.loadClass(ejbDescriptor.getHomeClassName());
452             Class JavaDoc remote = cl.loadClass(ejbDescriptor.getRemoteClassName());
453             if (declaringClass.isAssignableFrom(home)) {
454             this.ejbClassSymbol = EJB_HOME;
455             } else if (declaringClass.isAssignableFrom(remote)) {
456             this.ejbClassSymbol = EJB_REMOTE;
457             } else if (declaringClass.isAssignableFrom(bean)) {
458             this.ejbClassSymbol = EJB_BEAN;
459             }
460             else {
461             foundremote = false;
462             }
463         }
464
465                 boolean foundremotebusiness =
466                     ejbDescriptor.isRemoteBusinessInterfacesSupported();
467                 if( ejbDescriptor.isRemoteBusinessInterfacesSupported() ) {
468
469                     boolean match = false;
470                     for( String JavaDoc intfName :
471                              ejbDescriptor.getRemoteBusinessClassNames()) {
472
473                         Class JavaDoc intf = cl.loadClass(intfName);
474                         
475                         if( declaringClass.isAssignableFrom(intf) ) {
476                             this.ejbClassSymbol = EJB_REMOTE;
477                             match = true;
478                             break;
479                         } else if( declaringClass.isAssignableFrom(bean) ) {
480                             this.ejbClassSymbol = EJB_BEAN;
481                             match = true;
482                             break;
483                         }
484                     }
485                     foundremotebusiness = match;
486                 }
487
488
489         boolean foundlocal = ejbDescriptor.isLocalInterfacesSupported();
490         if ( !foundremote && !foundremotebusiness &&
491                      ejbDescriptor.isLocalInterfacesSupported() ) {
492             Class JavaDoc localhome = cl.loadClass(ejbDescriptor.getLocalHomeClassName());
493             Class JavaDoc local = cl.loadClass(ejbDescriptor.getLocalClassName());
494             if (declaringClass.isAssignableFrom(localhome)) {
495             this.ejbClassSymbol = EJB_LOCALHOME;
496             } else if (declaringClass.isAssignableFrom(local)) {
497             this.ejbClassSymbol = EJB_LOCAL;
498             } else if (declaringClass.isAssignableFrom(bean)) {
499             this.ejbClassSymbol = EJB_BEAN;
500             }
501             else {
502             foundlocal = false;
503             }
504                 }
505
506                 boolean foundlocalbusiness =
507                     ejbDescriptor.isLocalBusinessInterfacesSupported();
508                 if( ejbDescriptor.isLocalBusinessInterfacesSupported() ) {
509
510                     boolean match = false;
511                     for( String JavaDoc intfName :
512                              ejbDescriptor.getLocalBusinessClassNames()) {
513
514                         Class JavaDoc intf = cl.loadClass(intfName);
515                         
516                         if( declaringClass.isAssignableFrom(intf) ) {
517                             this.ejbClassSymbol = EJB_LOCAL;
518                             match = true;
519                             break;
520                         } else if( declaringClass.isAssignableFrom(bean) ) {
521                             this.ejbClassSymbol = EJB_BEAN;
522                             match = true;
523                             break;
524                         }
525                     }
526                     foundlocalbusiness = match;
527                 }
528
529
530                 boolean foundwebservice =
531                     ejbDescriptor.hasWebServiceEndpointInterface();
532
533                 if ( !foundremote && !foundremotebusiness && !foundlocal &&
534                      !foundlocalbusiness &&
535                      ejbDescriptor.hasWebServiceEndpointInterface() ) {
536             Class JavaDoc webServiceClass = classloader.loadClass
537                         (ejbDescriptor.getWebServiceEndpointInterfaceName());
538             if (declaringClass.isAssignableFrom(webServiceClass)) {
539             this.ejbClassSymbol = EJB_WEB_SERVICE;
540             } else if (declaringClass.isAssignableFrom(bean)) {
541             this.ejbClassSymbol = EJB_BEAN;
542             } else {
543             foundwebservice = false;
544             }
545                 }
546         if ( !foundlocal && !foundlocalbusiness && !foundremote &&
547                      !foundremotebusiness && !foundwebservice) {
548                     _logger.log(Level.FINE,"method class = " + declaringClass);
549                     _logger.log(Level.FINE,"bean class = " + bean);
550                     throw new IllegalArgumentException JavaDoc("Method is not on any EJB interface");
551                 }
552             }
553     } catch (Throwable JavaDoc t) {
554         if (_logger.isLoggable(Level.SEVERE)) {
555         _logger.log(Level.SEVERE,"enterprise.deployment.backend.methodClassLoadFailure",new Object JavaDoc[] {method});
556         }
557         
558         throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
559                                        "enterprise.deployment.exceptionmethodnotfound",
560                                        "{0} not found in {1}", new Object JavaDoc[] {method,ejbDescriptor}));
561     }
562         ejbName=ejbDescriptor.getName();
563      }
564     
565     public String JavaDoc getFormattedString() {
566     return this.getName() + this.getPrettyParameterString();
567     }
568     
569     public String JavaDoc getPrettyParameterString() {
570     String JavaDoc prettyParameterString = "(";
571     if (this.parameterClassNames != null) {
572         for (int i = 0; i < this.parameterClassNames.length; i++) {
573         int j = i + 1;
574         if (i > 0) {
575             prettyParameterString = prettyParameterString + ", " + this.parameterClassNames[i] + " p" + j;
576         } else {
577             prettyParameterString = prettyParameterString + this.parameterClassNames[i] + " p" + j;
578         }
579         }
580     }
581     prettyParameterString = prettyParameterString + ")";
582     return prettyParameterString;
583     }
584     
585     public String JavaDoc[] getParameterClassNames() {
586     return parameterClassNames;
587     }
588     public String JavaDoc[] getJavaParameterClassNames (){
589     return javaParameterClassNames;
590     }
591     private boolean stringArrayEquals(String JavaDoc[] s1, String JavaDoc[] s2) {
592     if (s1 == null && s2 == null) {
593         return true;
594     }
595     if (s1 == null && s2 != null) {
596         return false;
597     }
598     if (s2 == null && s1 != null) {
599         return false;
600     }
601     if (s1.length == s2.length) {
602         for (int i = 0; i < s1.length; i++) {
603         if (!s1[i].equals(s2[i])) {
604             return false;
605         }
606         }
607         return true;
608     } else {
609         return false;
610     }
611     }
612     
613     /** Equlity iff the parameter names match and the name matches.*/
614     public boolean equals(Object JavaDoc other) {
615     if (other != null && other instanceof MethodDescriptor) {
616         MethodDescriptor otherMethodDescriptor = (MethodDescriptor) other;
617         if (otherMethodDescriptor.getName().equals(getName())
618         && stringArrayEquals(otherMethodDescriptor.getParameterClassNames(), getParameterClassNames())) {
619                     if (getEjbClassSymbol()!=null && otherMethodDescriptor.getEjbClassSymbol()!=null) {
620                         return getEjbClassSymbol().equals(otherMethodDescriptor.getEjbClassSymbol());
621                     }
622                     // if the ejb class symbol is not defined in both descriptor, we consider
623
// the method described being the same.
624
return true;
625         }
626     }
627     return false;
628     }
629
630     /** Indicates if a method descriptor implies the other one*/
631     public boolean implies(Object JavaDoc other) {
632         if (other != null && other instanceof MethodDescriptor) {
633             MethodDescriptor otherMethodDescriptor = (MethodDescriptor) other;
634             if (getName().equals(ALL_METHODS) ||
635                 getName().equals(otherMethodDescriptor.getName())) {
636                 if (getParameterClassNames() == null ||
637                     stringArrayEquals(getParameterClassNames(),
638                         otherMethodDescriptor.getParameterClassNames())) {
639                     return true;
640                 }
641             }
642         }
643         return false;
644     }
645
646     
647     /** My Hashcode. */
648     public int hashCode() {
649     return this.getPrettyParameterString().hashCode() + this.getName().hashCode();
650     }
651     /** My pretty format. */
652     public void print(StringBuffer JavaDoc toStringBuffer) {
653     toStringBuffer.append("Method Descriptor").append((ejbName==null?"":" for ejb " + ejbName)).append(
654                 " name: ").append(this.getName()).append(" params: ").append(this.getPrettyParameterString()).append(
655                 " intf: ").append(this.ejbClassSymbol);
656     }
657     
658     public String JavaDoc prettyPrint() {
659         return "Name : " + this.getName() + " Params: " + this.getPrettyParameterString() + " Intf: " + this.ejbClassSymbol;
660     }
661     
662     
663     public String JavaDoc[] getParameterClassNamesFor(Method JavaDoc method) {
664     Class JavaDoc[] classes = method.getParameterTypes();
665         if (classes.length==0)
666             return null;
667     String JavaDoc[] classNames = new String JavaDoc[classes.length];
668     for (int i = 0; i < classes.length; i++) {
669         Class JavaDoc compType = classes[i].getComponentType();
670         if ( compType == null ) { // not an array
671
classNames[i] = classes[i].getName();
672         }
673         else {
674         // name of array types should be like int[][][]
675
// Class.getName() returns something like [[[I
676
int dimensions = 1;
677                 while(compType.getComponentType()!=null) {
678                     dimensions++;
679                     compType=compType.getComponentType();
680                 }
681                
682         classNames[i] = compType.getName();
683         // add "[]" depending on array dimension
684
for (int j=0;j<dimensions;j++) {
685                     classNames[i] += "[]";
686                 }
687         }
688     }
689     return classNames;
690     }
691
692     private int isJavaFormat (String JavaDoc[] params){
693     int ret = XML_JAVA_FORMAT;
694     for (int i=0; i<params.length; i++){
695         int index = params[i].indexOf ('[');
696         if (index == -1) {
697         //not an array thus cannot determine format
698
ret = XML_JAVA_FORMAT;
699         continue;
700         } else if (index == 0) {// begins with [ thus java format
701
return JAVA_FORMAT;
702         } else { // not java format thus of form int[][]
703
return XML_FORMAT;
704         }
705     }
706     return ret;
707     }
708
709     private String JavaDoc[] getJavaFormatClassNamesFor(Method JavaDoc method){
710     Class JavaDoc[] classes = method.getParameterTypes();
711     String JavaDoc[] classNames = new String JavaDoc[classes.length];
712     for (int i = 0; i < classes.length; i++) {
713         classNames[i] = classes[i].getName();
714     }
715     return classNames;
716     }
717     
718     private String JavaDoc[] fixParamClassNames(String JavaDoc[] paramClassNames)
719     {
720     if(paramClassNames == null) {
721         return null;
722     }
723
724     String JavaDoc[] newParams = new String JavaDoc[paramClassNames.length];
725
726     // This is done for backward compatibility with J2EE 1.2.1
727
// in which param classnames were wrongly generated in [[[I form.
728
for ( int i=0; i<paramClassNames.length; i++ ) {
729         newParams[i] = fixParamClassName(paramClassNames[i]);
730
731     }
732
733     return newParams;
734     }
735     private String JavaDoc[] xmlFormat2JavaClassNames (String JavaDoc[] from) {
736     String JavaDoc[] to = new String JavaDoc[from.length];
737     for (int i=0; i<from.length; i++) {
738         to[i] = new String JavaDoc (xmlFormat2JavaClassNames (from[i]));
739     }
740     return to;
741     }
742
743     // Convert arrays from form int[][][] to [[[L form.
744
public static String JavaDoc xmlFormat2JavaClassNames (String JavaDoc param){
745     int indexOfArray = param.indexOf ('[');
746     if (indexOfArray == -1) {// not an array
747
return param;
748     }
749     String JavaDoc buf = param.substring (0, indexOfArray);
750     int lastIndexOf = param.lastIndexOf (']');
751     int dimension = lastIndexOf - indexOfArray + 1;
752     dimension = dimension / 2;
753     StringBuffer JavaDoc fs = new StringBuffer JavaDoc ();
754     for (int i=0; i<dimension; i++) {
755         fs.append ("[");
756     }
757         String JavaDoc javaPrimitiveType = (String JavaDoc) getJavaPrimitiveTypes().get(buf);
758         if (javaPrimitiveType!=null) {
759             fs.append(javaPrimitiveType);
760     } else { //default it is a class or a interface
761
fs.append ("L");
762         fs.append (buf);
763         fs.append (";");
764     }
765     return fs.toString ();
766     }
767     
768     /**
769      * Computes the mapping between java primitive type and class loaders identifier
770      * for such types.
771      *
772      * @return the mapping with the java primitive type identifier as keys
773      */

774     public static Map getJavaPrimitiveTypes() {
775         if (javaPrimitivesTypes==null) {
776             javaPrimitivesTypes = new Hashtable();
777             javaPrimitivesTypes.put("char", "C");
778             javaPrimitivesTypes.put("byte", "B");
779             javaPrimitivesTypes.put("double", "D");
780             javaPrimitivesTypes.put("float", "F");
781             javaPrimitivesTypes.put("int", "I");
782             javaPrimitivesTypes.put("long", "J");
783             javaPrimitivesTypes.put("short", "S");
784             javaPrimitivesTypes.put("boolean", "Z");
785         }
786         return javaPrimitivesTypes;
787     }
788     
789     private static Map javaPrimitivesTypes;
790
791     // Convert arrays from [[[I form to int[][][] form.
792
public static String JavaDoc fixParamClassName(String JavaDoc param)
793     {
794     if ( param.charAt(0) == '[' ) { // an array
795
int dimensions = param.lastIndexOf('[') + 1;
796         char code = param.charAt(dimensions);
797         String JavaDoc newparam=null;
798         switch (code) {
799         case 'B':
800             newparam = "byte";
801         case 'C':
802             newparam = "char";
803         case 'D':
804             newparam = "double";
805         case 'F':
806             newparam = "float";
807         case 'I':
808             newparam = "int";
809         case 'J':
810             newparam = "long";
811         case 'S':
812             newparam = "short";
813         case 'Z':
814             newparam = "boolean";
815         case 'L':
816             newparam = param.substring(dimensions+1);
817         }
818         for ( int j=0; j<dimensions; j++ )
819         newparam += "[]";
820         return newparam;
821     }
822     else {
823         return param;
824     }
825     }
826
827 }
828
Popular Tags