-Contains an implementation of
-the JaxMe
persistence management framework
-targeting the XML::DB API.
-
XmlDbPM
.
- */
- public XmlDbPM() {
- }
-
- public void init(JMManager pManager) throws JAXBException {
- super.init(pManager);
-
- String driverClassName = pManager.getProperty("xmldb.driver");
- if (driverClassName == null || driverClassName.length() == 0) {
- throw new JAXBException("Missing property: 'xmldb.driver' (driver class name)");
- }
-
- String coll = pManager.getProperty("xmldb.collection");
- if (coll == null || coll.length() == 0) {
- throw new JAXBException("Missing property: 'xmldb.collection' (collection name)");
- }
- setCollection(coll);
- setUser(pManager.getProperty("xmldb.user"));
- setPassword(pManager.getProperty("xmldb.password"));
-
- for (int i = 0; ; i++) {
- String p = "xmldb.property." + i;
- String v = pManager.getProperty(p);
- if (v == null) {
- break;
- }
- int offset = v.indexOf('=');
- if (offset == -1) {
- throw new JAXBException("Invalid database property value " + p + ": Expected name=value, got " + v);
- }
- String name = v.substring(0, offset);
- String value = v.substring(offset+1);
- if (databaseProperties != null) {
- databaseProperties = new HashMap();
- }
- databaseProperties.put(name, value);
- }
-
- Class c;
- try {
- c = Class.forName(driverClassName);
- } catch (ClassNotFoundException e) {
- try {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- if (cl == null) {
- cl = getClass().getClassLoader();
- }
- c = cl.loadClass(driverClassName);
- } catch (ClassNotFoundException f) {
- throw new JAXBException("Failed to load driver class " + driverClassName, e);
- }
- }
- setDriverClass(c);
- }
-
- /** Returns the collection name.
- */
- public String getCollection() {
- return collection;
- }
-
- /** Sets the collection name.
- */
- public void setCollection(String pCollection) {
- collection = pCollection;
- }
-
- /** Returns the driver class.
- */
- public Class getDriverClass() {
- return driverClass;
- }
-
- /** Sets the driver class.
- */
- public void setDriverClass(Class pDriverClass) {
- driverClass = pDriverClass;
- }
-
- /** Returns the password.
- */
- public String getPassword() {
- return password;
- }
-
- /** Sets the password.
- */
- public void setPassword(String pPassword) {
- password = pPassword;
- }
-
- /** Returns the users name.
- */
- public String getUser() {
- return user;
- }
-
- /** Sets the users name.
- */
- public void setUser(String pUser) {
- user = pUser;
- }
-
- /** Returns the name of the XPathQueryService. Defaults to
- * "XPathQueryService".
- */
- public String getXPathQueryService() {
- return xPathQueryService;
- }
-
- /** Sets the name of the XPathQueryService. Defaults to
- * "XPathQueryService".
- */
- public void setXPathQueryService(String pXpathQueryService) {
- xPathQueryService = pXpathQueryService;
- }
-
- /** Returns the version of the XPathQueryService. Defaults to
- * "1.0".
- */
- public String getXPathQueryServiceVersion() {
- return xPathQueryServiceVersion;
- }
-
- /** Sets the version of the XPathQueryService. Defaults to
- * "1.0".
- */
- public void setXPathQueryServiceVersion(String pXpathQueryServiceVersion) {
- xPathQueryServiceVersion = pXpathQueryServiceVersion;
- }
-
- /** Returns the database collection by invoking
- * {@link DatabaseManager#getCollection(String)}.
- */
- protected Collection getXmlDbCollection()
- throws XMLDBException, IllegalAccessException, InstantiationException {
- Database database = (Database) getDriverClass().newInstance();
- if (databaseProperties != null) {
- for (Iterator iter = databaseProperties.entrySet().iterator(); iter.hasNext(); ) {
- Map.Entry entry = (Map.Entry) iter.next();
- database.setProperty((String) entry.getKey(), (String) entry.getValue());
- }
- }
- DatabaseManager.registerDatabase(database);
- return DatabaseManager.getCollection(getCollection());
- }
-
-
- /* (non-Javadoc)
- * @see org.apache.ws.jaxme.PM#select(org.apache.ws.jaxme.Observer, java.lang.String, org.apache.ws.jaxme.PMParams)
- */
- public void select(Observer pObserver, String pQuery, PMParams pPlaceHolderArgs) throws JAXBException {
- if (pPlaceHolderArgs != null) {
- pQuery = super.parseQuery(pQuery, pPlaceHolderArgs);
- }
-
- if (pQuery == null) {
- throw new IllegalArgumentException("A query must be specified");
- }
-
- Collection col;
- try {
- col = getXmlDbCollection();
- XPathQueryService service = (XPathQueryService) col.getService(getXPathQueryService(),
- getXPathQueryServiceVersion());
- ResourceSet result = service.query(pQuery);
- if (result != null) {
- ResourceIterator i = result.getIterator();
- if (i.hasMoreResources()) {
- JMUnmarshallerHandler handler = (JMUnmarshallerHandler) getManager().getFactory().createUnmarshaller().getUnmarshallerHandler();
- handler.setObserver(pObserver);
- while(i.hasMoreResources()) {
- XMLResource r = (XMLResource) i.nextResource();
- r.getContentAsSAX(handler);
- }
- }
- }
- } catch (XMLDBException e) {
- throw new PMException(e);
- } catch (IllegalAccessException e) {
- throw new PMException(e);
- } catch (InstantiationException e) {
- throw new PMException(e);
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.ws.jaxme.PM#insert(javax.xml.bind.Element)
- */
- public void insert(Element pElement) throws PMException {
- try {
- Collection col = getXmlDbCollection();
- String id = getId(pElement);
- XMLResource resource = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
- ContentHandler ch = resource.setContentAsSAX();
- Marshaller marshaller = getManager().getFactory().createMarshaller();
- marshaller.marshal(pElement, ch);
- col.storeResource(resource);
- } catch (XMLDBException e) {
- throw new PMException(e);
- } catch (IllegalAccessException e) {
- throw new PMException(e);
- } catch (InstantiationException e) {
- throw new PMException(e);
- } catch (NoSuchMethodException e) {
- throw new PMException(e);
- } catch (InvocationTargetException e) {
- throw new PMException(e.getTargetException());
- } catch (JAXBException e) {
- if (e instanceof PMException) {
- throw (PMException) e;
- } else {
- throw new PMException(e);
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.ws.jaxme.PM#update(javax.xml.bind.Element)
- */
- public void update(Element pElement) throws PMException {
- try {
- Collection col = getXmlDbCollection();
- String id = getId(pElement);
- XMLResource resource = (XMLResource) col.getResource(id);
- ContentHandler ch = resource.setContentAsSAX();
- Marshaller marshaller = getManager().getFactory().createMarshaller();
- marshaller.marshal(pElement, ch);
- col.storeResource(resource);
- } catch (XMLDBException e) {
- throw new PMException(e);
- } catch (IllegalAccessException e) {
- throw new PMException(e);
- } catch (InstantiationException e) {
- throw new PMException(e);
- } catch (NoSuchMethodException e) {
- throw new PMException(e);
- } catch (InvocationTargetException e) {
- throw new PMException(e.getTargetException());
- } catch (JAXBException e) {
- if (e instanceof PMException) {
- throw (PMException) e;
- } else {
- throw new PMException(e);
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.apache.ws.jaxme.PM#delete(javax.xml.bind.Element)
- */
- public void delete(Element pElement) throws PMException {
- try {
- Collection col = getXmlDbCollection();
- String id = getId(pElement);
- XMLResource resource = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
- col.removeResource(resource);
- } catch (XMLDBException e) {
- throw new PMException(e);
- } catch (IllegalAccessException e) {
- throw new PMException(e);
- } catch (InstantiationException e) {
- throw new PMException(e);
- } catch (InvocationTargetException e) {
- throw new PMException(e.getTargetException());
- } catch (NoSuchMethodException e) {
- throw new PMException(e);
- } catch (JAXBException e) {
- if (e instanceof PMException) {
- throw (PMException) e;
- } else {
- throw new PMException(e);
- }
- }
- }
-
-}