"Kumar, Ashok" wrote:

> As u might know the new J2EE classes uses a new db schema. And this is tested with 
>the new TC417 version only. And the mails also refer to TC417 beta version.
>

Dear Ashok,

It seems that I can't use of the J2EE, so I decide to write some code to prepare the 
pooled connection for slide 1.0.16.
So I have used the general skeleton of your code and tried to change it for pooled 
connection to oracle database. Here is the code I have provided. But when I
run it, catalina.out shows me some error that I can't handle it. May I ask you please 
have a look at it and tell me what you think of?

I am really sorry to take your valued time, but there is no way for me. Hope you 
forgive me.

Thanks Indeed,
Bita.
Starting service Tomcat-Standalone
Apache Tomcat/4.0-b6
21 Aug 2002 12:40:16 - org.apache.slide.common.Domain - INFO - Initializing Domain
21 Aug 2002 12:40:16 - org.apache.slide.common.Domain - INFO - Domain configuration : 
{org.apache.slide.security=true}
21 Aug 2002 12:40:16 - org.apache.slide.common.Domain - INFO - Initializing namespace 
: slide
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Loading namespace 
definition
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Node store: 
slidestore.reference.JDBCDescriptorsStore
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Security store 
references nodestore
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Lock store store 
references nodestore
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Revision descriptors 
store references nodestore
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Revision descriptor 
store references nodestore
21 Aug 2002 12:40:17 - org.apache.slide.common.Namespace - INFO - Content store: 
slidestore.reference.JDBCContentStore
21 Aug 2002 12:40:18 - org.apache.slide.common.Namespace - INFO - Registering Store 
jdbc of class class org.apache.slide.store.StandardStore with parameters {} on scope /
21 Aug 2002 12:40:18 - org.apache.slide.common.Namespace - INFO - Initializing Store 
jdbc(org.apache.slide.store.StandardStore)
21 Aug 2002 12:40:18 - slidestore.reference.JDBCDescriptorsStore - INFO - Loading and 
registering driver: oracle.jdbc.driver.OracleDriver
21 Aug 2002 12:40:22 - slidestore.reference.JDBCContentStore - INFO - Loading and 
registering driver: oracle.jdbc.driver.OracleDriver
21 Aug 2002 12:40:22 - org.apache.slide.common.Namespace - INFO - Loading namespace 
slide parameters
21 Aug 2002 12:40:22 - org.apache.slide.common.Namespace - INFO - Loading namespace 
slide base data
21 Aug 2002 12:40:22 - slidestore.reference.JDBCDescriptorsStore - INFO - Connecting 
to "jdbc:oracle:thin:@lunaleka.cs.bris.ac.uk:1521:csadmin" as user "slide"
21 Aug 2002 12:40:22 - slidestore.reference.JDBCContentStore - INFO - Connecting to 
"jdbc:oracle:thin:@lunaleka.cs.bris.ac.uk:1521:csadmin" as user "slide"
CONNDEBUG: Got connection successfully
21 Aug 2002 12:40:23 - org.apache.slide.transaction.SlideTransactionManager - INFO - 
Rollback Transaction 1 xid main-1029930022703-1- in thread main
CONNDEBUG: Got connection successfully
JDBCDescriptorStors.grantPermission()./-- permission involved.
JDBCDescriptorStors.enumeratePermissions()./-- permissions involved.
21 Aug 2002 12:40:23 - slidestore.reference.JDBCDescriptorsStore - INFO - Connecting 
to "jdbc:oracle:thin:@lunaleka.cs.bris.ac.uk:1521:csadmin" as user "slide"
21 Aug 2002 12:40:23 - slidestore.reference.JDBCContentStore - INFO - Connecting to 
"jdbc:oracle:thin:@lunaleka.cs.bris.ac.uk:1521:csadmin" as user "slide"
21 Aug 2002 12:40:23 - slidestore.reference.JDBCDescriptorsStore - ERROR - 
java.sql.SQLException: Logical handle no longer valid
java.sql.SQLException: Logical handle no longer valid
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:114)
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:156)
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:219)
        at 
oracle.jdbc.driver.OracleConnection.checkPhyiscalStatus(OracleConnection.java:289)
        at 
oracle.jdbc.driver.OracleConnection.prepareStatement(OracleConnection.java:335)
        at 
slidestore.reference.JDBCDescriptorsStore.createObject(JDBCDescriptorsStore.java:394)
        at org.apache.slide.store.AbstractStore.createObject(AbstractStore.java:569)
        at org.apache.slide.store.StandardStore.createObject(StandardStore.java:209)
        at org.apache.slide.common.Namespace.loadBaseData(Namespace.java:785)
        at org.apache.slide.common.Domain.initNamespace(Domain.java:795)
        at org.apache.slide.common.Domain.init(Domain.java:448)
        at org.apache.slide.common.Domain.init(Domain.java:398)
        at org.apache.slide.common.Domain.init(Domain.java:361)
        at org.apache.slide.webdav.WebdavServlet.init(WebdavServlet.java:414)
        at javax.servlet.GenericServlet.init(GenericServlet.java:258)
        at org.apache.slide.webdav.WebdavServlet.init(WebdavServlet.java:330)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:857)
        at 
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3196)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:3299)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:785)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:415)
        at org.apache.catalina.core.StandardHost.install(StandardHost.java:643)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:300)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:389)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:232)
        at 
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:155)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1131)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1123)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:278)
        at org.apache.catalina.core.StandardService.start(StandardService.java:353)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:458)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:737)
        at org.apache.catalina.startup.Catalina.execute(Catalina.java:657)
        at org.apache.catalina.startup.Catalina.process(Catalina.java:178)
        at java.lang.reflect.Method.invoke(Native Method)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:202)
21 Aug 2002 12:40:23 - org.apache.slide.common.Domain - WARNING - Service 
slidestore.reference.JDBCDescriptorsStore@3dc0bb access error : Logical handle no 
longer valid
21 Aug 2002 12:40:23 - org.apache.slide.store.StandardStore - INFO - Delisting service 
slidestore.reference.JDBCDescriptorsStore@3dc0bb from active transaction with failure
21 Aug 2002 12:40:23 - org.apache.slide.common.Namespace - INFO - Namespace base 
configuration was already done before
21 Aug 2002 12:40:23 - org.apache.slide.transaction.SlideTransactionManager - INFO - 
Rollback Transaction 2 xid main-1029930023160-2- in thread main
21 Aug 2002 12:40:23 - org.apache.slide.transaction.SlideTransaction - WARNING - 
Rollback failure: Resource manager slidestore.reference.JDBCDescriptorsStore@3dc0bb 
Error code XA_HEURCOM in Transaction 2 xid main-1029930023160-2- in thread main
21 Aug 2002 12:40:23 - org.apache.slide.common.Namespace - INFO - Loading namespace 
slide configuration
21 Aug 2002 12:40:23 - slidestore.reference.JDBCDescriptorsStore - INFO - No id for 
current thread (Thread[main,5,main]) - called outside transaction?
21 Aug 2002 12:40:23 - org.apache.slide.common.Domain - ERROR - 
org.apache.slide.structure.ObjectNotFoundException: No object found at /actions
org.apache.slide.structure.ObjectNotFoundException: No object found at /actions
        at 
slidestore.reference.JDBCDescriptorsStore.retrieveObject(JDBCDescriptorsStore.java:203)
        at org.apache.slide.store.StandardStore.retrieveObject(StandardStore.java:171)
        at 
org.apache.slide.common.NamespaceConfig.getActionNode(NamespaceConfig.java:1180)
        at 
org.apache.slide.common.NamespaceConfig.initializeNamespaceConfig(NamespaceConfig.java:663)
        at org.apache.slide.common.Namespace.loadConfiguration(Namespace.java:868)
        at org.apache.slide.common.Domain.initNamespace(Domain.java:800)
        at org.apache.slide.common.Domain.init(Domain.java:448)
        at org.apache.slide.common.Domain.init(Domain.java:398)
        at org.apache.slide.common.Domain.init(Domain.java:361)
        at org.apache.slide.webdav.WebdavServlet.init(WebdavServlet.java:414)
        at javax.servlet.GenericServlet.init(GenericServlet.java:258)
        at org.apache.slide.webdav.WebdavServlet.init(WebdavServlet.java:330)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:857)
        at 
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3196)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:3299)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:785)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:415)
        at org.apache.catalina.core.StandardHost.install(StandardHost.java:643)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:300)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:389)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:232)
        at 
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:155)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1131)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1123)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:278)
        at org.apache.catalina.core.StandardService.start(StandardService.java:353)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:458)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:737)
        at org.apache.catalina.startup.Catalina.execute(Catalina.java:657)
        at org.apache.catalina.startup.Catalina.process(Catalina.java:178)
        at java.lang.reflect.Method.invoke(Native Method)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:202)
Starting service Tomcat-Apache
Apache Tomcat/4.0-b6

package slidestore.reference;

import slidestore.reference.util.JDBCAwareInputStream;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Date;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.File;
import java.sql.*;
import javax.transaction.xa.XAException;
import javax.transaction.xa.Xid;
import javax.transaction.xa.XAResource;
import org.apache.slide.common.*;
import org.apache.slide.store.*;
import org.apache.slide.structure.*;
import org.apache.slide.security.*;
import org.apache.slide.content.*;
import org.apache.slide.util.logger.Logger;
//For the Oracle Pooled connection
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;
/**
 * JDBC 2.0 compliant implementation of ContentStore.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
 * @version $Revision: 1.12 $
 */
public class JDBCContentStore extends PooledStore
    implements ContentStore {
    
    
    // -------------------------------------------------------------- Constants
    
    
    public static final int BUFFER_SIZE = 2048;
    public static final String CHARACTER_ENCODING = "8859_1";
    protected static final int REVISION_URI = 1;
    protected static final int REVISION_NUMBER = 2;
    protected static final int REVISION_CONTENT = 3;

      
    // --------------------------------------------------- ContentStore Methods
    
    
    /**
     * Retrive revision content.
     *
     * @param uri Uri
     * @param revisionNumber Node revision number
     */
    public NodeRevisionContent retrieveRevisionContent
        (Uri uri, NodeRevisionDescriptor revisionDescriptor)
        throws ServiceAccessException, RevisionNotFoundException {
       
        Connection connection = getCurrentConnection();

        NodeRevisionContent result = null;
        String revisionUri = uri.toString();
        String revisionNumber = 
            revisionDescriptor.getRevisionNumber().toString();
        
        try {
            
            PreparedStatement selectStatement = connection.prepareStatement
                ("select * from revisioncontent where uri = ? and "
                 + "xnumber = ?");
            selectStatement.setString(1, revisionUri);
            selectStatement.setString(2, revisionNumber);
            ResultSet rs = selectStatement.executeQuery();
            
            if (!rs.next()) {
                rs.close();
                selectStatement.close();
                throw new RevisionNotFoundException
                    (uri.toString(),
                     revisionDescriptor.getRevisionNumber());
            }
            
            InputStream is = rs.getBinaryStream(REVISION_CONTENT);
            if (is == null) {
                throw new RevisionNotFoundException
                    (uri.toString(),
                     revisionDescriptor.getRevisionNumber());
            }
            
            result = new NodeRevisionContent();
            result.setContent(is);
            
            // this input stream passes on the closure of itself onto the
            // jdbc statement and resultSet
            result.setContent( new JDBCAwareInputStream(is,selectStatement) );

        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } catch (RevisionNotFoundException e) {
            // we do NOT want this caught by next clause.
            throw e;
        } catch (Exception e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        }
      
        return result;
        
    }
    
    
    /**
     * Create a new revision
     *
     * @param uri Uri
     * @param revisionDescriptor Node revision descriptor
     * @param revisionContent Node revision content
     */
    public void createRevisionContent
        (Uri uri, NodeRevisionDescriptor revisionDescriptor,
         NodeRevisionContent revisionContent)
        throws ServiceAccessException, RevisionAlreadyExistException {
       
        Connection connection = getCurrentConnection();

        String revisionUri = uri.toString();
        String revisionNumber = 
            revisionDescriptor.getRevisionNumber().toString();
        long contentLength = revisionDescriptor.getContentLength();
        PreparedStatement selectStatement = null;
        
        try {
            
            selectStatement = connection.prepareStatement
                ("select * from revisioncontent where uri = ? and "
                 + "xnumber = ?");
            selectStatement.setString(1, revisionUri);
            selectStatement.setString(2, revisionNumber);
            ResultSet rs = selectStatement.executeQuery();
            if (rs.next()) {
                rs.close();
                throw new RevisionAlreadyExistException
                    (uri.toString(),
                     revisionDescriptor.getRevisionNumber());
            }
            
            rs.close();
            
            storeContent(revisionUri, revisionNumber, revisionDescriptor, 
                         revisionContent);
            System.out.println("JDBCContent.createRevisionContent() "+ revisionUri); 
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } catch (IOException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } catch(RevisionAlreadyExistException e) {
            throw e; // we do NOT want this caught by next clause.
        } catch (Exception e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } finally {
            closeStatement(selectStatement);
        }
        
    }
    
    
    /**
     * Modify the latest revision of an object.
     *
     * @param uri Uri
     * @param revisionDescriptor Node revision descriptor
     * @param revisionContent Node revision content
     */
    public void storeRevisionContent
        (Uri uri, NodeRevisionDescriptor revisionDescriptor,
         NodeRevisionContent revisionContent)
        throws ServiceAccessException, RevisionNotFoundException {
        
        Connection connection = getCurrentConnection();

        String revisionUri = uri.toString();
        String revisionNumber = 
            revisionDescriptor.getRevisionNumber().toString();
        PreparedStatement selectStatement = null;
        
        try {
            
            selectStatement = connection.prepareStatement
                ("select * from revisioncontent where uri = ? and "
                 + "xnumber = ?");
            selectStatement.setString(1, revisionUri);
            selectStatement.setString(2, revisionNumber);
            ResultSet rs = selectStatement.executeQuery();
            if (!rs.next()) {
                rs.close();
                selectStatement.close();
                throw new RevisionNotFoundException
                    (uri.toString(),
                     revisionDescriptor.getRevisionNumber());
            }
            
            rs.close();
            
            removeContent(revisionUri, revisionNumber);
            storeContent(revisionUri, revisionNumber, revisionDescriptor, 
                         revisionContent);
            System.out.println("JDBCContent.storeRevisionContent() "+ revisionUri);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } catch (IOException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } catch(RevisionNotFoundException e) {
            throw e; // we do NOT want this caught by next clause.
        } catch (Exception e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        } finally {
            closeStatement(selectStatement);
        }
        
        
    }
    
    
    /**
     * Remove revision.
     *
     * @param uri Uri
     * @param revisionNumber Node revision number
     */
    public void removeRevisionContent
        (Uri uri, NodeRevisionDescriptor revisionDescriptor)
        throws ServiceAccessException {
        
        String revisionUri = uri.toString();
        String revisionNumber = 
            revisionDescriptor.getRevisionNumber().toString();
        
        try {
            
            removeContent(revisionUri, revisionNumber);
            System.out.println("JDBCContent.removeRevisionContent() "+ revisionUri);   
  
        } catch (Exception e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e.getMessage());
        }
       
    }
    
    
    // ------------------------------------------------------ Protected Methods
    
    
    /**
     * Store a revision.
     */
    protected void storeContent(String revisionUri, String revisionNumber,
                                NodeRevisionDescriptor revisionDescriptor,
                                NodeRevisionContent revisionContent)
        throws IOException, SQLException {
        
        Connection connection = getCurrentConnection();

        PreparedStatement insertStatement = connection.prepareStatement
            ("insert into revisioncontent values(?, ?, ?)");
        insertStatement.setString(1, revisionUri);
        insertStatement.setString(2, revisionNumber);
        
        InputStream is = revisionContent.streamContent();
        
        if (is != null) {
            
            OutputStream os = null;
            // We copy 8 ko with each read
            byte[] buffer = new byte[BUFFER_SIZE];
            long position = 0;
            
            long contentLength = revisionDescriptor.getContentLength();
            File tempFile = null;
            String tempFileName = null;
            
            if (contentLength == -1) {
                // If content length is unspecified, we have to buffer
                // to a temp file.
                try {
                    tempFileName = revisionUri + "-" + revisionNumber;
                    tempFileName = tempFileName.replace('/', '.');
                    int tempFileNameLength = tempFileName.length();
                    if (tempFileNameLength > 200)
                        tempFileName = tempFileName.substring
                            (tempFileNameLength - 200, tempFileNameLength);
                    tempFile = File.createTempFile(tempFileName, null);
                    
                    FileOutputStream fos = new FileOutputStream(tempFile);
                    while (true) {
                        int nChar = is.read(buffer);
                        if (nChar == -1) {
                            break;
                        }
                        fos.write(buffer, 0, nChar);
                        position = position + nChar;
                    }
                    fos.close();
                    
                    is = new FileInputStream(tempFile);
                    contentLength = tempFile.length();
                    System.out.println("JDBCContent.storeContent() "+ revisionUri);
                }
                catch (IOException ex) {
                    getLogger().log(ex.toString() + " during the calculation of the 
content length.",LOG_CHANNEL,Logger.ERROR);
                    getLogger().log("tempFileName: " + 
tempFileName,LOG_CHANNEL,Logger.ERROR);
                    getLogger().log("tempFile: " + 
tempFile.getAbsolutePath(),LOG_CHANNEL,Logger.ERROR);
                    throw ex;
                }
            }
            
            // FIXME ? Cast from long to int won't allow files > 4GB.
            insertStatement.setBinaryStream(3, is, (int) contentLength);
            insertStatement.executeUpdate();
            
            revisionDescriptor.setContentLength(contentLength);
            
            if (tempFile != null) {
                is.close();
                tempFile.delete();
            }
            
        }
       
        insertStatement.close();
        
    }
    
    
    /**
     * Remove content.
     */
    protected void removeContent(String revisionUri, String revisionNumber)
        throws SQLException {

        Connection connection = getCurrentConnection();

        PreparedStatement deleteStatement = connection.prepareStatement
            ("delete from revisioncontent where uri = ? and xnumber = ?");
        deleteStatement.setString(1, revisionUri);
        deleteStatement.setString(2, revisionNumber);
        deleteStatement.executeUpdate();
        deleteStatement.close();

    }
    
    
 
    
}
/*
 * $Header: 
/home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v 
1.28 2001/10/07 19:32:04 dirkv Exp $
 * $Revision: 1.28 $
 * $Date: 2001/10/07 19:32:04 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 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 acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", 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 [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * 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/>.
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 */ 

package slidestore.reference;

import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Date;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import javax.transaction.xa.XAException;
import javax.transaction.xa.Xid;
import org.apache.slide.common.*;
import org.apache.slide.store.*;
import org.apache.slide.structure.*;
import org.apache.slide.security.*;
import org.apache.slide.lock.*;
import org.apache.slide.content.*;
import org.apache.slide.util.logger.Logger;

/**
 * JDBC 1.0 and 2.0 compliant store implementation.
 * 
 * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
 * @author Dirk Verbeeck
 * @version $Revision: 1.28 $
 */

public class JDBCDescriptorsStore
    extends PooledStore
    implements LockStore, NodeStore, RevisionDescriptorsStore, 
    RevisionDescriptorStore, SecurityStore {
    
    
    // -------------------------------------------------------------- Constants
    
    
    // Column numbers
    
    // Structure descriptors
    
    protected static final int OBJECTS_URI = 1;
    protected static final int OBJECTS_CLASS = 2;
    
    protected static final int CHILDREN_URI = 1;
    protected static final int CHILDREN_CHILDURI = 2;
    
    protected static final int LINKS_LINK = 1;
    protected static final int LINKS_LINKTO = 2;
    
    // Security descriptors
    
    protected static final int PERMISSIONS_OBJECT = 1;
    protected static final int PERMISSIONS_REVISION_NUMBER = 2;
    protected static final int PERMISSIONS_SUBJECT = 3;
    protected static final int PERMISSIONS_ACTION = 4;
    protected static final int PERMISSIONS_INHERITABLE = 5;
    protected static final int PERMISSIONS_NEGATIVE = 6;
    
    // Lock descriptors
    
    protected static final int LOCKS_ID = 1;
    protected static final int LOCKS_OBJECT = 2;
    protected static final int LOCKS_SUBJECT = 3;
    protected static final int LOCKS_TYPE = 4;
    protected static final int LOCKS_EXPIRATIONDATE = 5;
    protected static final int LOCKS_INHERITABLE = 6;
    protected static final int LOCKS_EXCLUSIVE = 7;
    
    // Content descriptors
    
    protected static final int REVISIONS_URI = 1;
    protected static final int REVISIONS_ISVERSIONED = 2;
    protected static final int REVISIONS_INITIALREVISION = 3;
    
    protected static final int WORKINGREVISION_URI = 1;
    protected static final int WORKINGREVISION_BASEREVISION = 2;
    protected static final int WORKINGREVISION_NUMBER = 3;
    
    protected static final int LATESTREVISIONS_URI = 1;
    protected static final int LATESTREVISIONS_BRANCHNAME = 2;
    protected static final int LATESTREVISIONS_NUMBER = 3;
    
    protected static final int BRANCHES_URI = 1;
    protected static final int BRANCHES_NUMBER = 2;
    protected static final int BRANCHES_CHILDNUMBER = 3;
    
    protected static final int REVISION_URI = 1;
    protected static final int REVISION_NUMBER = 2;
    protected static final int REVISION_BRANCHNAME = 3;
    
    protected static final int LABEL_URI = 1;
    protected static final int LABEL_NUMBER = 2;
    protected static final int LABEL_LABEL = 3;
    
    protected static final int PROPERTY_URI = 1;
    protected static final int PROPERTY_NUMBER = 2;
    protected static final int PROPERTY_NAME = 3;
    protected static final int PROPERTY_VALUE = 4;
    protected static final int PROPERTY_NAMESPACE = 5;
    protected static final int PROPERTY_TYPE = 6;
    protected static final int PROPERTY_PROTECTED = 7;

    
   
    // ----------------------------------------------- DescriptorsStore Methods
    
    
    /**
     * Retrive an object.
     * 
     * @param uri Uri of the object we want to retrieve
     * @exception ServiceAccessException Error accessing the Service
     * @exception ObjectNotFoundException The object to retrieve was not found
     */
    public ObjectNode retrieveObject(Uri uri)
        throws ServiceAccessException, ObjectNotFoundException {
        
        ObjectNode result = null;
        Connection connection = getCurrentConnection();
        PreparedStatement statement = null;
        
        try {
            
            statement = connection.prepareStatement
                ("select * from objects where uri= ?");
            statement.setString(1, uri.toString());
            
            ResultSet res = statement.executeQuery();
            
            // Parsing result set
            
            String className;
            
            if (res.next()) {
                // Retrieving and loading the object
                className = res.getString(OBJECTS_CLASS);
            } else {
                // Object was not found ...
                throw new ObjectNotFoundException(uri);
            }
            
            closeStatement(statement);
            
            // Then, retrieve the children
            statement = connection.prepareStatement
                ("select * from children where uri= ?");
            statement.setString(1,uri.toString());
            res = statement.executeQuery();
            
            Vector childrenVector = new Vector();
            
            // Parse result set
            while (res.next()) {
                // Load each permission
                childrenVector.addElement(res.getString(CHILDREN_CHILDURI));
            }
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("select * from links where linkto= ?");
            statement.setString(1,uri.toString());
            res = statement.executeQuery();

            Vector linksVector = new Vector();

            // Parse result set
            while (res.next()) {
                // Load each permission
                linksVector.addElement(res.getString(LINKS_LINKTO));
            }

            closeStatement(statement);

            if (className.equals("org.apache.slide.structure.LinkNode")) {
                
                String linkTo = new String();
                statement = connection.prepareStatement
                    ("select * from links where link= ?");
                statement.setString(1,uri.toString());
                res = statement.executeQuery();
                
                if(res.next())
                    linkTo = res.getString(LINKS_LINKTO);
                
                closeStatement(statement);
                
                result = new LinkNode(uri.toString(), childrenVector,
                                      linksVector, linkTo);
                
            } else {
                
                try {
                    Class objclass = Class.forName(className);
                    
                    Class[] argClasses = { Class.forName("java.lang.String"),
                                           Class.forName("java.util.Vector"),
                                           Class.forName("java.util.Vector") };
                    Object[] arguments = { uri.toString(), 
                                           childrenVector, 
                                           linksVector };
                    
                    Constructor constructor = 
                        objclass.getConstructor(argClasses);
                    result = (ObjectNode)constructor.newInstance(arguments);
                } catch(Exception e) { 
                    // ClassNotFoundException, NoSuchMethodException, etc. 
                    throw new ServiceAccessException(this, e);
                }
                
            }
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }

        return result;
    }
    
    
    /**
     * Update an object.
     * 
     * @param object Object to update
     * @exception ServiceAccessException Error accessing the Service
     * @exception ObjectNotFoundException The object to update was not found
     */
    public void storeObject(Uri uri, ObjectNode object)
        throws ServiceAccessException, ObjectNotFoundException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();

        try {
            statement = connection.prepareStatement
                ("select * from objects where uri= ?");
            statement.setString(1, uri.toString());
            
            ResultSet res = statement.executeQuery();
            
            // Parsing result set
            
            if (!res.next()) {
                throw new ObjectNotFoundException(uri);
            }
            
            closeStatement(statement);
            
            // Updating children
            statement = connection.prepareStatement
                ("delete from children where uri= ?");
            statement.setString(1, object.getUri());
            statement.execute();
            closeStatement(statement);
            
            statement = null;
            Enumeration children = object.enumerateChildren();
            while (children.hasMoreElements()) {
                if (statement == null){
                    statement = connection.prepareStatement
                        ("insert into children values(?, ?)");
                }
                statement.setString(1, object.getUri());
                statement.setString(2, (String)children.nextElement());
                statement.execute();
            }
            closeStatement(statement);
            
            // Updating inbound links
            /*
            s = "delete from links where linkto='" + object.getUri() + "'";
            statement.execute(s);
            Enumeration links = object.enumerateLinks();
            while (children.hasMoreElements()) {
                s = "insert into links values('" 
                    + (String) links.nextElement() + "', '" 
                    + object.getUri() + "')";
                statement.execute(s);
            }
            */
            
            // Updating links
            statement = connection.prepareStatement
                ("delete from links where link= ?");
            statement.setString(1, object.getUri());
            statement.execute();
            closeStatement(statement);
            
            if (object instanceof LinkNode) {
                statement = connection.prepareStatement
                    ("insert into links values(?,?)");
                statement.setString(1, object.getUri());
                statement.setString(2, ((LinkNode) object).getLinkedUri());
                statement.execute();
                closeStatement(statement);
            }
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }

    }
    
    
    /**
     * Create a new object.
     * 
     * @param object ObjectNode
     * @param uri Uri of the object we want to create
     * @exception ServiceAccessException Error accessing the Service
     * @exception ObjectAlreadyExistsException An object already exists 
     * at this Uri
     */
    public void createObject(Uri uri, ObjectNode object)
        throws ServiceAccessException, ObjectAlreadyExistsException {
         System.out.println("create the : "+uri);
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();

        try {
            
            String className = object.getClass().getName();
            System.out.println("==========>hello ");

            statement = connection.prepareStatement
                ("select * from objects where uri= ?");
            System.out.println("==========>hello ");

            statement.setString(1, uri.toString());
            
            ResultSet res = statement.executeQuery();
            
            // Parsing result set
            
            if (res.next()) {
                throw new ObjectAlreadyExistsException(uri.toString());
            }
            
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("insert into objects values(?,?)");
            statement.setString(1, uri.toString());
            statement.setString(2, className );
            
            statement.execute();
            closeStatement(statement);
            
            statement = null;
            // Inserting children
            Enumeration children = object.enumerateChildren();
            while (children.hasMoreElements()) {
                if (statement == null){
                    statement = connection.prepareStatement
                        ("insert into children values(?,?)");
                }
                statement.setString(1, uri.toString());
                statement.setString(2, (String) children.nextElement());
                statement.execute();
            }
            closeStatement(statement);
            
            // Updating inbound links
            /*
            Enumeration links = object.enumerateLinks();
            while (children.hasMoreElements()) {
                s = "insert into links values('" 
                    + (String) links.nextElement() + "', '" 
                    + object.getUri() + "')";
                statement.execute(s);
            }
            */
            
            // If the object is a link, also store the link information
            if (object instanceof LinkNode) {
                statement = connection.prepareStatement
                    ("insert into links values(?,?)");
                statement.setString(1, uri.toString());
                statement.setString(2, ((LinkNode) object).getLinkedUri());
                statement.execute();
                closeStatement(statement);
            }
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.createObject()."
                    +uri+ "-- objects, children, links are involved."); 
    }
    
    
    /**
     * Remove an object.
     * 
     * @param object Object to remove
     * @exception ServiceAccessException Error accessing the Service
     * @exception ObjectNotFoundException The object to remove was not found
     */
    public void removeObject(Uri uri, ObjectNode object)
        throws ServiceAccessException, ObjectNotFoundException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            // Removing object
            statement = connection.prepareStatement
                ("delete from objects where uri= ?");
            statement.setString(1,object.getUri());
            statement.execute();
            closeStatement(statement);
            
            // Removing children
            statement = connection.prepareStatement
                ("delete from children where uri=?");
            statement.setString(1, object.getUri());
            statement.execute();
            closeStatement(statement);
            
            // Removing inbound links
            /*
            s = "delete from links where linkto='" + object.getUri() + "'";
            statement.execute(s);
            */
            
            // Removing links
            statement = connection.prepareStatement
                ("delete from links where link= ?");
            statement.setString(1, object.getUri());
            statement.execute();
            closeStatement(statement);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        }
        System.out.println("JDBCDescriptorStors.removeObject()."
                    +uri+ "-- objects, children, links are involved.");
    }
    
    
    /**
     * Grant a new permission.
     * 
     * @param permission Permission we want to create
     * @exception ServiceAccessException Error accessing the Service
     */
    public void grantPermission(Uri uri, NodePermission permission)
        throws ServiceAccessException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();

        try {
            int inheritable = 0;
            if (permission.isInheritable()) {
                inheritable = 1;
            }
            
            int negative = 0;
            if (permission.isNegative()) {
                negative = 1;
            }
            
            NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
            String revisionNumberStr = 
                (revisionNumber == null) ? null : revisionNumber.toString();
            
            statement = connection.prepareStatement
                ("insert into permissions values(?,?,?,?,?,?)");
            statement.setString(1, permission.getObjectUri());
            statement.setString(2, revisionNumberStr);
            statement.setString(3, permission.getSubjectUri());
            statement.setString(4, permission.getActionUri());
            statement.setInt(5, inheritable);
            statement.setInt(6, negative);
            statement.execute();
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.grantPermission()."
                    +uri+ "-- permission involved.");        
    }
    
    
    /**
     * Revoke a permission.
     * 
     * @param permission Permission we want to create
     * @exception ServiceAccessException Error accessing the Service
     */
    public void revokePermission(Uri uri, NodePermission permission)
        throws ServiceAccessException {
        
        /* Warning changes to this method should also be done to 
CloudscapeDescriptorsStore */
        
        Connection connection = getCurrentConnection();
        PreparedStatement statement = null;
        
        try {
            NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
            if(revisionNumber != null) {
                statement = connection.prepareStatement
                    ("delete from permissions where object= ? and subject = ? and 
action = ?  and revisionnumber = ? ");
                statement.setString(4, revisionNumber.toString());
            }
            else {
                statement = connection.prepareStatement
                    ("delete from permissions where object = ? and subject = ? and 
action = ? and revisionnumber is NULL");
            }

            statement.setString(1, permission.getObjectUri());
            statement.setString(2, permission.getSubjectUri());
            statement.setString(3, permission.getActionUri());
        
            statement.execute();
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
    }
    
    
    /**
     * Revoke all the permissions on an object.
     * 
     * @param permission Permission we want to create
     * @exception ServiceAccessException Error accessing the Service
     */
    public void revokePermissions(Uri uri)
        throws ServiceAccessException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            statement = connection.prepareStatement
                ("delete from permissions where object= ?");
            statement.setString(1, uri.toString());
            statement.execute();
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.revokePermissions()."
                          +uri+ "-- permissions involved.");   
    }
    
    
    /**
     * Enumerate permissions on an object.
     * 
     * @param permission Permission we want to create
     * @exception ServiceAccessException Error accessing the Service
     */
    public Enumeration enumeratePermissions(Uri uri)
        throws ServiceAccessException {
        
        Vector permissionVector = new Vector();
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            statement = connection.prepareStatement
                ("select * from permissions where object= ?");
            statement.setString(1, uri.toString());
            ResultSet res = statement.executeQuery();
            
            while (res.next()) {
                String object   = res.getString(PERMISSIONS_OBJECT);
                String revision = res.getString(PERMISSIONS_REVISION_NUMBER);
                String subject  = res.getString(PERMISSIONS_SUBJECT);
                String action   = res.getString(PERMISSIONS_ACTION);
 
                boolean inheritable = false;
                if (res.getInt(PERMISSIONS_INHERITABLE) == 1) {
                    inheritable = true;
                }
                boolean negative = false;
                if (res.getInt(PERMISSIONS_NEGATIVE) == 1) {
                    negative = true;
                }
                NodePermission permission = 
                    new NodePermission(object,revision,subject,
                                       action,inheritable,negative);
                permissionVector.addElement(permission);
            }
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.enumeratePermissions()."
                          +uri+ "-- permissions involved.");   
        return permissionVector.elements();
    }
    
    
    /**
     * Create a new lock.
     * 
     * @param lock Lock token
     * @exception ServiceAccessException Service access error
     */
    public void putLock(Uri uri, NodeLock lock)
        throws ServiceAccessException {
        
        Connection connection = getCurrentConnection();
        PreparedStatement statement = null;
        
        try {
            int inheritable = 0;
            if (lock.isInheritable()) {
                inheritable = 1;
            }
            
            int exclusive = 0;
            if (lock.isExclusive()) {
                exclusive = 1;
            }
            
            statement = connection.prepareStatement
                ("insert into locks values(?,?,?,?,?,?,?)");
            statement.setString(1, lock.getLockId());
            statement.setString(2, lock.getObjectUri());
            statement.setString(3, lock.getSubjectUri());
            statement.setString(4, lock.getTypeUri());
            statement.setString
                (5, String.valueOf(lock.getExpirationDate().getTime()));
            statement.setInt(6,inheritable);
            statement.setInt(7, exclusive);
            statement.execute();
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.putLock()."
                           +uri+ "-- locks involved.");       
    }
    
    
    /**
     * Renew a lock.
     * 
     * @param lock Token to renew
     * @exception ServiceAccessException Service access error
     * @exception LockTokenNotFoundException Lock token was not found
     */
    public void renewLock(Uri uri, NodeLock lock)
        throws ServiceAccessException, LockTokenNotFoundException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            int inheritable = 0;
            if (lock.isInheritable()) {
                inheritable = 1;
            }
            
            int exclusive = 0;
            if (lock.isExclusive()) {
                exclusive = 1;
            }
            
            statement = connection.prepareStatement
                ("delete from locks where id=?");
            statement.setString(1, lock.getLockId());
            statement.execute();
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("insert into locks values(?,?,?,?,?,?,?)");
            statement.setString(1, lock.getLockId());
            statement.setString(2, lock.getObjectUri());
            statement.setString(3, lock.getSubjectUri());
            statement.setString(4, lock.getTypeUri());
            statement.setString
                (5, String.valueOf(lock.getExpirationDate().getTime()));
            statement.setInt(6, inheritable);
            statement.setInt(7, exclusive);
            statement.execute();
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.renewLock()."
                           +uri+ "-- locks involved.");      
    }
    
    
    /**
     * Unlock.
     * 
     * @param lock Token to remove
     * @exception ServiceAccessException Service access error
     * @exception LockTokenNotFoundException Lock token was not found
     */
    public void removeLock(Uri uri, NodeLock lock)
        throws ServiceAccessException, LockTokenNotFoundException {
        
        Statement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            statement = connection.createStatement();
            
            int inheritable = 0;
            if (lock.isInheritable()) {
                inheritable = 1;
            }
            
            String s = null;
            
            s = "delete from locks where id='" + lock.getLockId() + "'";
            statement.execute(s);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.removeLock()."
                           +uri+ "-- locks involved.");       
    }
    
    
    /**
     * Kill a lock.
     * 
     * @param lock Token to remove
     * @exception ServiceAccessException Service access error
     * @exception LockTokenNotFoundException Lock token was not found
     */
    public void killLock(Uri uri, NodeLock lock)
        throws ServiceAccessException, LockTokenNotFoundException {
        
        removeLock(uri, lock);

    }
    
    
    /**
     * Enumerate locks on an object.
     * 
     * @param subject Subject
     * @return Enumeration List of locks which have been put on the subject
     * @exception ServiceAccessException Service access error
     */
    public Enumeration enumerateLocks(Uri uri)
        throws ServiceAccessException {
        
        Vector lockVector = new Vector();
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            statement = connection.prepareStatement
                ("select * from locks where object= ?");
            statement.setString(1, uri.toString());
            statement.execute();
            ResultSet res = statement.getResultSet();
            
            while (res.next()) {
                Date expirationDate = null;
                try {
                    Long timeValue = new Long(res.getString
                                              (LOCKS_EXPIRATIONDATE));
                    expirationDate = new Date(timeValue.longValue());
                } catch (NumberFormatException e) {
                    expirationDate = new Date();
                }
                NodeLock lock = 
                    new NodeLock(res.getString(LOCKS_ID), 
                                 res.getString(LOCKS_OBJECT),
                                 res.getString(LOCKS_SUBJECT), 
                                 res.getString(LOCKS_TYPE),
                                 expirationDate, 
                                 (res.getInt(LOCKS_INHERITABLE) == 1),
                                 (res.getInt(LOCKS_EXCLUSIVE) == 1));
                lockVector.addElement(lock);
            }
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.enumrateLocks()."
                           +uri+ "-- locks involved.");      
        return lockVector.elements();
    }
    
    
    /**
     * Retrieve the revisions informations of an object.
     * 
     * @param uri Uri
     * @exception ServiceAccessException Service access error
     * @exception RevisionDescriptorNotFoundException Revision descriptor 
     * was not found
     */
    public NodeRevisionDescriptors retrieveRevisionDescriptors(Uri uri)
        throws ServiceAccessException, RevisionDescriptorNotFoundException {
        
        NodeRevisionDescriptors revisionDescriptors = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        Connection connection = getCurrentConnection();
         
        try {
            ResultSet res = null;
            
            NodeRevisionNumber initialRevision = new NodeRevisionNumber();
            Hashtable workingRevisions = new Hashtable();
            Hashtable latestRevisionNumbers = new Hashtable();
            Hashtable branches = new Hashtable();
            boolean isVersioned = false;
            
            statement = connection.prepareStatement
                ("select * from revisions where uri= ?");
            statement.setString(1, uri.toString());
            res = statement.executeQuery();
            
            if (res.next()) {
                int isVersionedInt = res.getInt(REVISIONS_ISVERSIONED);
                if (isVersionedInt == 1) {
                    isVersioned = true;
                }
            } else {
                throw new RevisionDescriptorNotFoundException(uri.toString());
            }
            
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("select * from workingrevision where uri= ?");
            statement.setString(1, uri.toString());
            res = statement.executeQuery();
            
            while(res.next()) {
                // TODO : Parse each working revision definition
            }
            
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("select * from latestrevisions where uri=?");
            statement.setString(1, uri.toString());
            res = statement.executeQuery();
            
            while(res.next()) {
                latestRevisionNumbers
                    .put(res.getString(LATESTREVISIONS_BRANCHNAME), 
                         new NodeRevisionNumber
                             (res.getString(LATESTREVISIONS_NUMBER)));
            }
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("select * from revision where uri= ?");
            statement.setString(1, uri.toString());
            res = statement.executeQuery();
            
            while(res.next()) {
                String currentRevisionNumber = res.getString(REVISION_NUMBER);
                
                // We parse the revision list of the object
                if (statement2 == null){
                    statement2 = connection.prepareStatement
                        ("select * from branches where uri = ? and xnumber = ?");
                }
                statement2.setString(1, uri.toString());
                statement2.setString(2, currentRevisionNumber);
                ResultSet res2 = statement2.executeQuery();
                Vector childList = new Vector();
                
                while (res2.next()) {
                    childList.addElement(new NodeRevisionNumber
                        (res2.getString(BRANCHES_CHILDNUMBER)));
                }
                
                branches.put(new NodeRevisionNumber(currentRevisionNumber), 
                             childList);
                
                res2.close();
            }
            closeStatement(statement2);
            
            revisionDescriptors = new NodeRevisionDescriptors
                (uri.toString(), initialRevision, workingRevisions, 
                 latestRevisionNumbers, branches, isVersioned);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
            closeStatement(statement2);
        }
        System.out.println("JDBCDescriptorStors.retrieveRevisionDiscriptors()."
                           +uri+ "-- branches, revision,latestRevisions, 
workingrevision, revisions are  involved.");      
        return revisionDescriptors;
    }
    
    
    /**
     * Create a new revision information object.
     * 
     * @param uri Uri
     * @param revisionDescriptors Node revision descriptors
     * @exception ServiceAccessException Service access error
     */
    public void createRevisionDescriptors
        (Uri uri, NodeRevisionDescriptors revisionDescriptors)
        throws ServiceAccessException {
        
        // TODO : Here, we have the option of "cleaning up" before 
        // creating the new records in the database.
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            ResultSet res = null;
            
            // Creating record in revisions tables
            
            int isVersioned = 0;
            if (revisionDescriptors.isVersioned()) {
                isVersioned = 1;
            }
            
            statement = connection.prepareStatement
                ("insert into revisions values(?,?,?)");
            statement.setString(1,uri.toString());
            statement.setInt(2, isVersioned);
            statement.setString
                (3, revisionDescriptors.getInitialRevision().toString());
            statement.execute();
            closeStatement(statement);
            
            // Creating records in working revisions table
            // ... TODO (working revisions are not used for now)
            
            // Creating records in latest revisions table
            
            // For now, only the latest revision from the main branch is stored
            if (revisionDescriptors.getLatestRevision() != null) {
                statement = connection.prepareStatement
                    ("insert into latestrevisions values(?,?,?)");
                statement.setString(1, uri.toString());
                statement.setString
                    (2, NodeRevisionDescriptors.MAIN_BRANCH.toString());
                statement.setString
                    (3, revisionDescriptors.getLatestRevision().toString());
                statement.execute();
                closeStatement(statement);
            }
            
            // Creating records in the branches table
            // TODO
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }

    }
    
    
    /**
     * Update revision information.
     * 
     * @param uri Uri
     * @param revisionDescriptors Node revision descriptors
     * @exception ServiceAccessException Service access error
     * @exception RevisionDescriptorNotFoundException Revision descriptor 
     * was not found
     */
    public void storeRevisionDescriptors
        (Uri uri, NodeRevisionDescriptors revisionDescriptors)
        throws ServiceAccessException, RevisionDescriptorNotFoundException {
        
        removeRevisionDescriptors(uri);
        createRevisionDescriptors(uri, revisionDescriptors);

    }
    
    
    /**
     * Remove revision information.
     * 
     * @param uri Uri
     * @exception ServiceAccessException Service access error
     */
    public void removeRevisionDescriptors(Uri uri)
        throws ServiceAccessException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            statement = connection.prepareStatement
                ("delete from revisions where uri= ?");
            statement.setString(1, uri.toString());
            statement.execute();
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("delete from workingrevision where uri= ?");
            statement.setString(1, uri.toString());
            statement.execute();
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("delete from latestrevisions where uri= ?");
            statement.setString(1, uri.toString());
            statement.execute();
            closeStatement(statement);
            
            statement = connection.prepareStatement
                ("delete from branches where uri= ?");
            statement.setString(1, uri.toString());
            statement.execute();
            closeStatement(statement);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.removeRevisionDiscriptors()."
                           +uri+"-- revisions, 
