001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.server;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import javax.management.Descriptor;
028: import javax.management.MBeanParameterInfo;
029:
030: import org.jboss.mx.interceptor.AbstractInterceptor;
031: import org.jboss.mx.interceptor.Interceptor;
032: import org.jboss.util.Classes;
033:
034: /**
035: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
036: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
037: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
038: * @version $Revision: 57200 $
039: */
040: public class InvocationContext {
041:
042: // Constants -----------------------------------------------------
043:
044: public final static String OP_INVOKE = "invoke";
045: public final static String OP_GETATTRIBUTE = "getAttribute";
046: public final static String OP_SETATTRIBUTE = "setAttribute";
047: public final static String OP_GETMBEANINFO = "getMBeanInfo";
048:
049: public final static String OPERATION_IMPACT = "operation.impact";
050: public final static String ATTRIBUTE_ACCESS = "attribute.access";
051:
052: /* Marker for void */
053: private static final Class VOID = Void.class;
054:
055: public static final Class[] NOCLASSES = new Class[0];
056:
057: // Attributes ----------------------------------------------------
058:
059: private String attributeType = null;
060: private String name = null;
061: private String[] signature = null;
062: private String returnType = null;
063: private String type = null;
064: private boolean isWritable = true;
065: private boolean isReadable = true;
066:
067: List interceptors = null;
068: transient Interceptor dispatcher = new NullDispatcher();
069: transient Object target = null;
070: transient Descriptor descriptor = null;
071: transient AbstractMBeanInvoker invoker = null;
072: transient Class attributeTypeClass = null;
073: transient Class returnTypeClass = null;
074: transient Class[] signatureClasses = null;
075:
076: // Public --------------------------------------------------------
077:
078: public final void copy(final InvocationContext src) {
079: if (src == null)
080: return;
081:
082: this .attributeType = src.attributeType;
083: this .attributeTypeClass = src.attributeTypeClass;
084: this .name = src.name;
085: this .signature = src.signature;
086: this .signatureClasses = src.signatureClasses;
087: this .returnType = src.returnType;
088: this .returnTypeClass = src.returnTypeClass;
089: this .type = src.type;
090: this .isWritable = src.isWritable;
091: this .interceptors = src.interceptors;
092: this .dispatcher = src.dispatcher;
093: this .target = src.target;
094: this .descriptor = src.descriptor;
095: this .invoker = src.invoker;
096: }
097:
098: public void setType(String type) {
099: this .type = type;
100: }
101:
102: public String getType() {
103: return type;
104: }
105:
106: public void setName(String name) {
107: this .name = name;
108: }
109:
110: public String getName() {
111: return name;
112: }
113:
114: void setSignature(String[] signature) {
115: this .signature = signature;
116: }
117:
118: void setSignature(MBeanParameterInfo[] signature) {
119: this .signature = new String[signature.length];
120:
121: for (int i = 0; i < signature.length; ++i)
122: this .signature[i] = signature[i].getType();
123:
124: }
125:
126: public String[] getSignature() {
127: return signature;
128: }
129:
130: public Class[] getSignatureClasses() throws ClassNotFoundException {
131: if (signatureClasses != null)
132: return signatureClasses;
133: if (signature == null || signature.length == 0)
134: return NOCLASSES;
135: Class[] signatureClassesTemp = new Class[signature.length];
136: for (int i = 0; i < signature.length; ++i)
137: signatureClassesTemp[i] = Thread.currentThread()
138: .getContextClassLoader().loadClass(signature[i]);
139: signatureClasses = signatureClassesTemp;
140: return signatureClasses;
141: }
142:
143: public void setAttributeType(String attrType) {
144: this .attributeType = attrType;
145: this .attributeTypeClass = null;
146: }
147:
148: public String getAttributeType() {
149: return attributeType;
150: }
151:
152: public Class getAttributeTypeClass() throws ClassNotFoundException {
153: if (attributeType == null)
154: return null;
155: if (attributeTypeClass != null)
156: return attributeTypeClass;
157: attributeTypeClass = loadClass(attributeType);
158: return attributeTypeClass;
159: }
160:
161: public void setReturnType(String returnType) {
162: this .returnType = returnType;
163: this .returnTypeClass = null;
164: }
165:
166: public String getReturnType() {
167: return returnType;
168: }
169:
170: public Class getReturnTypeClass() throws ClassNotFoundException {
171: if (returnType == null)
172: return null;
173: if (returnTypeClass == VOID)
174: return null;
175: if (returnTypeClass != null)
176: return returnTypeClass;
177: if (returnType.equals("void")) {
178: returnTypeClass = VOID;
179: return null;
180: } else
181: returnTypeClass = loadClass(returnType);
182: return returnTypeClass;
183: }
184:
185: public boolean isReadable() {
186: return isReadable;
187: }
188:
189: public void setReadable(boolean readable) {
190: isReadable = readable;
191: }
192:
193: public boolean isWritable() {
194: return isWritable;
195: }
196:
197: public void setWritable(boolean writable) {
198: this .isWritable = writable;
199: }
200:
201: public void setInterceptors(List interceptors) {
202: // FIXME: make a copy
203: this .interceptors = interceptors;
204: }
205:
206: public List getInterceptors() {
207: // FIXME: return a copy
208: if (interceptors == null) {
209: interceptors = new ArrayList();
210: }
211: return interceptors;
212: }
213:
214: public void setDispatcher(Interceptor d) {
215: this .dispatcher = d;
216: }
217:
218: public Interceptor getDispatcher() {
219: return dispatcher;
220: }
221:
222: void setTarget(Object o) {
223: this .target = o;
224: }
225:
226: public Object getTarget() {
227: return target;
228: }
229:
230: public void setDescriptor(Descriptor d) {
231: this .descriptor = d;
232: }
233:
234: public Descriptor getDescriptor() {
235: return descriptor;
236: }
237:
238: public void setInvoker(AbstractMBeanInvoker mi) {
239: this .invoker = mi;
240: }
241:
242: public MBeanInvoker getInvoker() {
243: return invoker;
244: }
245:
246: /**
247: * Print what's inside the InvocationContext
248: */
249: public String toString() {
250: StringBuffer sbuf = new StringBuffer(256);
251:
252: sbuf.append("InvocationContext[").append(" name=").append(name)
253: .append(", type=").append(type).append(
254: ", attributeType=").append(attributeType)
255: .append(", isReadable=").append(isReadable).append(
256: ", isWritable=").append(isWritable).append(
257: ", returnType=").append(returnType);
258:
259: if (signature != null) {
260: sbuf.append(", signature=[");
261: for (int i = 0; i < signature.length; i++) {
262: sbuf.append(" arg[").append(i).append("]=").append(
263: signature[i]);
264: }
265: sbuf.append(" ] ]");
266: } else {
267: sbuf.append(", signature=null ]");
268: }
269: return sbuf.toString();
270: }
271:
272: // Private -------------------------------------------------------
273:
274: private Class loadClass(String clazz) throws ClassNotFoundException {
275: Class isPrimitive = Classes.getPrimitiveTypeForName(clazz);
276: if (isPrimitive != null)
277: return Classes.getPrimitiveWrapper(isPrimitive);
278: ClassLoader cl = Thread.currentThread().getContextClassLoader();
279: return cl.loadClass(clazz);
280: }
281:
282: // Inner Class ---------------------------------------------------
283:
284: class NullDispatcher extends AbstractInterceptor {
285: public NullDispatcher() {
286: super ("NullDispatcher");
287: }
288:
289: public Object invoke(Invocation invocation) throws Throwable {
290: return null;
291: }
292: }
293: }
|