001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.components.jndi;
018:
019: /**
020: * <p>
021: * Helper class to establish a jndi based datasource for commandline and maven based applications
022: *
023: * </p>
024: *
025: *
026: * @
027: * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
028: * @version $ $
029: *
030: */
031:
032: import java.util.Map;
033:
034: import org.apache.commons.pool.impl.GenericObjectPool;
035: import org.apache.jetspeed.components.SpringComponentManager;
036: import org.apache.jetspeed.components.datasource.BoundDBCPDatasourceComponent;
037: import org.apache.jetspeed.components.jndi.JNDIComponent;
038: import org.apache.jetspeed.components.jndi.TyrexJNDIComponent;
039:
040: public class SpringJNDIStarter {
041:
042: public static final String JNDI_DS_NAME = "jetspeed";
043: public static final String DATASOURCE_DRIVER = "org.apache.jetspeed.database.driver"
044: .intern();
045: public static final String DATASOURCE_URL = "org.apache.jetspeed.database.url"
046: .intern();
047: public static final String DATASOURCE_USERNAME = "org.apache.jetspeed.database.user"
048: .intern();
049: public static final String DATASOURCE_PASSWORD = "org.apache.jetspeed.database.password"
050: .intern();
051:
052: private final Map context;
053:
054: protected BoundDBCPDatasourceComponent datasourceComponent;
055: protected JNDIComponent jndi;
056: String appRoot = null;
057: String[] bootConfig = null;
058: String[] appConfig = null;
059: SpringComponentManager scm = null;
060:
061: /**
062: *
063: * Create an instance with a given context and the usual SpringComponent arguments
064: * @param context
065: * @param appRoot root directory of the application
066: * @param bootConfig (string-)list of files to process on boot
067: * @param appConfig (string-)list of files to process as configuration
068: */
069: public SpringJNDIStarter(Map context, String appRoot,
070: String[] bootConfig, String[] appConfig) {
071: this .context = context;
072: this .appRoot = appRoot;
073: this .bootConfig = bootConfig;
074: this .appConfig = appConfig;
075: }
076:
077: /**
078: * The main startup routine.
079: * Establishes a JNDI connection based on the following System parameters:
080: * <p> org.apache.jetspeed.database.url
081: * <p> org.apache.jetspeed.database.driver
082: * <p> org.apache.jetspeed.database.user
083: * <p> org.apache.jetspeed.database.password
084: * @throws Exception
085: */
086: public void setUp() throws Exception {
087: setupJNDI();
088: scm = new SpringComponentManager(bootConfig, appConfig, appRoot);
089: }
090:
091: public void tearDown() throws Exception {
092: try {
093: datasourceComponent.stop();
094: } catch (Exception e) {
095: System.out.println("datasourceComponent stop failed with "
096: + e.getLocalizedMessage());
097: e.printStackTrace();
098: }
099: try {
100: scm.stop();
101: } catch (Exception e) {
102: System.out
103: .println("SpringComponentManager stop failed with "
104: + e.getLocalizedMessage());
105: e.printStackTrace();
106: }
107:
108: try {
109: jndi.unbindFromCurrentThread();
110: } catch (Exception e) {
111: System.out
112: .println("JNDI unbindFromCurrentThread failed with "
113: + e.getLocalizedMessage());
114: e.printStackTrace();
115: }
116: }
117:
118: String getProperty(String name) {
119: String s = null;
120: try {
121: if (context != null)
122: s = (String) context.get(name);
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: if (s == null)
127: s = System.getProperty(name);
128: return s;
129: }
130:
131: public void setupJNDI() throws Exception {
132: jndi = new TyrexJNDIComponent();
133: String url = getProperty("org.apache.jetspeed.database.url");
134: String driver = getProperty("org.apache.jetspeed.database.driver");
135: String user = getProperty("org.apache.jetspeed.database.user");
136: String password = getProperty("org.apache.jetspeed.database.password");
137: datasourceComponent = new BoundDBCPDatasourceComponent(user,
138: password, driver, url, 20, 5000,
139: GenericObjectPool.WHEN_EXHAUSTED_GROW, true,
140: JNDI_DS_NAME, jndi);
141: datasourceComponent.start();
142: }
143:
144: public SpringComponentManager getComponentManager() {
145: return scm;
146: }
147:
148: public Map getContext() {
149: return context;
150: }
151:
152: }
|