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.net.URL;
  62. import java.util.ArrayList;
  63. import java.util.List;
  64. import org.apache.commons.digester.Digester;
  65. import org.apache.commons.logging.Log;
  66. import org.apache.commons.logging.LogFactory;
  67. import org.apache.commons.modeler.Registry;
  68. public class MbeansDescriptorsDigesterSource extends ModelerSource
  69. {
  70. private static Log log =
  71. LogFactory.getLog(MbeansDescriptorsDigesterSource.class);
  72. Registry registry;
  73. String location;
  74. String type;
  75. Object source;
  76. List mbeans=new ArrayList();
  77. public void setRegistry(Registry reg) {
  78. this.registry=reg;
  79. }
  80. public void setLocation( String loc ) {
  81. this.location=loc;
  82. }
  83. /** Used if a single component is loaded
  84. *
  85. * @param type
  86. */
  87. public void setType( String type ) {
  88. this.type=type;
  89. }
  90. public void setSource( Object source ) {
  91. this.source=source;
  92. }
  93. public List loadDescriptors( Registry registry, String location,
  94. String type, Object source)
  95. throws Exception
  96. {
  97. setRegistry(registry);
  98. setLocation(location);
  99. setType(type);
  100. setSource(source);
  101. execute();
  102. return mbeans;
  103. }
  104. public void execute() throws Exception {
  105. if( registry==null ) registry=Registry.getRegistry();
  106. InputStream stream=(InputStream)source;
  107. long t1=System.currentTimeMillis();
  108. Digester digester = new Digester();
  109. digester.setNamespaceAware(false);
  110. digester.setValidating(false);
  111. URL url = registry.getClass().getResource
  112. ("/org/apache/commons/modeler/mbeans-descriptors.dtd");
  113. digester.register
  114. ("-//Apache Software Foundation//DTD Model MBeans Configuration File",
  115. url.toString());
  116. // Push our registry object onto the stack
  117. digester.push(mbeans);
  118. // Configure the parsing rules
  119. digester.addObjectCreate
  120. ("mbeans-descriptors/mbean",
  121. "org.apache.commons.modeler.ManagedBean");
  122. digester.addSetProperties
  123. ("mbeans-descriptors/mbean");
  124. digester.addSetNext
  125. ("mbeans-descriptors/mbean",
  126. "add",
  127. "java.lang.Object");
  128. digester.addObjectCreate
  129. ("mbeans-descriptors/mbean/attribute",
  130. "org.apache.commons.modeler.AttributeInfo");
  131. digester.addSetProperties
  132. ("mbeans-descriptors/mbean/attribute");
  133. digester.addSetNext
  134. ("mbeans-descriptors/mbean/attribute",
  135. "addAttribute",
  136. "org.apache.commons.modeler.AttributeInfo");
  137. digester.addObjectCreate
  138. ("mbeans-descriptors/mbean/attribute/descriptor/field",
  139. "org.apache.commons.modeler.FieldInfo");
  140. digester.addSetProperties
  141. ("mbeans-descriptors/mbean/attribute/descriptor/field");
  142. digester.addSetNext
  143. ("mbeans-descriptors/mbean/attribute/descriptor/field",
  144. "addField",
  145. "org.apache.commons.modeler.FieldInfo");
  146. digester.addObjectCreate
  147. ("mbeans-descriptors/mbean/constructor",
  148. "org.apache.commons.modeler.ConstructorInfo");
  149. digester.addSetProperties
  150. ("mbeans-descriptors/mbean/constructor");
  151. digester.addSetNext
  152. ("mbeans-descriptors/mbean/constructor",
  153. "addConstructor",
  154. "org.apache.commons.modeler.ConstructorInfo");
  155. digester.addObjectCreate
  156. ("mbeans-descriptors/mbean/constructor/descriptor/field",
  157. "org.apache.commons.modeler.FieldInfo");
  158. digester.addSetProperties
  159. ("mbeans-descriptors/mbean/constructor/descriptor/field");
  160. digester.addSetNext
  161. ("mbeans-descriptors/mbean/constructor/descriptor/field",
  162. "addField",
  163. "org.apache.commons.modeler.FieldInfo");
  164. digester.addObjectCreate
  165. ("mbeans-descriptors/mbean/constructor/parameter",
  166. "org.apache.commons.modeler.ParameterInfo");
  167. digester.addSetProperties
  168. ("mbeans-descriptors/mbean/constructor/parameter");
  169. digester.addSetNext
  170. ("mbeans-descriptors/mbean/constructor/parameter",
  171. "addParameter",
  172. "org.apache.commons.modeler.ParameterInfo");
  173. digester.addObjectCreate
  174. ("mbeans-descriptors/mbean/descriptor/field",
  175. "org.apache.commons.modeler.FieldInfo");
  176. digester.addSetProperties
  177. ("mbeans-descriptors/mbean/descriptor/field");
  178. digester.addSetNext
  179. ("mbeans-descriptors/mbean/descriptor/field",
  180. "addField",
  181. "org.apache.commons.modeler.FieldInfo");
  182. digester.addObjectCreate
  183. ("mbeans-descriptors/mbean/notification",
  184. "org.apache.commons.modeler.NotificationInfo");
  185. digester.addSetProperties
  186. ("mbeans-descriptors/mbean/notification");
  187. digester.addSetNext
  188. ("mbeans-descriptors/mbean/notification",
  189. "addNotification",
  190. "org.apache.commons.modeler.NotificationInfo");
  191. digester.addObjectCreate
  192. ("mbeans-descriptors/mbean/notification/descriptor/field",
  193. "org.apache.commons.modeler.FieldInfo");
  194. digester.addSetProperties
  195. ("mbeans-descriptors/mbean/notification/descriptor/field");
  196. digester.addSetNext
  197. ("mbeans-descriptors/mbean/notification/descriptor/field",
  198. "addField",
  199. "org.apache.commons.modeler.FieldInfo");
  200. digester.addCallMethod
  201. ("mbeans-descriptors/mbean/notification/notification-type",
  202. "addNotifType", 0);
  203. digester.addObjectCreate
  204. ("mbeans-descriptors/mbean/operation",
  205. "org.apache.commons.modeler.OperationInfo");
  206. digester.addSetProperties
  207. ("mbeans-descriptors/mbean/operation");
  208. digester.addSetNext
  209. ("mbeans-descriptors/mbean/operation",
  210. "addOperation",
  211. "org.apache.commons.modeler.OperationInfo");
  212. digester.addObjectCreate
  213. ("mbeans-descriptors/mbean/operation/descriptor/field",
  214. "org.apache.commons.modeler.FieldInfo");
  215. digester.addSetProperties
  216. ("mbeans-descriptors/mbean/operation/descriptor/field");
  217. digester.addSetNext
  218. ("mbeans-descriptors/mbean/operation/descriptor/field",
  219. "addField",
  220. "org.apache.commons.modeler.FieldInfo");
  221. digester.addObjectCreate
  222. ("mbeans-descriptors/mbean/operation/parameter",
  223. "org.apache.commons.modeler.ParameterInfo");
  224. digester.addSetProperties
  225. ("mbeans-descriptors/mbean/operation/parameter");
  226. digester.addSetNext
  227. ("mbeans-descriptors/mbean/operation/parameter",
  228. "addParameter",
  229. "org.apache.commons.modeler.ParameterInfo");
  230. // Process the input file to configure our registry
  231. try {
  232. digester.parse(stream);
  233. } catch (Exception e) {
  234. log.error("Error digesting Registry data", e);
  235. throw e;
  236. }
  237. long t2=System.currentTimeMillis();
  238. // if( t2-t1 > 500 )
  239. log.info("Loaded registry information (digester) " + ( t2 - t1 ) + " ms");
  240. }
  241. }