KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > datasource > GenericHelperFactory


1 /*
2  * $Id: GenericHelperFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.datasource;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.entity.config.DatasourceInfo;
32 import org.ofbiz.entity.config.EntityConfigUtil;
33
34 /**
35  * Generic Entity Helper Factory Class
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class GenericHelperFactory {
42     
43     public static final String JavaDoc module = GenericHelperFactory.class.getName();
44
45     // protected static UtilCache helperCache = new UtilCache("entity.GenericHelpers", 0, 0);
46
protected static Map JavaDoc helperCache = new HashMap JavaDoc();
47
48     public static GenericHelper getHelper(String JavaDoc helperName) {
49         GenericHelper helper = (GenericHelper) helperCache.get(helperName);
50
51         if (helper == null) // don't want to block here
52
{
53             synchronized (GenericHelperFactory.class) {
54                 // must check if null again as one of the blocked threads can still enter
55
helper = (GenericHelper) helperCache.get(helperName);
56                 if (helper == null) {
57                     try {
58                         DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName);
59
60                         if (datasourceInfo == null) {
61                             throw new IllegalStateException JavaDoc("Could not find datasource definition with name " + helperName);
62                         }
63                         String JavaDoc helperClassName = datasourceInfo.helperClass;
64                         Class JavaDoc helperClass = null;
65
66                         if (helperClassName != null && helperClassName.length() > 0) {
67                             try {
68                                 ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
69                                 helperClass = loader.loadClass(helperClassName);
70                             } catch (ClassNotFoundException JavaDoc e) {
71                                 Debug.logWarning(e, module);
72                                 throw new IllegalStateException JavaDoc("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
73                             }
74                         }
75
76                         Class JavaDoc[] paramTypes = new Class JavaDoc[] {String JavaDoc.class};
77                         Object JavaDoc[] params = new Object JavaDoc[] {helperName};
78
79                         java.lang.reflect.Constructor JavaDoc helperConstructor = null;
80
81                         if (helperClass != null) {
82                             try {
83                                 helperConstructor = helperClass.getConstructor(paramTypes);
84                             } catch (NoSuchMethodException JavaDoc e) {
85                                 Debug.logWarning(e, module);
86                                 throw new IllegalStateException JavaDoc("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
87                             }
88                         }
89                         try {
90                             helper = (GenericHelper) helperConstructor.newInstance(params);
91                         } catch (IllegalAccessException JavaDoc e) {
92                             Debug.logWarning(e, module);
93                             throw new IllegalStateException JavaDoc("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
94                         } catch (InstantiationException JavaDoc e) {
95                             Debug.logWarning(e, module);
96                             throw new IllegalStateException JavaDoc("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
97                         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
98                             Debug.logWarning(e, module);
99                             throw new IllegalStateException JavaDoc("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
100                         }
101
102                         if (helper != null)
103                             helperCache.put(helperName, helper);
104                     } catch (SecurityException JavaDoc e) {
105                         Debug.logError(e, module);
106                         throw new IllegalStateException JavaDoc("Error loading GenericHelper class: " + e.toString());
107                     }
108                 }
109             }
110         }
111         return helper;
112     }
113 }
114
Popular Tags