1 6 7 package com.jofti.manager; 8 9 import java.io.InputStream ; 10 import java.lang.reflect.Constructor ; 11 import java.util.Iterator ; 12 import java.util.Map ; 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 72 public class IndexManagerImpl implements IndexManager 73 { 74 75 private static Log log = LogFactory.getLog(IndexManagerImpl.class); 76 77 private final Map cacheMap = new ConcurrentHashMap(); 78 79 private final Map configMap = new ConcurrentHashMap(); 80 81 private String configFile = null; 82 83 private boolean initialised = false; 84 85 86 87 public IndexManagerImpl() 88 { 89 90 } 91 92 96 public void setConfigFile(String configFile) 97 { 98 this.configFile = configFile; 99 } 100 101 109 public synchronized void init(String configFileName) throws JoftiException 110 { 111 112 if (!initialised) { 113 114 Map 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 140 public synchronized void init() throws JoftiException 141 { 142 143 if (!initialised) { 144 145 Map 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 180 public synchronized void init(InputStream configStream) 181 throws JoftiException 182 { 183 184 if (!initialised) { 185 186 Map 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 244 public Index addIndex(IndexConfig config, Object cache, 245 String classesFileName) throws JoftiException 246 { 247 248 config = parseClassesForConfig(config, classesFileName); 249 return createIndexedCache(config, cache); 250 251 } 252 253 278 public Index addIndexCache(IndexConfig config, 279 String classesFileName) throws JoftiException 280 { 281 282 config = parseClassesForConfig(config, classesFileName); 283 return createIndexedCache(config, null); 284 285 } 286 287 312 313 public Index addIndexCache(IndexConfig config) 314 throws JoftiException 315 { 316 317 return createIndexedCache(config, null); 318 319 } 320 321 358 public Index addIndex(IndexConfig config, Object cache) 359 throws JoftiException 360 { 361 362 return createIndexedCache(config, cache); 363 364 } 365 366 367 370 public Index addIndex(IndexConfig config, Object cache, InputStream 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 stream) throws JoftiException 381 { 382 if (log.isDebugEnabled()) { 383 log.info("parsing classes from inputStream "); 384 } 385 config = loadConfig(config,stream); 387 388 389 if (log.isDebugEnabled()) { 390 log.info("successfully parsed classes from inputStream "); 391 } 392 return config; 394 } 395 408 private IndexConfig parseClassesForConfig(IndexConfig config, 409 String fileName) throws JoftiException 410 { 411 if (log.isDebugEnabled()) { 412 log.info("parsing classes from file " + fileName); 413 } 414 config = loadConfig(config, fileName); 416 417 if (log.isDebugEnabled()) { 418 log.info("successfully parsed classes from file " + fileName); 419 } 420 return config; 422 } 423 424 425 437 private Index createIndexedCache(IndexConfig config, Object cache) 438 throws JoftiException 439 { 440 441 if (log.isInfoEnabled()) { 443 log.info("loading cache '" + config.getName() + "' with adapter " + config.getCacheAdapter() ); 444 } 445 LifeCycleAdapter cacheAdaptor = null; 446 447 if (cache == null) { 449 cacheAdaptor = loadAdaptor(config); 450 } else { 451 cacheAdaptor = loadAdaptor(config, cache); 452 } 453 454 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 462 cacheAdaptor.init(config.getAdapterProperties()); 463 464 if (log.isInfoEnabled()) { 465 log.info("Configuring cache '" + cacheAdaptor.getName() + "'"); 466 } 467 468 cacheAdaptor = configureIndexedCache(config, cacheAdaptor); 470 471 if (log.isInfoEnabled()) { 472 log.info("Starting cache '" + cacheAdaptor.getName() + "'"); 473 } 474 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 497 private void configureIndexes(Map configuredMap) throws JoftiException 498 { 499 500 configMap.putAll(configuredMap); 501 502 if (log.isInfoEnabled()) { 504 log.info("Initialising indexes and caches"); 505 } 506 for (Iterator it = configMap.entrySet().iterator(); it.hasNext();) { 508 Map.Entry entry = (Map.Entry ) it.next(); 509 IndexConfig config = (IndexConfig) entry.getValue(); 510 LifeCycleAdapter adapter = null; 511 512 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 if (log.isInfoEnabled()) { 525 log.info("Configuring index for cache '" + config.getName() 526 + "'"); 527 } 528 adapter = configureIndexedCache(config, adapter); 530 531 533 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 adapter = loadAdaptor(config); 557 adapter.setName(config.getName()); 558 if (log.isInfoEnabled()) { 560 log.info("initialising adapter '" + config.getName() + "'"); 561 } 562 try { 563 adapter.init(config.getAdapterProperties()); 565 } catch (Throwable 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 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 589 590 private LifeCycleAdapter loadAdaptor(IndexConfig config, Object 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 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 InternalIndex index = null; 649 650 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 it = config.getQueryMappings().entrySet().iterator();it.hasNext();){ 681 Map.Entry entry = (Map.Entry )it.next(); 682 index.getParserManager().addQuery((String )entry.getKey(),(String )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 e) { 691 log.error("Unable to instantiate index for '" 692 + config.getName() + "':" + e); 693 try { 695 if (adapter != null) { 696 adapter.destroy(); 697 698 } 699 700 } catch (Throwable t) { 701 throw new JoftiException(t); 702 } 703 throw new JoftiException(e); 704 705 } 706 } 707 return adapter; 708 } 709 710 private Object createClass(String className) throws JoftiException 711 { 712 713 Object obj = null; 714 try { 715 obj = ReflectionUtil.classForName(className).newInstance(); 716 } catch (Exception e) { 717 throw new JoftiException(e); 718 } 719 return obj; 720 721 } 722 723 private Object createClass(String className, Object param) 724 throws JoftiException 725 { 726 727 Object obj = null; 728 try { 729 Class types[] = new Class [1]; 730 types[0] = Object .class; 731 Class clazz = ReflectionUtil.classForName(className); 732 Constructor cons = clazz.getConstructor(types); 733 obj = cons.newInstance(new Object [] { param }); 734 } catch (Throwable e) { 735 throw new JoftiException(e); 736 } 737 return obj; 738 739 } 740 741 752 public Index getIndexCache(String 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 log.warn("No cache exists under the name "+indexName); 764 } 765 766 return (Index)adapter; 767 } 768 769 780 public NameSpacedIndex getNameSpacedIndex(String 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 loadConfig(String 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 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 classes) throws JoftiException 824 { 825 826 ConfigFileParser confParser = new ConfigFileParser(); 827 return confParser.parseClassMapping(config, classes); 828 829 } 830 private synchronized Map loadConfig(InputStream configStream) 831 throws JoftiException 832 { 833 834 ConfigFileParser confParser = new ConfigFileParser(); 835 return confParser.parseIndexConfig(configStream); 836 837 } 838 839 840 847 public synchronized void destroyIndex(Object 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 e) { 863 log.error(e); 864 } finally { 865 cacheMap.remove(wrapper.getName()); 866 } 867 } 868 869 } 870 871 872 875 public synchronized void destroy() 876 { 877 for(Iterator 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 890 public synchronized Object removeIndex(Object cache) 891 { 892 if (cache instanceof LifeCycleAdapter) { 893 894 Object obj = cacheMap.remove(((LifeCycleAdapter)cache).getName()); 895 return obj; 896 } 897 return null; 898 899 } 900 901 } 902 | Popular Tags |