View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  {
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              //ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
85              
86              //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);
91  
92          return handler;
93      }
94  }