001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose.vendor.standalone;
021:
022: import uk.org.primrose.GeneralException;
023: import uk.org.primrose.pool.PoolException;
024: import uk.org.primrose.pool.core.*;
025: import uk.org.primrose.pool.datasource.*;
026:
027: import java.io.*;
028: import java.util.*;
029: import javax.naming.*;
030:
031: import javax.naming.Context;
032:
033: public class PrimroseLoader {
034:
035: public static void main(String args[]) throws Exception, Throwable {
036: if (args.length != 1) {
037: System.out
038: .println("java uk.org.primrose.vendor.standalone.PrimroseLoader <config file>");
039: System.exit(0);
040: }
041:
042: load(args[0], true);
043: }
044:
045: /**
046: * Stop the pool given by the name
047: * @throws PoolException
048: */
049: public static void stopPool(String name) throws PoolException {
050: PoolLoader.stopPool(name);
051: }
052:
053: /**
054: * Stop the ALL pools that are loaded in the JVM.
055: * @throws PoolException
056: */
057: public static void stopAllPools() throws PoolException {
058: PoolLoader.stopAllPools();
059: }
060:
061: /**
062: * Load a pool(s), or admin data from a config file.
063: * If admin data, then createNewPools is ignored.
064: * If createNewPools is true, then an existing pool is located, altered accordingly and restarted.
065: * If createNewPools is false, then existing pools for the poolName(s) passed are stopped, and replaced.
066: */
067: public static List<String> load(String configFile,
068: boolean createNewPools) throws GeneralException,
069: IOException {
070: setJNDIEnv();
071: List<String> loadedPoolNames = PoolLoader.loadPool(configFile,
072: createNewPools);
073: for (String poolName : loadedPoolNames) {
074: bindPoolToJNDI(poolName);
075: }
076:
077: return loadedPoolNames;
078: }
079:
080: /**
081: * Load a pool, or admin data from a Properties object.
082: * One pool, or admin data per load operation.
083: * If admin data, then createNewPools is ignored.
084: * If createNewPools is true, then an existing pool is located, altered accordingly and restarted.
085: * If createNewPools is false, then existing pools for the poolName(s) passed are stopped, and replaced.
086: */
087: public static void load(Properties config, boolean createNewPools)
088: throws GeneralException {
089: setJNDIEnv();
090: String poolName = PoolLoader.loadPool(config, createNewPools);
091: bindPoolToJNDI(config.getProperty(poolName));
092: }
093:
094: private static void bindPoolToJNDI(String poolName)
095: throws GeneralException {
096: try {
097: Context initCtx = new InitialContext();
098: Context ctx = (Context) initCtx.lookup("java:comp");
099: Context envCtx = null;
100: try {
101: envCtx = (Context) ctx.lookup("env");
102: } catch (NamingException e) {
103: envCtx = ctx.createSubcontext("env");
104: }
105:
106: PrimroseDataSource pds = new PrimroseDataSource();
107: pds.setPoolName(poolName);
108: envCtx.rebind(poolName, pds);
109:
110: } catch (Exception e) {
111: throw new GeneralException(
112: "Error creating JNDI subcontext 'env', while loading pool '"
113: + poolName + "'", e);
114: }
115: }
116:
117: private static void setJNDIEnv() {
118: System.setProperty("java.naming.factory.url.pkgs",
119: "uk.org.primrose.jndi");
120: System.setProperty("java.naming.factory.initial",
121: "uk.org.primrose.jndi.PrimroseInitialContextFactory");
122: }
123:
124: }
|