KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > metamodel > descriptor > JBossWebDDObjectFactory


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 package org.jboss.web.metamodel.descriptor;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.metamodel.descriptor.DDObjectFactory;
26 import org.jboss.metamodel.descriptor.EjbLocalRef;
27 import org.jboss.metamodel.descriptor.EjbRef;
28 import org.jboss.metamodel.descriptor.EnvEntry;
29 import org.jboss.metamodel.descriptor.MessageDestination;
30 import org.jboss.metamodel.descriptor.MessageDestinationRef;
31 import org.jboss.metamodel.descriptor.ResourceEnvRef;
32 import org.jboss.metamodel.descriptor.ResourceRef;
33 import org.jboss.metamodel.descriptor.ServiceRef;
34 import org.jboss.util.xml.JBossEntityResolver;
35 import org.jboss.xb.binding.JBossXBException;
36 import org.jboss.xb.binding.ObjectModelFactory;
37 import org.jboss.xb.binding.Unmarshaller;
38 import org.jboss.xb.binding.UnmarshallerFactory;
39 import org.jboss.xb.binding.UnmarshallingContext;
40 import org.xml.sax.Attributes JavaDoc;
41
42 import java.io.IOException JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.net.URL JavaDoc;
45
46 /**
47  * org.jboss.xb.binding.ObjectModelFactory implementation that accepts data
48  * chuncks from unmarshaller and assembles them into an WebDD instance.
49  *
50  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
51  * @version <tt>$Revision: 58121 $</tt>
52  */

