01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: private final Map handlerCache = new HashMap();
36: private Map classNameMapping = new HashMap();
37:
38: public HandlerFactoryImpl(Map classNameMapping) {
39: this .classNameMapping = classNameMapping;
40: }
41:
42: public void addClassNameMapping(String className,
43: String handlerClassName) {
44: classNameMapping.put(className, handlerClassName);
45: }
46:
47: /**
48: * Returns parsed object handler for specific object
49: *
50: * @param obj
51: * @return
52: */
53: public ObjectHandler getHandler(Object obj) throws Exception {
54: return getHandler(obj.getClass().getName());
55:
56: }
57:
58: /**
59: * Returns parsed object handler for specific object
60: *
61: * @param obj
62: * @return
63: */
64: public ObjectHandler getHandler(String className) throws Exception {
65: ObjectHandler handler = null;
66:
67: if (handlerCache.containsKey(className)) {
68: handler = (ObjectHandler) handlerCache.get(className);
69: } else {
70: String handlerClass = (String) classNameMapping
71: .get(className);
72:
73: if (handlerClass == null) {
74: throw new Exception(
75: "No handler was found for document type: "
76: + className);
77: }
78:
79: //ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
80:
81: //handler = (ObjectHandler) classLoader.loadClass(handlerClass).newInstance();
82: handler = (ObjectHandler) Class.forName(handlerClass)
83: .newInstance();
84: handlerCache.put(className, handler);
85: }
86: //System.out.println("HandlerFactory: returning handler " + handler + " for " + obj);
87:
88: return handler;
89: }
90: }
|