KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > FactoryUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.cms.core.SystemProperty;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.beanutils.ConstructorUtils;
22 import org.apache.commons.discovery.tools.DiscoverClass;
23 import org.apache.commons.discovery.tools.DiscoverSingleton;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26
27
28 /**
29  * @author Philipp Bracher
30  * @version $Revision: 7335 $ ($Author: philipp $)
31  */

32 public class FactoryUtil {
33
34     public interface InstanceFactory {
35         public Object JavaDoc newInstance();
36     }
37
38     /**
39      * Logger.
40      */

41     protected static Logger log;// = LoggerFactory.getLogger(FactoryUtil.class);
42

43     protected static DiscoverClass discovery = new DiscoverClass();
44
45     /**
46      * Registered singleton instances
47      */

48     protected static Map JavaDoc instances = new HashMap JavaDoc();
49
50     /**
51      * Registered Prototypes used for new Instance
52      */

53     protected static Map JavaDoc factories = new HashMap JavaDoc();
54
55     private FactoryUtil() {
56
57     }
58
59     /**
60      * @deprecated use newInstance
61      */

62     public static Object JavaDoc getInstance(Class JavaDoc interf) {
63         return newInstance(interf);
64     }
65
66     public static Object JavaDoc newInstance(Class JavaDoc interf) {
67         try {
68             if (factories.containsKey(interf)) {
69                 return ((InstanceFactory) factories.get(interf)).newInstance();
70             }
71             // make interf the default implementation
72
return discovery.newInstance(interf, SystemProperty.getProperties(), interf.getName());
73         }
74         catch (Exception JavaDoc e) {
75             log.error("can't instantiate an implementation of this class [" + interf.getName() + "]");
76         }
77         return null;
78     }
79
80     /**
81      * @deprecated use newInstance
82      */

83     public static Object JavaDoc getInstanceWithoutDiscovery(String JavaDoc className, Object JavaDoc[] args) {
84         return newInstanceWithoutDiscovery(className, args);
85     }
86
87     /**
88      * This method does not use discovery! It is a util method for easy instantiating. In any case of an exception null is returned.
89      *
90      * @param className
91      * @return
92      */

93     public static Object JavaDoc newInstanceWithoutDiscovery(String JavaDoc className, Object JavaDoc[] args) {
94
95         if (StringUtils.isEmpty(className)) {
96             return null;
97         }
98
99         Class JavaDoc clazz;
100         try {
101             clazz = ClassUtil.classForName(className);
102         }
103         catch (ClassNotFoundException JavaDoc e) {
104             log.error("can't find class: " + className, e);
105             return null;
106         }
107         try {
108             return ConstructorUtils.invokeConstructor(clazz, args);
109         }
110         catch (Exception JavaDoc e) {
111             log.error("can't instantiate: " + className, e);
112         }
113         return null;
114     }
115
116     /**
117      * @deprecated use newInstance
118      */

119     public static Object JavaDoc getInstanceWithoutDiscovery(String JavaDoc className) {
120         return newInstanceWithoutDiscovery(className);
121     }
122
123     public static Object JavaDoc newInstanceWithoutDiscovery(String JavaDoc className) {
124         return newInstanceWithoutDiscovery(className, new Object JavaDoc[]{});
125     }
126
127     public static Object JavaDoc getSingleton(Class JavaDoc interf) {
128         Object JavaDoc instance = instances.get(interf);
129         if (instance == null) {
130             if (factories.containsKey(interf)) {
131                 instance = ((InstanceFactory) factories.get(interf)).newInstance();
132             } else {
133                 instance = DiscoverSingleton.find(interf, SystemProperty.getProperties(), interf.getName());
134             }
135             instances.put(interf, instance);
136         }
137         return instance;
138     }
139
140     public static void setDefaultImplementation(Class JavaDoc interf, Class JavaDoc impl) {
141         setDefaultImplementation(interf, impl.getName());
142     }
143
144     /**
145      * @param interf
146      * @param impl
147      */

148     public static void setDefaultImplementation(Class JavaDoc interf, String JavaDoc impl) {
149         if(!SystemProperty.getProperties().containsKey(interf.getName())){
150             setImplementation(interf, impl);
151         }
152     }
153     
154     public static void setImplementation(Class JavaDoc interf, Class JavaDoc impl) {
155         setDefaultImplementation(interf, impl.getName());
156     }
157
158     /**
159      * @param interf
160      * @param impl
161      */

162     public static void setImplementation(Class JavaDoc interf, String JavaDoc impl) {
163         SystemProperty.getProperties().setProperty(interf.getName(), impl);
164     }
165     
166
167     /**
168      * Register an instance which will be returned by getSingleton()
169      *
170      * @param interf
171      * @param instance
172      */

173     public static void setInstance(Class JavaDoc interf, Object JavaDoc instance) {
174         instances.put(interf, instance);
175     }
176
177     /**
178      * newInstance will use this prototype for cloning a new object
179      *
180      * @param interf
181      * @param factory
182      */

183     public static void setInstanceFactory(Class JavaDoc interf, InstanceFactory factory) {
184         factories.put(interf, factory);
185     }
186
187     public static void clear() {
188         factories.clear();
189         instances.clear();
190         DiscoverSingleton.release();
191     }
192
193 }
194
Popular Tags