53 public class JBossWebDDObjectFactory extends DDObjectFactory
54 {
55    private static final Logger log = Logger
56            .getLogger(JBossWebDDObjectFactory.class);
57
58    private WebDD dd;
59
60    public static WebDD parse(URL JavaDoc ddResource, WebDD dd)
61            throws JBossXBException, IOException JavaDoc
62    {
63       if (ddResource == null) return null;
64       log.debug("found jboss-web.xml " + ddResource);
65
66       InputStream JavaDoc is = ddResource.openStream();
67
68       return parse(dd, is);
69    }
70
71    public static WebDD parse(WebDD dd, InputStream JavaDoc is)
72            throws JBossXBException
73    {
74       ObjectModelFactory factory = null;
75       Unmarshaller unmarshaller = null;
76
77       if (dd == null) dd = new WebDD();
78
79       factory = new JBossWebDDObjectFactory(dd);
80       UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
81       unmarshallerFactory.setFeature(Unmarshaller.SCHEMA_VALIDATION, Boolean.TRUE);
82       unmarshaller = unmarshallerFactory.newUnmarshaller();
83       JBossEntityResolver entityResolver = new JBossEntityResolver();
84       unmarshaller.setEntityResolver(entityResolver);
85
86       dd = (WebDD) unmarshaller.unmarshal(is, factory, null);
87
88       return dd;
89    }
90
91    public JBossWebDDObjectFactory(WebDD dd)
92    {
93       super();
94       this.dd = dd;
95    }
96
97    /**
98     * Return the root.
99     */

100    public Object JavaDoc newRoot(Object JavaDoc root, UnmarshallingContext navigator,
101                          String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
102    {
103       return dd;
104    }
105
106    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx,
107                               String JavaDoc uri, String JavaDoc name)
108    {
109       return root;
110    }
111
112    // Methods discovered by introspection
113

114    /**
115     * Called when parsing of a new element started.
116     */

117    public Object JavaDoc newChild(WebDD dd, UnmarshallingContext navigator,
118                           String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
119    {
120       Object JavaDoc child = null;
121       log.debug("newChild, "+localName);
122       if ((child = newEnvRefGroupChild(localName)) != null)
123          return child;
124       else if (localName.equals("security-role"))
125          child = new SecurityRole();
126       else if (localName.equals("servlet"))
127          child = new Servlet();
128       else if (localName.equals("replication-config"))
129          child = new ReplicationConfig();
130       else if (localName.equals("message-destination"))
131       {
132          child = new MessageDestination();
133       }
134       return child;
135    }
136
137    public void addChild(WebDD parent, ReplicationConfig config,
138                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
139    {
140       parent.setReplicationConfig(config);
141    }
142
143    public void addChild(WebDD parent, EjbLocalRef ref,
144                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
145    {
146       parent.updateEjbLocalRef(ref);
147    }
148
149    public void addChild(WebDD parent, EjbRef ref,
150                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
151    {
152       parent.updateEjbRef(ref);
153    }
154
155    public void addChild(WebDD parent, EnvEntry ref,
156                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
157    {
158       parent.addEnvEntry(ref);
159    }
160
161    public void addChild(WebDD parent, MessageDestination destination,
162          UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
163    {
164       log.debug("addMessageDestination, "+destination);
165       parent.updateMessageDestination(destination);
166       // See if this message-destination resolves to an existing ref link
167
String JavaDoc link = destination.getMessageDestinationName();
168       if( link != null )
169       {
170          MessageDestinationRef ref = parent.getMessageDestinationRefForLink(link);
171          if( ref != null )
172          {
173             log.debug("ressolved "+ref+" to link mapedName: "+destination.getMappedName());
174             ref.setMappedName(destination.getMappedName());
175          }
176       }
177    }
178    
179    public void addChild(WebDD parent, MessageDestinationRef ref, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
180    {
181       log.debug("addMessageDestinationRef, " + ref);
182       parent.updateMessageDestinationRef(ref);
183    }
184
185    public void addChild(WebDD parent, ServiceRef ref, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
186    {
187       parent.addServiceRef(ref);
188    }
189
190    public void addChild(WebDD parent, ResourceEnvRef ref,
191                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
192    {
193       parent.updateResourceEnvRef(ref);
194    }
195
196    public void addChild(WebDD parent, ResourceRef ref,
197                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
198    {
199       parent.updateResourceRef(ref);
200    }
201
202    public void addChild(WebDD parent, SecurityRole role,
203                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
204    {
205       parent.updateSecurityRole(role);
206    }
207
208    public void addChild(WebDD parent, Servlet servlet,
209                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
210    {
211       parent.updateServlet(servlet);
212    }
213
214    public void setValue(WebDD dd,
215                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
216                         String JavaDoc value)
217    {
218       if (localName.equals("depends"))
219       {
220          dd.addDependency(value);
221       }
222       else if (localName.equals("security-domain"))
223       {
224          dd.setSecurityDomain(value);
225       }
226    }
227
228    public void setValue(ReplicationConfig config,
229                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
230                         String JavaDoc value)
231    {
232       if (localName.equals("replication-trigger"))
233       {
234          config.setTrigger(value);
235       }
236       else if (localName.equals("replication-granularity"))
237       {
238          config.setGranularity(value);
239       }
240       else if (localName.equals("replication-field-batch-mode"))
241       {
242          config.setFieldBatchMode(value);
243       }
244    }
245
246    public void setValue(Servlet servlet,
247                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
248                         String JavaDoc value)
249    {
250       if (localName.equals("servlet-name"))
251       {
252          servlet.setName(value);
253       }
254       else if (localName.equals("run-as-principal"))
255       {
256          servlet.addRunAsPrincipal(value);
257       }
258    }
259
260    public void setValue(ServletMapping mapping,
261                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
262                         String JavaDoc value)
263    {
264       if (localName.equals("servlet-name"))
265       {
266          mapping.setName(value);
267       }
268       else if (localName.equals("url-pattern"))
269       {
270          mapping.setUrlPattern(value);
271       }
272    }
273
274    public void setValue(SecurityRole role,
275                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
276                         String JavaDoc value)
277    {
278       if (localName.equals("principal-name"))
279       {
280          role.setPrincipalName(value);
281       }
282       else
283          super.setValue(role, navigator, namespaceURI, localName, value);
284    }
285 }
286
Popular Tags