📄 picocontainerwrapper.java
字号:
/**
* Copyright 2003-2005 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jdon.container.pico;
import java.util.List;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.defaults.ComponentAdapterFactory;
import org.picocontainer.defaults.ConstantParameter;
import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
import org.picocontainer.defaults.DefaultPicoContainer;
import com.jdon.aop.interceptor.InterceptorsChain;
import com.jdon.container.ContainerWrapper;
import com.jdon.container.finder.ContainerCallback;
import com.jdon.container.visitor.ComponentVisitor;
import com.jdon.controller.cache.InstanceCache;
import com.jdon.util.Debug;
/**
* Picocontainer is the implemention of containerWrapper.
*
*
* @author banq
*/
public class PicoContainerWrapper implements ContainerWrapper {
public final static String module = PicoContainerWrapper.class.getName();
private MutablePicoContainer container;
/**
* construct a picocontainer without cache.
*
*/
public PicoContainerWrapper() {
this.container = new JdonPicoContainer(new ConstructorInjectionComponentAdapterFactory());
registerContainerCallback();
}
public PicoContainerWrapper(MutablePicoContainer container) {
this.container = container;
registerContainerCallback();
}
public synchronized void registerContainerCallback(){
register(ContainerCallback.NAME, new ContainerCallback(this));
}
/**
* registe a child container in parent container
*/
public synchronized void registerChild(String name) {
try {
MutablePicoContainer child = new DefaultPicoContainer(container);
register(name, child);
//todo: bidirector register, so components can find each other
//child.registerComponentInstance(name, container);
} catch (Exception ex) {
Debug.logWarning(" registe error: " + name, module);
}
}
/**
* get the child container from the parent container
*/
public ContainerWrapper getChild(String name) {
MutablePicoContainer child = (MutablePicoContainer) lookup(name);
return new PicoContainerWrapper(child);
}
public synchronized void register(String name, Class className) {
try {
Debug.logVerbose("registe: name=" + name + " class=" + className.getName(), module);
container.registerComponentImplementation(name, className);
} catch (Exception ex) {
Debug.logWarning(" registe error: " + name, module);
}
}
public synchronized void register(String name) {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class oClass = classLoader.loadClass(name);
Debug.logVerbose("registe: name=" + name + " class=" + oClass.getName(), module);
register(name, oClass);
} catch (Exception ex) {
Debug.logWarning(" registe error: " + name, module);
}
}
public synchronized void register(String name, Class className, String[] constructors){
if (constructors == null){
register(name, className);
return;
}
try {
Debug.logVerbose("registe: name=" + name + " class=" + className.getName(), module);
Debug.logVerbose(" constructor params size =" + constructors.length, module);
Parameter[] params = new Parameter[constructors.length];
for(int i=0;i<constructors.length;i++){
ConstantParameter param = new ConstantParameter(new String(constructors[i]));
Debug.logVerbose(" register its constructor value is " + constructors[i], module);
params[i] = param;
}
container.registerComponentImplementation(name, className, params);
} catch (Exception ex) {
Debug.logWarning(" registe error: " + name, module);
}
}
public synchronized void register(String name, Object instance) {
try {
Debug.logVerbose("registe: name=" + name + " class=" + instance.getClass().getName(), module);
container.registerComponentInstance(name, instance);
} catch (Exception ex) {
Debug.logWarning(" registe error: " + name, module);
}
}
public synchronized void start() {
try {
container.start();
} catch (RuntimeException e) {
Debug.logError(" container start error: " + e, module);
}
}
public void stop() {
container.stop();
container.dispose();
}
public List getAllInstances() {
return container.getComponentInstances();
}
public Object lookup(String name) {
Debug.logVerbose("lookup: name=" + name, module);
Object object = container.getComponentInstance(name);
if (object == null)
Debug.logVerbose("Not find the component in container :" + name, module);
return object;
}
/**
* This method will usually create a new instance each time it is called
* @param name component name
* @return object new instance
*/
public Object getComponentNewInstance(String name) {
Debug.logVerbose("getComponentNewInstance: name=" + name, module);
ComponentAdapter componentAdapter = container.getComponentAdapter(name);
if (componentAdapter == null)
Debug.logVerbose("Not find the component in container :" + name, module);
return componentAdapter.getComponentInstance(container);
}
public Class getComponentClass(String name) {
Debug.logVerbose("getComponentClass: name=" + name, module);
ComponentAdapter componentAdapter = container.getComponentAdapter(name);
if (componentAdapter == null)
Debug.logVerbose("Not find the component in container :" + name, module);
return componentAdapter.getComponentImplementation();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -