KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > core > GenericIndexFactory


1 /*
2  * Created on 03-Apr-2004
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */

7 package com.jofti.core;
8
9 import java.util.Properties JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14
15 import com.jofti.btree.BTree;
16 import com.jofti.exception.JoftiException;
17 import com.jofti.introspect.ClassIntrospector;
18 import com.jofti.manager.IndexManagerImpl;
19 import com.jofti.tree.TreeIndex;
20 import com.jofti.util.ReflectionUtil;
21
22 /**
23
24  *
25  * Used to produce indexes. The IndexCache is not initialised when created.<p>
26  *
27  * @author xenephon (xenephon@jofti.com)
28  * @version 1.0
29  */

30 public class GenericIndexFactory {
31     
32     private static GenericIndexFactory factory = new GenericIndexFactory();
33     
34       
35      private static Log log = LogFactory.getLog(IndexManagerImpl.class);
36      
37     public static GenericIndexFactory getInstance(){
38         return factory;
39     }
40     
41     /**
42      * creates an Index of the type passed in IndexType.<p>
43      *
44      * @param indexType
45      * @param parser
46      * @param props
47      * @return The Index created by the factory
48      * @throws JoftiException
49      */

50     public InternalIndex createIndex(String JavaDoc indexType,
51             ClassIntrospector parser, Properties JavaDoc props, String JavaDoc name)
52             throws JoftiException {
53         InternalIndex index =null;
54         try {
55             Class JavaDoc indexClass = ReflectionUtil.classForName(indexType);
56
57             if (!(InternalIndex.class.isAssignableFrom(indexClass))) {
58                 throw new JoftiException("Class '" + indexClass
59                         + "' must implement Index interface ");
60             }
61         
62             index = (InternalIndex)indexClass.newInstance();
63             
64         } catch (InstantiationException JavaDoc e) {
65             throw new JoftiException("Problem instantiating Index", e);
66         } catch (ClassNotFoundException JavaDoc e) {
67             throw new JoftiException("Problem finding Index class", e);
68         } catch (IllegalAccessException JavaDoc e) {
69             throw new JoftiException("Problem instantiating Index", e);
70         } catch (Exception JavaDoc e) {
71             throw new JoftiException("Problem calling init() method on Index '"
72                     + indexType + "'", e);
73
74         }
75         IStoreManager manager =null;
76         try{
77             manager = configureOverflowManager(props,name);
78         } catch (InstantiationException JavaDoc e) {
79             throw new JoftiException("Problem instantiating disk-overflow provider " +props, e);
80         } catch (ClassNotFoundException JavaDoc e) {
81             throw new JoftiException("Problem instantiating disk-overflow provider "+props, e);
82         } catch (IllegalAccessException JavaDoc e) {
83             throw new JoftiException("Problem instantiating disk-overflow provider "+props, e);
84         } catch (Exception JavaDoc e) {
85             throw new JoftiException("Problem calling init() method on overflow provider '"
86                     + name + "'", e);
87
88         }
89             
90             
91             
92             
93             
94             if (index instanceof TreeIndex){
95
96                 BTree bTree = new BTree();
97                 ((TreeIndex)index).setTree(bTree);
98                 ((TreeIndex)index).setParser(parser);
99                 
100                 if (manager != null){
101                     bTree.setOverflowManager(manager);
102                 }
103                 
104             }
105             ((TreeIndex)index).init(props);
106
107             return index;
108         
109         
110         
111     }
112     
113     private IStoreManager configureOverflowManager(Properties JavaDoc props,String JavaDoc name) throws Exception JavaDoc{
114         
115         String JavaDoc providerString = props.getProperty("provider");
116         Class JavaDoc providerClass = null;
117         if (providerString != null){
118             providerClass =ReflectionUtil.classForName(providerString);
119         }
120         IStoreManager manager = null;
121         
122         if (providerClass != null){
123             manager = (IStoreManager) providerClass.newInstance();
124             
125             if (manager != null){
126                 manager.setName(name);
127                 manager.init(props);
128             }
129     
130         }
131         return manager;
132         
133     }
134     
135 }
136
Popular Tags