You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/05/17 20:33:55 UTC

cvs commit: jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb CollectionFactory.java XmlDbPersistenceManagerFactory.java XmlDbStorage.java

baliuka     02/05/17 11:33:55

  Added:       simplestore/src/xmldb/org/apache/commons/simplestore/xmldb
                        CollectionFactory.java
                        XmlDbPersistenceManagerFactory.java
                        XmlDbStorage.java
  Log:
  Started frontend for XmlDb api
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/CollectionFactory.java
  
  Index: CollectionFactory.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.simplestore.xmldb;
  
  import org.xmldb.api.base.*;
  /**
   *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   *@version    $Id: CollectionFactory.java,v 1.1 2002/05/17 18:33:55 baliuka Exp $
   */
  public interface CollectionFactory {
      
   public Collection  getCollection();
   
  }
  
  
  
  1.1                  jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/XmlDbPersistenceManagerFactory.java
  
  Index: XmlDbPersistenceManagerFactory.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.simplestore.xmldb;
  
  import org.apache.commons.simplestore.persistence.PersistenceManagerFactory;
  import org.apache.commons.simplestore.persistence.PersistenceManager;
  import org.apache.commons.simplestore.persistence.Storage;
  import org.apache.commons.simplestore.persistence.SimplestoreException;
  import org.apache.commons.simplestore.persistence.Transaction;
  import org.apache.commons.simplestore.persistence.impl.*;
  import org.apache.commons.simplestore.cache.impl.SoftRefMemoryCache;
  
  
  import org.xmldb.api.base.*;
  import org.xmldb.api.modules.*;
  import org.xmldb.api.*;
  
  /**
   *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   *@version    $Id: XmlDbPersistenceManagerFactory.java,v 1.1 2002/05/17 18:33:55 baliuka Exp $
  */
  public class XmlDbPersistenceManagerFactory implements PersistenceManagerFactory{
      
      static private PersistenceManager pm;
      
      /** Holds value of property url. */
      private String url;
      
      /** Holds value of property driver. */
      private String driver;
      
       /** Holds value of property metaResource. */
      private String metaResource;
     
       /** Holds value of property maxStrongRef. */
      private int maxStrongRef = 0xFF;
     
      /** Holds value of property loader. */
      private ClassLoader loader;
      
      
      /** Creates a new instance of XmlDbPersistenceManagerFactory */
      public XmlDbPersistenceManagerFactory() {
      }
      
      public PersistenceManager getPersistenceManager() {
          
           try{
          if( pm == null ){
            
           if( loader == null ){
               loader = PersistenceManager.class.getClassLoader();
           }
           
         MetaClassImpl.parse(loader.getResourceAsStream( metaResource),loader);
          
         
          MetaClassImpl mclass = new MetaClassImpl( MetaClassImpl.class.getClassLoader());
            
           Class c = Class.forName(driver,true,loader);
           
           Database database = (Database) c.newInstance();
           DatabaseManager.registerDatabase(database);
           
          XmlDbStorage storage = new XmlDbStorage( new CollectionFactory(){
            public Collection  getCollection(){
              try{  
               return DatabaseManager.getCollection(url);
              }catch(Exception e){
                throw new SimplestoreException(e.getClass()+":"+e.getMessage());
              }
            }
          
          } );
          ContextImpl context = new ContextImpl(
                mclass, new DefaultTypeConverter(),
                new RandomOIDGenerator(),
                storage,
                SoftRefMemoryCache.getInstance(new java.util.HashMap(),maxStrongRef),
                          storage );
          pm = PersistenceManagerImpl.getPersistenceManager(context);
          
          }
          return pm;
          }catch( Exception e) {
            throw new SimplestoreException(e.getClass()+":"+e.getMessage());
          }
      }    
  
      /** Getter for property url.
       * @return Value of property url.
       */
      public String getUrl() {
          return this.url;
      }    
      
      /** Setter for property url.
       * @param url New value of property url.
       */
      public void setUrl(String url) {
          this.url = url;
      }
      
      /** Getter for property driver.
       * @return Value of property driver.
       */
      public String getDriver() {
          return this.driver;
      }
      
      /** Setter for property driver.
       * @param driver New value of property driver.
       */
      public void setDriver(String driver) {
          this.driver = driver;
      }
   /** Getter for property loader.
       * @return Value of property loader.
       */
      public ClassLoader getLoader() {
          return this.loader;
      }
      
      /** Setter for property loader.
       * @param loader New value of property loader.
       */
      public void setLoader(ClassLoader loader) {
          this.loader = loader;
      }
      
      /** Getter for property metaResource.
       * @return Value of property metaResource.
       */
      public String getMetaResource() {
          return this.metaResource;
      }
      
      /** Setter for property metaResource.
       * @param metaResource New value of property metaResource.
       */
      public void setMetaResource(String metaResource) {
          this.metaResource = metaResource;
      }
      
      /** Getter for property maxStrongRef.
       * @return Value of property maxStrongRef.
       */
      public int getMaxStrongRef() {
          return this.maxStrongRef;
      }
      
      /** Setter for property maxStrongRef.
       * @param maxStrongRef New value of property maxStrongRef.
       */
      public void setMaxStrongRef(int maxStrongRef) {
          this.maxStrongRef = maxStrongRef;
      }
         
  }
  
  
  
  1.1                  jakarta-commons-sandbox/simplestore/src/xmldb/org/apache/commons/simplestore/xmldb/XmlDbStorage.java
  
  Index: XmlDbStorage.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.simplestore.xmldb;
  
  import org.apache.commons.simplestore.cache.Cache;
  import org.apache.commons.simplestore.persistence.InternalTransaction;
  import org.apache.commons.simplestore.persistence.Storage;
  import org.apache.commons.simplestore.persistence.MetaObject;
  import org.apache.commons.simplestore.persistence.MetaClass;
  import org.apache.commons.simplestore.persistence.ObjectNotFound;
  import org.apache.commons.simplestore.persistence.Persistent;
  import org.apache.commons.simplestore.persistence.StorageException;
  import org.apache.commons.simplestore.persistence.Context;
  import org.apache.commons.simplestore.persistence.IllegalTransactionStateException;
  import org.apache.commons.simplestore.persistence.EnumeratorCallback;
  import org.apache.commons.simplestore.persistence.impl.AbstractStorage;
  
  import org.xmldb.api.base.*;
  
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Set;
  
  
  
  /**
   *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   *@version    $Id: XmlDbStorage.java,v 1.1 2002/05/17 18:33:55 baliuka Exp $
   */
  
  
  public class XmlDbStorage extends AbstractStorage {
     
     final static String BEGIN_XUPDATE = "<xu:modifications version=\"1.0\"" +
              "      xmlns:xu=\"http://www.xmldb.org/xupdate\">";
     
     final static String REMOVE_XUPDATE ="<xu:remove select=\"{0}\"/>";
     final static String UPDATE_XUPDATE ="<xu:update select=\"{0}\">{1}</xu:update>" ;
              
     final static String END_XUPDATE ="</xu:modifications>";
  
      
      CollectionFactory collectionFactory;
      
      /** Creates a new instance of XmlDbStorage */
      public XmlDbStorage(CollectionFactory collectionFactory) {
          this.collectionFactory = collectionFactory;
      }
      
      private Collection getCollection(){
         return collectionFactory.getCollection();
      }
      protected  String getPath(MetaObject properties){
        return "/" + properties.getMetaClass().getName();
      }
      protected  String getPath(MetaObject properties, String storageName){
        return getPath(properties) + "/" + storageName;
      }
     // Trancient at this time
     private void appendUpdate(String update,MetaClass mclass){
      System.out.println(update);
     } 
      
      public  void storeObject(MetaObject properties)
                     throws StorageException{
      
      
      }  
      
      public  void enumerateInternal(final Class clasz, Set objects, final EnumeratorCallback callback)
                                       throws StorageException {
      
      
      }
      
      protected  void createObject(MetaObject properties) throws StorageException{
         MetaClass mClass = context.getMetaClass(properties.getPersistentClass());
         
          final Object id = properties.getOID();
          final java.beans.PropertyDescriptor[] beanProps = mClass.getProperties();
         
         appendUpdate( "<xupdate:append select=\"/\" >", mClass );
         
         appendUpdate("<xupdate:element name=\"" + mClass.getName() +"\">", mClass);
          String oidName = mClass.getOIDName();
          appendUpdate("<"+ oidName +">" + id + "</"+ oidName +">",mClass);
          appendUpdate("</xupdate:element>",mClass);
         
         for (int i = 0; i < beanProps.length; i++) {
              
              java.beans.PropertyDescriptor descriptor = beanProps[i];
              if (descriptor.getWriteMethod() != null) {
                  
                  Object value =  properties.getProperty(i) ;
                  if (value == null) {
                      continue;
                  }
                appendUpdate("<xupdate:element name=\"" + mClass.getName() +"\">", mClass);
                String name = mClass.getPropertyName(i); 
                appendUpdate("<"+ name +">" + value + "</"+ name +">",mClass);
                appendUpdate("</xupdate:element>",mClass);
              }
          }
        
        appendUpdate( "</xupdate:append>",mClass );
          
      
      }
      
      protected  void removeObject(MetaObject properties) throws StorageException{
          
         appendUpdate( 
          java.text.MessageFormat.format(REMOVE_XUPDATE, new Object[]{ getPath(properties) } ),
          properties.getMetaClass()
         );
      
      }
      
      protected  void internalCommit() throws StorageException{
      
      
      }
      
      protected  void internalRollback() throws StorageException{
      
      
      }
      
      protected  void internalBegin() throws StorageException{
      
      
      }
    
      
      public  void retrieveInternal(Class clasz, int index, Object value,java.util.Set objects)throws StorageException{
      
      }
      
       public Object retrieveObject( final Class clasz, Object id ) throws StorageException {
         return null;
       }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>