KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > impl > Configuration


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.impl;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.xml.bind.JAXBException;
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.ws.jaxme.JMManager;
26 import org.apache.ws.jaxme.xs.xml.XsQName;
27 import org.xml.sax.SAXException JavaDoc;
28
29
30
31 /** <p>An instance of this class represents a config file.
32  * A JAXBContext requires an associated Configuration which
33  * is located through the classpath.</p>
34  *
35  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
36  * @version $Id: Configuration.java,v 1.7 2005/03/10 10:14:03 jochen Exp $
37  */

38 public class Configuration {
39   JAXBContextImpl context;
40   Manager currentManager;
41   Map JavaDoc managers = new HashMap JavaDoc();
42   private Class JavaDoc jmMarshallerClass = JMMarshallerImpl.class;
43   private Class JavaDoc jmUnmarshallerClass = JMUnmarshallerImpl.class;
44   private Class JavaDoc jmValidatorClass = JMValidatorImpl.class;
45
46   public Configuration(JAXBContextImpl pContext) {
47     context = pContext;
48   }
49
50   public class Manager implements JMManager {
51     public class Property {
52       private String JavaDoc managerName;
53       private String JavaDoc value;
54
55       public String JavaDoc getName() { return managerName; }
56       public void setName(String JavaDoc pName) { managerName = pName; }
57       public String JavaDoc getValue() { return value; }
58       public void setValue(String JavaDoc pValue) { value = pValue; }
59       public void finish() throws SAXException JavaDoc {
60         if (managerName == null) {
61           throw new NullPointerException JavaDoc("Missing 'name' attribute in 'property' element.");
62         }
63         if (value == null) {
64           throw new NullPointerException JavaDoc("Missing 'value' attribute in 'property' element.");
65         }
66         if (properties == null) {
67           properties = new HashMap JavaDoc();
68         }
69         if (properties.put(managerName, value) != null) {
70           throw new IllegalStateException JavaDoc("The property " + managerName + " was specified more than once.");
71         }
72       }
73     }
74
75     private QName JavaDoc name;
76     private Class JavaDoc elementInterface;
77     private Class JavaDoc elementClass;
78     private Class JavaDoc handlerClass;
79     private Class JavaDoc driverClass;
80     private Class JavaDoc pmClass;
81     private String JavaDoc prefix;
82     private Map JavaDoc properties;
83
84     public String JavaDoc getPrefix() {
85         return prefix;
86     }
87     /** Sets the suggested prefix for the elements namespace.
88      */

89     public void setPrefix(String JavaDoc prefix) {
90         this.prefix = prefix;
91     }
92     
93     public void setQName(QName JavaDoc pName) { name = pName; }
94     public QName JavaDoc getQName() { return name; }
95     public void setElementClass(String JavaDoc pElementClass) throws ClassNotFoundException JavaDoc {
96         elementClass = context.getClassLoader().loadClass(pElementClass);
97     }
98     public Class JavaDoc getElementClass() { return elementClass; }
99     public void setElementInterface(String JavaDoc pElementInterface) throws ClassNotFoundException JavaDoc {
100         elementInterface = context.getClassLoader().loadClass(pElementInterface);
101     }
102     public Class JavaDoc getElementInterface() { return elementInterface; }
103     public Class JavaDoc getHandlerClass() { return handlerClass; }
104     public void setHandlerClass(String JavaDoc pHandlerClass) throws ClassNotFoundException JavaDoc {
105         handlerClass = context.getClassLoader().loadClass(pHandlerClass);
106         if (!JMSAXElementParser.class.isAssignableFrom(handlerClass)) {
107             throw new IllegalStateException JavaDoc("The class " + handlerClass.getName()
108                                             + " is not implementing "
109                                             + JMSAXElementParser.class.getName());
110         }
111     }
112     public void setDriverClass(String JavaDoc pMarshallerClass) throws ClassNotFoundException JavaDoc {
113         driverClass = context.getClassLoader().loadClass(pMarshallerClass);
114         if (!JMSAXDriver.class.isAssignableFrom(driverClass)) {
115             throw new IllegalStateException JavaDoc("The class " + driverClass.getName()
116                                             + " is not implementing "
117                                             + JMSAXDriver.class.getName());
118         }
119     }
120     public Class JavaDoc getDriverClass() { return driverClass; }
121     public JMSAXDriver getDriver() throws SAXException JavaDoc {
122         try {
123             return (JMSAXDriver) driverClass.newInstance();
124         } catch (InstantiationException JavaDoc e) {
125             throw new SAXException JavaDoc("Unable to instantiate driver class " + driverClass.getName(), e);
126         } catch (IllegalAccessException JavaDoc e) {
127             throw new SAXException JavaDoc("Illegal access to driver class " + driverClass.getName(), e);
128         }
129     }
130     /** Sets the persistence manager class.
131      */

132     public void setPmClass(String JavaDoc pPersistencyClass) throws ClassNotFoundException JavaDoc {
133         pmClass = context.getClassLoader().loadClass(pPersistencyClass);
134     }
135     public Class JavaDoc getPmClass() { return pmClass; }
136     public JAXBContextImpl getFactory() { return context; }
137     public Property createProperty() {
138       return new Property();
139     }
140     public String JavaDoc getProperty(String JavaDoc pName) {
141       if (pName == null) {
142         throw new IllegalArgumentException JavaDoc("The property name must not be null.");
143       }
144       if (properties == null) {
145         return null;
146       }
147       return (String JavaDoc) properties.get(pName);
148     }
149     public void finish() throws SAXException JavaDoc {
150       if (currentManager != this) {
151         throw new IllegalStateException JavaDoc("currentManager != this");
152       }
153       try {
154           if (prefix != null) {
155               name = new QName JavaDoc(name.getNamespaceURI(), name.getLocalPart(), prefix);
156           }
157           context.addManager(currentManager);
158           currentManager = null;
159       } catch (Exception JavaDoc e) {
160         throw new SAXException JavaDoc(e.getMessage(), e);
161       }
162     }
163
164     public JMSAXElementParser getHandler() throws SAXException JavaDoc {
165         try {
166             return (JMSAXElementParser) handlerClass.newInstance();
167         } catch (InstantiationException JavaDoc e) {
168             throw new SAXException JavaDoc("Unable to instantiate handler class "
169                                    + jmUnmarshallerClass.getName(), e);
170         } catch (IllegalAccessException JavaDoc e) {
171             throw new SAXException JavaDoc("Illegal access to handler class "
172                                    + jmUnmarshallerClass.getName(), e);
173         }
174     }
175
176     public Object JavaDoc getElementJ() throws JAXBException {
177         try {
178             return elementClass.newInstance();
179         } catch (InstantiationException JavaDoc e) {
180             throw new JAXBException("Unable to instantiate handler class "
181                                     + jmUnmarshallerClass.getName(), e);
182         } catch (IllegalAccessException JavaDoc e) {
183             throw new JAXBException("Illegal access to handler class "
184                                     + jmUnmarshallerClass.getName(), e);
185         }
186     }
187
188     public Object JavaDoc getElementS() throws SAXException JavaDoc {
189         try {
190             return elementClass.newInstance();
191         } catch (InstantiationException JavaDoc e) {
192             throw new SAXException JavaDoc("Unable to instantiate handler class "
193                                    + jmUnmarshallerClass.getName(), e);
194         } catch (IllegalAccessException JavaDoc e) {
195             throw new SAXException JavaDoc("Illegal access to handler class "
196                                    + jmUnmarshallerClass.getName(), e);
197         }
198     }
199   }
200
201   /** <p>Creates a new Manager.</p>
202    */

203   public Manager createManager() {
204     if (currentManager != null) {
205       throw new IllegalStateException JavaDoc("currentManager != null");
206     }
207     currentManager = new Manager();
208     return currentManager;
209   }
210
211   /** <p>Sets the JMMarshaller class.</p>
212    */

213   public void setJMMarshallerClass(Class JavaDoc pJMMarshallerClass) {
214     jmMarshallerClass = pJMMarshallerClass;
215   }
216
217   /** <p>Returns the JMMarshaller class.</p>
218    */

219   public Class JavaDoc getJMMarshallerClass() {
220     return jmMarshallerClass;
221   }
222
223   /** <p>Sets the JMUnmarshaller class.</p>
224    */

225   public void setJMUnmarshallerClass(Class JavaDoc pJMUnmarshallerClass) {
226     jmUnmarshallerClass = pJMUnmarshallerClass;
227   }
228
229   /** <p>Returns the JMUnmarshaller class.</p>
230    */

231   public Class JavaDoc getJMUnmarshallerClass() {
232     return jmUnmarshallerClass;
233   }
234
235   /** <p>Sets the JMValidator class.</p>
236    */

237   public void setJMValidatorClass(Class JavaDoc pJMValidatorClass) {
238     jmValidatorClass = pJMValidatorClass;
239   }
240
241   public Class JavaDoc getJMValidatorClass() {
242     return jmValidatorClass;
243   }
244 }
245
Popular Tags