KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > manager > IndexManagerImpl


1 /*
2  * IndexManager.java
3  *
4  * Created on 01 June 2003, 08:17
5  */

6
7 package com.jofti.manager;
8
9 import java.io.InputStream JavaDoc;
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17
18 import com.jofti.api.Index;
19 import com.jofti.api.IndexManager;
20 import com.jofti.api.NameSpacedIndex;
21 import com.jofti.cache.LifeCycleAdapter;
22 import com.jofti.cache.NameSpacedCacheAdapter;
23 import com.jofti.config.ConfigFileParser;
24 import com.jofti.config.IndexConfig;
25 import com.jofti.core.GenericIndexFactory;
26 import com.jofti.core.InternalIndex;
27 import com.jofti.exception.JoftiException;
28 import com.jofti.introspect.ClassIntrospector;
29 import com.jofti.util.ReflectionUtil;
30
31 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
32
33 /**
34  *
35
36  *
37  *
38  * The manager is responsible for providing access to caches. This class is not a singleton
39  * and is not intended to be used in that manner.<p>
40  *
41  * The usage for the cache manager is:<p>
42  *
43  * IndexManager manager = new IndexManagerImpl();<br>
44  * manager.setConfigFile("configFile");<br>
45  * manager.init(); <br>
46  * IndexCache index = manager.getIndex("name");<br>
47  * <p>
48  * or:
49  * <p>
50  * IndexManager manager = new IndexManagerImpl();<br>
51  * manager.init(inputStream); <br>
52  * IndexCache index = manager.getIndex("name");<br>
53  * <p>
54  * or
55  * <p>
56  * IndexManager manager = new IndexManagerImpl();<br>
57  * manager.init(configFile); <br>
58  * IndexCache index = manager.getIndex("name");<br>
59  * <p>
60  * The name of the index is one of the indexs configured in the configFile.
61  * <p>
62  * NameSpaced caches such as JBossCache are obtained using:
63  * <p>
64  * IndexManager manager = new IndexManagerImpl();<br>
65  * manager.init(configFile); <br>
66  * NameSpacedIndex index = manager.getNameSpacedIndex("name");<br>
67  * <p>
68  * @author Steve Woodcock
69  * @version 1.0
70  *
71  */

