001: /*
002: * Created on 03-Apr-2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package com.jofti.core;
008:
009: import java.util.Properties;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013:
014: import com.jofti.btree.BTree;
015: import com.jofti.exception.JoftiException;
016: import com.jofti.introspect.ClassIntrospector;
017: import com.jofti.manager.IndexManagerImpl;
018: import com.jofti.tree.TreeIndex;
019: import com.jofti.util.ReflectionUtil;
020:
021: /**
022:
023: *
024: * Used to produce indexes. The IndexCache is not initialised when created.<p>
025: *
026: * @author xenephon (xenephon@jofti.com)
027: * @version 1.0
028: */
029: public class GenericIndexFactory {
030:
031: private static GenericIndexFactory factory = new GenericIndexFactory();
032:
033: private static Log log = LogFactory.getLog(IndexManagerImpl.class);
034:
035: public static GenericIndexFactory getInstance() {
036: return factory;
037: }
038:
039: /**
040: * creates an Index of the type passed in IndexType.<p>
041: *
042: * @param indexType
043: * @param parser
044: * @param props
045: * @return The Index created by the factory
046: * @throws JoftiException
047: */
048: public InternalIndex createIndex(String indexType,
049: ClassIntrospector parser, Properties props, String name)
050: throws JoftiException {
051: InternalIndex index = null;
052: try {
053: Class indexClass = ReflectionUtil.classForName(indexType);
054:
055: if (!(InternalIndex.class.isAssignableFrom(indexClass))) {
056: throw new JoftiException("Class '" + indexClass
057: + "' must implement Index interface ");
058: }
059:
060: index = (InternalIndex) indexClass.newInstance();
061:
062: } catch (InstantiationException e) {
063: throw new JoftiException("Problem instantiating Index", e);
064: } catch (ClassNotFoundException e) {
065: throw new JoftiException("Problem finding Index class", e);
066: } catch (IllegalAccessException e) {
067: throw new JoftiException("Problem instantiating Index", e);
068: } catch (Exception e) {
069: throw new JoftiException(
070: "Problem calling init() method on Index '"
071: + indexType + "'", e);
072:
073: }
074: IStoreManager manager = null;
075: try {
076: manager = configureOverflowManager(props, name);
077: } catch (InstantiationException e) {
078: throw new JoftiException(
079: "Problem instantiating disk-overflow provider "
080: + props, e);
081: } catch (ClassNotFoundException e) {
082: throw new JoftiException(
083: "Problem instantiating disk-overflow provider "
084: + props, e);
085: } catch (IllegalAccessException e) {
086: throw new JoftiException(
087: "Problem instantiating disk-overflow provider "
088: + props, e);
089: } catch (Exception e) {
090: throw new JoftiException(
091: "Problem calling init() method on overflow provider '"
092: + name + "'", e);
093:
094: }
095:
096: if (index instanceof TreeIndex) {
097:
098: BTree bTree = new BTree();
099: ((TreeIndex) index).setTree(bTree);
100: ((TreeIndex) index).setParser(parser);
101:
102: if (manager != null) {
103: bTree.setOverflowManager(manager);
104: }
105:
106: }
107: ((TreeIndex) index).init(props);
108:
109: return index;
110:
111: }
112:
113: private IStoreManager configureOverflowManager(Properties props,
114: String name) throws Exception {
115:
116: String providerString = props.getProperty("provider");
117: Class providerClass = null;
118: if (providerString != null) {
119: providerClass = ReflectionUtil.classForName(providerString);
120: }
121: IStoreManager manager = null;
122:
123: if (providerClass != null) {
124: manager = (IStoreManager) providerClass.newInstance();
125:
126: if (manager != null) {
127: manager.setName(name);
128: manager.init(props);
129: }
130:
131: }
132: return manager;
133:
134: }
135:
136: }
|