KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > pattern > InterfaceDescription


1 package org.apache.ws.jaxme.js.pattern;
2
3 import java.net.URL JavaDoc;
4
5 import org.apache.ws.jaxme.js.JavaSource;
6 import org.apache.ws.jaxme.js.JavaSourceFactory;
7
8
9 /** The <code>InterfaceDescription</code> is used by the
10  * {@link ProxyGenerator} as information storage about
11  * the interfaces being implemented.<br>
12  * The main purporse of an intermediate class is to
13  * encapsulate the way, how information about these
14  * classes is gathered:
15  * <ol>
16  * <li>If the interface being implemented is a compiled
17  * class, then Java reflection is used.</li>
18  * <li>Otherwise, if the interface being implemented is
19  * present as a Java source file, then the
20  * {@link org.apache.ws.jaxme.js.util.JavaParser}
21  * is used.</li>
22  * </ol
23  */

24 public class InterfaceDescription {
25       private boolean isMandatory = true;
26       private String JavaDoc interfaceName;
27       private String JavaDoc type;
28       private JavaSource javaSource;
29
30       private ClassLoader JavaDoc[] getClassLoaders() {
31           return new ClassLoader JavaDoc[]{
32               Thread.currentThread().getContextClassLoader(),
33               getClass().getClassLoader(),
34               ClassLoader.getSystemClassLoader()
35           };
36       }
37       
38       /** Sets the name of the interface being implemented.
39        */

40       public void setInterface(String JavaDoc pName) {
41           interfaceName = pName;
42       }
43       
44       /** Returns the name of the interface being implemented.
45        */

46       public String JavaDoc getInterface() {
47           return interfaceName;
48       }
49       
50       /** Sets, how to gather information about the interface.
51        * Supported values are "Reflection" (Java reflection),
52        * or "Source" ({@link org.apache.ws.jaxme.js.util.JavaParser}).
53        * The default is null, in which case "Reflection" and "Source"
54        * are tried, in that order.
55        */

56       public void setType(String JavaDoc pType) {
57           if (pType == null
58               || "Reflection".equalsIgnoreCase(pType)
59               || "Source".equalsIgnoreCase(pType)) {
60               type = pType;
61           } else {
62               throw new IllegalArgumentException JavaDoc("Invalid type: " + pType +
63                                                  ", expected 'Reflection', 'Source', or null.");
64           }
65       }
66       
67       /** Returns, how to gather information about the interface.
68        * Supported values are "Reflection" (Java reflection),
69        * or "Source" ({@link org.apache.ws.jaxme.js.util.JavaParser}).
70        * The default is null, in which case "Reflection" and "Source"
71        * are tried, in that order.
72        */

73       public String JavaDoc getType() {
74           return type;
75       }
76       
77       /** Sets whether this interface is mandatory. By default interfaces
78        * are mandatory and the backing objects must implement this interface.
79        * If an interface isn't mandatory, then a Proxy instance can be created
80        * even for objects which don't implement the interface. However, in that
81        * case it may happen that a ClassCastException is thrown while invoking
82        * a method declared by the interface.
83        */

84       public void setMandatory(boolean pMandatory) { isMandatory = pMandatory; }
85
86       /** Returns whether this interface is mandatory. By default interfaces
87        * are mandatory and the backing objects must implement this interface.
88        * If an interface isn't mandatory, then a Proxy instance can be created
89        * even for objects which don't implement the interface. However, in that
90        * case it may happen that a ClassCastException is thrown while invoking
91        * a method declared by the interface.
92        */

93       public boolean isMandatory() { return isMandatory; }
94
95       /** Returns an instance of {@link JavaSource}, matching
96        * the interface {@link #getInterface()}.
97        */

98       public JavaSource getJavaSource() throws Exception JavaDoc {
99           if (javaSource == null) {
100               javaSource = initJavaSource();
101           }
102           return javaSource;
103       }
104
105       /** Initializes the object, after all parameters are set.
106        */

107       private JavaSource initJavaSource() throws Exception JavaDoc {
108           Exception JavaDoc ex = null;
109           String JavaDoc mode = getType();
110           if (mode == null || "Reflection".equals(mode)) {
111               try {
112                   ClassLoader JavaDoc[] cls = getClassLoaders();
113                   for (int i = 0; i < cls.length; i++) {
114                       if (cls[i] == null) {
115                           continue;
116                       }
117                       
118                       Class JavaDoc c = cls[i].loadClass(getInterface());
119                       if (c != null) {
120                           return new CompiledClassReflector(c).getJavaSource(new JavaSourceFactory());
121                       }
122                   }
123               } catch (Exception JavaDoc e) {
124                   if (ex == null) {
125                       ex = e;
126                   }
127               }
128           }
129           if (mode == null || "Source".equals(mode)) {
130               ClassLoader JavaDoc[] cls = getClassLoaders();
131               for (int i = 0; i < cls.length; i++) {
132                   if (cls[i] == null) {
133                       continue;
134                   }
135                   URL JavaDoc url = cls[i].getResource(getInterface().replace('.', '/') + ".java");
136                   if (url != null) {
137                       SourceReflector reflector = new SourceReflector(url);
138                       return reflector.getJavaSource(new JavaSourceFactory());
139                   }
140               }
141           }
142           if (ex == null) {
143             throw new IllegalStateException JavaDoc("Failed to locate Java class "
144                     + getInterface());
145           } else {
146             throw ex;
147           }
148       }
149 }
Popular Tags