KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > web > JBossWebMetaDataObjectFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.metadata.web;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.metadata.DDObjectFactory;
30 import org.jboss.metadata.EjbLocalRefMetaData;
31 import org.jboss.metadata.EjbRefMetaData;
32 import org.jboss.metadata.EnvEntryMetaData;
33 import org.jboss.metadata.MessageDestinationMetaData;
34 import org.jboss.metadata.MessageDestinationRefMetaData;
35 import org.jboss.metadata.ResourceEnvRefMetaData;
36 import org.jboss.metadata.ResourceRefMetaData;
37 import org.jboss.metadata.SecurityRoleMetaData;
38 import org.jboss.metadata.WebMetaData;
39 import org.jboss.util.xml.JBossEntityResolver;
40 import org.jboss.xb.binding.JBossXBException;
41 import org.jboss.xb.binding.ObjectModelFactory;
42 import org.jboss.xb.binding.Unmarshaller;
43 import org.jboss.xb.binding.UnmarshallerFactory;
44 import org.jboss.xb.binding.UnmarshallingContext;
45 import org.xml.sax.Attributes JavaDoc;
46
47 public class JBossWebMetaDataObjectFactory extends DDObjectFactory
48 {
49    private static Logger log = Logger.getLogger(JBossWebMetaDataObjectFactory.class);
50    /** The current WebMetaData being parsed */
51    private static final ThreadLocal JavaDoc<WebMetaData> activeMetaData = new ThreadLocal JavaDoc<WebMetaData>();
52    private WebMetaData metaData;
53
54    public static WebMetaData parse(URL JavaDoc ddResource, WebMetaData dd)
55            throws JBossXBException, IOException JavaDoc
56    {
57       if (ddResource == null) return null;
58       log.debug("found jboss-web.xml " + ddResource);
59
60       InputStream JavaDoc is = ddResource.openStream();
61
62       return parse(dd, is);
63    }
64
65    public static WebMetaData parse(WebMetaData metaData, InputStream JavaDoc is)
66            throws JBossXBException
67    {
68       ObjectModelFactory factory = null;
69       Unmarshaller unmarshaller = null;
70
71       if (metaData == null)
72          metaData = new WebMetaData();
73
74       activeMetaData.set(metaData);
75       factory = new JBossWebMetaDataObjectFactory(metaData);
76       UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
77       // unmarshallerFactory.setFeature(Unmarshaller.SCHEMA_VALIDATION, Boolean.TRUE);
78
unmarshaller = unmarshallerFactory.newUnmarshaller();
79       JBossEntityResolver entityResolver = new JBossEntityResolver();
80       unmarshaller.setEntityResolver(entityResolver);
81
82       metaData = (WebMetaData) unmarshaller.unmarshal(is, factory, null);
83       return metaData;
84    }
85
86    public JBossWebMetaDataObjectFactory(WebMetaData dd)
87    {
88       super();
89       this.metaData = dd;
90    }
91
92    /**
93     * Return the root.
94     */

95    public Object JavaDoc newRoot(Object JavaDoc root, UnmarshallingContext navigator,
96                          String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
97    {
98       // If both the root and metaData are null the deployer is likely in the wrong order
99
if( root == null && metaData == null )
100          throw new IllegalStateException JavaDoc("No existing WebMetaData, check the JBossWebAppParsingDeployer order");
101
102       return root == null ? metaData : root;
103    }
104
105    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx,
106                               String JavaDoc uri, String JavaDoc name)
107    {
108       return root;
109    }
110
111    // Methods discovered by introspection
112

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

116    public Object JavaDoc newChild(WebMetaData dd, UnmarshallingContext navigator,
117                           String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
118    {
119       Object JavaDoc child = null;
120       log.debug("newChild, "+localName);
121       if ((child = newEnvRefGroupChild(localName)) != null)
122          return child;
123       else if (localName.equals("security-role"))
124          child = new SecurityRoleMetaData();
125       else if (localName.equals("servlet"))
126          child = new Servlet();
127       else if (localName.equals("replication-config"))
128          child = new ReplicationConfig();
129       else if (localName.equals("passivation-config"))
130          child = new PassivationConfig();
131       else if (localName.equals("message-destination"))
132       {
133          child = new MessageDestinationMetaData();
134       }
135       return child;
136    }
137
138    public void addChild(WebMetaData parent, ReplicationConfig config,
139                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
140    {
141       parent.setReplicationConfig(config);
142    }
143
144    public void addChild(WebMetaData parent, PassivationConfig config,
145                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
146    {
147       parent.setPassivationConfig(config);
148    }
149
150    public void addChild(WebMetaData parent, EjbLocalRefMetaData ref,
151                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
152    {
153       parent.addEjbLocalRef(ref);
154    }
155
156    public void addChild(WebMetaData parent, EjbRefMetaData ref,
157                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
158    {
159       parent.addEjbRef(ref);
160    }
161
162    public void addChild(WebMetaData parent, EnvEntryMetaData ref,
163                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
164    {
165       parent.addEnvEntry(ref);
166    }
167
168    public void addChild(WebMetaData parent, MessageDestinationMetaData destination,
169          UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
170    {
171       log.debug("addMessageDestinationMetaData, "+destination);
172       parent.addMessageDestination(destination);
173       // See if this message-destination resolves to an existing ref link
174
String JavaDoc link = destination.getJNDIName();
175       if( link != null )
176       {
177          MessageDestinationMetaData ref = parent.getMessageDestination(link);
178          if( ref != null )
179          {
180             log.debug("ressolved "+ref+" to link JndiName: "+destination.getJNDIName());
181             ref.setJNDIName(destination.getJNDIName());
182          }
183       }
184    }
185    public void addChild(WebMetaData parent, MessageDestinationRefMetaData ref,
186                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
187    {
188       log.debug("addMessageDestinationMetaDataRef, "+ref);
189       parent.addMessageDestinationRef(ref);
190    }
191
192    public void addChild(WebMetaData parent, ResourceEnvRefMetaData ref,
193                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
194    {
195       // Need to merge this with any existing version
196
parent.updateResourceEnvRef(ref);
197    }
198
199    public void addChild(WebMetaData parent, ResourceRefMetaData ref,
200                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
201    {
202       // Need to merge this with any existing version
203
parent.updateResourceRef(ref);
204    }
205
206    public void addChild(WebMetaData parent, SecurityRoleMetaData role,
207                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
208    {
209       parent.addSecurityRole(role);
210    }
211
212    public void addChild(WebMetaData parent, Servlet servlet,
213                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
214    {
215       parent.updateServlet(servlet);
216    }
217
218    public void setValue(WebMetaData dd,
219                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
220                         String JavaDoc value)
221    {
222       if (localName.equals("depends"))
223       {
224          dd.addDependency(value);
225       }
226       else if (localName.equals("context-root"))
227       {
228          dd.setContextRoot(value);
229       }
230       else if (localName.equals("security-domain"))
231       {
232          dd.setSecurityDomain(value);
233       }
234    }
235
236    public void setValue(ReplicationConfig config,
237                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
238                         String JavaDoc value)
239    {
240       if (localName.equals("replication-trigger"))
241       {
242          config.setTrigger(value);
243       }
244       else if (localName.equals("replication-granularity"))
245       {
246          config.setGranularity(value);
247       }
248       else if (localName.equals("replication-field-batch-mode"))
249       {
250          config.setFieldBatchMode(value);
251       }
252    }
253    
254    public void setValue(PassivationConfig config,
255                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
256                         String JavaDoc value)
257    {
258       if (localName.equals("use-session-passivation"))
259       {
260          config.setUseSessionPassivation(value);
261       }
262       else if (localName.equals("passivation-min-idle-time"))
263       {
264          config.setPassivationMinIdleTime(value);
265       }
266       else if (localName.equals("passivation-max-idle-time"))
267       {
268          config.setPassivationMaxIdleTime(value);
269       }
270    }
271
272    public void setValue(Servlet servlet,
273                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
274                         String JavaDoc value)
275    {
276       if (localName.equals("servlet-name"))
277       {
278          servlet.setName(value);
279       }
280       else if (localName.equals("run-as-principal"))
281       {
282          servlet.addRunAsPrincipal(value);
283       }
284    }
285
286    public void setValue(ServletMapping mapping,
287                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
288                         String JavaDoc value)
289    {
290       if (localName.equals("servlet-name"))
291       {
292          mapping.setName(value);
293       }
294       else if (localName.equals("url-pattern"))
295       {
296          mapping.setUrlPattern(value);
297       }
298    }
299
300    public void setValue(SecurityRoleMetaData role,
301                         UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
302                         String JavaDoc value)
303    {
304       if (localName.equals("principal-name"))
305       {
306          role.setRoleName(value);
307       }
308       else
309          super.setValue(role, navigator, namespaceURI, localName, value);
310    }
311
312 }
313
Popular Tags