001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixxml;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Properties;
025: import java.util.TreeSet;
026:
027: import org.apache.log4j.Logger;
028: import org.apache.log4j.spi.ThrowableInformation;
029:
030: import de.schlund.pfixcore.util.PropertiesUtils;
031: import de.schlund.pfixxml.util.Misc;
032:
033: public class FactoryInitUtil {
034: private final static Logger LOG = Logger
035: .getLogger(FactoryInitUtil.class);
036:
037: private static boolean initialized = false;
038:
039: public static boolean isInitialized() {
040: return initialized;
041: }
042:
043: public static synchronized void initialize(Properties properties)
044: throws FactoryInitException {
045: if (initialized) {
046: return;
047: }
048:
049: HashMap<String, String> factoryProps = PropertiesUtils
050: .selectProperties(properties, "factory.initialize");
051: if (factoryProps != null) {
052: // sort key to initialize the factories in defined order
053: TreeSet<String> keys = new TreeSet<String>(factoryProps
054: .keySet());
055: for (Iterator<String> i = keys.iterator(); i.hasNext();) {
056: String key = i.next();
057: String factoryClassName = factoryProps.get(key);
058: try {
059: LOG.debug(">>>> Init key: [" + key + "] class: ["
060: + factoryClassName + "] <<<<");
061: long start = 0;
062: long stop = 0;
063: Class<?> clazz = Class.forName(factoryClassName);
064: Object factory = clazz.getMethod("getInstance",
065: Misc.NO_CLASSES).invoke(null,
066: Misc.NO_OBJECTS);
067: LOG.debug(" Object ID: " + factory);
068: start = System.currentTimeMillis();
069: clazz.getMethod("init",
070: new Class[] { Properties.class }).invoke(
071: factory, new Object[] { properties });
072: stop = System.currentTimeMillis();
073: LOG.debug("Init of " + factory + " took "
074: + (stop - start) + " ms");
075: } catch (Exception e) {
076: LOG.error(e.toString());
077:
078: Throwable cause = e;
079: if (e instanceof InvocationTargetException
080: && e.getCause() != null)
081: cause = e.getCause();
082:
083: FactoryInitException initException = new FactoryInitException(
084: factoryClassName, cause);
085:
086: ThrowableInformation info = new ThrowableInformation(
087: e);
088: String[] trace = info.getThrowableStrRep();
089: StringBuffer strerror = new StringBuffer();
090: for (int ii = 0; ii < trace.length; ii++) {
091: strerror.append("->" + trace[ii] + "\n");
092: }
093: LOG.error(strerror.toString());
094:
095: throw initException;
096: }
097: }
098: }
099:
100: initialized = true;
101: }
102: }
|