workingrevision,latestrevisions,branches");    
    }
    
    
    /**
     * Retrieve an individual object's revision descriptor.
     * 
     * @param Uri uri
     * @param revisionNumber Node revision number
     */
    public NodeRevisionDescriptor retrieveRevisionDescriptor
        (Uri uri, NodeRevisionNumber revisionNumber)
        throws ServiceAccessException, RevisionDescriptorNotFoundException {
        
        NodeRevisionDescriptor revisionDescriptor = null;
        Connection connection = getCurrentConnection();
        PreparedStatement statement = null;

    if(revisionNumber == null)
        throw new RevisionDescriptorNotFoundException(uri.toString());
        
        try {
            
            ResultSet res = null;
    
            String branchName = null;
            Vector labels = new Vector();
            Hashtable properties = new Hashtable();
            
            // Retrieving branch name (and also check that revision 
            // does indeed exist)
            
            statement = connection.prepareStatement
                ("select * from revision where uri= ? and xnumber = ?");
            statement.setString(1, uri.toString());
            statement.setString(2, revisionNumber.toString());
            res = statement.executeQuery();
            
            if (res.next()) {
                branchName = res.getString(REVISION_BRANCHNAME);
            } else {
                throw new RevisionDescriptorNotFoundException(uri.toString());
            }
            
            closeStatement(statement);
            
            // Retrieve labels
            
            statement = connection.prepareStatement
                ("select * from label where uri= ? and xnumber = ?");
            statement.setString(1, uri.toString());
            statement.setString(2, revisionNumber.toString());
            res = statement.executeQuery();
            
            while (res.next()) {
                labels.addElement(res.getString(LABEL_LABEL));
            }
            
            closeStatement(statement);
            
            // Retrieve properties
            
            statement = connection.prepareStatement
                ("select * from property where uri= ? and xnumber = ?");
            statement.setString(1, uri.toString());
            statement.setString(2, revisionNumber.toString());
            res = statement.executeQuery();
            
            while (res.next()) {
                String propertyName = res.getString(PROPERTY_NAME);
                String propertyNamespace = res.getString(PROPERTY_NAMESPACE);
                NodeProperty property = 
                    new NodeProperty(propertyName, 
                                     res.getString(PROPERTY_VALUE), 
                                     propertyNamespace, 
                                     res.getString(PROPERTY_TYPE), 
                                     (res.getInt(PROPERTY_PROTECTED) == 1));
                properties.put(propertyNamespace + propertyName, property);
            }
            
            revisionDescriptor = 
                new NodeRevisionDescriptor(revisionNumber, branchName, 
                                           labels, properties);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.retrieveRevisionDiscriptor()."
                           +uri+"-- property, lable, revision are involved ");
        return revisionDescriptor;
    }
    
    
    /**
     * Create a new revision descriptor.
     * 
     * @param uri Uri
     * @param revisionDescriptor Node revision descriptor
     * @exception ServiceAccessException Service access error
     */
    public void createRevisionDescriptor
        (Uri uri, NodeRevisionDescriptor revisionDescriptor)
        throws ServiceAccessException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            ResultSet res = null;
            
            statement = connection.prepareStatement
                ("insert into revision values(?, ?, ?)");
            statement.setString(1, uri.toString());
            statement.setString
                (2, revisionDescriptor.getRevisionNumber().toString());
            statement.setString(3, revisionDescriptor.getBranchName());
            statement.execute();
            closeStatement(statement);
            
            // Creating revision labels
            statement = null;
            Enumeration labels = revisionDescriptor.enumerateLabels();
            while (labels.hasMoreElements()) {
                if (statement == null){
                    statement = connection.prepareStatement
                        ("insert into label values(?,?,?)");
                }
                statement.setString(1, uri.toString());
                statement.setString
                    (2, revisionDescriptor.getRevisionNumber().toString());
                statement.setString(3, (String)labels.nextElement());
                statement.execute();
            }
            closeStatement(statement);

            // Creating associated properties
            statement = null;
            Enumeration properties = revisionDescriptor.enumerateProperties();
            while (properties.hasMoreElements()) {
                NodeProperty property = 
                    (NodeProperty) properties.nextElement();
                int protectedProperty = 0;
                if (property.isProtected()) {
                    protectedProperty = 1;
                }
                if (statement == null){
                    statement = connection.prepareStatement
                        ("insert into property values(?,?,?,?,?,?,?)");
                }
                statement.setString(1, uri.toString());
                statement.setString
                    (2, revisionDescriptor.getRevisionNumber().toString());
                statement.setString(3, property.getName());
                statement.setString(4, property.getValue().toString());
                statement.setString(5, property.getNamespace());
                statement.setString(6, property.getType());
                statement.setInt(7, protectedProperty);
                statement.execute();
            }
            closeStatement(statement);
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }
        System.out.println("JDBCDescriptorStors.createRevisionDiscriptor()."
                           +uri+"-- property, lable, revision are involved ");  
    }
    
    
    /**
     * Update a revision descriptor.
     * 
     * @param uri Uri
     * @param revisionDescriptors Node revision descriptor
     * @exception ServiceAccessException Service access error
     * @exception RevisionDescriptorNotFoundException Revision descriptor 
     * was not found
     */
    public void storeRevisionDescriptor
        (Uri uri, NodeRevisionDescriptor revisionDescriptor)
        throws ServiceAccessException, RevisionDescriptorNotFoundException {
        
        removeRevisionDescriptor(uri, revisionDescriptor.getRevisionNumber());
        createRevisionDescriptor(uri, revisionDescriptor);
    }
    
    
    /**
     * Remove a revision descriptor.
     * 
     * @param uri Uri
     * @param revisionNumber Revision number
     * @exception ServiceAccessException Service access error
     */
    public void removeRevisionDescriptor(Uri uri, NodeRevisionNumber number)
        throws ServiceAccessException {
        
        PreparedStatement statement = null;
        Connection connection = getCurrentConnection();
        
        try {
            
            statement = connection.prepareStatement
                ("delete from revision where uri= ? and xnumber = ?");
            statement.setString(1, uri.toString());
            statement.setString(2, number.toString());
            statement.execute();
            closeStatement(statement);
            
            // Removing revision labels
            
            statement = connection.prepareStatement
                ("delete from label where uri= ? and xnumber = ?");
            statement.setString(1, uri.toString());
            statement.setString(2, number.toString());
            statement.execute();
            closeStatement(statement);
            
            // Removing associated properties
            
            statement = connection.prepareStatement
                ("delete from property where uri= ? and xnumber = ?");
            statement.setString(1, uri.toString());
            statement.setString(2, number.toString());
            statement.execute();
            
        } catch (SQLException e) {
            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            throw new ServiceAccessException(this, e);
        } finally {
            closeStatement(statement);
        }

    }
    
}
package slidestore.reference;

