KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > xmldb > XmlDbPM


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.pm.xmldb;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.xml.bind.Element;
25 import javax.xml.bind.JAXBException;
26 import javax.xml.bind.Marshaller;
27
28 import org.apache.ws.jaxme.JMManager;
29 import org.apache.ws.jaxme.JMUnmarshallerHandler;
30 import org.apache.ws.jaxme.Observer;
31 import org.apache.ws.jaxme.PMException;
32 import org.apache.ws.jaxme.PMParams;
33 import org.apache.ws.jaxme.pm.impl.PMIdImpl;
34 import org.xml.sax.ContentHandler JavaDoc;
35 import org.xmldb.api.DatabaseManager;
36 import org.xmldb.api.base.Collection;
37 import org.xmldb.api.base.Database;
38 import org.xmldb.api.base.ResourceIterator;
39 import org.xmldb.api.base.ResourceSet;
40 import org.xmldb.api.base.XMLDBException;
41 import org.xmldb.api.modules.XMLResource;
42 import org.xmldb.api.modules.XPathQueryService;
43
44
45 /** This is a persistence manager for the XML::DB interface. In other
46  * words, using this persistence manager you may read documents from a
47  * database via the XML::DB API or write them into the database using the
48  * same API.<br>
49  * This persistence manager is generic: The same manager works fine for
50  * any JAXB element.
51  */

