class CrescentCollectionHandler {

	private void loadFromXML() {
		
		
		XStream stream = new XStream();
		stream.processAnnotations(CrescentCollections.class);
		stream.alias( "collections", CrescentCollections.class );
		stream.addImplicitCollection( CrescentCollections.class, "crescentCollections" );
		
		logger.info("collectionsXmlLocation : {}", collectionsXmlLocation);
		
		ResourceLoader resourceLoader = new ResourceLoader(collectionsXmlLocation);
		InputStream inputStream = resourceLoader.getInputStream();
		
		crescentCollections = (CrescentCollections)stream.fromXML(inputStream);
		
		if(crescentCollections == null) {
			String errorMsg = "Crescent Collections is not loaded from xml : ["+collectionsXmlLocation+"]";
			logger.error(errorMsg);
			
			throw new IllegalStateException(errorMsg);
		}
		
		// indexingDirectory가 절대경로가 아닌경우 임의로 경로 수정,  maven local profile에서 사용
		List<CrescentCollection> list = crescentCollections.getCrescentCollections();
		for (CrescentCollection collection : list) {
			String path = collection.getIndexingDirectory();
			File file = new File(path);
			if (!file.isAbsolute()) {
				String webRoot = System.getProperty("webapp.root");
				if (webRoot != null)
					collection.setIndexingDirectory(webRoot + path);
			}
		}
		
		try {
			inputStream.close();
		} catch (Exception e) {
			logger.error("stream close error ; ", e);
		}
		
		//Analyzer 생성
		for (CrescentCollection collection : list) {
			List<CrescentAnalyzerHolder> analyzerHolders = collection.getAnalyzers();
			
			for(CrescentAnalyzerHolder analyzerHolder : analyzerHolders) {
				String type = analyzerHolder.getType();
				String className = analyzerHolder.getClassName();
				String constructorArgs = analyzerHolder.getConstructorArgs();
				
				Analyzer analyzer = null;
				
				try {
					@SuppressWarnings("unchecked")
					Class<Analyzer> analyzerClass = (Class<Analyzer>) Class.forName(className);	
					
					if(constructorArgs == null || constructorArgs.trim().length() == 0) {
						
						analyzer = analyzerClass.newInstance();
						
					} else if ("true".equals(constructorArgs.toLowerCase()) || "false".equals(constructorArgs.toLowerCase())) {
					
						boolean booleanStr = Boolean.valueOf(constructorArgs);
						Class<?>[] intArgsClass = new Class<?>[] {boolean.class};
						Object[] intArgs = new Object[] {booleanStr};
						
						Constructor<Analyzer> intArgsConstructor = analyzerClass.getConstructor(intArgsClass);
						analyzer = (Analyzer) intArgsConstructor.newInstance(intArgs);
					
					} else {
						
						Class<?> initArgClass = Class.forName(constructorArgs);
						
						Class<?>[] intArgsClass = new Class<?>[] {initArgClass.getClass()};
						Object[] intArgs = new Object[] {initArgClass.newInstance()};
						
						Constructor<Analyzer> intArgsConstructor = analyzerClass.getConstructor(intArgsClass);
						analyzer = (Analyzer) intArgsConstructor.newInstance(intArgs);
					}
					
					if("indexing".equals(type)) {

						collection.setIndexingModeAnalyzer(analyzer);
					
					} else if("search".equals(type)) {
					
						collection.setSearchModeAnalyzer(analyzer);
						
					} else {
						throw new IllegalStateException("정의되지 않은 Analyzer type 입니다. ["+type+"]");
					}
				
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}