com.alogic.idnote.IdNoteFactory Maven / Gradle / Ivy
package com.alogic.idnote;
import java.io.InputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.alogic.idnote.naming.Inner;
import com.alogic.naming.Context;
import com.alogic.naming.Naming;
import com.anysoft.util.Factory;
import com.anysoft.util.IOTools;
import com.anysoft.util.Properties;
import com.anysoft.util.Settings;
import com.anysoft.util.XmlTools;
import com.anysoft.util.resource.ResourceFactory;
/**
* 工厂类
* @author yyduan
* @since 1.6.12.13 [20181213 duanyy]
*/
public class IdNoteFactory extends Naming{
/**
* 缺省配置文件
*/
protected static final String DEFAULT
= "java:///com/alogic/idnote/default.xml#" + IdNoteFactory.class.getName();
/**
* 唯一实例
*/
protected static IdNoteFactory theInstance = null;
@Override
protected Context newInstance(Element e, Properties p,
String attrName) {
Factory> f = new Factory>();
return f.newInstance(e,p,attrName,Inner.class.getName());
}
@Override
protected String getContextName(){
return "context";
}
/**
* 根据id获取Store
* @param id Store的id
* @return Store实例
*/
public static IdNoteGroup get(String id){
IdNoteFactory src = IdNoteFactory.get();
return src.lookup(id);
}
/**
* 获取唯一实例
* @return 唯一实例
*/
public static IdNoteFactory get(){
if (theInstance != null){
return theInstance;
}
synchronized (IdNoteFactory.class){
if (theInstance == null){
theInstance = (IdNoteFactory)newInstance(Settings.get(), new IdNoteFactory());
}
}
return theInstance;
}
public static Context newInstance(Element doc,Properties p){
if (doc == null) return null;
Factory> f = new Factory>();
return f.newInstance(doc, p);
}
protected static Context newInstance(Properties p,Context instance){
String configFile = p.GetValue("idnote.master",DEFAULT);
String secondaryFile = p.GetValue("idnote.secondary", DEFAULT);
ResourceFactory rm = Settings.getResourceFactory();
InputStream in = null;
try {
in = rm.load(configFile,secondaryFile, null);
Document doc = XmlTools.loadFromInputStream(in);
if (doc != null){
if (instance == null){
return newInstance(doc.getDocumentElement(),p);
}else{
instance.configure(doc.getDocumentElement(), p);
return instance;
}
}
} catch (Exception ex){
LOG.error("Error occurs when load xml file,source=" + configFile, ex);
}finally {
IOTools.closeStream(in);
}
return null;
}
}