1 22 23 package org.jboss.test.xml.mbeanserver; 24 25 import java.util.Properties ; 26 import javax.xml.namespace.QName ; 27 import javax.xml.namespace.NamespaceContext ; 28 29 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingInitializer; 30 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding; 31 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding; 32 import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler; 33 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding; 34 import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding; 35 import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtElementHandler; 36 import org.jboss.util.propertyeditor.PropertyEditors; 37 import org.xml.sax.Attributes ; 38 39 46 public class JavaBeanSchemaInitializer implements SchemaBindingInitializer 47 { 48 49 private static final String JAVABEAN_NS = "urn:jboss:simplejavabean:1.0"; 50 51 52 private static final QName javabeanTypeQName = new QName (JavaBeanSchemaInitializer.JAVABEAN_NS, "javabeanType"); 53 54 static 55 { 56 PropertyEditors.init(); 57 } 58 59 public SchemaBinding init(SchemaBinding schema) 60 { 61 TypeBinding beanType = schema.getType(JavaBeanSchemaInitializer.javabeanTypeQName); 63 113 114 beanType.setHandler(new RtElementHandler() 115 { 116 public Object startParticle(Object parent, 117 QName elementName, 118 ParticleBinding particle, 119 Attributes attrs, 120 NamespaceContext nsCtx) 121 { 122 Holder o = new Holder(); 123 attributes(o, elementName, (ElementBinding)particle.getTerm(), attrs, nsCtx); 124 return o; 125 } 126 127 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 128 { 129 Holder holder = (Holder) o; 130 String className = null; 131 Properties properties = holder.getProperties(); 132 for (int i = 0; i < attrs.getLength(); ++i) 133 { 134 String localName = attrs.getLocalName(i); 135 String value = attrs.getValue(i); 136 if ("class".equals(localName)) 137 { 138 className = value; 139 holder.setType(className); 140 continue; 141 } 142 properties.put(localName, value); 143 } 144 145 if (className == null) 146 throw new IllegalArgumentException ("No class attribute for " + elementName); 147 } 148 149 public Object endParticle(Object o, QName qName, ParticleBinding particle) 150 { 151 Holder holder = (Holder) o; 152 Object bean; 153 try 154 { 155 bean = holder.getBean(); 156 Properties props = holder.getProperties(); 157 PropertyEditors.mapJavaBeanProperties(bean, props, true); 159 } 160 catch (Exception e) 161 { 162 throw new IllegalStateException ("Failed to init bean: "+qName+"::"+ e.getMessage()); 163 } 164 return bean; 165 } 166 }); 167 168 return schema; 169 } 170 171 public static class Holder 172 { 173 private String clazz; 174 private Object bean; 175 private Properties properties = new Properties (); 176 177 public Holder() 178 { 179 } 180 181 public Object getBean() throws Exception 182 { 183 if( bean == null ) 184 { 185 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 186 Class c = loader.loadClass(clazz); 187 bean = c.newInstance(); 188 } 189 return bean; 190 } 191 public Properties getProperties() 192 { 193 return properties; 194 } 195 public String getType() 196 { 197 return clazz; 198 } 199 public void setType(String clazz) 200 { 201 this.clazz = clazz; 202 } 203 } 204 205 public static class Property 206 { 207 private String name; 208 private String value; 209 210 public String getName() 211 { 212 return name; 213 } 214 215 public void setName(String name) 216 { 217 this.name = name; 218 } 219 220 public String getValue() 221 { 222 return value; 223 } 224 225 public void setValue(String value) 226 { 227 this.value = value; 228 } 229 } 230 } 231 | Popular Tags |