1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */1718packageorg.apache.jetspeed.search.handlers;
1920import java.util.HashMap;
21import java.util.Map;
2223import org.apache.jetspeed.search.HandlerFactory;
24import org.apache.jetspeed.search.ObjectHandler;
2526/***27 * Search object handler factory28 *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 */34publicclassHandlerFactoryImpl implements HandlerFactory
35 {
36privatefinal Map handlerCache = new HashMap();
37private Map classNameMapping = new HashMap();
3839publicHandlerFactoryImpl(Map classNameMapping)
40 {
41this.classNameMapping = classNameMapping;
42 }
4344publicvoid addClassNameMapping(String className, String handlerClassName)
45 {
46 classNameMapping.put(className, handlerClassName);
47 }
4849/***50 * Returns parsed object handler for specific object51 * 52 * @param obj53 * @return 54 */55public ObjectHandler getHandler(Object obj) throws Exception
56 {
57return getHandler(obj.getClass().getName());
5859 }
6061/***62 * Returns parsed object handler for specific object63 * 64 * @param obj65 * @return 66 */67public ObjectHandler getHandler(String className) throws Exception
68 {
69 ObjectHandler handler = null;
7071if(handlerCache.containsKey(className))
72 {
73 handler = (ObjectHandler)handlerCache.get(className);
74 }
75else76 {
77 String handlerClass = (String) classNameMapping.get(className);
7879if (handlerClass == null)
80 {
81thrownew Exception("No handler was found for document type: " + className);
82 }
8384//ClassLoader classLoader = Thread.currentThread().getContextClassLoader();8586//handler = (ObjectHandler) classLoader.loadClass(handlerClass).newInstance();87 handler = (ObjectHandler)Class.forName(handlerClass).newInstance();
88 handlerCache.put(className, handler);
89 }
90//System.out.println("HandlerFactory: returning handler " + handler + " for " + obj);9192return handler;
93 }
94 }