import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Date;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.File;
import java.sql.*;
import javax.transaction.xa.XAException;
import javax.transaction.xa.Xid;
import javax.transaction.xa.XAResource;
import org.apache.slide.common.*;
import org.apache.slide.store.*;
import org.apache.slide.structure.*;
import org.apache.slide.security.*;
import org.apache.slide.content.*;
import org.apache.slide.util.logger.Logger;
//For the Oracle Pooled connection
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;

/**
 * JDBC 2.0 store implementation implements the shared parts of
 * the two (content, descriptors).
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Bita Shadgar</a>
 * @version $Revision: 1.16 $
 */

public abstract class  PooledStore extends AbstractSimpleService {

    // -------------------------------------------------------------- Constants
    
    public static final int TX_IDLE = 0;
    public static final int TX_PREPARED = 1;
    public static final int TX_SUSPENDED = 1;
    
    // ----------------------------------------------------- Instance Variables
     protected Hashtable connectionMap = new Hashtable();
    /**
     * Database pooled connection.
     */
    protected PooledConnection pc;
    
    /**
     * A connection used when we aren't associated with a transaction correctly.
     */
    protected Connection globalConnection;


    /**
     * Driver class name.
     */
    protected String driver;
    
    
    /**
     * Connection URL.
     */
    protected String url;
    
    
    /**
     * User name.
     */
    protected String user;
    
    
    /**
     * Password.
     */
    protected String password;    
    