72 public class IndexManagerImpl implements IndexManager
73 {
74
75     private static Log log = LogFactory.getLog(IndexManagerImpl.class);
76
77     private final Map JavaDoc cacheMap = new ConcurrentHashMap();
78
79     private final Map JavaDoc configMap = new ConcurrentHashMap();
80
81     private String JavaDoc configFile = null;
82
83     private boolean initialised = false;
84     
85
86     /** Creates a new instance of IndexManagerImpl */
87     public IndexManagerImpl()
88     {
89         
90     }
91
92     /**
93      * Sets a config file to use in the indexManager. This file must be on the
94      * classpath.
95      */

96     public void setConfigFile(String JavaDoc configFile)
97     {
98         this.configFile = configFile;
99     }
100
101     /**
102      *
103      * Initialise method that takes a config file name. This over-rides the
104      * fileName (if any) set in the setConfigFile() method.
105      *
106      * @param configFileName
107      * @throws JoftiException
108      */

109     public synchronized void init(String JavaDoc configFileName) throws JoftiException
110     {
111
112         if (!initialised) {
113
114             Map JavaDoc temp = null;
115             if (log.isInfoEnabled()) {
116                 log.info("Loading config from file '" + configFileName + "'");
117             }
118             temp = loadConfig(configFileName);
119             if (log.isInfoEnabled()) {
120                 log.info("Loaded config from file '" + configFileName + "'");
121             }
122             configureIndexes(temp);
123             initialised = true;
124         } else {
125             if (log.isWarnEnabled()) {
126                 log
127                         .warn("IndexCache is already initialised - ignoring repeat attempt using '"
128                                 + configFileName + "'");
129             }
130         }
131     }
132
133     /**
134      *
135      * Initialise method that no parameters. This configures the Indexes with the
136      * fileName set in the setConfigFile() method.
137      *
138      * @throws JoftiException
139      */

140     public synchronized void init() throws JoftiException
141     {
142
143         if (!initialised) {
144
145             Map JavaDoc temp = null;
146             if (configFile != null) {
147                 if (log.isInfoEnabled()) {
148                     log.info("Loading config from file '" + configFile + "'");
149                 }
150
151                 temp = loadConfig(configFile);
152
153                 if (log.isInfoEnabled()) {
154                     log.info("Loaded config from file '" + configFile + "'");
155                 }
156                 configureIndexes(temp);
157             } else {
158                 if (log.isInfoEnabled()) {
159                     log
160                             .info("No config file configured - not loading any caches");
161                 }
162             }
163             initialised = true;
164         } else {
165             if (log.isWarnEnabled()) {
166                 log
167                         .warn("IndexCache is already initialised - ignoring repeat attempt ");
168             }
169         }
170     }
171
172     /**
173      *
174      * Initialise method that takes an inputstream. This over-rides the fileName
175      * (if any) set in the setConfigFile() method.
176      *
177      * @param configStream
178      * @throws JoftiException
179      */

180     public synchronized void init(InputStream JavaDoc configStream)
181             throws JoftiException
182     {
183
184         if (!initialised) {
185            
186             Map JavaDoc temp = null;
187             if (log.isInfoEnabled()) {
188                 log.info("Loading config from stream");
189             }
190             temp = loadConfig(configStream);
191             if (log.isInfoEnabled()) {
192                 log.info("Loaded config from stream");
193             }
194             configureIndexes(temp);
195             initialised = true;
196         } else {
197             if (log.isWarnEnabled()) {
198                 log.warn("IndexCache is already initialised - ignoring repeat attempt ");
199             }
200         }
201     }
202
203     /**
204      * This method allows cache instances to be added to the cache
205      * programatically rather than at start-up. <p>
206      * The usage is for example:<p>
207      *
208      * DefaultIndexConfig config = new DefaultIndexConfig();<br>
209      * config.setName("test");<br>
210      * IndexCache index = (IndexCache)<br>
211      * manager.addIndexedCache(config, <cacheImpl>,
212      * <xml-filename-with-classes-in-it>);<br>
213      * <p>
214      * any type of cache can be added in this manner, providing a CacheAdapter
215      * exists for it and the correct adapter has been configured in the
216      * @link IndexConfig class.
217      * <p>
218      * If you are using a nameSpaced cache like JBossCache then the usage would
219      * be:
220      * <p>
221      * DefaultIndexConfig config = new DefaultIndexConfig();<br>
222      * config.setName("test"); <br>
223      * NameSpacedIndex index = (NameSpacedIndex)
224      * manager.addIndexedCache(config, <cacheImpl>, <xml-filename-with-classes-in-it>);<br>
225      * <p>
226      * Note: The cache implementations must be started correctly before they are
227      * passed into this method. Added caches are assumed to have been started
228      * and the manager will NOT attempt to initialise the actual cache
229      * implementation.
230      * <p>
231      *
232      * @param config -
233      * the config class containing definitions of the adapter, index
234      * type and parser to use.
235      * @param cache -
236      * the cache implementation. Passing NULL will result in the adapter creating a new
237      * cache instance.
238      * @param classesFileName -
239      * the xml file containing the classes definitions for the cache.
240      * This file must be available on the classpath.
241      * @return The added cache.
242      * @throws JoftiException
243      */

244     public Index addIndex(IndexConfig config, Object JavaDoc cache,
245             String JavaDoc classesFileName) throws JoftiException
246     {
247
248         config = parseClassesForConfig(config, classesFileName);
249         return createIndexedCache(config, cache);
250
251     }
252
253     /**
254      * This method allows cache instances to be added to the cache
255      * programatically rather than at start-up. This method will result in the
256      * adapter used creating a new instance of its cache type.<p>
257      *
258      *
259      *
260      * Any type of cache can be added in this manner, providing a CacheAdapter
261      * exists for it and the correct adapter has been configured in the
262      * @link IndexConfig class.
263      * <p>
264      * Note: This method is the equivalent of an index entry in the
265      * configuration file. The manager will atttempt to construct and initialise
266      * a new indexed cache based on the attributes in the cacheConfig class and
267      * the class definition file.
268      * <p>
269      * @param config -
270      * the config class containing definitions of the adapter, index
271      * type and parser to use.
272      * @param classesFileName -
273      * the xml file containing the classes definitions for the
274      * cache.This file must be available on the classpath.
275      * @return The added cache.
276      * @throws JoftiException
277      */

278     public Index addIndexCache(IndexConfig config,
279             String JavaDoc classesFileName) throws JoftiException
280     {
281
282         config = parseClassesForConfig(config, classesFileName);
283         return createIndexedCache(config, null);
284
285     }
286
287     /**
288      * This method allows cache instances to be added to the cache
289      * programatically rather than at start-up.
290      * <p>
291      *
292      *
293      * Any type of cache can be added in this manner, providing a CacheAdapter
294      * exists for it and the correct adapter has been configured in the
295      * @link IndexConfig class. This method will result in the
296      * adapter used creating a new instance of its cache type.
297      * <p>
298      * Note: This method is the equivalent of an index entry in the
299      * configuration file. The manager will atttempt to construct and initialise
300      * a new indexed cache based solely on the attributes in the cacheConfig
301      * class.
302      * <p>
303      * Class definitions can be added using attribute classMappings in the
304      * IndexConfig class. See this class for details on how to configure these.
305      * <p>
306      * @param config -
307      * the config class containing definitions of the adapter, index
308      * type and parser to use.
309      * @return The added cache.
310      * @throws JoftiException
311      */

312
313     public Index addIndexCache(IndexConfig config)
314             throws JoftiException
315     {
316
317         return createIndexedCache(config, null);
318
319     }
320
321     /**
322      * This method allows cache instances to be added to the cache
323      * programatically rather than at start-up. The usage is for example:
324      * <p>
325      * DefaultIndexConfig config = new DefaultIndexConfig();<br>
326      * config.setName("test"); <br>
327      * IndexCache index = (IndexCache)
328      * manager.addIndexedCache(config, <cacheImpl>,
329      * <xml-filename-with-classes-in-it>);<br>
330      * <p>
331      * any type of cache can be added in this manner, providing a CacheAdapter
332      * exists for it and the correct adapter has been configured in the
333      * @link IndexConfig class.
334      * <p>
335      * If you are using a nameSpaced cache like JBossCache then the usage would
336      * be:
337      * <p>
338      * DefaultIndexConfig config = new DefaultIndexConfig();<br>
339      * config.setName("test");<br>
340      * NameSpacedIndex index = (NameSpacedIndex)
341      * manager.addIndexedCache(config, <cacheImpl>);<br>
342      * <p>
343      * Note: The cache implementations must be started correctly before they are
344      * passed into this method. Added caches are assumed to have been started
345      * and the manager will NOT attempt to initialise the actual cache
346      * implementation.
347      * <p>
348      *
349      * @param config -
350      * the config class containing definitions of the adapter, index
351      * type and parser to use.
352      * @param cache -
353      * the cache implementation. Passing NULL will result in the adapter creating a new
354      * cache instance.
355      * @return The added cache.
356      * @throws JoftiException
357      */

358     public Index addIndex(IndexConfig config, Object JavaDoc cache)
359     throws JoftiException
360     {
361
362         return createIndexedCache(config, cache);
363
364     }
365     
366     
367     /* (non-Javadoc)
368      * @see com.jofti.api.IndexManager#addIndexedCache(com.jofti.config.IndexConfig, java.lang.Object, java.io.InputStream)
369      */

370     public Index addIndex(IndexConfig config, Object JavaDoc cache, InputStream JavaDoc stream)
371     throws JoftiException
372     {
373         config = parseClassesForConfig(config,stream);
374         return createIndexedCache(config, cache);
375
376     }
377     
378     
379     private IndexConfig parseClassesForConfig(IndexConfig config,
380             InputStream JavaDoc stream) throws JoftiException
381     {
382         if (log.isDebugEnabled()) {
383             log.info("parsing classes from inputStream ");
384         }
385         // load the classes to be indexed from the supplied file
386
config = loadConfig(config,stream);
387         
388
389         if (log.isDebugEnabled()) {
390             log.info("successfully parsed classes from inputStream ");
391         }
392         // return the config with the parsed classes in it.
393
return config;
394     }
395     /**
396      * This method does the actual work of loading in the class definitions from the file.
397      *
398      *
399      * @param config -
400      * the config class containing definitions of the adapter, index
401      * type and parser to use.
402      * @param classesFileName -
403      * the xml file containing the classes definitions for the
404      * cache.This file must be available on the classpath.
405      * @return The cacheConfig object with the class definitions added.
406      * @throws JoftiException
407      */

408     private IndexConfig parseClassesForConfig(IndexConfig config,
409             String JavaDoc fileName) throws JoftiException
410     {
411         if (log.isDebugEnabled()) {
412             log.info("parsing classes from file " + fileName);
413         }
414         // load the classes to be indexed from the supplied file
415
config = loadConfig(config, fileName);
416         
417         if (log.isDebugEnabled()) {
418             log.info("successfully parsed classes from file " + fileName);
419         }
420         // return the config with the parsed classes in it.
421
return config;
422     }
423
424   
425     /**
426      * This method does the actual work of creating the cache for the addIndexCache methods.
427      *
428      *
429      * @param config -
430      * the config class containing definitions of the adapter, index
431      * type and parser to use.
432      * @param cache -
433      * the cache implementation.
434      * @return The added cache.
435      * @throws JoftiException
436      */

437     private Index createIndexedCache(IndexConfig config, Object JavaDoc cache)
438             throws JoftiException
439     {
440
441         // load up the adaptor
442
if (log.isInfoEnabled()) {
443             log.info("loading cache '" + config.getName() + "' with adapter " + config.getCacheAdapter() );
444         }
445         LifeCycleAdapter cacheAdaptor = null;
446         
447         // if we do not have a supplied cache then the adapter will create one
448
if (cache == null) {
449             cacheAdaptor = loadAdaptor(config);
450         } else {
451             cacheAdaptor = loadAdaptor(config, cache);
452         }
453         
454         // set the name for the adapter - used as the key in the cacheMap
455
cacheAdaptor.setName(config.getName());
456         
457         if (cacheAdaptor.getName() == null){
458             throw new JoftiException("A cache must have a name to be inserted into the cache");
459         }
460         // we should now call init - it is up to the dapter to protect itself
461

462         cacheAdaptor.init(config.getAdapterProperties());
463         
464         if (log.isInfoEnabled()) {
465             log.info("Configuring cache '" + cacheAdaptor.getName() + "'");
466         }
467         
468         // create the wrapper for the cache
469
cacheAdaptor = configureIndexedCache(config, cacheAdaptor);
470         
471         if (log.isInfoEnabled()) {
472             log.info("Starting cache '" + cacheAdaptor.getName() + "'");
473         }
474         // start the wrapper to start the caches
475
cacheAdaptor.start();
476
477
478         
479         if (cacheMap.get(config.getName()) != null) {
480             log.warn("added cache '" + cacheAdaptor.getName() +
481                     "' will replace an already existing cache of the same name in the loaded cache mappings");
482         }
483         cacheMap.put(config.getName(), cacheAdaptor);
484         if (log.isInfoEnabled()) {
485             log.info("Added cache '" + cacheAdaptor.getName() + "' to loaded caches");
486         }
487         return (Index)cacheAdaptor;
488     }
489
490     
491     /**
492      * Configures each of the caches in turn from the config file used to initialise the manager.
493      *
494      * @param configuredMap
495      * @throws JoftiException
496      */

497     private void configureIndexes(Map JavaDoc configuredMap) throws JoftiException
498     {
499
500         configMap.putAll(configuredMap);
501
502         // we have loaded the map so lets now create our internal stuff
503
if (log.isInfoEnabled()) {
504             log.info("Initialising indexes and caches");
505         }
506         // loop through the map of config entries and try and inialise each one
507
for (Iterator JavaDoc it = configMap.entrySet().iterator(); it.hasNext();) {
508             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
509             IndexConfig config = (IndexConfig) entry.getValue();
510             LifeCycleAdapter adapter = null;
511
512             // see if cache is lazy loaded
513

514                 if (log.isInfoEnabled()) {
515                     log.info("instantiating adapter '" + config.getName() + "'");
516                 }
517                 adapter = instantiateAdapter(config);
518                 if (log.isInfoEnabled()) {
519                     log.info("instantiated adapter '" + config.getName() + "'");
520                 }
521            
522             // carry on and do the indexes even if we have no cache started
523
// configure each cache
524
if (log.isInfoEnabled()) {
525                 log.info("Configuring index for cache '" + config.getName()
526                         + "'");
527             }
528             // configure the wrapper
529
adapter = configureIndexedCache(config, adapter);
530             
531             //add the queries
532

533             // start the caches
534
adapter.start();
535             if (log.isInfoEnabled()) {
536                 log.info("IndexCache Configured for cache '" + config.getName()
537                         + "'");
538             }
539
540             cacheMap.put(config.getName(), adapter);
541         }
542
543     }
544
545   
546     private synchronized LifeCycleAdapter instantiateAdapter(IndexConfig config)
547             throws JoftiException
548     {
549         LifeCycleAdapter adapter = null;
550
551         if (config == null) {
552             return adapter;
553         } else {
554
555             // create the cache adapter
556
adapter = loadAdaptor(config);
557             adapter.setName(config.getName());
558             // now try and do the complicated stuff
559
if (log.isInfoEnabled()) {
560                 log.info("initialising adapter '" + config.getName() + "'");
561             }
562             try {
563                 // initialise the cache - we should pass in some properties here
564
adapter.init(config.getAdapterProperties());
565             } catch (Throwable JavaDoc e) {
566                 log.error("Unable to instantiate adapter for '"
567                         + config.getName() + "':" + e);
568                 try {
569                     if (adapter != null) {
570                         adapter.destroy();
571                     }
572                 } catch (Throwable JavaDoc t) {
573                     throw new JoftiException(t);
574                 }
575                 throw new JoftiException(e);
576             }
577             if (log.isInfoEnabled()) {
578                 log.info("initialised adapter '" + config.getName() + "'");
579             }
580         }
581         return adapter;
582     }
583
584     /**
585      * @param config
586      * @return
587      * @throws JoftiException
588      */

589
590     private LifeCycleAdapter loadAdaptor(IndexConfig config, Object JavaDoc cacheImpl)
591             throws JoftiException
592     {
593         LifeCycleAdapter cache;
594         if (log.isDebugEnabled()) {
595             log.debug("Creating adaptor " + config.getCacheAdapter() + " for '"
596                     + config.getName() + "'");
597         }
598
599         cache = (LifeCycleAdapter) createClass(config.getCacheAdapter(),
600                 cacheImpl);
601         if (log.isDebugEnabled()) {
602             log.debug("Created adaptor " + config.getCacheAdapter() + " for '"
603                     + config.getName() + "'");
604         }
605         return cache;
606     }
607
608     private LifeCycleAdapter loadAdaptor(IndexConfig config)
609             throws JoftiException
610     {
611         LifeCycleAdapter cache;
612         if (log.isDebugEnabled()) {
613             log.debug("Creating adaptor " + config.getCacheAdapter() + " for '"
614                     + config.getName() + "'");
615         }
616         cache = (LifeCycleAdapter) createClass(config.getCacheAdapter());
617         if (log.isDebugEnabled()) {
618             log.debug("Created adaptor " + config.getCacheAdapter() + " for '"
619                     + config.getName() + "'");
620         }
621         return cache;
622     }
623
624     private LifeCycleAdapter configureIndexedCache(IndexConfig config,
625             LifeCycleAdapter adapter) throws JoftiException
626     {
627
628
629         if (config == null) {
630             return null;
631         } else {
632
633             try {
634                 // create the parser
635
if (log.isDebugEnabled()) {
636                     log.debug("Creating parser " + config.getParserType()
637                             + " for '" + config.getName() + "'");
638                 }
639                 ClassIntrospector parser = (ClassIntrospector) createClass(config
640                         .getParserType());
641
642                 if (log.isDebugEnabled()) {
643                     log.debug("Created parser " + config.getParserType()
644                             + " for '" + config.getName() + "'");
645                 }
646
647                 //create the index
648
InternalIndex index = null;
649
650                 // construct the class/method map so we can do all the
651
// reflection stuff early
652
if (log.isDebugEnabled()) {
653                     log.debug("Parsing Config " + config.getIndexMappings()
654                             + " for '" + config.getName() + "'");
655                 }
656                 parser.parseConfig(config);
657                 
658                 if (log.isDebugEnabled()) {
659                     log.debug("Parsed Config for '" + config.getName() + "'");
660                 }
661                 
662                 if (log.isDebugEnabled()) {
663                     log.debug("Creating index " + config.getIndexType()
664                             + " for '" + config.getName() + "'");
665                 }
666                 
667                 index = GenericIndexFactory.getInstance().createIndex(
668                         config.getIndexType(), parser,
669                         config.getIndexProperties(),config.getName());
670                 
671                 if (log.isDebugEnabled()) {
672                     log.debug("Created index " + config.getIndexType()
673                             + " for '" + config.getName() + "'");
674                 }
675                 if (log.isDebugEnabled()) {
676                     log.debug("adding preconfigured queries " + config.getQueryMappings()
677                             + " for '" + config.getName() + "'");
678                 }
679                 
680                 for (Iterator JavaDoc it = config.getQueryMappings().entrySet().iterator();it.hasNext();){
681                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
682                     index.getParserManager().addQuery((String JavaDoc)entry.getKey(),(String JavaDoc)entry.getValue());
683                 }
684                 if (log.isDebugEnabled()) {
685                     log.debug("added preconfigured queries "
686                             + " for '" + config.getName() + "'");
687                 }
688                 adapter.setInternalIndex(index);
689
690             } catch (Exception JavaDoc e) {
691                 log.error("Unable to instantiate index for '"
692                         + config.getName() + "':" + e);
693                 // shut down the cache if we get an error configuring stuff
694
try {
695                     if (adapter != null) {
696                         adapter.destroy();
697
698                     }
699
700                 } catch (Throwable JavaDoc t) {
701                     throw new JoftiException(t);
702                 }
703                 throw new JoftiException(e);
704
705             }
706         }
707         return adapter;
708     }
709
710     private Object JavaDoc createClass(String JavaDoc className) throws JoftiException
711     {
712
713         Object JavaDoc obj = null;
714         try {
715             obj = ReflectionUtil.classForName(className).newInstance();
716         } catch (Exception JavaDoc e) {
717             throw new JoftiException(e);
718         }
719         return obj;
720
721     }
722
723     private Object JavaDoc createClass(String JavaDoc className, Object JavaDoc param)
724             throws JoftiException
725     {
726
727         Object JavaDoc obj = null;
728         try {
729             Class JavaDoc types[] = new Class JavaDoc[1];
730             types[0] = Object JavaDoc.class;
731             Class JavaDoc clazz = ReflectionUtil.classForName(className);
732             Constructor JavaDoc cons = clazz.getConstructor(types);
733             obj = cons.newInstance(new Object JavaDoc[] { param });
734         } catch (Throwable JavaDoc e) {
735             throw new JoftiException(e);
736         }
737         return obj;
738
739     }
740
741     /**
742      * Retrieves a cache from the manager. If the cache does not exist in the manager the
743      * method returns NULL, rather than throw an exception.
744      *
745      * Attempting to retrieve a name spaced cache using this method will result in an
746      * exception.
747      * @param indexName - the key name to retrive the cache. This set in the config as the cacheName.
748      * @return - the cache or NULL if no cache can be found under that name.
749      *
750      * @throws JoftiException
751      */

752     public Index getIndexCache(String JavaDoc indexName) throws JoftiException
753     {
754
755         if (!initialised) {
756             throw new JoftiException("IndexCache has not been initialised");
757         }
758        
759         LifeCycleAdapter adapter = (LifeCycleAdapter) cacheMap.get(indexName);
760
761         if (adapter == null) {
762             // check if lazily loaded
763
log.warn("No cache exists under the name "+indexName);
764         }
765         
766         return (Index)adapter;
767     }
768
769     /**
770      * Retrieves a name spaced indexed cache from the manager. If the cache does not exist in the manager the
771      * method returns NULL, rather than throw an exception.
772      *
773      * Attempting to retrieve a non-name spaced cache using this method will result in an
774      * exception.
775      * @param indexName - the key name to retrive the cache. This set in the config as the cacheName.
776      * @return - the cache or NULL if no cache can be found under that name.
777      *
778      * @throws JoftiException
779      */

780     public NameSpacedIndex getNameSpacedIndex(String JavaDoc indexName)
781             throws JoftiException
782     {
783
784         if (!initialised) {
785             throw new JoftiException("IndexCache has not been initialised");
786         }
787        
788         LifeCycleAdapter adapter = (LifeCycleAdapter) cacheMap.get(indexName);
789
790         if (adapter == null) {
791          
792                     log.warn("No cache exists under name " + indexName);
793            
794             if (!(adapter instanceof NameSpacedCacheAdapter)) {
795                 return null;
796             }
797
798         }
799         return (NameSpacedIndex) adapter;
800     }
801
802    
803
804     private synchronized Map JavaDoc loadConfig(String JavaDoc configfile)
805             throws JoftiException
806     {
807
808         ConfigFileParser confParser = new ConfigFileParser();
809         return confParser.parseIndexConfig(configfile);
810
811     }
812
813     private synchronized IndexConfig loadConfig(IndexConfig config,
814             String JavaDoc classesFileName) throws JoftiException
815     {
816
817         ConfigFileParser confParser = new ConfigFileParser();
818         return confParser.parseClassMapping(config, classesFileName);
819
820     }
821     
822     private synchronized IndexConfig loadConfig(IndexConfig config,
823             InputStream JavaDoc classes) throws JoftiException
824     {
825
826         ConfigFileParser confParser = new ConfigFileParser();
827         return confParser.parseClassMapping(config, classes);
828
829     }
830     private synchronized Map JavaDoc loadConfig(InputStream JavaDoc configStream)
831             throws JoftiException
832     {
833
834         ConfigFileParser confParser = new ConfigFileParser();
835         return confParser.parseIndexConfig(configStream);
836
837     }
838
839     
840     /**
841      * Used to shutdown and remove a cache from the manager. You should always use this method
842      * to remove a cache from the manager - as some cache implementations explicitly require a
843      * shutdown phase to be run before they can be removed.
844      *
845      * @param cache - the cache to be destroyed.
846      */

847     public synchronized void destroyIndex(Object JavaDoc cache)
848     {
849         if (cache instanceof LifeCycleAdapter) {
850             LifeCycleAdapter wrapper = (LifeCycleAdapter) cache;
851             removeCache(wrapper);
852         }
853     }
854
855     
856     protected synchronized void removeCache(LifeCycleAdapter wrapper)
857     {
858
859         if (wrapper != null) {
860             try {
861                 wrapper.destroy();
862             } catch (Exception JavaDoc e) {
863                 log.error(e);
864             } finally {
865                 cacheMap.remove(wrapper.getName());
866             }
867         }
868
869     }
870
871
872     /* (non-Javadoc)
873      * @see com.jofti.api.IndexManager#destroy()
874      */

875     public synchronized void destroy()
876     {
877         for(Iterator JavaDoc it = cacheMap.values().iterator();it.hasNext();){
878             LifeCycleAdapter adapter= (LifeCycleAdapter)it.next();
879             removeCache(adapter);
880         }
881         configMap.clear();
882         initialised =false;
883         
884     }
885     
886
887     /* (non-Javadoc)
888      * @see com.jofti.api.IndexManager#removeIndexedCache(java.lang.Object)
889      */

890     public synchronized Object JavaDoc removeIndex(Object JavaDoc cache)
891     {
892          if (cache instanceof LifeCycleAdapter) {
893             
894              Object JavaDoc obj = cacheMap.remove(((LifeCycleAdapter)cache).getName());
895              return obj;
896          }
897          return null;
898         
899     }
900
901 }
902
Popular Tags