001: /*
002: * $Id: GenericHelperFactory.java,v 1.1 2003/08/17 04:56:27 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.entity.datasource;
026:
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.entity.config.EntityConfigUtil;
032:
033: /**
034: * Generic Entity Helper Factory Class
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.1 $
038: * @since 2.0
039: */
040: public class GenericHelperFactory {
041:
042: public static final String module = GenericHelperFactory.class
043: .getName();
044:
045: // protected static UtilCache helperCache = new UtilCache("entity.GenericHelpers", 0, 0);
046: protected static Map helperCache = new HashMap();
047:
048: public static GenericHelper getHelper(String helperName) {
049: GenericHelper helper = (GenericHelper) helperCache
050: .get(helperName);
051:
052: if (helper == null) // don't want to block here
053: {
054: synchronized (GenericHelperFactory.class) {
055: // must check if null again as one of the blocked threads can still enter
056: helper = (GenericHelper) helperCache.get(helperName);
057: if (helper == null) {
058: try {
059: EntityConfigUtil.DatasourceInfo datasourceInfo = EntityConfigUtil
060: .getDatasourceInfo(helperName);
061:
062: if (datasourceInfo == null) {
063: throw new IllegalStateException(
064: "Could not find datasource definition with name "
065: + helperName);
066: }
067: String helperClassName = datasourceInfo.helperClass;
068: Class helperClass = null;
069:
070: if (helperClassName != null
071: && helperClassName.length() > 0) {
072: try {
073: ClassLoader loader = Thread
074: .currentThread()
075: .getContextClassLoader();
076: helperClass = loader
077: .loadClass(helperClassName);
078: } catch (ClassNotFoundException e) {
079: Debug.logWarning(e, module);
080: throw new IllegalStateException(
081: "Error loading GenericHelper class \""
082: + helperClassName
083: + "\": "
084: + e.getMessage());
085: }
086: }
087:
088: Class[] paramTypes = new Class[] { String.class };
089: Object[] params = new Object[] { helperName };
090:
091: java.lang.reflect.Constructor helperConstructor = null;
092:
093: if (helperClass != null) {
094: try {
095: helperConstructor = helperClass
096: .getConstructor(paramTypes);
097: } catch (NoSuchMethodException e) {
098: Debug.logWarning(e, module);
099: throw new IllegalStateException(
100: "Error loading GenericHelper class \""
101: + helperClassName
102: + "\": "
103: + e.getMessage());
104: }
105: }
106: try {
107: helper = (GenericHelper) helperConstructor
108: .newInstance(params);
109: } catch (IllegalAccessException e) {
110: Debug.logWarning(e, module);
111: throw new IllegalStateException(
112: "Error loading GenericHelper class \""
113: + helperClassName + "\": "
114: + e.getMessage());
115: } catch (InstantiationException e) {
116: Debug.logWarning(e, module);
117: throw new IllegalStateException(
118: "Error loading GenericHelper class \""
119: + helperClassName + "\": "
120: + e.getMessage());
121: } catch (java.lang.reflect.InvocationTargetException e) {
122: Debug.logWarning(e, module);
123: throw new IllegalStateException(
124: "Error loading GenericHelper class \""
125: + helperClassName + "\": "
126: + e.getMessage());
127: }
128:
129: if (helper != null)
130: helperCache.put(helperName, helper);
131: } catch (SecurityException e) {
132: Debug.logError(e, module);
133: throw new IllegalStateException(
134: "Error loading GenericHelper class: "
135: + e.toString());
136: }
137: }
138: }
139: }
140: return helper;
141: }
142: }
|