    // -------------------------------------------------------- Service Methods
    

    /**
     * Read parameters.
     *
     * @param parameters Hashtable containing the parameters' name
     * and associated value
     */
    public synchronized void setParameters(Hashtable parameters)
        throws ServiceParameterErrorException,
        ServiceParameterMissingException {
        
        // Driver classname
        driver = (String) parameters.get("driver");
        
        // Connection url
        url = (String) parameters.get("url");
        
        // FIXME: before slide 1.0.12 the database url was passed without "jdbc:"
        //        this compatibility code sould be removed in the future
        //        code changed 18 jul 2001
        if (!url.startsWith("jdbc:"))
            url="jdbc:" + url;
        // end compatibility code
        
        // User name
        user = (String) parameters.get("user");
        if (user == null) {
            user = new String();
        }
        
        // Password
        password = (String) parameters.get("password");
        if (password == null) {
            password = new String();
        }
        
    }
    
    
    /**
     * Connects to JDBC and creates the basic table structure.
     *
     * @exception ServiceConnectionFailedException Connection to the 
     * database failed
     */
    public synchronized void connect()
        throws ServiceConnectionFailedException {

        getLogger().log("Connecting to \"" + url + "\" as user \"" + user + 
"\"",LOG_CHANNEL,Logger.INFO);
        try {
           
            // BITA: Get a Logical connection while using the connection pool
            globalConnection = pc.getConnection();

            if (globalConnection == null) System.out.println( "+++++> Connection is 
null");
            //connection = DriverManager.getConnection
            //    (url, user, password);
        } catch (SQLException e) {
            getLogger().log("Connecting to \"" + url + "\" as user \"" + user + "\" 
failed",LOG_CHANNEL,Logger.ERROR);
            getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
            throw new ServiceConnectionFailedException(this, e);
        }

    }
       
    /**
     * Returns connection status
     */
    public boolean isConnected() {
        try {
            return pc!=null && globalConnection != null && 
!globalConnection.isClosed();
        } catch(SQLException e) {
            return false;
        }
    }
    
    
    /**
     * Disconnects from content store.
     *
     * @exception ServiceDisconnectionFailedException
     */
    public synchronized void disconnect()
        throws ServiceDisconnectionFailedException {
        try {
            globalConnection.close(); 
        } catch (SQLException e) {
            getLogger().log("Failed to close special global connection: "+
                    e.getMessage(), LOG_CHANNEL, Logger.ERROR);
        }
    }

     /**
     * Initializes content store.
     *
     * @exception ServiceInitializationFailedException Throws an exception
     * if the store has already been initialized before
     */
    public synchronized void initialize(NamespaceAccessToken token)
        throws ServiceInitializationFailedException {
        try {
            // Loading and registering driver
            token.getLogger().log("Loading and registering driver: " + 
driver,LOG_CHANNEL,Logger.INFO);
           
            // Create a OracleConnectionPoolDataSource instance
            OracleConnectionPoolDataSource ocpds =
                new OracleConnectionPoolDataSource();

            // Set connection parameters
            ocpds.setURL(url);
            ocpds.setUser(user);
            ocpds.setPassword(password);

            // Create a pooled connection
            pc  = ocpds.getPooledConnection();
         } catch (ClassCastException e) {
            token.getLogger().log("Loading and registering driver " + driver + " 
failed",LOG_CHANNEL,Logger.ERROR);
            token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
            throw new ServiceInitializationFailedException(this, e.getMessage());   
        } catch (SQLException e) {
            token.getLogger().log("Loading and registering driver " + driver + " 
failed",LOG_CHANNEL,Logger.ERROR);
            token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
            throw new ServiceInitializationFailedException(this, e.getMessage());
        } catch (Exception e) {
            token.getLogger().log("Loading and registering driver " + driver + " 
failed",LOG_CHANNEL,Logger.ERROR);
            token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
            throw new ServiceInitializationFailedException(this, e.getMessage());
        }
        if( pc == null) {
            token.getLogger().log("Pooled connection is null, can't initialise store");
            throw new ServiceInitializationFailedException(this, "Null pooled 
connection from context");
        }
    }
    

    /**
     * Deletes data source. Should remove stored data if possible.
     *
     * @exception ServiceResetFailedException Reset failed
     */
    public synchronized void reset()
        throws ServiceResetFailedException {
    }
    

    // ----------------------------------------------------- XAResource Methods
    /**
     * Get the transaction timeout value for this XAResource.
     * Just returns 0, we don't have a way of doing transaction timeouts
     * with the connection.
     */
    public int getTransactionTimeout() throws XAException {
        return 0;
    }

    /**
     * Set transaction timeout, not implemented (returns false).
     */
    public boolean setTransactionTimeout(int timeout) throws XAException {
        return false;
    }

    public Xid[] recover(int flag) 
        throws XAException {
        
        getLogger().log("recover() for thread: "+Thread.currentThread(), LOG_CHANNEL, 
Logger.DEBUG);    
        TransactionId id = (TransactionId)connectionMap.get(
                Thread.currentThread());

        if(id != null && id.status == TX_PREPARED) {
            Xid[] xids = new Xid[1];
            xids[0] = id.xid;
            return xids;
        }
        else
            return new Xid[0];
    }

    public int prepare(Xid xid) 
        throws XAException {
        
        getLogger().log("prepare() for thread: "+Thread.currentThread(), LOG_CHANNEL, 
Logger.DEBUG);    
        TransactionId id = (TransactionId)connectionMap.get(
                Thread.currentThread());

        if(id == null)
            throw new XAException(XAException.XAER_NOTA);
        if(xid == null)
            throw new XAException(XAException.XAER_INVAL);
        
        if(id.status != TX_IDLE && id.status != TX_SUSPENDED)
            throw new XAException(XAException.XAER_PROTO);

        if(id.rollbackOnly)
            throw new XAException(XAException.XA_RBROLLBACK);

        id.status = TX_PREPARED;

        return XAResource.XA_OK;
    }

    public boolean isSameRM(XAResource xares) throws XAException {
        if(xares == null)
            return false;
        else
            return this == xares;
    }

    public void forget(Xid xid) throws XAException {

        getLogger().log("forget() for thread: "+Thread.currentThread(), LOG_CHANNEL, 
Logger.DEBUG);    
        TransactionId id = (TransactionId)connectionMap.get(
                Thread.currentThread());

        if(id == null || id.xid == null)
            throw new XAException(XAException.XAER_NOTA);

        if(xid == null)
            throw new XAException(XAException.XAER_INVAL);

        try {
            id.connection.close();
        } catch(SQLException e) {
            getLogger().log("Couldn't close connection.", LOG_CHANNEL, Logger.ERROR);
        }
        getLogger().log("forget(): removing from map: "+Thread.currentThread(), 
LOG_CHANNEL, Logger.DEBUG);    
        connectionMap.remove(Thread.currentThread()); 
    }

    public void end(Xid xid, int flags) throws XAException {

        getLogger().log("end() for thread: "+Thread.currentThread(), LOG_CHANNEL, 
Logger.DEBUG);
        TransactionId id = (TransactionId)connectionMap.get(Thread.currentThread());
        if(id == null || id.xid == null)
            throw new XAException(XAException.XAER_NOTA);
        if(xid == null)
            throw new XAException(XAException.XAER_INVAL);

        if(flags == XAResource.TMSUSPEND)
            id.status = TX_SUSPENDED;

        if(flags == XAResource.TMFAIL) 
            id.rollbackOnly = true;

    }


    
    /**
     * Commit the global transaction specified by xid.
     */
    public void commit(Xid xid, boolean onePhase)
        throws XAException {

        getLogger().log("commit() for thread "+Thread.currentThread()+", removing from 
map",
                LOG_CHANNEL, Logger.DEBUG);

        TransactionId id = (TransactionId)connectionMap.remove(
                Thread.currentThread());
        if(id == null) {
            getLogger().log("Error committing: no transaction associated with current 
thread", LOG_CHANNEL, Logger.ERROR);
            throw new XAException(XAException.XAER_NOTA);
        }
        if(xid == null)
            throw new XAException(XAException.XAER_INVAL);

        if(!onePhase && id.status != TX_PREPARED)
            throw new XAException(XAException.XAER_PROTO);
        if(onePhase && (!(id.status == TX_IDLE || id.status == TX_SUSPENDED)))
            throw new XAException(XAException.XAER_PROTO);

        Connection conn = id.connection;

        if(conn == null) {
            getLogger().log("commit(): No connection in connectionMap for id 
\""+id+"\"", LOG_CHANNEL, Logger.ERROR);
            throw new XAException(XAException.XAER_NOTA);
        }

        try {
            if(id.rollbackOnly)
                conn.rollback();
            else
                conn.commit();
        } catch(SQLException e) {
            throw new XAException(XAException.XA_RBCOMMFAIL);
        } finally {
            try {
                conn.close(); /* We must always return connections to the pool,
                                 or we'd eventually run out. */
            } catch(SQLException e) {
                getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            }
        }
    }

    /**
     * Inform the resource manager to roll back work done on behalf of a
     * transaction branch.
     */
    public void rollback(Xid xid)
        throws XAException {

        getLogger().log("rollback() for thread "+Thread.currentThread()+", removing 
from map",
                LOG_CHANNEL, Logger.DEBUG);

        TransactionId id = (TransactionId)connectionMap.remove(
                Thread.currentThread());
        if(id == null) {
            getLogger().log("No transaction associated with current thread, can't 
rollback", LOG_CHANNEL, Logger.ERROR);
            throw new XAException(XAException.XAER_NOTA);
        }

        Connection conn = id.connection;
        if(conn == null) {
            getLogger().log("rollback(): No connection in connectionMap for id 
\""+id+"\"", LOG_CHANNEL, Logger.ERROR);
            throw new XAException(XAException.XAER_NOTA);
        }
        try {
            conn.rollback();
        } catch (SQLException e) {
            throw new XAException(XAException.XA_HEURCOM);
        } finally {
            try {
                conn.close(); /* We must always return connections to the pool,
                                 or we'd eventually run out. */
            } catch(SQLException e) {
                getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            }
        }
    }
    
    
   /**
    * Start work on behalf of a transaction branch specified in xid.
    */
   public void start(Xid xid, int flags)
       throws XAException {
           getLogger().log("start(): beginning transaction with xid "+xid, 
LOG_CHANNEL, Logger.DEBUG);

       TransactionId id = (TransactionId)connectionMap.get(Thread.currentThread());

       switch(flags) {
           case XAResource.TMNOFLAGS:
               if(id != null)
                   throw new XAException(XAException.XAER_INVAL);
               id = new TransactionId(xid, TX_IDLE);

               getLogger().log("start(): adding to map for "+Thread.currentThread(),
                   LOG_CHANNEL, Logger.DEBUG);

               connectionMap.put(Thread.currentThread(), id);
               break;
           case XAResource.TMJOIN:
               getLogger().log("TMJOIN for transaction in thread: 
"+Thread.currentThread(), LOG_CHANNEL, Logger.DEBUG);
               if(id == null)
                   throw new XAException(XAException.XAER_NOTA);
               break;
           case XAResource.TMRESUME:
               getLogger().log("TMRESUME for transaction in thread: 
"+Thread.currentThread(), LOG_CHANNEL, Logger.DEBUG);
               if(id == null)
                   throw new XAException(XAException.XAER_NOTA);
               if(id.status != TX_SUSPENDED)
                   throw new XAException(XAException.XAER_INVAL);
               id.status = TX_IDLE;
               break;
       }

   } 


  
    // ------------------------------------------------------ Protected Methods
    
    
    /**
     * Close specified statement.
     */
    protected void closeStatement(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
            }
        }
    }
      
    /** 
     * Get the Connection object associated with the current transaction.
     */

    protected Connection getCurrentConnection() {

        TransactionId id = (TransactionId)connectionMap.get(Thread.currentThread());
        if(id == null) {
            getLogger().log("No id for current thread ("+Thread.currentThread()+") - 
called outside transaction?", LOG_CHANNEL, Logger.INFO);
            //System.out.println("globalconnection is used. ");
            return globalConnection;
        }
        
        Connection conn = id.connection;

        if(conn == null) {
            getLogger().log("No connection for current id - shouldn't be possible", 
LOG_CHANNEL, Logger.ERROR);
            //System.out.println("globalconnection is used. ");
            return globalConnection;
        }

        getLogger().log("Returning current valid connection from map", LOG_CHANNEL, 
Logger.DEBUG);
        //System.out.println("current valid connection from map is used. ");
        return conn;
    }
    
    //----------------------------------------------------------------------- Inner 
class

    private class TransactionId 
    {
        public Xid xid;
        public int status;
        public boolean rollbackOnly;
        Connection connection;

        public TransactionId(Xid xid, int status) {
            this.xid = xid;
            this.status = status;
            rollbackOnly = false;

            /* Now, fetch a connection from the DataSource */
            try {
                connection = pc.getConnection();
                if(connection == null) {
                    System.out.println("CONNDEBUG: Got null connection from 
datasource");
                    connection = globalConnection;
                    return;
                }

                connection.setAutoCommit(false);
                System.out.println("CONNDEBUG: Got connection successfully");
            } catch(SQLException e) {
                System.out.println("CONNDEBUG: Couldn't get connection: 
"+e.getMessage());
                connection = globalConnection;
                e.printStackTrace();
            }
        }
    }


    
}
   
 

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to