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

📄 torqueinstance.java

📁 另外一种持久性o/m软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * Determine whether Torque has already been initialized.     *     * @return true if Torque is already initialized     */    public boolean isInit()    {        return isInit;    }    /**     * Sets the configuration for Torque and all dependencies.     *     * @param conf the Configuration     */    public void setConfiguration(Configuration conf)    {        log.debug("setConfiguration(" + conf + ")");        this.conf = conf;    }    /**     * Get the configuration for this component.     *     * @return the Configuration     */    public Configuration getConfiguration()    {        log.debug("getConfiguration() = " + conf);        return conf;    }    /**     * This method returns a Manager for the given name.     *     * @param name name of the manager     * @return a Manager     */    public AbstractBaseManager getManager(String name)    {        AbstractBaseManager m = (AbstractBaseManager) managers.get(name);        if (m == null)        {            log.error("No configured manager for key " + name + ".");        }        return m;    }    /**     * This methods returns either the Manager from the configuration file,     * or the default one provided by the generated code.     *     * @param name name of the manager     * @param defaultClassName the class to use if name has not been configured     * @return a Manager     */    public AbstractBaseManager getManager(String name,            String defaultClassName)    {        AbstractBaseManager m = (AbstractBaseManager) managers.get(name);        if (m == null)        {            log.debug("Added late Manager mapping for Class: "                    + name + " -> " + defaultClassName);            try            {                initManager(name, defaultClassName);            }            catch (TorqueException e)            {                log.error(e.getMessage(), e);            }            // Try again now that the default manager should be in the map            m = (AbstractBaseManager) managers.get(name);        }        return m;    }    /**     * Shuts down the service.     *     * This method halts the IDBroker's daemon thread in all of     * the DatabaseMap's. It also closes all SharedPoolDataSourceFactories     * and PerUserPoolDataSourceFactories initialized by Torque.     * @exception TorqueException if a DataSourceFactory could not be closed      *            cleanly. Only the first exception is rethrown, any following      *            exceptions are logged but ignored.     */    public synchronized void shutdown()         throws TorqueException    {        if (dbMaps != null)        {            for (Iterator it = dbMaps.values().iterator(); it.hasNext();)            {                DatabaseMap map = (DatabaseMap) it.next();                IDBroker idBroker = map.getIDBroker();                if (idBroker != null)                {                    idBroker.stop();                }            }        }        TorqueException exception = null;        for (Iterator it = dsFactoryMap.keySet().iterator(); it.hasNext();)        {            Object dsfKey = it.next();            DataSourceFactory dsf                     = (DataSourceFactory) dsFactoryMap.get(dsfKey);            try             {                dsf.close();                it.remove();            }            catch (TorqueException e)            {                log.error("Error while closing the DataSourceFactory "                        + dsfKey,                         e);                if (exception == null)                {                	exception = e;                }            }        }        if (exception != null)        {            throw exception;        }        resetConfiguration();    }    /**     * Resets some internal configuration variables to     * their defaults.     */    private void resetConfiguration()    {        mapBuilders = Collections.synchronizedList(new ArrayList());        managers = new HashMap();        isInit = false;    }    /**     * Returns the default database map information.     *     * @return A DatabaseMap.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public DatabaseMap getDatabaseMap()            throws TorqueException    {        return getDatabaseMap(getDefaultDB());    }    /**     * Returns the database map information. Name relates to the name     * of the connection pool to associate with the map.     *     * @param name The name of the database corresponding to the     *        <code>DatabaseMap</code> to retrieve.     * @return The named <code>DatabaseMap</code>.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public DatabaseMap getDatabaseMap(String name)            throws TorqueException    {        if (name == null)        {            throw new TorqueException ("DatabaseMap name was null!");        }        if (dbMaps == null)        {            throw new TorqueException("Torque was not initialized properly.");        }        synchronized (dbMaps)        {            DatabaseMap map = (DatabaseMap) dbMaps.get(name);            if (map == null)            {                // Still not there.  Create and add.                map = initDatabaseMap(name);            }            return map;        }    }    /**     * Creates and initializes the mape for the named database.     * Assumes that <code>dbMaps</code> member is sync'd.     *     * @param name The name of the database to map.     * @return The desired map.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    private final DatabaseMap initDatabaseMap(String name)            throws TorqueException    {        DatabaseMap map = new DatabaseMap(name);        // Add info about IDBroker's table.        setupIdTable(map);        // Setup other ID generators for this map.        try        {            String key = getDatabaseProperty(name, "adapter");            if (StringUtils.isEmpty(key))            {                key = getDatabaseProperty(name, "driver");            }            DB db = DBFactory.create(key);            for (int i = 0; i < IDGeneratorFactory.ID_GENERATOR_METHODS.length;                 i++)            {                map.addIdGenerator(IDGeneratorFactory.ID_GENERATOR_METHODS[i],                        IDGeneratorFactory.create(db, name));            }        }        catch (java.lang.InstantiationException e)        {            throw new TorqueException(e);        }        // Avoid possible ConcurrentModificationException by        // constructing a copy of dbMaps.        Map newMaps = new HashMap(dbMaps);        newMaps.put(name, map);        dbMaps = newMaps;        return map;    }    /**     * Register a MapBuilder     *     * @param className the MapBuilder     */    public void registerMapBuilder(String className)    {        mapBuilders.add(className);    }    /**     * Returns the specified property of the given database, or the empty     * string if no value is set for the property.     *     * @param db   The name of the database whose property to get.     * @param prop The name of the property to get.     * @return     The property's value.     */    private String getDatabaseProperty(String db, String prop)    {        return conf.getString(new StringBuffer("database.")                .append(db)                .append('.')                .append(prop)                .toString(), "");    }    /**     * Setup IDBroker's table information within given database map.     *     * This method should be called on all new database map to ensure that     * IDBroker functionality is available in all databases used by the     * application.     *     * @param map the DataBaseMap to setup.     */    private final void setupIdTable(DatabaseMap map)    {        map.setIdTable("ID_TABLE");        TableMap tMap = map.getIdTable();        tMap.addPrimaryKey("ID_TABLE_ID", new Integer(0));        tMap.addColumn("TABLE_NAME", "");        tMap.addColumn("NEXT_ID", new Integer(0));        tMap.addColumn("QUANTITY", new Integer(0));    }    /**     * This method returns a Connection from the default pool.     *     * @return The requested connection.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public Connection getConnection()            throws TorqueException    {        return getConnection(getDefaultDB());    }    /**     *     * @param name The database name.     * @return a database connection     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public Connection getConnection(String name)            throws TorqueException    {        Connection con = null;        DataSourceFactory dsf = null;        try        {            return getDataSourceFactory(name).getDataSource().getConnection();        }        catch(SQLException se)        {            throw new TorqueException(se);        }    }    /**     * Returns a DataSourceFactory      *     * @param name Name of the DSF to get     * @return A DataSourceFactory object     */    protected DataSourceFactory getDataSourceFactory(String name)            throws TorqueException    {    	if (!isInit())     	{            throw new TorqueException("Torque is not initialized.");    		    	}    	DataSourceFactory dsf = null;        try        {            dsf = (DataSourceFactory) dsFactoryMap.get(name);        }        catch (Exception e)        {            throw new TorqueException(e);        }                    if (dsf == null)        {            throw new NullPointerException(                    "There was no DataSourceFactory "                    + "configured for the connection " + name);        }        return dsf;    }    /**     * This method returns a Connecton using the given parameters.     * You should only use this method if you need user based access to the     * database!     *     * @param name The database name.     * @param username The name of the database user.     * @param password The password of the database user.     * @return A Connection.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public Connection getConnection(String name, String username,            String password)            throws TorqueException    {        try        {            return getDataSourceFactory(name).getDataSource().getConnection(username, password);        }        catch(SQLException se)        {            throw new TorqueException(se);        }    }    /**     * Returns database adapter for a specific connection pool.     *     * @param name A pool name.     * @return The corresponding database adapter.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public DB getDB(String name) throws TorqueException    {        return (DB) adapterMap.get(name);    }    ///////////////////////////////////////////////////////////////////////////    /**     * Returns the name of the default database.     *     * @return name of the default DB, or null if Torque is not initialized yet     */    public String getDefaultDB()    {        return defaultDBName;    }    /**     * Closes a connection.     *     * @param con A Connection to close.     */    public void closeConnection(Connection con)    {        if (con != null)        {            try            {                con.close();            }            catch (SQLException e)            {                log.error("Error occured while closing connection.", e);            }        }    }    /**     * Sets the current schema for a database connection     *     * @param name The database name.     * @param schema The current schema name     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public void setSchema(String name, String schema)            throws TorqueException    {        getDataSourceFactory(name).setSchema(schema);    }    /**     * This method returns the current schema for a database connection     *     * @param name The database name.     * @return The current schema name. Null means, no schema has been set.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public String getSchema(String name)        throws TorqueException    {        return getDataSourceFactory(name).getSchema();    }}

⌨️ 快捷键说明

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