View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.components.jndi;
18  
19  /***
20   * <p>
21   * Helper class to establish a jndi based datasource for commandline and maven based applications
22   *  
23   * </p>
24   * 
25   * 
26   * @
27   * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
28   * @version $ $
29   *
30   */
31  
32  
33  import java.util.Map;
34  
35  import org.apache.commons.pool.impl.GenericObjectPool;
36  import org.apache.jetspeed.components.SpringComponentManager;
37  import org.apache.jetspeed.components.datasource.BoundDBCPDatasourceComponent;
38  import org.apache.jetspeed.components.jndi.JNDIComponent;
39  import org.apache.jetspeed.components.jndi.TyrexJNDIComponent;
40  
41  public class SpringJNDIStarter
42  {
43  
44  	   public static final String JNDI_DS_NAME = "jetspeed";
45  		public static final String DATASOURCE_DRIVER = "org.apache.jetspeed.database.driver".intern();
46  		public static final String DATASOURCE_URL = "org.apache.jetspeed.database.url".intern();
47  		public static final String DATASOURCE_USERNAME = "org.apache.jetspeed.database.user".intern();
48  		public static final String DATASOURCE_PASSWORD = "org.apache.jetspeed.database.password".intern();
49  
50  
51  		
52  		
53  		private final Map context;
54  	   
55  	    protected BoundDBCPDatasourceComponent datasourceComponent;
56  	    protected JNDIComponent jndi;
57  	    String appRoot = null; 
58  	    String[] bootConfig = null;
59          String[] appConfig = null;
60          SpringComponentManager scm = null;
61  	
62          
63     /***
64      * 
65      * Create an instance with a given context  and the usual SpringComponent arguments 
66      * @param context
67      * @param appRoot root directory of the application
68      * @param bootConfig (string-)list of files to process on boot
69      * @param appConfig (string-)list of files to process as configuration 
70      */
71          public SpringJNDIStarter(Map context,String appRoot, String[] bootConfig, String[] appConfig)
72  	    {
73  	    	this.context = context;
74  	    	this.appRoot = appRoot;
75  	    	this.bootConfig = bootConfig;
76  	    	this.appConfig = appConfig;
77  	    }
78  
79  /***
80   * The main startup routine.
81   * Establishes a JNDI connection based on the following System parameters:
82   * <p> org.apache.jetspeed.database.url
83   * <p> org.apache.jetspeed.database.driver
84   * <p> org.apache.jetspeed.database.user
85   * <p> org.apache.jetspeed.database.password
86   * @throws Exception
87   */
88      public void setUp() throws Exception
89      {
90      	setupJNDI();
91           scm = new SpringComponentManager(bootConfig, appConfig,  appRoot );
92          }
93  
94      public void tearDown() throws Exception
95      {
96      	try
97      	{
98      		datasourceComponent.stop();
99      	}
100     	catch (Exception e)
101     	{
102     		System.out.println("datasourceComponent stop failed with " + e.getLocalizedMessage());
103     		e.printStackTrace();
104     	}
105     	try
106     	{
107     		scm.stop();
108     	}
109     	catch (Exception e)
110     	{
111     		System.out.println("SpringComponentManager stop failed with " + e.getLocalizedMessage());
112     		e.printStackTrace();
113     	}
114     	
115     	try
116     	{
117     		jndi.unbindFromCurrentThread();
118 		}
119 		catch (Exception e)
120 		{
121 			System.out.println("JNDI  unbindFromCurrentThread failed with " + e.getLocalizedMessage());
122 			e.printStackTrace();
123 		}
124     }
125     
126     String getProperty(String name)
127     {
128     	String s = null;
129     	try
130     	{
131     		if (context != null)
132     			s = (String) context.get(name);
133     	}
134     	catch (Exception e)
135     	{
136     		e.printStackTrace();
137     	}
138     	if (s == null)
139     		s = System.getProperty(name);
140     	return s;
141     }
142     
143     
144     public void setupJNDI() throws Exception
145     {
146         jndi = new TyrexJNDIComponent();
147         String url = getProperty("org.apache.jetspeed.database.url");
148         String driver = getProperty("org.apache.jetspeed.database.driver");
149         String user = getProperty("org.apache.jetspeed.database.user");
150         String password = getProperty("org.apache.jetspeed.database.password");
151         datasourceComponent = new BoundDBCPDatasourceComponent(user, password, driver, url, 20, 5000,
152                 GenericObjectPool.WHEN_EXHAUSTED_GROW, true, JNDI_DS_NAME, jndi);
153         datasourceComponent.start();
154     }
155 
156 	public SpringComponentManager getComponentManager()
157 	{
158 		return scm;
159 	}
160 
161     public Map getContext()
162     {
163         return context;
164     }
165     
166     
167 }