KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > search > HandlerFactory


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.services.search;
18
19 // Turbine APIs
20
import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.turbine.services.resources.ResourceService;
25 import org.apache.turbine.services.TurbineServices;
26
27 /**
28  * Search object handler factory
29  *
30  * @author <a HREF="morciuch@apache.org">Mark Orciuch</a>
31  *
32  * @version $Id: HandlerFactory.java,v 1.4 2004/02/23 03:48:47 jford Exp $
33  */

34 public class HandlerFactory
35 {
36     private static final Map JavaDoc handlerCache = Collections.synchronizedMap(new HashMap JavaDoc());
37     
38     /**
39      * Returns parsed object handler for specific object
40      *
41      * @param obj
42      * @return
43      */

44     public static ObjectHandler getHandler(Object JavaDoc obj) throws Exception JavaDoc
45     {
46         return getHandler(obj.getClass().getName());
47
48     }
49     
50     /**
51     * Returns parsed object handler for specific object
52     *
53     * @param obj
54     * @return
55     */

56     public static ObjectHandler getHandler(String JavaDoc className) throws Exception JavaDoc
57     {
58         ObjectHandler handler = null;
59         
60         if(handlerCache.containsKey(className))
61         {
62             handler = (ObjectHandler)handlerCache.get(className);
63         }
64         else
65         {
66             ResourceService serviceConf = ((TurbineServices) TurbineServices.getInstance())
67                                               .getResources(SearchService.SERVICE_NAME);
68             String JavaDoc handlerClass = serviceConf.getString("document." + className);
69     
70             if (handlerClass == null)
71             {
72                 throw new Exception JavaDoc("No handler was found for document type: " + className);
73             }
74     
75             handler = (ObjectHandler) Class.forName(handlerClass).newInstance();
76             handlerCache.put(className, handler);
77         }
78         //System.out.println("HandlerFactory: returning handler " + handler + " for " + obj);
79

80         return handler;
81     }
82 }
83
Popular Tags