1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jetspeed.search.handlers;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.jetspeed.search.HandlerFactory;
24 import org.apache.jetspeed.search.ObjectHandler;
25
26 /***
27 * Search object handler factory
28 *
29 * @author <a href="mailto: morciuch@apache.org">Mark Orciuch</a>
30 * @author <a href="mailto: jford@apache.org">Jeremy Ford</a>
31 *
32 * @version $Id: HandlerFactoryImpl.java 516448 2007-03-09 16:25:47Z ate $
33 */
34 public class HandlerFactoryImpl implements HandlerFactory
35 {
36 private final Map handlerCache = new HashMap();
37 private Map classNameMapping = new HashMap();
38
39 public HandlerFactoryImpl(Map classNameMapping)
40 {
41 this.classNameMapping = classNameMapping;
42 }
43
44 public void addClassNameMapping(String className, String handlerClassName)
45 {
46 classNameMapping.put(className, handlerClassName);
47 }
48
49 /***
50 * Returns parsed object handler for specific object
51 *
52 * @param obj
53 * @return
54 */
55 public ObjectHandler getHandler(Object obj) throws Exception
56 {
57 return getHandler(obj.getClass().getName());
58
59 }
60
61 /***
62 * Returns parsed object handler for specific object
63 *
64 * @param obj
65 * @return
66 */
67 public ObjectHandler getHandler(String className) throws Exception
68 {
69 ObjectHandler handler = null;
70
71 if(handlerCache.containsKey(className))
72 {
73 handler = (ObjectHandler)handlerCache.get(className);
74 }
75 else
76 {
77 String handlerClass = (String) classNameMapping.get(className);
78
79 if (handlerClass == null)
80 {
81 throw new Exception("No handler was found for document type: " + className);
82 }
83
84
85
86
87 handler = (ObjectHandler)Class.forName(handlerClass).newInstance();
88 handlerCache.put(className, handler);
89 }
90
91
92 return handler;
93 }
94 }