52 public class XmlDbPM extends PMIdImpl {
53   private Class JavaDoc driverClass;
54   private String JavaDoc collection;
55   private String JavaDoc user;
56   private String JavaDoc password;
57   private Map JavaDoc databaseProperties;
58   private String JavaDoc xPathQueryService = "XPathQueryService";
59   private String JavaDoc xPathQueryServiceVersion = "1.0";
60
61   /** Creates a new instance of <code>XmlDbPM</code>.
62    */

63   public XmlDbPM() {
64   }
65
66   public void init(JMManager pManager) throws JAXBException {
67     super.init(pManager);
68
69     String JavaDoc driverClassName = pManager.getProperty("xmldb.driver");
70     if (driverClassName == null || driverClassName.length() == 0) {
71       throw new JAXBException("Missing property: 'xmldb.driver' (driver class name)");
72     }
73
74     String JavaDoc coll = pManager.getProperty("xmldb.collection");
75     if (coll == null || coll.length() == 0) {
76       throw new JAXBException("Missing property: 'xmldb.collection' (collection name)");
77     }
78     setCollection(coll);
79     setUser(pManager.getProperty("xmldb.user"));
80     setPassword(pManager.getProperty("xmldb.password"));
81
82     for (int i = 0; ; i++) {
83       String JavaDoc p = "xmldb.property." + i;
84       String JavaDoc v = pManager.getProperty(p);
85       if (v == null) {
86         break;
87       }
88       int offset = v.indexOf('=');
89       if (offset == -1) {
90         throw new JAXBException("Invalid database property value " + p + ": Expected name=value, got " + v);
91       }
92       String JavaDoc name = v.substring(0, offset);
93       String JavaDoc value = v.substring(offset+1);
94       if (databaseProperties != null) {
95         databaseProperties = new HashMap JavaDoc();
96       }
97       databaseProperties.put(name, value);
98     }
99
100     Class JavaDoc c;
101     try {
102       c = Class.forName(driverClassName);
103     } catch (ClassNotFoundException JavaDoc e) {
104       try {
105         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
106         if (cl == null) {
107           cl = getClass().getClassLoader();
108         }
109         c = cl.loadClass(driverClassName);
110       } catch (ClassNotFoundException JavaDoc f) {
111         throw new JAXBException("Failed to load driver class " + driverClassName, e);
112       }
113     }
114     setDriverClass(c);
115   }
116
117   /** Returns the collection name.
118    */

119   public String JavaDoc getCollection() {
120     return collection;
121   }
122
123   /** Sets the collection name.
124    */

125   public void setCollection(String JavaDoc pCollection) {
126     collection = pCollection;
127   }
128
129   /** Returns the driver class.
130    */

131   public Class JavaDoc getDriverClass() {
132     return driverClass;
133   }
134
135   /** Sets the driver class.
136    */

137   public void setDriverClass(Class JavaDoc pDriverClass) {
138     driverClass = pDriverClass;
139   }
140
141   /** Returns the password.
142    */

143   public String JavaDoc getPassword() {
144     return password;
145   }
146
147   /** Sets the password.
148    */

149   public void setPassword(String JavaDoc pPassword) {
150     password = pPassword;
151   }
152
153   /** Returns the users name.
154    */

155   public String JavaDoc getUser() {
156     return user;
157   }
158
159   /** Sets the users name.
160    */

161   public void setUser(String JavaDoc pUser) {
162     user = pUser;
163   }
164
165   /** Returns the name of the XPathQueryService. Defaults to
166    * "XPathQueryService".
167    */

168   public String JavaDoc getXPathQueryService() {
169     return xPathQueryService;
170   }
171
172   /** Sets the name of the XPathQueryService. Defaults to
173    * "XPathQueryService".
174    */

175   public void setXPathQueryService(String JavaDoc pXpathQueryService) {
176     xPathQueryService = pXpathQueryService;
177   }
178
179   /** Returns the version of the XPathQueryService. Defaults to
180    * "1.0".
181    */

182   public String JavaDoc getXPathQueryServiceVersion() {
183     return xPathQueryServiceVersion;
184   }
185
186   /** Sets the version of the XPathQueryService. Defaults to
187    * "1.0".
188    */

189   public void setXPathQueryServiceVersion(String JavaDoc pXpathQueryServiceVersion) {
190     xPathQueryServiceVersion = pXpathQueryServiceVersion;
191   }
192
193   /** Returns the database collection by invoking
194    * {@link DatabaseManager#getCollection(String)}.
195    */

196   protected Collection getXmlDbCollection()
197       throws XMLDBException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
198     Database database = (Database) getDriverClass().newInstance();
199     if (databaseProperties != null) {
200       for (Iterator JavaDoc iter = databaseProperties.entrySet().iterator(); iter.hasNext(); ) {
201         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
202         database.setProperty((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
203       }
204     }
205     DatabaseManager.registerDatabase(database);
206     return DatabaseManager.getCollection(getCollection());
207   }
208
209
210   /* (non-Javadoc)
211    * @see org.apache.ws.jaxme.PM#select(org.apache.ws.jaxme.Observer, java.lang.String, org.apache.ws.jaxme.PMParams)
212    */

213   public void select(Observer pObserver, String JavaDoc pQuery, PMParams pPlaceHolderArgs) throws JAXBException {
214     if (pPlaceHolderArgs != null) {
215       pQuery = super.parseQuery(pQuery, pPlaceHolderArgs);
216     }
217
218     if (pQuery == null) {
219       throw new IllegalArgumentException JavaDoc("A query must be specified");
220     }
221
222     Collection col;
223     try {
224         col = getXmlDbCollection();
225         XPathQueryService service = (XPathQueryService) col.getService(getXPathQueryService(),
226                                                                        getXPathQueryServiceVersion());
227         ResourceSet result = service.query(pQuery);
228         if (result != null) {
229             ResourceIterator i = result.getIterator();
230             if (i.hasMoreResources()) {
231                 JMUnmarshallerHandler handler = (JMUnmarshallerHandler) getManager().getFactory().createUnmarshaller().getUnmarshallerHandler();
232                 handler.setObserver(pObserver);
233                 while(i.hasMoreResources()) {
234                     XMLResource r = (XMLResource) i.nextResource();
235                     r.getContentAsSAX(handler);
236                 }
237             }
238         }
239     } catch (XMLDBException e) {
240         throw new PMException(e);
241     } catch (IllegalAccessException JavaDoc e) {
242         throw new PMException(e);
243     } catch (InstantiationException JavaDoc e) {
244         throw new PMException(e);
245     }
246   }
247
248   /* (non-Javadoc)
249    * @see org.apache.ws.jaxme.PM#insert(javax.xml.bind.Element)
250    */

251   public void insert(Element pElement) throws PMException {
252     try {
253       Collection col = getXmlDbCollection();
254       String JavaDoc id = getId(pElement);
255       XMLResource resource = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
256       ContentHandler JavaDoc ch = resource.setContentAsSAX();
257       Marshaller marshaller = getManager().getFactory().createMarshaller();
258       marshaller.marshal(pElement, ch);
259       col.storeResource(resource);
260     } catch (XMLDBException e) {
261       throw new PMException(e);
262     } catch (IllegalAccessException JavaDoc e) {
263       throw new PMException(e);
264     } catch (InstantiationException JavaDoc e) {
265       throw new PMException(e);
266     } catch (NoSuchMethodException JavaDoc e) {
267       throw new PMException(e);
268     } catch (InvocationTargetException JavaDoc e) {
269       throw new PMException(e.getTargetException());
270     } catch (JAXBException e) {
271       if (e instanceof PMException) {
272         throw (PMException) e;
273       } else {
274         throw new PMException(e);
275       }
276     }
277   }
278
279   /* (non-Javadoc)
280    * @see org.apache.ws.jaxme.PM#update(javax.xml.bind.Element)
281    */

282   public void update(Element pElement) throws PMException {
283     try {
284       Collection col = getXmlDbCollection();
285       String JavaDoc id = getId(pElement);
286       XMLResource resource = (XMLResource) col.getResource(id);
287       ContentHandler JavaDoc ch = resource.setContentAsSAX();
288       Marshaller marshaller = getManager().getFactory().createMarshaller();
289       marshaller.marshal(pElement, ch);
290       col.storeResource(resource);
291     } catch (XMLDBException e) {
292       throw new PMException(e);
293     } catch (IllegalAccessException JavaDoc e) {
294       throw new PMException(e);
295     } catch (InstantiationException JavaDoc e) {
296       throw new PMException(e);
297     } catch (NoSuchMethodException JavaDoc e) {
298       throw new PMException(e);
299     } catch (InvocationTargetException JavaDoc e) {
300       throw new PMException(e.getTargetException());
301     } catch (JAXBException e) {
302       if (e instanceof PMException) {
303         throw (PMException) e;
304       } else {
305         throw new PMException(e);
306       }
307     }
308   }
309
310   /* (non-Javadoc)
311    * @see org.apache.ws.jaxme.PM#delete(javax.xml.bind.Element)
312    */

313   public void delete(Element pElement) throws PMException {
314     try {
315      Collection col = getXmlDbCollection();
316      String JavaDoc id = getId(pElement);
317      XMLResource resource = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
318      col.removeResource(resource);
319    } catch (XMLDBException e) {
320      throw new PMException(e);
321    } catch (IllegalAccessException JavaDoc e) {
322      throw new PMException(e);
323    } catch (InstantiationException JavaDoc e) {
324      throw new PMException(e);
325    } catch (InvocationTargetException JavaDoc e) {
326      throw new PMException(e.getTargetException());
327    } catch (NoSuchMethodException JavaDoc e) {
328      throw new PMException(e);
329     } catch (JAXBException e) {
330       if (e instanceof PMException) {
331         throw (PMException) e;
332       } else {
333         throw new PMException(e);
334       }
335    }
336   }
337
338 }
339
Popular Tags