KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > metamodel > ApplicationClientDDObjectFactory


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.ejb3.metamodel;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.jboss.metamodel.descriptor.DDObjectFactory;
29 import org.jboss.metamodel.descriptor.EnvEntry;
30 import org.jboss.util.xml.JBossEntityResolver;
31 import org.jboss.xb.binding.JBossXBException;
32 import org.jboss.xb.binding.Unmarshaller;
33 import org.jboss.xb.binding.UnmarshallerFactory;
34 import org.jboss.xb.binding.UnmarshallingContext;
35 import org.xml.sax.Attributes JavaDoc;
36
37 /**
38  * A JBossXB object factory for parsing application client descriptor files.
39  *
40  * http://java.sun.com/xml/ns/javaee/application-client_5.xsd
41  *
42  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
43  * @version $Revision: $
44  */

45 public class ApplicationClientDDObjectFactory extends DDObjectFactory
46 {
47    private static final Logger log = Logger.getLogger(ApplicationClientDDObjectFactory.class);
48    
49    // made public for the parsing deployer
50
public ApplicationClientDDObjectFactory()
51    {
52       
53    }
54    
55    public static ApplicationClientDD parse(URL JavaDoc ddResource) throws JBossXBException, IOException JavaDoc
56    {
57       if(ddResource == null)
58          return null;
59       
60       log.debug("found application-client.xml " + ddResource);
61       
62       ApplicationClientDDObjectFactory factory = new ApplicationClientDDObjectFactory();
63       UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
64       unmarshallerFactory.setFeature(Unmarshaller.SCHEMA_VALIDATION, true);
65       Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
66       JBossEntityResolver entityResolver = new JBossEntityResolver();
67       unmarshaller.setEntityResolver(entityResolver);
68
69       ApplicationClientDD dd = (ApplicationClientDD) unmarshaller.unmarshal(ddResource.openStream(), factory, null);
70       
71       return dd;
72    }
73    
74    /**
75     * Called when parsing character is complete.
76     */

77    public void addChild(ApplicationClientDD parent, EnvEntry entry, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
78    {
79       parent.addEnvEntry(entry);
80    }
81
82    public void addChild(ApplicationClientDD parent, LifecycleCallback lifecycleCallback, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
83    {
84       if(localName.equals("post-construct"))
85          parent.getPostConstructs().add(lifecycleCallback);
86       else if(localName.equals("pre-destroy"))
87          parent.getPreDestroys().add(lifecycleCallback);
88       else
89          throw new IllegalArgumentException JavaDoc(localName);
90    }
91    
92    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc uri, String JavaDoc name)
93    {
94       throw new RuntimeException JavaDoc("NYI");
95    }
96
97    public Object JavaDoc newChild(ApplicationClientDD parent, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
98    {
99       Object JavaDoc child = super.newEnvRefGroupChild(localName);
100       if(child != null) return child;
101       
102       if(localName.equals("post-construct") || localName.equals("pre-destroy"))
103       {
104          child = new LifecycleCallback();
105       }
106       
107       // ignore things like display-name & description
108

109       return child;
110    }
111    
112    public Object JavaDoc newRoot(Object JavaDoc root, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc attrs)
113    {
114       final ApplicationClientDD dd;
115       if(root == null)
116          root = dd = new ApplicationClientDD();
117       else
118          dd = (ApplicationClientDD) root;
119       
120       if(attrs.getLength() > 0)
121       {
122          for (int i = 0; i < attrs.getLength(); ++i)
123          {
124             if (attrs.getLocalName(i).equals("version"))
125             {
126                dd.setVersion(attrs.getValue(i));
127             }
128             else if(attrs.getLocalName(i).equals("metadata-complete"))
129             {
130                dd.setMetadataComplete(Boolean.parseBoolean(attrs.getValue(i)));
131             }
132             else if(attrs.getLocalName(i).equals("schemaLocation"))
133             {
134                // ignore
135
}
136             else
137                throw new IllegalArgumentException JavaDoc(attrs.getLocalName(i));
138          }
139       }
140       
141       return root;
142    }
143
144    /**
145     * Called when a child element with simple content is read for DD.
146     */

147    public void setValue(ApplicationClientDD dd, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
148    {
149       if (localName.equals("display-name"))
150       {
151          dd.setDisplayName(getValue(localName, value));
152       }
153 // else if(localName.equals("description"))
154
// {
155
// // ignore
156
// }
157
// else
158
// throw new IllegalArgumentException(localName);
159
}
160    
161    public void setValue(LifecycleCallback lifecycleCallback, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
162    {
163       if(localName.equals("lifecycle-callback-class"))
164          lifecycleCallback.setLifecycleCallbackClass(value);
165       else if(localName.equals("lifecycle-callback-method"))
166          lifecycleCallback.setLifecycleCallbackMethod(value);
167       else
168          throw new IllegalArgumentException JavaDoc(localName);
169    }
170 }
171
Popular Tags