📄 defaultminicontainer.java
字号:
/**
* Pxb IOC.
* Copyright 2008 Panxiaobo.
* All rights reserved.
* $Id: DefaultMiniContainer.java 40 2008-08-18 11:16:55Z Panxiaobo $
*/
package pxb.ioc.impl;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import pxb.ioc.ImplementedBy;
import pxb.ioc.Inject;
import pxb.ioc.LifeCycle;
import pxb.ioc.MiniContainer;
/**
* @author Panxiaobo [pxb1988@126.com]
*/
public class DefaultMiniContainer implements MiniContainer {
/**
*
*/
private static final long serialVersionUID = 1L;
private ClassLoader classLoader = this.getClass().getClassLoader();
private String configFile = null;
private Properties contents = new Properties();
private Map<Class<?>, Object> injected = new HashMap<Class<?>, Object>();
private Set<LifeCycle> lifeCycles = new HashSet<LifeCycle>();
private Logger log = Logger.getLogger(DefaultMiniContainer.class.getName());
private Map<Class<?>, Class<?>> toInject = new HashMap<Class<?>, Class<?>>();
public DefaultMiniContainer() {
this.addClassMap(ClassLoader.class, classLoader);
this.addClassMap(MiniContainer.class, this);
}
public <T> void addClassMap(Class<T> a, Class<T> b) {
this.toInject.put(a, b);
}
public <T> void addClassMap(Class<T> type, T t) {
this.injected.put(type, t);
}
public void clear() {
this.classLoader = null;
this.configFile = null;
contents.clear();
this.contents = null;
this.injected.clear();
this.injected = null;
this.toInject.clear();
this.toInject = null;
log.info("Run System.gc()");
System.gc();
}
/*
* (non-Javadoc)
*
* @see pxb.ioc.LifeCycle#destroy()
*/
public void destroy() throws Exception {
for (LifeCycle lc : lifeCycles) {
lc.destroy();
}
this.lifeCycles.clear();
this.lifeCycles = null;
}
@SuppressWarnings("unchecked")
private synchronized <T> T doInstance(Class<?> type) throws Exception {
Object o = this.injected.get(type);
if (o != null)
return (T) o;
Class b = toInject.get(type);
if (b == null) {
try {
ImplementedBy implementedBy = type
.getAnnotation(ImplementedBy.class);
if (implementedBy != null) {
Class impl = classLoader.loadClass(implementedBy.value());
o = impl.newInstance();
}
} catch (Exception e) {
}
if (o == null) {
try {
o = type.newInstance();
} catch (Exception e) {
return null;
}
}
} else
o = b.newInstance();
injected.put(type, o);
this.inject(o);
if (o instanceof LifeCycle) {
LifeCycle lc = (LifeCycle) o;
lc.init();
this.lifeCycles.add(lc);
}
return (T) o;
}
/*
* (non-Javadoc)
*
* @see pxb.ioc.InnerContainer#getProperty(java.lang.String)
*/
public String getProperty(String key) {
return this.contents.getProperty(key);
}
public synchronized void init() {
this.loadConfig();
this.loadClassMap();
this.loadModule();
}
@SuppressWarnings("unchecked")
public <T> T inject(Class<T> type) throws Exception {
Object o = this.injected.get(type);
if (o != null)
return (T) o;
return this.doInstance(type);
}
/*
* (non-Javadoc)
*
* @see pxb.ioc.InnerContainer#inject(java.lang.Object)
*/
@SuppressWarnings("unchecked")
public <T> T inject(T t) throws Exception {
if (t == null)
return null;
Class<? extends Object> type = t.getClass();
Method methods[] = type.getMethods();
for (Method method : methods) {
Inject inject = method.getAnnotation(Inject.class);
if (inject != null) {
if (!Modifier.isPublic(method.getModifiers())) {
log.info("Method:[" + method + "] Not Public");
continue;
}
Class[] types = method.getParameterTypes();
Object parameters[] = new Object[types.length];
if (types.length == 1 && String.class.equals(types[0])) {
String key = inject.value();
String value = contents.getProperty(key);
if (value == null) {
log.info("Can't Inject Method:[" + method
+ "] type:String,name:" + key);
value = "";
}
parameters[0] = value;
} else {
for (int i = 0; i < types.length; i++) {
parameters[i] = this.inject(types[i]);
}
}
method.invoke(t, parameters);
}
}
return t;
}
@SuppressWarnings("unchecked")
public void loadClassMap() {
Properties p = new Properties();
try {
String classMappingFiles = contents
.getProperty("pxb.ioc.mini.classMappingFiles");
if (classMappingFiles != null && !classMappingFiles.equals("")) {
for (String classMappingFile : classMappingFiles.split(",")) {
String path = this.contents.getProperty(String.format(
"pxb.ioc.mini.classMappingFile.%s.path",
classMappingFile));
InputStream ins = classLoader.getResourceAsStream(path);
p.load(ins);
ins.close();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
for (Map.Entry<Object, Object> entry : p.entrySet()) {
try {
Class a = classLoader.loadClass(entry.getKey().toString());
Class b = classLoader.loadClass(entry.getValue().toString());
toInject.put(a, b);
} catch (Exception e) {
log.log(Level.INFO, "无法加载类", e);
}
}
}
public void loadConfig() {
InputStream ins = null;
try {
if (configFile != null && !configFile.equals("")) {
ins = classLoader.getResourceAsStream(configFile);
contents.load(ins);
ins.close();
}
String configs = this.contents
.getProperty("pxb.ioc.mini.configFiles");
if (configs != null && !configs.equals("")) {
for (String config : configs.split(",")) {
String path = this.getProperty(String.format(
"pxb.ioc.mini.configFile.%s.path", config));
ins = classLoader.getResourceAsStream(path);
contents.load(ins);
ins.close();
}
}
if (configFile != null && !configFile.equals("")) {
ins = classLoader.getResourceAsStream(configFile);
contents.load(ins);
ins.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void loadModule() {
try {
String ms_String = contents.getProperty("pxb.ioc.mini.modules");
if (ms_String == null || ms_String.equals(""))
return;
String[] moduleNames = ms_String.split(",");
log.info("Start Loading MiniModules...");
for (String moduleName : moduleNames) {
Class<?> moduleClass = classLoader.loadClass(contents
.getProperty(String.format(
"pxb.ioc.mini.module.%s.class", moduleName)));
MiniModule module = (MiniModule) this.inject(moduleClass);
module.configure(this);
}
log.info("Loading MiniModule Complete...");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @param classLoader
* the classLoader to set
*/
public void setClassLoader(ClassLoader classLoader) {
this.addClassMap(ClassLoader.class, classLoader);
this.classLoader = classLoader;
}
/**
* @param configFile
* the configFile to set
*/
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
/*
* (non-Javadoc)
*
* @see pxb.ioc.InnerContainer#setProperty(java.lang.String,
* java.lang.String)
*/
public void setProperty(String key, String value) {
this.contents.put(key, value);
}
/*
* (non-Javadoc)
*
* @see pxb.ioc.MiniContainer#getVersion()
*/
public String getVersion() {
return "$Id: DefaultMiniContainer.java 40 2008-08-18 11:16:55Z Panxiaobo $";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -