001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.metamodel;
023:
024: import java.io.IOException;
025: import java.net.URL;
026:
027: import org.jboss.logging.Logger;
028: import org.jboss.metamodel.descriptor.DDObjectFactory;
029: import org.jboss.metamodel.descriptor.EjbRef;
030: import org.jboss.metamodel.descriptor.EnvEntry;
031: import org.jboss.util.xml.JBossEntityResolver;
032: import org.jboss.xb.binding.JBossXBException;
033: import org.jboss.xb.binding.Unmarshaller;
034: import org.jboss.xb.binding.UnmarshallerFactory;
035: import org.jboss.xb.binding.UnmarshallingContext;
036: import org.xml.sax.Attributes;
037:
038: /**
039: * A JBossXB object factory for parsing application client descriptor files.
040: *
041: * http://java.sun.com/xml/ns/javaee/application-client_5.xsd
042: *
043: * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
044: * @version $Revision: $
045: */
046: public class ApplicationClientDDObjectFactory extends DDObjectFactory {
047: private static final Logger log = Logger
048: .getLogger(ApplicationClientDDObjectFactory.class);
049:
050: // made public for the parsing deployer
051: public ApplicationClientDDObjectFactory() {
052:
053: }
054:
055: public static ApplicationClientDD parse(URL ddResource)
056: throws JBossXBException, IOException {
057: if (ddResource == null)
058: return null;
059:
060: log.debug("found application-client.xml " + ddResource);
061:
062: ApplicationClientDDObjectFactory factory = new ApplicationClientDDObjectFactory();
063: UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory
064: .newInstance();
065: Unmarshaller unmarshaller = unmarshallerFactory
066: .newUnmarshaller();
067: unmarshaller.setEntityResolver(new JBossEntityResolver());
068: unmarshaller.setNamespaceAware(true);
069: unmarshaller.setSchemaValidation(true);
070: unmarshaller.setValidation(true);
071:
072: ApplicationClientDD dd = (ApplicationClientDD) unmarshaller
073: .unmarshal(ddResource.openStream(), factory, null);
074:
075: return dd;
076: }
077:
078: public void addChild(ApplicationClientDD parent, EjbRef ref,
079: UnmarshallingContext navigator, String namespaceURI,
080: String localName) {
081: parent.addEjbRef(ref);
082: }
083:
084: /**
085: * Called when parsing character is complete.
086: */
087: public void addChild(ApplicationClientDD parent, EnvEntry entry,
088: UnmarshallingContext navigator, String namespaceURI,
089: String localName) {
090: parent.addEnvEntry(entry);
091: }
092:
093: public void addChild(ApplicationClientDD parent,
094: LifecycleCallback lifecycleCallback,
095: UnmarshallingContext navigator, String namespaceURI,
096: String localName) {
097: if (localName.equals("post-construct"))
098: parent.getPostConstructs().add(lifecycleCallback);
099: else if (localName.equals("pre-destroy"))
100: parent.getPreDestroys().add(lifecycleCallback);
101: else
102: throw new IllegalArgumentException(localName);
103: }
104:
105: public Object completeRoot(Object root, UnmarshallingContext ctx,
106: String uri, String name) {
107: throw new RuntimeException("NYI");
108: }
109:
110: public Object newChild(ApplicationClientDD parent,
111: UnmarshallingContext navigator, String namespaceURI,
112: String localName, Attributes attrs) {
113: Object child = super .newEnvRefGroupChild(localName);
114: if (child != null)
115: return child;
116:
117: if (localName.equals("post-construct")
118: || localName.equals("pre-destroy")) {
119: child = new LifecycleCallback();
120: }
121:
122: // ignore things like display-name & description
123:
124: return child;
125: }
126:
127: public Object newRoot(Object root, UnmarshallingContext navigator,
128: String namespaceURI, String localName, Attributes attrs) {
129: final ApplicationClientDD dd;
130: if (root == null)
131: root = dd = new ApplicationClientDD();
132: else
133: dd = (ApplicationClientDD) root;
134:
135: if (attrs.getLength() > 0) {
136: for (int i = 0; i < attrs.getLength(); ++i) {
137: if (attrs.getLocalName(i).equals("version")) {
138: dd.setVersion(attrs.getValue(i));
139: } else if (attrs.getLocalName(i).equals(
140: "metadata-complete")) {
141: dd.setMetadataComplete(Boolean.parseBoolean(attrs
142: .getValue(i)));
143: } else if (attrs.getLocalName(i).equals(
144: "schemaLocation")) {
145: // ignore
146: } else if (attrs.getLocalName(i).equals("id")) {
147: // ignore
148: } else
149: throw new IllegalArgumentException(attrs
150: .getLocalName(i));
151: }
152: }
153:
154: return root;
155: }
156:
157: /**
158: * Called when a child element with simple content is read for DD.
159: */
160: public void setValue(ApplicationClientDD dd,
161: UnmarshallingContext navigator, String namespaceURI,
162: String localName, String value) {
163: if (localName.equals("display-name")) {
164: dd.setDisplayName(getValue(localName, value));
165: }
166: // else if(localName.equals("description"))
167: // {
168: // // ignore
169: // }
170: // else
171: // throw new IllegalArgumentException(localName);
172: }
173:
174: public void setValue(LifecycleCallback lifecycleCallback,
175: UnmarshallingContext navigator, String namespaceURI,
176: String localName, String value) {
177: if (localName.equals("lifecycle-callback-class"))
178: lifecycleCallback.setLifecycleCallbackClass(value);
179: else if (localName.equals("lifecycle-callback-method"))
180: lifecycleCallback.setLifecycleCallbackMethod(value);
181: else
182: throw new IllegalArgumentException(localName);
183: }
184: }
|