KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > mbeanserver > JavaBeanSchemaInitializer


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.test.xml.mbeanserver;
24
25 import java.util.Properties JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27 import javax.xml.namespace.NamespaceContext JavaDoc;
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 JavaDoc;
38
39 /**
40  * JavaBeanSchemaInitializer.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 42298 $
45  */

46 public class JavaBeanSchemaInitializer implements SchemaBindingInitializer
47 {
48    /** The namespace */
49    private static final String JavaDoc JAVABEAN_NS = "urn:jboss:simplejavabean:1.0";
50
51    /** The javabean binding */
52    private static final QName JavaDoc javabeanTypeQName = new QName JavaDoc(JavaBeanSchemaInitializer.JAVABEAN_NS, "javabeanType");
53
54    static
55    {
56       PropertyEditors.init();
57    }
58
59    public SchemaBinding init(SchemaBinding schema)
60    {
61       // javabean binding
62
TypeBinding beanType = schema.getType(JavaBeanSchemaInitializer.javabeanTypeQName);
63 /*
64       beanType.setHandler(new DefaultElementHandler()
65       {
66          public Object startElement(Object parent, QName name, ElementBinding element)
67          {
68             return new Holder();
69          }
70
71          public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx)
72          {
73             Holder holder = (Holder) o;
74             String className = null;
75             Properties properties = holder.getProperties();
76             for (int i = 0; i < attrs.getLength(); ++i)
77             {
78                String localName = attrs.getLocalName(i);
79                String value = attrs.getValue(i);
80                if ("class".equals(localName))
81                {
82                   className = value;
83                   holder.setType(className);
84                   continue;
85                }
86                properties.put(localName, value);
87             }
88
89             if (className == null)
90                throw new IllegalArgumentException("No class attribute for " + elementName);
91          }
92
93          public Object endElement(Object o, QName qName, ElementBinding element)
94          {
95             Holder holder = (Holder) o;
96             Object bean;
97             try
98             {
99                bean = holder.getBean();
100                Properties props = holder.getProperties();
101                System.out.println("Converting properties: "+props);
102                PropertyEditors.mapJavaBeanProperties(bean, props, true);
103             }
104             catch (Exception e)
105             {
106                throw new IllegalStateException("Failed to init bean: "+qName+"::"+ e.getMessage());
107             }
108             return bean;
109          }
110
111       });
112 */

113
114       beanType.setHandler(new RtElementHandler()
115       {
116          public Object JavaDoc startParticle(Object JavaDoc parent,
117                                      QName JavaDoc elementName,
118                                      ParticleBinding particle,
119                                      Attributes JavaDoc attrs,
120                                      NamespaceContext JavaDoc 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 JavaDoc o, QName JavaDoc elementName, ElementBinding element, Attributes JavaDoc attrs, NamespaceContext JavaDoc nsCtx)
128          {
129             Holder holder = (Holder) o;
130             String JavaDoc className = null;
131             Properties JavaDoc properties = holder.getProperties();
132             for (int i = 0; i < attrs.getLength(); ++i)
133             {
134                String JavaDoc localName = attrs.getLocalName(i);
135                String JavaDoc 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 JavaDoc("No class attribute for " + elementName);
147          }
148
149          public Object JavaDoc endParticle(Object JavaDoc o, QName JavaDoc qName, ParticleBinding particle)
150          {
151             Holder holder = (Holder) o;
152             Object JavaDoc bean;
153             try
154             {
155                bean = holder.getBean();
156                Properties JavaDoc props = holder.getProperties();
157                //System.out.println("Converting properties: "+props);
158
PropertyEditors.mapJavaBeanProperties(bean, props, true);
159             }
160             catch (Exception JavaDoc e)
161             {
162                throw new IllegalStateException JavaDoc("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 JavaDoc clazz;
174       private Object JavaDoc bean;
175       private Properties JavaDoc properties = new Properties JavaDoc();
176
177       public Holder()
178       {
179       }
180
181       public Object JavaDoc getBean() throws Exception JavaDoc
182       {
183          if( bean == null )
184          {
185             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
186             Class JavaDoc c = loader.loadClass(clazz);
187             bean = c.newInstance();
188          }
189          return bean;
190       }
191       public Properties JavaDoc getProperties()
192       {
193          return properties;
194       }
195       public String JavaDoc getType()
196       {
197          return clazz;
198       }
199       public void setType(String JavaDoc clazz)
200       {
201          this.clazz = clazz;
202       }
203    }
204
205    public static class Property
206    {
207       private String JavaDoc name;
208       private String JavaDoc value;
209
210       public String JavaDoc getName()
211       {
212          return name;
213       }
214
215       public void setName(String JavaDoc name)
216       {
217          this.name = name;
218       }
219
220       public String JavaDoc getValue()
221       {
222          return value;
223       }
224
225       public void setValue(String JavaDoc value)
226       {
227          this.value = value;
228       }
229    }
230 }
231
Popular Tags