001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.datasource;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.entity.config.DatasourceInfo;
025: import org.ofbiz.entity.config.EntityConfigUtil;
026:
027: /**
028: * Generic Entity Helper Factory Class
029: *
030: */
031: public class GenericHelperFactory {
032:
033: public static final String module = GenericHelperFactory.class
034: .getName();
035:
036: // protected static UtilCache helperCache = new UtilCache("entity.GenericHelpers", 0, 0);
037: protected static Map helperCache = new HashMap();
038:
039: public static GenericHelper getHelper(String helperName) {
040: GenericHelper helper = (GenericHelper) helperCache
041: .get(helperName);
042:
043: if (helper == null) // don't want to block here
044: {
045: synchronized (GenericHelperFactory.class) {
046: // must check if null again as one of the blocked threads can still enter
047: helper = (GenericHelper) helperCache.get(helperName);
048: if (helper == null) {
049: try {
050: DatasourceInfo datasourceInfo = EntityConfigUtil
051: .getDatasourceInfo(helperName);
052:
053: if (datasourceInfo == null) {
054: throw new IllegalStateException(
055: "Could not find datasource definition with name "
056: + helperName);
057: }
058: String helperClassName = datasourceInfo.helperClass;
059: Class helperClass = null;
060:
061: if (helperClassName != null
062: && helperClassName.length() > 0) {
063: try {
064: ClassLoader loader = Thread
065: .currentThread()
066: .getContextClassLoader();
067: helperClass = loader
068: .loadClass(helperClassName);
069: } catch (ClassNotFoundException e) {
070: Debug.logWarning(e, module);
071: throw new IllegalStateException(
072: "Error loading GenericHelper class \""
073: + helperClassName
074: + "\": "
075: + e.getMessage());
076: }
077: }
078:
079: Class[] paramTypes = new Class[] { String.class };
080: Object[] params = new Object[] { helperName };
081:
082: java.lang.reflect.Constructor helperConstructor = null;
083:
084: if (helperClass != null) {
085: try {
086: helperConstructor = helperClass
087: .getConstructor(paramTypes);
088: } catch (NoSuchMethodException e) {
089: Debug.logWarning(e, module);
090: throw new IllegalStateException(
091: "Error loading GenericHelper class \""
092: + helperClassName
093: + "\": "
094: + e.getMessage());
095: }
096: }
097: try {
098: helper = (GenericHelper) helperConstructor
099: .newInstance(params);
100: } catch (IllegalAccessException e) {
101: Debug.logWarning(e, module);
102: throw new IllegalStateException(
103: "Error loading GenericHelper class \""
104: + helperClassName + "\": "
105: + e.getMessage());
106: } catch (InstantiationException e) {
107: Debug.logWarning(e, module);
108: throw new IllegalStateException(
109: "Error loading GenericHelper class \""
110: + helperClassName + "\": "
111: + e.getMessage());
112: } catch (java.lang.reflect.InvocationTargetException e) {
113: Debug.logWarning(e, module);
114: throw new IllegalStateException(
115: "Error loading GenericHelper class \""
116: + helperClassName + "\": "
117: + e.getMessage());
118: }
119:
120: if (helper != null)
121: helperCache.put(helperName, helper);
122: } catch (SecurityException e) {
123: Debug.logError(e, module);
124: throw new IllegalStateException(
125: "Error loading GenericHelper class: "
126: + e.toString());
127: }
128: }
129: }
130: }
131: return helper;
132: }
133: }
|