⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcdescriptorsstore.java

📁 jetspeed源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 *
 * 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", "Jetspeed", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their 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 org.apache.jetspeed.services.cms.repository.slide;

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.
 * This class comes from Slide code. Can be replace by another descriptor
 * store.
 *
 *
 *
 * TO DO : Use J2EE Datastore instead of this class
 *
 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
 * @author Dirk Verbeeck
 * @author <a href="mailto:christophe.lombart@skynet.be">Christophe Lombart</a>
 */

public class JDBCDescriptorsStore
    extends AbstractSimpleService
    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;


    // ----------------------------------------------------- Instance Variables


    /**
     * Database connection.
     */
    protected Connection connection;


    /**
     * Driver class name.
     */
    protected String driver;


    /**
     * Connection URL.
     */
    protected String url;


    /**
     * User name.
     */
    protected String user;


    /**
     * Password.
     */
    protected String password;


    /**
     * JDBC Version to use.
     */
    protected int jdbcVersion;

    /**
     * This store doesn't handle nested transactions, this variable keeps track
     * if the store is already enlisted to a transaction.
     */
    protected boolean alreadyEnlisted=false;

    // -------------------------------------------------------- Service Methods

    /**
     * Returns the sql statements to create the database objects.
     */
    protected String[] getDatabaseCreateStatements()
    {
        String[] statements = {
            "create table objects(uri varchar(65536) primary key," +
            "    classname varchar(4096))",
            "create table children(uri varchar(65536), " +
            "    childuri varchar(65536))",
            "create table links(link varchar(65536), " +
            "    linkto varchar(65536))",
            "create table permissions(object varchar(65536)," +
            "    revisionnumber varchar(20), " +
            "    subject varchar(65536), action varchar(65536), " +
            "    inheritable int, negative int)",
            "create table locks(id varchar(65536), object varchar(4096)," +
            "    subject varchar(4096), type varchar(4096), " +
            "    expirationdate varchar(15), inheritable int, " +
            "    xexclusive int)",
            "create table revisions(uri varchar(65536) primary key, " +
            "    isversioned int, initialrevision varchar(10))",
            "create table workingrevision(uri varchar(65536), " +
            "    baserevision varchar(20), xnumber varchar(20))",
            "create table latestrevisions(uri varchar(65536), " +
            "    branchname varchar(4096), xnumber varchar(20))",
            "create table branches(uri varchar(65536), xnumber varchar(20)," +
            "    childnumber varchar(20))",
            "create table revision(uri varchar(65536), xnumber varchar(20)," +
            "    branchname varchar(4096))",
            "create table label(uri varchar(65536), xnumber varchar(20)," +
            "    label varchar(4096))",
            "create table property(uri varchar(65536), xnumber varchar(20)," +
            "    name varchar(4096), value varchar(65536), " +
            "    namespace varchar(4096), type varchar(100), protected int)"};

        return statements;
    }

    /**
     * Initializes the data source with a set of parameters.
     *
     * @param parameters Hashtable containing the parameters' name
     * and associated value
     * @exception ServiceParameterErrorException Incorrect service parameter
     * @exception ServiceParameterMissingException Service parameter missing
     */
    public 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();
        }

        // JDBC version to use
        jdbcVersion = 1;
        String version = (String) parameters.get("jdbcversion");
        if (version != null)
        {
            jdbcVersion = (new Integer(version)).intValue();
        }
    }

    /**
     * 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
        {
        	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);
        }

        // all updates must be done inside a transaction, no auto commits
        try
        {
            connection.setAutoCommit(false);
        }
        catch (SQLException e)
        {
		}
       /*
        Statement statement = null;
        try {
            statement = connection.createStatement();
            String[] statements = getDatabaseCreateStatements();
            for (int i=0; i<statements.length ; i++ ) {
                statement.execute(statements[i]);
            }

            // Cloudscape needs a commit on DDL statements (create,...)
            connection.commit();

        } catch (SQLException e) {
            try { connection.rollback(); } catch (SQLException ex) { }
        } finally {
            closeStatement(statement);
        }
        */
        // we are just connected and are not enlisted
    	alreadyEnlisted=false;
    }

    /**
     * Disconnects from data source.
     *
     * @exception ServiceDisconnectionFailedException Disconnection
     * from database failed
     */
    public void disconnect()
        throws ServiceDisconnectionFailedException
	{
        getLogger().log("Disconnecting from \"" + url + "\" as user \"" + user + "\"",LOG_CHANNEL,Logger.INFO);
        try
        {
        	if (connection != null)
        	{
                connection.close();
			}
            connection = null;
        }
        catch (SQLException e)
        {
            getLogger().log("Disconnecting from \"" + url + "\" as user \"" + user + "\" failed",LOG_CHANNEL,Logger.ERROR);
            getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
            throw new ServiceDisconnectionFailedException(this, e);
        }
    }

    /**
     * Initializes data source.
     * <p/>
     * Occurs in four steps :
     * <li>Driver class is loaded</li>
     * <li>Driver is intantiated</li>
     * <li>Driver registration in the driver manager</li>
     * <li>Creation of the basic tables, if they didn't exist before</li>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -