KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > server > InvocationContext


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.mx.server;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.management.Descriptor JavaDoc;
28 import javax.management.MBeanParameterInfo JavaDoc;
29
30 import org.jboss.mx.interceptor.AbstractInterceptor;
31 import org.jboss.mx.interceptor.Interceptor;
32 import org.jboss.util.Classes;
33
34 /**
35  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
36  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
37  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
38  * @version $Revision: 39975 $
39  */

40 public class InvocationContext
41 {
42
43    // Constants -----------------------------------------------------
44

45    public final static String JavaDoc OP_INVOKE = "invoke";
46    public final static String JavaDoc OP_GETATTRIBUTE = "getAttribute";
47    public final static String JavaDoc OP_SETATTRIBUTE = "setAttribute";
48    public final static String JavaDoc OP_GETMBEANINFO = "getMBeanInfo";
49
50    public final static String JavaDoc OPERATION_IMPACT = "operation.impact";
51    public final static String JavaDoc ATTRIBUTE_ACCESS = "attribute.access";
52
53    /* Marker for void */
54    private static final Class JavaDoc VOID = Void JavaDoc.class;
55
56    public static final Class JavaDoc[] NOCLASSES = new Class JavaDoc[0];
57    
58    // Attributes ----------------------------------------------------
59

60    private String JavaDoc attributeType = null;
61    private String JavaDoc name = null;
62    private String JavaDoc[] signature = null;
63    private String JavaDoc returnType = null;
64    private String JavaDoc type = null;
65    private boolean isWritable = true;
66    private boolean isReadable = true;
67
68    List JavaDoc interceptors = null;
69    transient Interceptor dispatcher = new NullDispatcher();
70    transient Object JavaDoc target = null;
71    transient Descriptor JavaDoc descriptor = null;
72    transient AbstractMBeanInvoker invoker = null;
73    transient Class JavaDoc attributeTypeClass = null;
74    transient Class JavaDoc returnTypeClass = null;
75    transient Class JavaDoc[] signatureClasses = null;
76
77    // Public --------------------------------------------------------
78

79    public final void copy(final InvocationContext src)
80    {
81       if (src == null)
82          return;
83
84       this.attributeType = src.attributeType;
85       this.attributeTypeClass = src.attributeTypeClass;
86       this.name = src.name;
87       this.signature = src.signature;
88       this.signatureClasses = src.signatureClasses;
89       this.returnType = src.returnType;
90       this.returnTypeClass = src.returnTypeClass;
91       this.type = src.type;
92       this.isWritable = src.isWritable;
93       this.interceptors = src.interceptors;
94       this.dispatcher = src.dispatcher;
95       this.target = src.target;
96       this.descriptor = src.descriptor;
97       this.invoker = src.invoker;
98    }
99
100    public void setType(String JavaDoc type)
101    {
102       this.type = type;
103    }
104
105    public String JavaDoc getType()
106    {
107       return type;
108    }
109
110    public void setName(String JavaDoc name)
111    {
112       this.name = name;
113    }
114
115    public String JavaDoc getName()
116    {
117       return name;
118    }
119
120    void setSignature(String JavaDoc[] signature)
121    {
122       this.signature = signature;
123    }
124
125    void setSignature(MBeanParameterInfo JavaDoc[] signature)
126    {
127       this.signature = new String JavaDoc[signature.length];
128
129       for (int i = 0; i < signature.length; ++i)
130          this.signature[i] = signature[i].getType();
131
132    }
133
134    public String JavaDoc[] getSignature()
135    {
136       return signature;
137    }
138    
139    public Class JavaDoc[] getSignatureClasses() throws ClassNotFoundException JavaDoc
140    {
141       if (signatureClasses != null)
142          return signatureClasses;
143       if (signature == null || signature.length == 0)
144          return NOCLASSES;
145       Class JavaDoc[] signatureClassesTemp = new Class JavaDoc[signature.length];
146       for (int i = 0; i < signature.length; ++i)
147          signatureClassesTemp[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
148       signatureClasses = signatureClassesTemp;
149       return signatureClasses;
150    }
151
152    public void setAttributeType(String JavaDoc attrType)
153    {
154       this.attributeType = attrType;
155       this.attributeTypeClass = null;
156    }
157
158    public String JavaDoc getAttributeType()
159    {
160       return attributeType;
161    }
162
163    public Class JavaDoc getAttributeTypeClass() throws ClassNotFoundException JavaDoc
164    {
165       if (attributeType == null)
166          return null;
167       if (attributeTypeClass != null)
168          return attributeTypeClass;
169       attributeTypeClass = loadClass(attributeType);
170       return attributeTypeClass;
171    }
172
173    public void setReturnType(String JavaDoc returnType)
174    {
175       this.returnType = returnType;
176       this.returnTypeClass = null;
177    }
178
179    public String JavaDoc getReturnType()
180    {
181       return returnType;
182    }
183
184    public Class JavaDoc getReturnTypeClass() throws ClassNotFoundException JavaDoc
185    {
186       if (returnType == null)
187          return null;
188       if (returnTypeClass == VOID)
189          return null;
190       if (returnTypeClass != null)
191          return returnTypeClass;
192       if (returnType.equals("void"))
193       {
194          returnTypeClass = VOID;
195          return null;
196       }
197       else
198          returnTypeClass = loadClass(returnType);
199       return returnTypeClass;
200    }
201
202    public boolean isReadable()
203    {
204       return isReadable;
205    }
206
207    public void setReadable(boolean readable)
208    {
209       isReadable = readable;
210    }
211
212    public boolean isWritable()
213    {
214       return isWritable;
215    }
216
217    public void setWritable(boolean writable)
218    {
219       this.isWritable = writable;
220    }
221
222    public void setInterceptors(List JavaDoc interceptors)
223    {
224       // FIXME: make a copy
225
this.interceptors = interceptors;
226    }
227
228    public List JavaDoc getInterceptors()
229    {
230       // FIXME: return a copy
231
if (interceptors == null)
232       {
233          interceptors = new ArrayList JavaDoc();
234       }
235       return interceptors;
236    }
237
238    public void setDispatcher(Interceptor d)
239    {
240       this.dispatcher = d;
241    }
242
243    public Interceptor getDispatcher()
244    {
245       return dispatcher;
246    }
247
248    void setTarget(Object JavaDoc o)
249    {
250       this.target = o;
251    }
252
253    public Object JavaDoc getTarget()
254    {
255       return target;
256    }
257
258    public void setDescriptor(Descriptor JavaDoc d)
259    {
260       this.descriptor = d;
261    }
262
263    public Descriptor JavaDoc getDescriptor()
264    {
265       return descriptor;
266    }
267
268    public void setInvoker(AbstractMBeanInvoker mi)
269    {
270       this.invoker = mi;
271    }
272
273    public MBeanInvoker getInvoker()
274    {
275       return invoker;
276    }
277
278    /**
279     * Print what's inside the InvocationContext
280     */

281    public String JavaDoc toString()
282    {
283       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(256);
284       
285       sbuf.append("InvocationContext[")
286           .append(" name=").append(name)
287           .append(", type=").append(type)
288           .append(", attributeType=").append(attributeType)
289           .append(", isReadable=").append(isReadable)
290           .append(", isWritable=").append(isWritable)
291           .append(", returnType=").append(returnType);
292       
293       if (signature != null)
294       {
295          sbuf.append(", signature=[");
296          for (int i = 0; i < signature.length; i++)
297          {
298             sbuf.append(" arg[").append(i).append("]=").append(signature[i]);
299          }
300          sbuf.append(" ] ]");
301       }
302       else
303       {
304          sbuf.append(", signature=null ]");
305       }
306       return sbuf.toString();
307    }
308
309    // Private -------------------------------------------------------
310

311    private Class JavaDoc loadClass(String JavaDoc clazz) throws ClassNotFoundException JavaDoc
312    {
313       Class JavaDoc isPrimitive = Classes.getPrimitiveTypeForName(clazz);
314       if (isPrimitive != null)
315          return Classes.getPrimitiveWrapper(isPrimitive);
316       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
317       return cl.loadClass(clazz);
318    }
319    
320    // Inner Class ---------------------------------------------------
321

322    class NullDispatcher extends AbstractInterceptor
323    {
324       public NullDispatcher()
325       {
326          super("NullDispatcher");
327       }
328       
329       public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
330       {
331          return null;
332       }
333    }
334 }
335
Popular Tags