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

📄 dboperator.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.storage.dbsql;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.rapla.entities.RaplaType;
import org.rapla.entities.User;
import org.rapla.entities.storage.RefEntity;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaDefaultContext;
import org.rapla.framework.RaplaException;
import org.rapla.framework.internal.ConfigTools;
import org.rapla.storage.CachableStorageOperator;
import org.rapla.storage.IOContext;
import org.rapla.storage.UpdateEvent;
import org.rapla.storage.UpdateResult;
import org.rapla.storage.impl.AbstractCachableOperator;
import org.rapla.storage.impl.EntityStore;
import org.rapla.storage.xml.RaplaInput;

/** This Operator is used to store the data in a SQL-DBMS.*/
public class DBOperator extends AbstractCachableOperator
implements
Disposable
{
    private String driverClassname;
    protected String datasourceName;
    protected String user;
    protected String password;
    protected String dbURL;
    protected Driver dbDriver;
    protected boolean isConnected;
    Properties dbProperties = new Properties();
    boolean bSupportsTransactions = false;
    boolean hsqldb = false;
    boolean oldResourceTableName;
    
    public DBOperator(RaplaContext context, Configuration config) throws RaplaException {
        super( context );
        datasourceName = config.getChild("datasource").getValue(null);
        // dont use datasource (we have to configure a driver )
        if ( datasourceName == null)
        {
	        try 
	        {
	            driverClassname = config.getChild("driver").getValue();
	            dbURL = ConfigTools.resolveContext( config.getChild("url").getValue(), serviceManager);
	        } 
	        catch (ConfigurationException e) 
	        {
	            throw new RaplaException( e );
	        }
	        dbProperties.setProperty("user", config.getChild("user").getValue("") );
	        dbProperties.setProperty("password", config.getChild("password").getValue("") );
	        hsqldb = config.getChild("hsqldb-shutdown").getValueAsBoolean( false );
	        try 
	        {
	            dbDriver = (Driver) getClass().getClassLoader().loadClass(driverClassname).newInstance();
	        } 
	        catch (ClassNotFoundException e) 
	        {
	            throw new RaplaException("DB-Driver not found: " + driverClassname +
	                                          "\nCheck classpath!");
	        } 
	        catch (Exception e) 
	        {
	            throw new RaplaException("Could not instantiate DB-Driver: " + driverClassname, e);
	        }
        }
    }

    public boolean supportsActiveMonitoring() {
        return false;
    }

    public Connection createConnection() throws RaplaException {
        try {
        	 Connection connection;
        	 //datasource lookup 
        	 if ( datasourceName != null)
        	 {
        		 Context cxt = new InitialContext();
        		 DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/" + datasourceName );
        		 if ( ds == null )
        		 {
        			 throw new RaplaDBException("Datasource not found"); 
        		 }
        		 connection = ds.getConnection();
        	 }
        	 // or driver initialization
        	 else
        	 {
        		 connection = dbDriver.connect(dbURL, dbProperties);
                 if (connection == null)
                 {
                     throw new RaplaDBException("No driver found for: " + dbURL + "\nCheck url!");
                 }
        	 }
             
             bSupportsTransactions = connection.getMetaData().supportsTransactions();
             if (bSupportsTransactions)
             {
                 connection.setAutoCommit( false );
             }
             else
             {
                 getLogger().warn("No Transaction support");
             }
             return connection;
        } catch (Throwable ex) {
             if ( ex instanceof RaplaDBException)
             {
                 throw (RaplaDBException) ex;
             }
             ex.printStackTrace();
             throw new RaplaDBException("DB-Connection aborted",ex);
        }
    }

    public void connect() throws RaplaException {
        if (isConnected())
        {
            return;
        }
        loadData();
        isConnected = true;
    }

    public void connect(String username,char[] password) throws RaplaException {
        connect();
    }

    public boolean isConnected() {
        return isConnected;
    }
    
    final public void refresh() throws RaplaException {
        getLogger().warn("Incremental refreshs are not supported");
    }

    public void refreshFull() throws RaplaException {
        try 
        {
            loadData();
        } 
        catch (Exception ex) 
        {
            cache.clearAll();
            disconnect();
            if (ex instanceof RaplaException)
            {
                throw (RaplaException)ex;
            }
            else
            {
                throw new RaplaException(ex);
            }
        }
    }

    public void forceDisconnect() {
        try 
        {
            disconnect();
        } 
        catch (Exception ex) 
        {
            getLogger().error("Error during disconnect ", ex);
        }
    }

    public void disconnect() throws RaplaException 
    {
        cache.clearAll();
        idTable.setCache( cache );
        // HSQLDB Special
        if ( hsqldb ) 
        {
            String sql  ="SHUTDOWN COMPACT";
            try 
            {
                Connection connection = createConnection();
                Statement statement = connection.createStatement();
                statement.executeQuery(sql);
            } 
            catch (SQLException ex) 
            {
                 throw new RaplaException( ex);
            }
        }
        isConnected = false;
        fireStorageDisconnected();
    }

    public void dispose() 
    {
        forceDisconnect();
    }

    final protected void loadData() throws RaplaException {
        Connection connection = createConnection();
        try {
        	// Upgrade db if neccessary
        	{
                ResultSet categoryTable = connection.prepareStatement("select * from CATEGORY" ).executeQuery();
                if ( categoryTable.getMetaData().getColumnCount() == 4) 
                {
                    getLogger().warn("Patching Database for table CATEGORY");
	            	try {
	            		connection.prepareStatement("ALTER TABLE CATEGORY ADD COLUMN DEFINITION TEXT").execute();
	            		connection.commit();

⌨️ 快捷键说明

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