KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > LoadersManager


1 package jodd.bean;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import jodd.bean.loaders.MapLoader;
8 import jodd.bean.loaders.MultipartRequestLoader;
9 import jodd.bean.loaders.RequestLoader;
10 import jodd.bean.loaders.ResultSetLoader;
11
12 /**
13  * Bean loaders manager.
14  * <p>
15  *
16  * A bean loader is a class that "knows" how to populate a java bean from
17  * given object. For example, the <code>Map</code> bean loader iterates given
18  * <code>Map</code>, reads items and key/values pairs and sets beans property
19  * that is named as items key with value of items value.
20  * <p>
21  *
22  * This class registers few default loaders. Most of default loaders are made
23  * for j2sdk classes. However, there are loaders that are made for j2ee
24  * classes, that may not exists on client system. Therefore, during the
25  * registration of default classes, <code>Class.forName</code> is used for
26  * checking if certain class exists.
27  */

28 public class LoadersManager {
29
30     // ---------------------------------------------------------------- manager
31

32     private static HashMap loaders = new HashMap();
33
34     /**
35      * Returns the Map where all loaders are registered.
36      *
37      * @return loaders Map
38      */

39     public static Map getMap() {
40         return loaders;
41     }
42
43     static {
44         registerDefaults();
45     }
46
47     /**
48      * Registers default set of loaders.
49      * <p>
50      *
51      * Important note: class that doesn'tcome with JDK is first being examined
52      * for existence. Examination is done with <code>Class.forName()</code>
53      * If class exists, it will be registered. If not, it will be skipped.
54      *
55      * @see #register
56      */

57     public static void registerDefaults() {
58         loaders.clear();
59         register(java.util.Map.class, new MapLoader());
60         register(java.sql.ResultSet.class, new ResultSetLoader());
61         try {
62             Class.forName("javax.servlet.http.HttpServletRequest");
63             register(javax.servlet.http.HttpServletRequest.class, new RequestLoader());
64             register(jodd.servlet.MultipartRequest.class, new MultipartRequestLoader());
65         } catch (ClassNotFoundException cnfex) {
66         }
67     }
68
69     /**
70      * Registers a loader for an objects of specific type.
71      *
72      * @param key type of object that will be used by loader to populate bean.
73      * @param load loader object that populates a bean.
74      *
75      * @see #registerDefaults
76      */

77     public static void register(Class key, Loader load) {
78         loaders.put(key, load);
79     }
80
81
82     /**
83      * Returns loader for the specific object type.
84      *
85      * @param key type of object that will be used by loader to populate bean.
86      *
87      * @return loader for objects of specific type, <code>null</code> if no loader found.
88      * @see #get(Class key)
89      */

90     public static Loader get(Class key) {
91         return (Loader) loaders.get(key);
92     }
93
94     // ---------------------------------------------------------------- loader
95

96     /**
97      * Performs more throughly search for bean loader. It examines all available
98      * loaders and returns the first that matches the object.
99      *
100      * @param obj
101      *
102      * @return loader
103      */

104     public static Loader get(Object obj) {
105         Class objclass = obj.getClass();
106         Loader load = LoadersManager.get(objclass);
107         if (load == null) { // class not found, scan for instanceof matching
108
Iterator it = LoadersManager.getMap().keySet().iterator();
109             while (it.hasNext()) {
110                 Class key = (Class) it.next();
111                 if (key.isInstance(obj)) {
112                     load = LoadersManager.get(key);
113                     break;
114                 }
115             }
116         }
117         return load;
118     }
119
120
121
122
123
124
125 }
126
Popular Tags