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 018 019 package org.apache.commons.modeler.modules; 020 021 import java.io.InputStream; 022 import java.util.ArrayList; 023 import java.util.List; 024 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 import org.apache.commons.modeler.AttributeInfo; 028 import org.apache.commons.modeler.ConstructorInfo; 029 import org.apache.commons.modeler.FieldInfo; 030 import org.apache.commons.modeler.ManagedBean; 031 import org.apache.commons.modeler.NotificationInfo; 032 import org.apache.commons.modeler.OperationInfo; 033 import org.apache.commons.modeler.ParameterInfo; 034 import org.apache.commons.modeler.Registry; 035 import org.apache.commons.modeler.util.DomUtil; 036 import org.w3c.dom.Document; 037 import org.w3c.dom.Node; 038 039 040 public class MbeansDescriptorsDOMSource extends ModelerSource 041 { 042 private static Log log = LogFactory.getLog(MbeansDescriptorsDOMSource.class); 043 044 Registry registry; 045 String location; 046 String type; 047 Object source; 048 List mbeans=new ArrayList(); 049 050 public void setRegistry(Registry reg) { 051 this.registry=reg; 052 } 053 054 public void setLocation( String loc ) { 055 this.location=loc; 056 } 057 058 /** Used if a single component is loaded 059 * 060 * @param type 061 */ 062 public void setType( String type ) { 063 this.type=type; 064 } 065 066 public void setSource( Object source ) { 067 this.source=source; 068 } 069 070 public List loadDescriptors( Registry registry, String location, 071 String type, Object source) 072 throws Exception 073 { 074 setRegistry(registry); 075 setLocation(location); 076 setType(type); 077 setSource(source); 078 execute(); 079 return mbeans; 080 } 081 082 public void execute() throws Exception { 083 if( registry==null ) registry=Registry.getRegistry(); 084 085 try { 086 InputStream stream=(InputStream)source; 087 long t1=System.currentTimeMillis(); 088 Document doc=DomUtil.readXml(stream); 089 // Ignore for now the name of the root element 090 Node descriptorsN=doc.getDocumentElement(); 091 //Node descriptorsN=DomUtil.getChild(doc, "mbeans-descriptors"); 092 if( descriptorsN == null ) { 093 log.error("No descriptors found"); 094 return; 095 } 096 097 Node firstMbeanN=null; 098 if( "mbean".equals( descriptorsN.getNodeName() ) ) { 099 firstMbeanN=descriptorsN; 100 } else { 101 firstMbeanN=DomUtil.getChild(descriptorsN, "mbean"); 102 } 103 104 if( firstMbeanN==null ) { 105 log.error(" No mbean tags "); 106 return; 107 } 108 109 // Process each <mbean> element 110 for (Node mbeanN = firstMbeanN; mbeanN != null; 111 mbeanN= DomUtil.getNext(mbeanN)) 112 { 113 114 // Create a new managed bean info 115 ManagedBean managed=new ManagedBean(); 116 DomUtil.setAttributes(managed, mbeanN); 117 Node firstN; 118 119 // Process descriptor subnode 120 Node mbeanDescriptorN = 121 DomUtil.getChild(mbeanN, "descriptor"); 122 if (mbeanDescriptorN != null) { 123 Node firstFieldN = 124 DomUtil.getChild(mbeanDescriptorN, "field"); 125 for (Node fieldN = firstFieldN; fieldN != null; 126 fieldN = DomUtil.getNext(fieldN)) { 127 FieldInfo fi = new FieldInfo(); 128 DomUtil.setAttributes(fi, fieldN); 129 managed.addField(fi); 130 } 131 } 132 133 // process attribute nodes 134 firstN=DomUtil.getChild( mbeanN, "attribute"); 135 for (Node descN = firstN; descN != null; 136 descN = DomUtil.getNext( descN )) 137 { 138 139 // Create new attribute info 140 AttributeInfo ai=new AttributeInfo(); 141 DomUtil.setAttributes(ai, descN); 142 143 // Process descriptor subnode 144 Node descriptorN = 145 DomUtil.getChild(descN, "descriptor"); 146 if (descriptorN != null) { 147 Node firstFieldN = 148 DomUtil.getChild(descriptorN, "field"); 149 for (Node fieldN = firstFieldN; fieldN != null; 150 fieldN = DomUtil.getNext(fieldN)) { 151 FieldInfo fi = new FieldInfo(); 152 DomUtil.setAttributes(fi, fieldN); 153 ai.addField(fi); 154 } 155 } 156 157 // Add this info to our managed bean info 158 managed.addAttribute( ai ); 159 if (log.isTraceEnabled()) { 160 log.trace("Create attribute " + ai); 161 } 162 163 } 164 165 // process constructor nodes 166 firstN=DomUtil.getChild( mbeanN, "constructor"); 167 for (Node descN = firstN; descN != null; 168 descN = DomUtil.getNext( descN )) { 169 170 // Create new constructor info 171 ConstructorInfo ci=new ConstructorInfo(); 172 DomUtil.setAttributes(ci, descN); 173 174 // Process descriptor subnode 175 Node firstDescriptorN = 176 DomUtil.getChild(descN, "descriptor"); 177 if (firstDescriptorN != null) { 178 Node firstFieldN = 179 DomUtil.getChild(firstDescriptorN, "field"); 180 for (Node fieldN = firstFieldN; fieldN != null; 181 fieldN = DomUtil.getNext(fieldN)) { 182 FieldInfo fi = new FieldInfo(); 183 DomUtil.setAttributes(fi, fieldN); 184 ci.addField(fi); 185 } 186 } 187 188 // Process parameter subnodes 189 Node firstParamN=DomUtil.getChild( descN, "parameter"); 190 for (Node paramN = firstParamN; paramN != null; 191 paramN = DomUtil.getNext(paramN)) 192 { 193 ParameterInfo pi=new ParameterInfo(); 194 DomUtil.setAttributes(pi, paramN); 195 ci.addParameter( pi ); 196 } 197 198 // Add this info to our managed bean info 199 managed.addConstructor( ci ); 200 if (log.isTraceEnabled()) { 201 log.trace("Create constructor " + ci); 202 } 203 204 } 205 206 // process notification nodes 207 firstN=DomUtil.getChild( mbeanN, "notification"); 208 for (Node descN = firstN; descN != null; 209 descN = DomUtil.getNext( descN )) 210 { 211 212 // Create new notification info 213 NotificationInfo ni=new NotificationInfo(); 214 DomUtil.setAttributes(ni, descN); 215 216 // Process descriptor subnode 217 Node firstDescriptorN = 218 DomUtil.getChild(descN, "descriptor"); 219 if (firstDescriptorN != null) { 220 Node firstFieldN = 221 DomUtil.getChild(firstDescriptorN, "field"); 222 for (Node fieldN = firstFieldN; fieldN != null; 223 fieldN = DomUtil.getNext(fieldN)) { 224 FieldInfo fi = new FieldInfo(); 225 DomUtil.setAttributes(fi, fieldN); 226 ni.addField(fi); 227 } 228 } 229 230 // Process notification-type subnodes 231 Node firstParamN=DomUtil.getChild( descN, "notification-type"); 232 for (Node paramN = firstParamN; paramN != null; 233 paramN = DomUtil.getNext(paramN)) 234 { 235 ni.addNotifType( DomUtil.getContent(paramN) ); 236 } 237 238 // Add this info to our managed bean info 239 managed.addNotification( ni ); 240 if (log.isTraceEnabled()) { 241 log.trace("Created notification " + ni); 242 } 243 244 } 245 246 // process operation nodes 247 firstN=DomUtil.getChild( mbeanN, "operation"); 248 for (Node descN = firstN; descN != null; 249 descN = DomUtil.getNext( descN )) 250 251 { 252 253 // Create new operation info 254 OperationInfo oi=new OperationInfo(); 255 DomUtil.setAttributes(oi, descN); 256 257 // Process descriptor subnode 258 Node firstDescriptorN = 259 DomUtil.getChild(descN, "descriptor"); 260 if (firstDescriptorN != null) { 261 Node firstFieldN = 262 DomUtil.getChild(firstDescriptorN, "field"); 263 for (Node fieldN = firstFieldN; fieldN != null; 264 fieldN = DomUtil.getNext(fieldN)) { 265 FieldInfo fi = new FieldInfo(); 266 DomUtil.setAttributes(fi, fieldN); 267 oi.addField(fi); 268 } 269 } 270 271 // Process parameter subnodes 272 Node firstParamN=DomUtil.getChild( descN, "parameter"); 273 for (Node paramN = firstParamN; paramN != null; 274 paramN = DomUtil.getNext(paramN)) 275 { 276 ParameterInfo pi=new ParameterInfo(); 277 DomUtil.setAttributes(pi, paramN); 278 if( log.isTraceEnabled()) 279 log.trace("Add param " + pi.getName()); 280 oi.addParameter( pi ); 281 } 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 } 290 291 // Add the completed managed bean info to the registry 292 //registry.addManagedBean(managed); 293 mbeans.add( managed ); 294 295 } 296 297 long t2=System.currentTimeMillis(); 298 log.debug( "Reading descriptors ( dom ) " + (t2-t1)); 299 } catch( Exception ex ) { 300 log.error( "Error reading descriptors ", ex); 301 } 302 } 303 }