1 17 package org.apache.ws.jaxme.pm.xmldb; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 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 ; 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 52 public class XmlDbPM extends PMIdImpl { 53 private Class driverClass; 54 private String collection; 55 private String user; 56 private String password; 57 private Map databaseProperties; 58 private String xPathQueryService = "XPathQueryService"; 59 private String xPathQueryServiceVersion = "1.0"; 60 61 63 public XmlDbPM() { 64 } 65 66 public void init(JMManager pManager) throws JAXBException { 67 super.init(pManager); 68 69 String 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 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 p = "xmldb.property." + i; 84 String 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 name = v.substring(0, offset); 93 String value = v.substring(offset+1); 94 if (databaseProperties != null) { 95 databaseProperties = new HashMap (); 96 } 97 databaseProperties.put(name, value); 98 } 99 100 Class c; 101 try { 102 c = Class.forName(driverClassName); 103 } catch (ClassNotFoundException e) { 104 try { 105 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 106 if (cl == null) { 107 cl = getClass().getClassLoader(); 108 } 109 c = cl.loadClass(driverClassName); 110 } catch (ClassNotFoundException f) { 111 throw new JAXBException("Failed to load driver class " + driverClassName, e); 112 } 113 } 114 setDriverClass(c); 115 } 116 117 119 public String getCollection() { 120 return collection; 121 } 122 123 125 public void setCollection(String pCollection) { 126 collection = pCollection; 127 } 128 129 131 public Class getDriverClass() { 132 return driverClass; 133 } 134 135 137 public void setDriverClass(Class pDriverClass) { 138 driverClass = pDriverClass; 139 } 140 141 143 public String getPassword() { 144 return password; 145 } 146 147 149 public void setPassword(String pPassword) { 150 password = pPassword; 151 } 152 153 155 public String getUser() { 156 return user; 157 } 158 159 161 public void setUser(String pUser) { 162 user = pUser; 163 } 164 165 168 public String getXPathQueryService() { 169 return xPathQueryService; 170 } 171 172 175 public void setXPathQueryService(String pXpathQueryService) { 176 xPathQueryService = pXpathQueryService; 177 } 178 179 182 public String getXPathQueryServiceVersion() { 183 return xPathQueryServiceVersion; 184 } 185 186 189 public void setXPathQueryServiceVersion(String pXpathQueryServiceVersion) { 190 xPathQueryServiceVersion = pXpathQueryServiceVersion; 191 } 192 193 196 protected Collection getXmlDbCollection() 197 throws XMLDBException, IllegalAccessException , InstantiationException { 198 Database database = (Database) getDriverClass().newInstance(); 199 if (databaseProperties != null) { 200 for (Iterator iter = databaseProperties.entrySet().iterator(); iter.hasNext(); ) { 201 Map.Entry entry = (Map.Entry ) iter.next(); 202 database.setProperty((String ) entry.getKey(), (String ) entry.getValue()); 203 } 204 } 205 DatabaseManager.registerDatabase(database); 206 return DatabaseManager.getCollection(getCollection()); 207 } 208 209 210 213 public void select(Observer pObserver, String 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 ("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 e) { 242 throw new PMException(e); 243 } catch (InstantiationException e) { 244 throw new PMException(e); 245 } 246 } 247 248 251 public void insert(Element pElement) throws PMException { 252 try { 253 Collection col = getXmlDbCollection(); 254 String id = getId(pElement); 255 XMLResource resource = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE); 256 ContentHandler 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 e) { 263 throw new PMException(e); 264 } catch (InstantiationException e) { 265 throw new PMException(e); 266 } catch (NoSuchMethodException e) { 267 throw new PMException(e); 268 } catch (InvocationTargetException 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 282 public void update(Element pElement) throws PMException { 283 try { 284 Collection col = getXmlDbCollection(); 285 String id = getId(pElement); 286 XMLResource resource = (XMLResource) col.getResource(id); 287 ContentHandler 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 e) { 294 throw new PMException(e); 295 } catch (InstantiationException e) { 296 throw new PMException(e); 297 } catch (NoSuchMethodException e) { 298 throw new PMException(e); 299 } catch (InvocationTargetException 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 313 public void delete(Element pElement) throws PMException { 314 try { 315 Collection col = getXmlDbCollection(); 316 String 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 e) { 322 throw new PMException(e); 323 } catch (InstantiationException e) { 324 throw new PMException(e); 325 } catch (InvocationTargetException e) { 326 throw new PMException(e.getTargetException()); 327 } catch (NoSuchMethodException 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 |