001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.modeler.modules;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import javax.management.DynamicMBean;
023: import javax.management.MBeanAttributeInfo;
024: import javax.management.MBeanInfo;
025: import javax.management.MBeanOperationInfo;
026: import javax.management.MBeanParameterInfo;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.commons.modeler.AttributeInfo;
031: import org.apache.commons.modeler.ManagedBean;
032: import org.apache.commons.modeler.OperationInfo;
033: import org.apache.commons.modeler.ParameterInfo;
034: import org.apache.commons.modeler.Registry;
035:
036: /** Extract metadata from a dynamic mbean.
037: * Used to wrap a dynamic mbean in order to implement persistence.
038: *
039: * This is really an ugly asspect of the JMX spec - we need to convery
040: * from normal metainfo to model metainfo. The info is the same, but
041: * they use a different class. Just like the DOM spec - where all implementations
042: * get an order of unneeded complexity from the various types.
043: *
044: */
045: public class MbeansDescriptorsDynamicMBeanSource extends ModelerSource {
046: private static Log log = LogFactory
047: .getLog(MbeansDescriptorsDynamicMBeanSource.class);
048:
049: Registry registry;
050: String location;
051: String type;
052: Object source;
053: List mbeans = new ArrayList();
054:
055: public void setRegistry(Registry reg) {
056: this .registry = reg;
057: }
058:
059: public void setLocation(String loc) {
060: this .location = loc;
061: }
062:
063: /** Used if a single component is loaded
064: *
065: * @param type
066: */
067: public void setType(String type) {
068: this .type = type;
069: }
070:
071: public void setSource(Object source) {
072: this .source = source;
073: }
074:
075: public List loadDescriptors(Registry registry, String location,
076: String type, Object source) throws Exception {
077: setRegistry(registry);
078: setLocation(location);
079: setType(type);
080: setSource(source);
081: execute();
082: return mbeans;
083: }
084:
085: public void execute() throws Exception {
086: if (registry == null)
087: registry = Registry.getRegistry();
088: try {
089: ManagedBean managed = createManagedBean(registry, null,
090: source, type);
091: if (managed == null)
092: return;
093: managed.setName(type);
094:
095: mbeans.add(managed);
096:
097: } catch (Exception ex) {
098: log.error("Error reading descriptors ", ex);
099: }
100: }
101:
102: // ------------ Implementation for non-declared introspection classes
103:
104: /**
105: * XXX Find if the 'className' is the name of the MBean or
106: * the real class ( I suppose first )
107: * XXX Read (optional) descriptions from a .properties, generated
108: * from source
109: * XXX Deal with constructors
110: *
111: */
112: public ManagedBean createManagedBean(Registry registry,
113: String domain, Object realObj, String type) {
114: if (!(realObj instanceof DynamicMBean)) {
115: return null;
116: }
117: DynamicMBean dmb = (DynamicMBean) realObj;
118:
119: ManagedBean mbean = new ManagedBean();
120:
121: MBeanInfo mbi = dmb.getMBeanInfo();
122:
123: try {
124: MBeanAttributeInfo attInfo[] = mbi.getAttributes();
125: for (int i = 0; i < attInfo.length; i++) {
126: MBeanAttributeInfo mai = attInfo[i];
127: String name = mai.getName();
128:
129: AttributeInfo ai = new AttributeInfo();
130: ai.setName(name);
131:
132: ai.setType(mai.getType());
133: ai.setReadable(mai.isReadable());
134: ai.setWriteable(mai.isWritable());
135:
136: mbean.addAttribute(ai);
137: }
138:
139: MBeanOperationInfo opInfo[] = mbi.getOperations();
140: for (int i = 0; i < opInfo.length; i++) {
141: MBeanOperationInfo moi = opInfo[i];
142: OperationInfo op = new OperationInfo();
143:
144: op.setName(moi.getName());
145: op.setReturnType(moi.getReturnType());
146:
147: MBeanParameterInfo parms[] = moi.getSignature();
148: for (int j = 0; j < parms.length; j++) {
149: ParameterInfo pi = new ParameterInfo();
150: pi.setType(parms[i].getType());
151: pi.setName(parms[i].getName());
152: op.addParameter(pi);
153: }
154: mbean.addOperation(op);
155: }
156:
157: if (log.isDebugEnabled())
158: log.debug("Setting name: " + type);
159:
160: mbean.setName(type);
161:
162: return mbean;
163: } catch (Exception ex) {
164: ex.printStackTrace();
165: return null;
166: }
167: }
168:
169: }
|