001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EmbeddedTest.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.core;
025:
026: import java.io.FileInputStream;
027: import java.io.FileNotFoundException;
028: import java.io.IOException;
029: import java.util.Hashtable;
030: import java.util.Properties;
031:
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035:
036: import org.ow2.easybeans.server.EmbeddedException;
037: import org.ow2.easybeans.tests.common.db.DBManager;
038:
039: /**
040: * Publishes the databases in the context. This class will be deprecated when
041: * the container provides this funcionality.
042: * @author Gisele Pinheiro Souza
043: * @author Eduardo Studzinski Estima de Castro
044: */
045: public class EmbeddedTest {
046:
047: /**
048: * The file separator for the current operating system.
049: */
050: private static final String FILE_SEPARATOR = System
051: .getProperty("file.separator");
052:
053: /**
054: * Path of the properties file. The path is current diretory + /tests/conf.
055: */
056: private static final String PATH_PROPERTIES_FILE = System
057: .getProperty("user.dir")
058: + FILE_SEPARATOR
059: + "tests"
060: + FILE_SEPARATOR
061: + "conf"
062: + FILE_SEPARATOR;
063:
064: /**
065: * Alternate Path of the properties file. The path is current diretory + /tests/conf.
066: */
067: private static final String ALTERNATE_PATH_PROPERTIES_FILE = System
068: .getProperty("user.dir")
069: + FILE_SEPARATOR + "conf" + FILE_SEPARATOR;
070:
071: /**
072: * The name of the properties file.
073: */
074: private static final String PROPERTIES_FILE = "dbTest.properties";
075:
076: /**
077: * Says if the postgresql database must be published.
078: */
079: private static final boolean BIND_POSTGRESQL = false;
080:
081: /**
082: * Says if the oracle database must be published.
083: */
084: private static final boolean BIND_ORACLE = false;
085:
086: /**
087: * Says if the mysql database must be published.
088: */
089: private static final boolean BIND_MYSQL = false;
090:
091: /**
092: * The propriety file.
093: */
094: private Properties properties = new Properties();
095:
096: /**
097: * Sets the property file.
098: * @throws EmbeddedException if an error to load the property file occurs.
099: */
100: public EmbeddedTest() throws EmbeddedException {
101: try {
102: properties.load(new FileInputStream(PATH_PROPERTIES_FILE
103: + PROPERTIES_FILE));
104: } catch (FileNotFoundException e) {
105: // try alternate file
106: try {
107: properties.load(new FileInputStream(
108: ALTERNATE_PATH_PROPERTIES_FILE
109: + PROPERTIES_FILE));
110: } catch (IOException ioe) {
111: throw new EmbeddedException(
112: "Cannot open the properties file.", ioe);
113: }
114: } catch (IOException ioe) {
115: throw new EmbeddedException(
116: "Cannot open the properties file.", ioe);
117: }
118: }
119:
120: /**
121: * Sets the parameters to publish the postgresql database.
122: * @return the parameters values.
123: */
124: private Hashtable<Integer, String> createDefaultPostgreSQL() {
125: Hashtable<Integer, String> htParameters = new Hashtable<Integer, String>();
126: htParameters.put(DBManager.JDBC_DRIVER, properties
127: .getProperty("PostgresqlDriver"));
128: htParameters.put(DBManager.URL, properties
129: .getProperty("PostgresqlURL"));
130: htParameters.put(DBManager.LOGIN, properties
131: .getProperty("PostgresqlLogin"));
132: htParameters.put(DBManager.PASSWD, properties
133: .getProperty("PostgresqlPassword"));
134: return htParameters;
135:
136: }
137:
138: /**
139: * Sets the parameters to publish the mysql database.
140: * @return the parameters values.
141: */
142: private Hashtable<Integer, String> createDefaultMySQL() {
143: Hashtable<Integer, String> htParameters = new Hashtable<Integer, String>();
144: htParameters.put(DBManager.JDBC_DRIVER, properties
145: .getProperty("MysqlDriver"));
146: htParameters.put(DBManager.URL, properties
147: .getProperty("MysqlURL"));
148: htParameters.put(DBManager.LOGIN, properties
149: .getProperty("MysqlLogin"));
150: htParameters.put(DBManager.PASSWD, properties
151: .getProperty("MysqlPassword"));
152: return htParameters;
153:
154: }
155:
156: /**
157: * Sets the parameters to publish the oracle database.
158: * @return the parameters values.
159: */
160: private Hashtable<Integer, String> createDefaultOracle() {
161: Hashtable<Integer, String> htParameters = new Hashtable<Integer, String>();
162: htParameters.put(DBManager.JDBC_DRIVER, properties
163: .getProperty("OracleDriver"));
164: htParameters.put(DBManager.URL, properties
165: .getProperty("OracleURL"));
166: htParameters.put(DBManager.LOGIN, properties
167: .getProperty("OracleLogin"));
168: htParameters.put(DBManager.PASSWD, properties
169: .getProperty("OraclePassword"));
170: return htParameters;
171:
172: }
173:
174: /**
175: * Publishes the databases.
176: * @throws EmbeddedException if a naming error occurs or when the jdbc
177: * drivers were not found.
178: */
179: public void bindDatabases() throws EmbeddedException {
180: // if user don't use jclient/client container
181: System
182: .setProperty(Context.INITIAL_CONTEXT_FACTORY,
183: "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
184: // Adds new databases
185: // Bind a PostgreSQL datasource
186: if (BIND_POSTGRESQL) {
187: try {
188: new InitialContext().rebind("postgresql",
189: new DBManager(createDefaultPostgreSQL()));
190: } catch (NamingException e) {
191: throw new EmbeddedException(
192: "Cannot start the PostgresSQL database.", e);
193: } catch (ClassNotFoundException e) {
194: throw new EmbeddedException(
195: "Cannot start the Postgres database.", e);
196: }
197: }
198: // Bind a MySQL datasource
199: if (BIND_MYSQL) {
200: try {
201: new InitialContext().rebind("mysql", new DBManager(
202: createDefaultMySQL()));
203: } catch (NamingException e) {
204: throw new EmbeddedException(
205: "Cannot start the MySQL database.", e);
206: } catch (ClassNotFoundException e) {
207: throw new EmbeddedException(
208: "Cannot start the MySQL database.", e);
209: }
210: }
211:
212: // Bind a Oracle datasource
213: if (BIND_ORACLE) {
214: try {
215: new InitialContext().rebind("oracle", new DBManager(
216: createDefaultOracle()));
217: } catch (NamingException e) {
218: throw new EmbeddedException(
219: "Cannot start the Oracle database.", e);
220: } catch (ClassNotFoundException e) {
221: throw new EmbeddedException(
222: "Cannot start the Oracle database.", e);
223: }
224: }
225:
226: }
227:
228: /**
229: * Unbind the databases of the registry.
230: * @throws EmbeddedException if a naming error occurs.
231: */
232: public void unbindDataBases() throws EmbeddedException {
233: // if user don't use jclient/client container
234: System
235: .setProperty(Context.INITIAL_CONTEXT_FACTORY,
236: "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
237:
238: if (BIND_POSTGRESQL) {
239: unbindDataBase("postgresql");
240: }
241:
242: if (BIND_MYSQL) {
243: unbindDataBase("mysql");
244: }
245: if (BIND_ORACLE) {
246: unbindDataBase("oracle");
247: }
248:
249: }
250:
251: /**
252: * Deletes the database of the registry.
253: * @param dbName the database name in the registry.
254: * @throws EmbeddedException if a naming error occurs
255: */
256: private void unbindDataBase(final String dbName)
257: throws EmbeddedException {
258: try {
259: new InitialContext().unbind(dbName);
260: } catch (NamingException e) {
261: throw new EmbeddedException("Cannot stop the " + dbName
262: + "dataBase", e);
263: }
264: }
265: }
|