1. /*
  2. * ====================================================================
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  7. * reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. The end-user documentation included with the redistribution, if
  22. * any, must include the following acknowlegement:
  23. * "This product includes software developed by the
  24. * Apache Software Foundation (http://www.apache.org/)."
  25. * Alternately, this acknowlegement may appear in the software itself,
  26. * if and wherever such third-party acknowlegements normally appear.
  27. *
  28. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  29. * Foundation" must not be used to endorse or promote products derived
  30. * from this software without prior written permission. For written
  31. * permission, please contact apache@apache.org.
  32. *
  33. * 5. Products derived from this software may not be called "Apache"
  34. * nor may "Apache" appear in their names without prior written
  35. * permission of the Apache Group.
  36. *
  37. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  38. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  43. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  44. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  45. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  47. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This software consists of voluntary contributions made by many
  52. * individuals on behalf of the Apache Software Foundation. For more
  53. * information on the Apache Software Foundation, please see
  54. * <http://www.apache.org/>.
  55. *
  56. * [Additional notices, if required by prior licensing conditions]
  57. *
  58. */
  59. package org.apache.commons.modeler.modules;
  60. import java.io.InputStream;
  61. import java.util.ArrayList;
  62. import java.util.List;
  63. import org.apache.commons.logging.Log;
  64. import org.apache.commons.logging.LogFactory;
  65. import org.apache.commons.modeler.AttributeInfo;
  66. import org.apache.commons.modeler.ConstructorInfo;
  67. import org.apache.commons.modeler.FieldInfo;
  68. import org.apache.commons.modeler.ManagedBean;
  69. import org.apache.commons.modeler.NotificationInfo;
  70. import org.apache.commons.modeler.OperationInfo;
  71. import org.apache.commons.modeler.ParameterInfo;
  72. import org.apache.commons.modeler.Registry;
  73. import org.apache.commons.modeler.util.DomUtil;
  74. import org.w3c.dom.Document;
  75. import org.w3c.dom.Node;
  76. public class MbeansDescriptorsDOMSource extends ModelerSource
  77. {
  78. private static Log log = LogFactory.getLog(MbeansDescriptorsDOMSource.class);
  79. Registry registry;
  80. String location;
  81. String type;
  82. Object source;
  83. List mbeans=new ArrayList();
  84. public void setRegistry(Registry reg) {
  85. this.registry=reg;
  86. }
  87. public void setLocation( String loc ) {
  88. this.location=loc;
  89. }
  90. /** Used if a single component is loaded
  91. *
  92. * @param type
  93. */
  94. public void setType( String type ) {
  95. this.type=type;
  96. }
  97. public void setSource( Object source ) {
  98. this.source=source;
  99. }
  100. public List loadDescriptors( Registry registry, String location,
  101. String type, Object source)
  102. throws Exception
  103. {
  104. setRegistry(registry);
  105. setLocation(location);
  106. setType(type);
  107. setSource(source);
  108. execute();
  109. return mbeans;
  110. }
  111. public void execute() throws Exception {
  112. if( registry==null ) registry=Registry.getRegistry();
  113. try {
  114. InputStream stream=(InputStream)source;
  115. long t1=System.currentTimeMillis();
  116. Document doc=DomUtil.readXml(stream);
  117. // Ignore for now the name of the root element
  118. Node descriptorsN=doc.getDocumentElement();
  119. //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors");
  120. if( descriptorsN == null ) {
  121. log.error("No descriptors found");
  122. return;
  123. }
  124. Node firstMbeanN=null;
  125. if( "mbean".equals( descriptorsN.getNodeName() ) ) {
  126. firstMbeanN=descriptorsN;
  127. } else {
  128. firstMbeanN=DomUtil.getChild(descriptorsN, "mbean");
  129. }
  130. if( firstMbeanN==null ) {
  131. log.error(" No mbean tags ");
  132. return;
  133. }
  134. // Process each <mbean> element
  135. for (Node mbeanN = firstMbeanN; mbeanN != null;
  136. mbeanN= DomUtil.getNext(mbeanN))
  137. {
  138. // Create a new managed bean info
  139. ManagedBean managed=new ManagedBean();
  140. DomUtil.setAttributes(managed, mbeanN);
  141. Node firstN;
  142. // Process descriptor subnode
  143. Node mbeanDescriptorN =
  144. DomUtil.getChild(mbeanN, "descriptor");
  145. if (mbeanDescriptorN != null) {
  146. Node firstFieldN =
  147. DomUtil.getChild(mbeanDescriptorN, "field");
  148. for (Node fieldN = firstFieldN; fieldN != null;
  149. fieldN = DomUtil.getNext(fieldN)) {
  150. FieldInfo fi = new FieldInfo();
  151. DomUtil.setAttributes(fi, fieldN);
  152. managed.addField(fi);
  153. }
  154. }
  155. // process attribute nodes
  156. firstN=DomUtil.getChild( mbeanN, "attribute");
  157. for (Node descN = firstN; descN != null;
  158. descN = DomUtil.getNext( descN ))
  159. {
  160. // Create new attribute info
  161. AttributeInfo ai=new AttributeInfo();
  162. DomUtil.setAttributes(ai, descN);
  163. // Process descriptor subnode
  164. Node descriptorN =
  165. DomUtil.getChild(descN, "descriptor");
  166. if (descriptorN != null) {
  167. Node firstFieldN =
  168. DomUtil.getChild(descriptorN, "field");
  169. for (Node fieldN = firstFieldN; fieldN != null;
  170. fieldN = DomUtil.getNext(fieldN)) {
  171. FieldInfo fi = new FieldInfo();
  172. DomUtil.setAttributes(fi, fieldN);
  173. ai.addField(fi);
  174. }
  175. }
  176. // Add this info to our managed bean info
  177. managed.addAttribute( ai );
  178. if (log.isTraceEnabled()) {
  179. log.trace("Create attribute " + ai);
  180. }
  181. }
  182. // process constructor nodes
  183. firstN=DomUtil.getChild( mbeanN, "constructor");
  184. for (Node descN = firstN; descN != null;
  185. descN = DomUtil.getNext( descN )) {
  186. // Create new constructor info
  187. ConstructorInfo ci=new ConstructorInfo();
  188. DomUtil.setAttributes(ci, descN);
  189. // Process descriptor subnode
  190. Node firstDescriptorN =
  191. DomUtil.getChild(descN, "descriptor");
  192. if (firstDescriptorN != null) {
  193. Node firstFieldN =
  194. DomUtil.getChild(firstDescriptorN, "field");
  195. for (Node fieldN = firstFieldN; fieldN != null;
  196. fieldN = DomUtil.getNext(fieldN)) {
  197. FieldInfo fi = new FieldInfo();
  198. DomUtil.setAttributes(fi, fieldN);
  199. ci.addField(fi);
  200. }
  201. }
  202. // Process parameter subnodes
  203. Node firstParamN=DomUtil.getChild( descN, "parameter");
  204. for (Node paramN = firstParamN; paramN != null;
  205. paramN = DomUtil.getNext(paramN))
  206. {
  207. ParameterInfo pi=new ParameterInfo();
  208. DomUtil.setAttributes(pi, paramN);
  209. ci.addParameter( pi );
  210. }
  211. // Add this info to our managed bean info
  212. managed.addConstructor( ci );
  213. if (log.isTraceEnabled()) {
  214. log.trace("Create constructor " + ci);
  215. }
  216. }
  217. // process notification nodes
  218. firstN=DomUtil.getChild( mbeanN, "notification");
  219. for (Node descN = firstN; descN != null;
  220. descN = DomUtil.getNext( descN ))
  221. {
  222. // Create new notification info
  223. NotificationInfo ni=new NotificationInfo();
  224. DomUtil.setAttributes(ni, descN);
  225. // Process descriptor subnode
  226. Node firstDescriptorN =
  227. DomUtil.getChild(descN, "descriptor");
  228. if (firstDescriptorN != null) {
  229. Node firstFieldN =
  230. DomUtil.getChild(firstDescriptorN, "field");
  231. for (Node fieldN = firstFieldN; fieldN != null;
  232. fieldN = DomUtil.getNext(fieldN)) {
  233. FieldInfo fi = new FieldInfo();
  234. DomUtil.setAttributes(fi, fieldN);
  235. ni.addField(fi);
  236. }
  237. }
  238. // Process notification-type subnodes
  239. Node firstParamN=DomUtil.getChild( descN, "notification-type");
  240. for (Node paramN = firstParamN; paramN != null;
  241. paramN = DomUtil.getNext(paramN))
  242. {
  243. ni.addNotifType( DomUtil.getContent(paramN) );
  244. }
  245. // Add this info to our managed bean info
  246. managed.addNotification( ni );
  247. if (log.isTraceEnabled()) {
  248. log.trace("Created notification " + ni);
  249. }
  250. }
  251. // process operation nodes
  252. firstN=DomUtil.getChild( mbeanN, "operation");
  253. for (Node descN = firstN; descN != null;
  254. descN = DomUtil.getNext( descN ))
  255. {
  256. // Create new operation info
  257. OperationInfo oi=new OperationInfo();
  258. DomUtil.setAttributes(oi, descN);
  259. // Process descriptor subnode
  260. Node firstDescriptorN =
  261. DomUtil.getChild(descN, "descriptor");
  262. if (firstDescriptorN != null) {
  263. Node firstFieldN =
  264. DomUtil.getChild(firstDescriptorN, "field");
  265. for (Node fieldN = firstFieldN; fieldN != null;
  266. fieldN = DomUtil.getNext(fieldN)) {
  267. FieldInfo fi = new FieldInfo();
  268. DomUtil.setAttributes(fi, fieldN);
  269. oi.addField(fi);
  270. }
  271. }
  272. // Process parameter subnodes
  273. Node firstParamN=DomUtil.getChild( descN, "parameter");
  274. for (Node paramN = firstParamN; paramN != null;
  275. paramN = DomUtil.getNext(paramN))
  276. {
  277. ParameterInfo pi=new ParameterInfo();
  278. DomUtil.setAttributes(pi, paramN);
  279. if( log.isTraceEnabled())
  280. log.trace("Add param " + pi.getName());
  281. oi.addParameter( pi );
  282. }
  283. // Add this info to our managed bean info
  284. managed.addOperation( oi );
  285. if( log.isTraceEnabled()) {
  286. log.trace("Create operation " + oi);
  287. }
  288. }
  289. // Add the completed managed bean info to the registry
  290. //registry.addManagedBean(managed);
  291. mbeans.add( managed );
  292. }
  293. long t2=System.currentTimeMillis();
  294. log.debug( "Reading descriptors ( dom ) " + (t2-t1));
  295. } catch( Exception ex ) {
  296. log.error( "Error reading descriptors ", ex);
  297. }
  298. }
  299. }