001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx;
009:
010: import java.lang.reflect.InvocationTargetException;
011: import java.lang.reflect.Constructor;
012: import javax.management.MBeanException;
013: import javax.management.ReflectionException;
014: import javax.management.RuntimeMBeanException;
015: import javax.management.RuntimeErrorException;
016:
017: import org.huihoo.jfox.jmx.loading.SignatureClassLoader;
018:
019: /**
020: *
021: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
022: */
023:
024: public class MBeanInstantiatorSupport implements MBeanInstantiator {
025:
026: private SignatureClassLoader signatureLoader = SignatureClassLoader
027: .getInstance();
028: // private Set interceptors = new HashSet();
029:
030: private static MBeanInstantiator me = new MBeanInstantiatorSupport();
031:
032: private MBeanInstantiatorSupport() {
033:
034: }
035:
036: public static MBeanInstantiator getInstance() {
037: return me;
038: }
039:
040: public Object instantiate(Class mbeanClass)
041: throws ReflectionException, MBeanException {
042: Object obj = null;
043: try {
044: Constructor constructor = mbeanClass.getConstructor(null);
045: obj = constructor.newInstance(null);
046: } catch (NoSuchMethodException e) {
047: throw new ReflectionException(e);
048: } catch (InstantiationException e) {
049: throw new ReflectionException(e);
050: } catch (IllegalAccessException e) {
051: throw new ReflectionException(e);
052: } catch (InvocationTargetException e) {
053: Throwable throwable = e.getTargetException();
054: if (throwable instanceof RuntimeException)
055: throw new RuntimeMBeanException(
056: (RuntimeException) throwable);
057: if (throwable instanceof Error)
058: throw new RuntimeErrorException((Error) throwable);
059: else
060: throw new MBeanException((Exception) throwable);
061: }
062:
063: return obj;
064: }
065:
066: // instantiate a mbean use it consturctor with some args
067: public Object instantiate(Class mbeanClass, Object[] params,
068: String[] signatures) throws ReflectionException,
069: MBeanException {
070: Object obj = null;
071: Class[] _signatures = this .loadSignatureClasses(signatures);
072: try {
073: Constructor constructor = mbeanClass
074: .getConstructor(_signatures);
075: obj = constructor.newInstance(params);
076: } catch (NoSuchMethodException e) {
077: throw new ReflectionException(e, e.toString()
078: + ", No Such Constructor");
079: } catch (InstantiationException e) {
080: throw new ReflectionException(e, e.toString());
081: } catch (IllegalAccessException e) {
082: throw new ReflectionException(e, e.toString());
083: } catch (InvocationTargetException e) {
084: Throwable throwable = e.getTargetException();
085: if (throwable instanceof RuntimeException)
086: throw new RuntimeMBeanException(
087: (RuntimeException) throwable);
088: if (throwable instanceof Error)
089: throw new RuntimeErrorException((Error) throwable);
090: else
091: throw new MBeanException((Exception) throwable);
092: }
093:
094: return obj;
095: }
096:
097: private Class[] loadSignatureClasses(String[] signatures)
098: throws ReflectionException {
099: Class[] signatureClasses = null;
100: try {
101: if (signatures != null && signatures.length != 0)
102: signatureClasses = signatureLoader
103: .loadClasses(signatures);
104: } catch (ClassNotFoundException e) {
105: throw new ReflectionException(e, e.toString());
106: }
107: return signatureClasses;
108: }
109:
110: /*
111: public Object invoke(MBeanMetaData meta, String operationName, Object[] params, String[] signatures) throws MBeanException, ReflectionException {
112: this.preInvoke(meta.getObjectName(),MBeanServerInterceptor.INVOKE,operationName,params);
113:
114: Object obj = meta.invoke(operationName,params,signatures);
115:
116: this.postInvoke(meta.getObjectName(),MBeanServerInterceptor.INVOKE,operationName,params);
117:
118: return obj;
119: }
120:
121: public Object getAttribute(MBeanMetaData meta, String attribute) throws MBeanException, AttributeNotFoundException, ReflectionException {
122: this.preInvoke(meta.getObjectName(),MBeanServerInterceptor.GET_ATTRIBUTE,"get" + attribute,null);
123:
124: Object obj = meta.getAttribute(attribute);
125:
126: this.postInvoke(meta.getObjectName(),MBeanServerInterceptor.GET_ATTRIBUTE,"get" + attribute,null);
127:
128: return obj;
129:
130: }
131:
132: public AttributeList getAttributes(MBeanMetaData meta, String[] attributes) throws MBeanException, AttributeNotFoundException, ReflectionException {
133: this.preInvoke(meta.getObjectName(),MBeanServerInterceptor.GET_ATTRIBUTES,MBeanServerInterceptor.GET_ATTRIBUTES,attributes);
134: AttributeList attrList = meta.getAttributes(attributes);
135: this.postInvoke(meta.getObjectName(),MBeanServerInterceptor.GET_ATTRIBUTES,MBeanServerInterceptor.GET_ATTRIBUTES,attributes);
136: return attrList;
137: }
138:
139: public AttributeList setAttributes(MBeanMetaData meta, AttributeList attributes) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
140: Attribute[] preAttributes = (Attribute[])attributes.toArray(new Attribute[0]);
141: this.preInvoke(meta.getObjectName(),MBeanServerInterceptor.SET_ATTRIBUTES,MBeanServerInterceptor.SET_ATTRIBUTES,preAttributes);
142: AttributeList _attributes = meta.setAttributes(attributes);
143: Attribute[] postAttributes = (Attribute[])_attributes.toArray(new Attribute[0]);
144: this.postInvoke(meta.getObjectName(),MBeanServerInterceptor.SET_ATTRIBUTES,MBeanServerInterceptor.SET_ATTRIBUTES,postAttributes);
145: return _attributes;
146: }
147:
148: public void setAttribute(MBeanMetaData meta, Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
149: String attributeName = attribute.getNameSpace();
150: this.preInvoke(meta.getObjectName(),MBeanServerInterceptor.SET_ATTRIBUTE,"set" + attributeName,new Attribute[]{attribute});
151:
152: meta.setAttribute(attribute);
153:
154: this.postInvoke(meta.getObjectName(),MBeanServerInterceptor.SET_ATTRIBUTE,"set" + attributeName,new Attribute[]{attribute});
155:
156: }
157: // instantiate a mbean use it consturctor with no args
158:
159:
160: public void registerInterceptor(MBeanServerInterceptor interceptor, MBeanServerInterceptorFilter filter) {
161: if(existIntercetptor(interceptor) == null) interceptors.add(new FilteredMBeanServerInterceptor(interceptor,filter));
162: }
163:
164: public void unregisterInterceptor(MBeanServerInterceptor interceptor) {
165: FilteredMBeanServerInterceptor fmsi = existIntercetptor(interceptor);
166: if(fmsi != null) interceptors.remove(fmsi);
167: }
168:
169: private FilteredMBeanServerInterceptor existIntercetptor(MBeanServerInterceptor interceptor){
170: Iterator it = interceptors.iterator();
171: while(it.hasNext()){
172: FilteredMBeanServerInterceptor fmsi = (FilteredMBeanServerInterceptor)it.next();
173: if(fmsi.getInterceptor().getClass().getNameSpace().equals(interceptor.getClass().getNameSpace())){
174: return fmsi;
175: }
176: }
177: return null;
178: }
179:
180: private void preInvoke(ObjectName DEFAULT_OBJECTNAME, String methodName,String operationName,Object[] params) throws MBeanException {
181: Iterator it = interceptors.iterator();
182: while(it.hasNext()){
183: FilteredMBeanServerInterceptor fmsi = (FilteredMBeanServerInterceptor)it.next();
184: MBeanServerInterceptorFilter filter = fmsi.getFilter();
185: if(filter != null && filter.isMethodEnabled(DEFAULT_OBJECTNAME,methodName,params)){
186: try {
187: fmsi.getInterceptor().preInvoke(DEFAULT_OBJECTNAME,methodName,operationName,params);
188: }
189: catch(Exception e){
190: throw new MBeanException(e,e.toString());
191: }
192: }
193: }
194: }
195:
196: private void postInvoke(ObjectName DEFAULT_OBJECTNAME, String methodName,String operationName, Object[] params) throws MBeanException {
197: Iterator it = interceptors.iterator();
198: while(it.hasNext()){
199: FilteredMBeanServerInterceptor fmsi = (FilteredMBeanServerInterceptor)it.next();
200: MBeanServerInterceptorFilter filter = fmsi.getFilter();
201: if(filter != null && filter.isMethodEnabled(DEFAULT_OBJECTNAME,methodName,params)){
202: try {
203: fmsi.getInterceptor().postInvoke(DEFAULT_OBJECTNAME,methodName,operationName,params);
204: }
205: catch(Exception e){
206: throw new MBeanException(e,e.toString());
207: }
208: }
209: }
210: }
211:
212: */
213:
214: }
|