KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > core > EmbeddedTest


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EmbeddedTest.java 703 2006-06-21 14:33:25Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.core;
26
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.objectweb.easybeans.server.EmbeddedException;
37 import org.objectweb.easybeans.tests.common.db.DBManager;
38
39 /**
40  * Publishes the databases in the context. This class will be deprecated when
41  * the container provides this funcionality.
42  * @author Gisele Pinheiro Souza
43  * @author Eduardo Studzinski Estima de Castro
44  */

45 public class EmbeddedTest {
46
47     /**
48      * The file separator for the current operating system.
49      */

50     private static final String JavaDoc FILE_SEPARATOR = System.getProperty("file.separator");
51
52     /**
53      * Path of the properties file. The path is current diretory + /tests/conf.
54      */

55     private static final String JavaDoc PATH_PROPERTIES_FILE = System.getProperty("user.dir") + FILE_SEPARATOR + "tests"
56             + FILE_SEPARATOR + "conf" + FILE_SEPARATOR;
57
58     /**
59      * The name of the properties file.
60      */

61     private static final String JavaDoc PROPERTIES_FILE = "dbTest.properties";
62
63     /**
64      * Says if the postgresql database must be published.
65      */

66     private static final boolean BIND_POSTGRESQL = false;
67
68     /**
69      * Says if the oracle database must be published.
70      */

71     private static final boolean BIND_ORACLE = false;
72
73     /**
74      * Says if the mysql database must be published.
75      */

76     private static final boolean BIND_MYSQL = false;
77
78     /**
79      * The propriety file.
80      */

81     private Properties JavaDoc properties = new Properties JavaDoc();
82
83     /**
84      * Sets the property file.
85      * @throws EmbeddedException if an error to load the property file occurs.
86      */

87     public EmbeddedTest() throws EmbeddedException {
88         try{
89             properties.load(new FileInputStream JavaDoc(PATH_PROPERTIES_FILE + PROPERTIES_FILE));
90         }catch(IOException JavaDoc e){
91             e.printStackTrace();
92             throw new EmbeddedException("Cannot open the properties file.");
93         }
94     }
95
96     /**
97      * Sets the parameters to publish the postgresql database.
98      * @return the parameters values.
99      */

100     private Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> createDefaultPostgreSQL() {
101         Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> htParameters = new Hashtable JavaDoc<Integer JavaDoc, String JavaDoc>();
102         htParameters.put(DBManager.JDBC_DRIVER, properties.getProperty("PostgresqlDriver"));
103         htParameters.put(DBManager.URL, properties.getProperty("PostgresqlURL"));
104         htParameters.put(DBManager.LOGIN, properties.getProperty("PostgresqlLogin"));
105         htParameters.put(DBManager.PASSWD, properties.getProperty("PostgresqlPassword"));
106         return htParameters;
107
108     }
109
110     /**
111      * Sets the parameters to publish the mysql database.
112      * @return the parameters values.
113      */

114     private Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> createDefaultMySQL() {
115         Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> htParameters = new Hashtable JavaDoc<Integer JavaDoc, String JavaDoc>();
116         htParameters.put(DBManager.JDBC_DRIVER, properties.getProperty("MysqlDriver"));
117         htParameters.put(DBManager.URL, properties.getProperty("MysqlURL"));
118         htParameters.put(DBManager.LOGIN, properties.getProperty("MysqlLogin"));
119         htParameters.put(DBManager.PASSWD, properties.getProperty("MysqlPassword"));
120         return htParameters;
121
122     }
123
124     /**
125      * Sets the parameters to publish the oracle database.
126      * @return the parameters values.
127      */

128     private Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> createDefaultOracle() {
129         Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> htParameters = new Hashtable JavaDoc<Integer JavaDoc, String JavaDoc>();
130         htParameters.put(DBManager.JDBC_DRIVER, properties.getProperty("OracleDriver"));
131         htParameters.put(DBManager.URL, properties.getProperty("OracleURL"));
132         htParameters.put(DBManager.LOGIN, properties.getProperty("OracleLogin"));
133         htParameters.put(DBManager.PASSWD, properties.getProperty("OraclePassword"));
134         return htParameters;
135
136     }
137
138     /**
139      * Publishes the databases.
140      * @throws EmbeddedException if a naming error occurs or when the jdbc
141      * drivers were not found.
142      */

143     public void bindDatabases() throws EmbeddedException {
144         // if user don't use jclient/client container
145
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
146                 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
147         // Adds new databases
148
// Bind a PostgreSQL datasource
149
if (BIND_POSTGRESQL) {
150             try {
151                 new InitialContext JavaDoc().rebind("postgresql", new DBManager(createDefaultPostgreSQL()));
152             } catch (NamingException JavaDoc e) {
153                 throw new EmbeddedException("Cannot start the PostgresSQL database.", e);
154             } catch (ClassNotFoundException JavaDoc e) {
155                 throw new EmbeddedException("Cannot start the Postgres database.", e);
156             }
157         }
158         // Bind a MySQL datasource
159
if (BIND_MYSQL) {
160             try {
161                 new InitialContext JavaDoc().rebind("mysql", new DBManager(createDefaultMySQL()));
162             } catch (NamingException JavaDoc e) {
163                 throw new EmbeddedException("Cannot start the MySQL database.", e);
164             } catch (ClassNotFoundException JavaDoc e) {
165                 throw new EmbeddedException("Cannot start the MySQL database.", e);
166             }
167         }
168
169         // Bind a Oracle datasource
170
if (BIND_ORACLE) {
171             try {
172                 new InitialContext JavaDoc().rebind("oracle", new DBManager(createDefaultOracle()));
173             } catch (NamingException JavaDoc e) {
174                 throw new EmbeddedException("Cannot start the Oracle database.", e);
175             } catch (ClassNotFoundException JavaDoc e) {
176                 throw new EmbeddedException("Cannot start the Oracle database.", e);
177             }
178         }
179
180     }
181
182     /**
183      * Unbind the databases of the registry.
184      * @throws EmbeddedException if a naming error occurs.
185      */

186     public void unbindDataBases() throws EmbeddedException {
187         // if user don't use jclient/client container
188
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
189                 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
190
191         if (BIND_POSTGRESQL) {
192             unbindDataBase("postgresql");
193         }
194
195         if (BIND_MYSQL) {
196             unbindDataBase("mysql");
197         }
198         if (BIND_ORACLE) {
199             unbindDataBase("oracle");
200         }
201
202     }
203
204     /**
205      * Deletes the database of the registry.
206      * @param dbName the database name in the registry.
207      * @throws EmbeddedException if a naming error occurs
208      */

209     private void unbindDataBase(final String JavaDoc dbName) throws EmbeddedException {
210         try {
211             new InitialContext JavaDoc().unbind(dbName);
212         } catch (NamingException JavaDoc e) {
213             throw new EmbeddedException("Cannot stop the " + dbName + "dataBase", e);
214         }
215     }
216 }
217
Popular Tags