001: package com.bm.cfg;
002:
003: import java.io.InputStream;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.Collection;
007: import java.util.List;
008: import java.util.Properties;
009:
010: import javax.persistence.EntityManagerFactory;
011:
012: import org.hibernate.ejb.Ejb3Configuration;
013:
014: /**
015: * This class reads and holds the ejb3unit configuration.
016: *
017: * @author Daniel Wiese
018: */
019: public final class Ejb3UnitCfg {
020:
021: /** Konfiguration key. * */
022: public static final String EJB3UNIT_PROPERTIES_NAME = "ejb3unit.properties";
023:
024: /** Konfiguration key. * */
025: public static final String KEY_AUTOMATIC_SCHEMA_UPDATE = "ejb3unit.schema.update";
026:
027: /** Konfiguration key. * */
028: public static final String KEY_CONNECTION_DRIVER_CLASS = "ejb3unit.connection.driver_class";
029:
030: /** Konfiguration key. * */
031: public static final String KEY_CONNECTION_PASSWORD = "ejb3unit.connection.password";
032:
033: /** Konfiguration key. * */
034: public static final String KEY_CONNECTION_URL = "ejb3unit.connection.url";
035:
036: /** Konfiguration key. * */
037: public static final String KEY_CONNECTION_USERNAME = "ejb3unit.connection.username";
038:
039: /** Konfiguration key. * */
040: public static final String KEY_IN_MEMORY_TEST = "ejb3unit.inMemoryTest";
041:
042: /** Konfiguration key. * */
043: public static final String KEY_SHOW_SQL = "ejb3unit.show_sql";
044:
045: /** Konfiguration key. * */
046: public static final String KEY_SQL_DIALECT = "ejb3unit.dialect";
047:
048: /** Konfiguration key. * */
049: public static final String KEY_CACHE_PROVIDER = "ejb3unit.cache_provider";
050:
051: /** Konfiguration key. * */
052: public static final String KEY_USE_SECOND_LEVEL_CACHE = "ejb3unit.use_second_level_cache";
053:
054: /** Konfiguration key. * */
055: public static final String KEY_USE_QUERY_CACHE = "ejb3unit.use_query_cache";
056:
057: private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
058: .getLogger(Ejb3UnitCfg.class);
059:
060: /** Konfiguration key. * */
061: private static Ejb3UnitCfg singelton = null;
062:
063: private final Properties config;
064:
065: private Collection<Class<? extends Object>> currentEntytiesToTest;
066:
067: private EntityManagerFactory entityManagerFactory = null;
068:
069: private boolean inMemory = true;
070:
071: private Ejb3Configuration cfg;
072:
073: private Ejb3UnitCfg() {
074: this .currentEntytiesToTest = new ArrayList<Class<? extends Object>>();
075: try {
076: final InputStream inStr = Thread.currentThread()
077: .getContextClassLoader().getResourceAsStream(
078: EJB3UNIT_PROPERTIES_NAME);
079: config = new Properties();
080: if (inStr != null) {
081: config.load(inStr);
082:
083: } else {
084: // run the system in memory if no config if found
085: System.setProperty(KEY_IN_MEMORY_TEST, "true");
086: }
087: } catch (Exception e) {
088: // propagete the exception
089: throw new RuntimeException(e);
090: }
091: }
092:
093: /**
094: * Returns the hibernate configuration settings which will be converted form
095: * ejb3configaration settings.
096: *
097: * @return hibernate entity manager Ejb3Configuration
098: */
099: public Ejb3Configuration getEJB3Configuration() {
100: Ejb3Configuration cfg = new Ejb3Configuration();
101: // tranform the ejb3unit configuration to the hibernate
102: // configuration
103: final Properties prop = cfg.getProperties();
104: if (Boolean.valueOf(this .config.getProperty(KEY_IN_MEMORY_TEST,
105: "true"))) {
106: // configuration for in memory db
107: this .inMemory = true;
108: this .setProperty(prop, "hibernate.connection.url",
109: "jdbc:hsqldb:mem:ejb3unit");
110: this .setProperty(prop, "hibernate.connection.driver_class",
111: "org.hsqldb.jdbcDriver");
112: this .setProperty(prop, "hibernate.connection.username",
113: "sa");
114: this .setProperty(prop, "hibernate.connection.password", "");
115: this .setProperty(prop, "hibernate.dialect",
116: "org.hibernate.dialect.HSQLDialect");
117: this .setProperty(prop, "hibernate.hbm2ddl.auto",
118: "create-drop");
119: } else {
120: this .inMemory = false;
121: this .setProperty(prop, "hibernate.connection.url",
122: this .config.getProperty(KEY_CONNECTION_URL));
123: this .setProperty(prop, "hibernate.connection.driver_class",
124: this .config
125: .getProperty(KEY_CONNECTION_DRIVER_CLASS));
126: this .setProperty(prop, "hibernate.connection.username",
127: this .config.getProperty(KEY_CONNECTION_USERNAME));
128: this .setProperty(prop, "hibernate.connection.password",
129: this .config.getProperty(KEY_CONNECTION_PASSWORD));
130: this .setProperty(prop, "hibernate.dialect", this .config
131: .getProperty(KEY_SQL_DIALECT));
132: this .setProperty(prop, "hibernate.hbm2ddl.auto",
133: this .config
134: .getProperty(KEY_AUTOMATIC_SCHEMA_UPDATE));
135: }
136: this .setProperty(prop, "hibernate.cache.provider_class",
137: this .config.getProperty(KEY_CACHE_PROVIDER));
138: this .setProperty(prop, "hibernate.show_sql", this .config
139: .getProperty(KEY_SHOW_SQL));
140: this .setProperty(prop,
141: "hibernate.cache.use_second_level_cache", this .config
142: .getProperty(KEY_USE_SECOND_LEVEL_CACHE));
143: this .setProperty(prop, "hibernate.cache.use_query_cache",
144: this .config.getProperty(KEY_USE_QUERY_CACHE));
145: // static properties
146: this .setProperty(prop, "hibernate.transaction.factory_class",
147: "org.hibernate.transaction.JDBCTransactionFactory");
148: return cfg;
149: }
150:
151: /**
152: * Returns the entityManagerFactory.
153: *
154: * @return Returns the entityManagerFactory.
155: */
156: public EntityManagerFactory getEntityManagerFactory() {
157: // lazy initialization
158: if (this .entityManagerFactory == null) {
159: cfg = this .getEJB3Configuration();
160: // add anotated entity beans (to test)
161: if (this .currentEntytiesToTest != null) {
162: for (Class<? extends Object> entityToTest : this .currentEntytiesToTest) {
163: cfg.addAnnotatedClass(entityToTest);
164: }
165: }
166: // fix later for now itīs working
167: this .entityManagerFactory = cfg
168: .createEntityManagerFactory();
169: }
170:
171: return this .entityManagerFactory;
172: }
173:
174: /**
175: * Returns the schema gen script.
176: *
177: * @param dialect
178: * for dialect
179: * @return the ddl
180: */
181: public String getSchemaGenScript(DBDialect dialect) {
182: String[] generateSchemaCreationScript = cfg
183: .getHibernateConfiguration()
184: .generateSchemaCreationScript(dialect.getDialect());
185: final StringBuilder sb = new StringBuilder();
186: for (String ddl : generateSchemaCreationScript) {
187: sb.append(ddl).append("\n");
188: }
189: System.out.println(sb.toString());
190: return sb.toString();
191: }
192:
193: /**
194: * Retruns a value of a config key.
195: *
196: * @author Daniel Wiese
197: * @since 08.11.2005
198: * @param key -
199: * the key
200: * @return - a value of an config key
201: */
202: public String getValue(String key) {
203: return this .config.getProperty(key);
204: }
205:
206: /**
207: * Returns the inMemory.
208: *
209: * @return Returns the inMemory.
210: */
211: public boolean isInMemory() {
212: return inMemory;
213: }
214:
215: /**
216: * This helper method will ignore null properties (keys or values)
217: *
218: * @param prop -
219: * the propertiy file
220: * @param key -
221: * the key
222: * @param value -
223: * the value
224: */
225: private void setProperty(Properties prop, String key, String value) {
226: if (key != null && value != null) {
227: prop.setProperty(key, value);
228: }
229: }
230:
231: /**
232: * Add entities to test to the configuration.
233: *
234: * @param entytiesToTest -
235: * the used entity beans
236: */
237: public static synchronized void addEntytiesToTest(
238: Class<?>... entytiesToTest) {
239: addEntytiesToTest(Arrays.asList(entytiesToTest));
240: }
241:
242: /**
243: * Add entities to test to the configuration.
244: *
245: * @param entytiesToTest -
246: * the used entity beans
247: */
248: public static synchronized void addEntytiesToTest(
249: Collection<Class<?>> entytiesToTest) {
250: // init only if the entits for the test change or not initialized at all
251: if (singelton == null) {
252: singelton = new Ejb3UnitCfg();
253: }
254:
255: singelton.currentEntytiesToTest.addAll(entytiesToTest);
256: // reset the factory (if already created) because we have new beans
257: singelton.entityManagerFactory = null;
258: }
259:
260: /**
261: * Creates / returns a singelton instance of the configuration.
262: *
263: * @return - a singelton instance
264: */
265: public static synchronized Ejb3UnitCfg getConfiguration() {
266: if (singelton == null) {
267: singelton = new Ejb3UnitCfg();
268: }
269:
270: return singelton;
271: }
272:
273: /**
274: * Liefert die properties der Jndi rules.
275: *
276: * @author Daniel Wiese
277: * @since 06.07.2006
278: * @param key -
279: * der key
280: * @return - die sell rules
281: */
282: public static List<JndiProperty> getJndiBindings() {
283: return getNestedProperty("ejb3unit_jndi", JndiProperty.class);
284: }
285:
286: /**
287: * Checks if the configuration was initialized.
288: *
289: * @return - a singelton instance
290: */
291: public static boolean isInitialized() {
292: return (singelton != null);
293: }
294:
295: /**
296: * Liest ein NestedProperty Objekte ein.
297: *
298: * @author Daniel Wiese
299: * @since 04.12.2005
300: * @param key -
301: * ConfigKeys - die keys um im Property-File einen Wert
302: * auszulesen
303: * @param toRead -
304: * die klasse die eingelesen werden soll
305: * @return - eine Liste mit gelesenen NestedProperty Objekten.
306: */
307: private static <T extends NestedProperty> List<T> getNestedProperty(
308: String key, Class<T> toRead) {
309: try {
310: final List<T> back = new ArrayList<T>();
311: boolean continueRead = true;
312: int counter = 0;
313: StringBuilder sb = null;
314: while (continueRead) {
315: T currentInstance = toRead.newInstance();
316:
317: counter++;
318: sb = new StringBuilder();
319: sb.append(key).append(".").append(counter);
320:
321: for (String currentInnerValue : currentInstance
322: .innerValues()) {
323: StringBuilder innerValue = new StringBuilder(sb);
324: innerValue.append(".").append(currentInnerValue);
325: final String value = getConfiguration().getValue(
326: innerValue.toString());
327: if (value != null) {
328: currentInstance.setValue(currentInnerValue,
329: value);
330: } else {
331: continueRead = false;
332: break;
333: }
334: }
335:
336: if (continueRead) {
337: back.add(currentInstance);
338: }
339: }
340:
341: return back;
342: } catch (InstantiationException e) {
343: log.error("Canīt instantiate class: " + toRead);
344: throw new IllegalArgumentException(
345: "Canīt instantiate class: " + toRead);
346: } catch (IllegalAccessException e) {
347: log.error("Canīt instantiate class: " + toRead);
348: throw new IllegalArgumentException(
349: "Canīt instantiate class: " + toRead);
350: }
351: }
352:
353: }
|