📄 jdonpicocontainer.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.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoException;
import org.picocontainer.PicoRegistrationException;
import org.picocontainer.PicoVerificationException;
import org.picocontainer.PicoVisitor;
import org.picocontainer.alternatives.ImmutablePicoContainer;
import org.picocontainer.defaults.AmbiguousComponentResolutionException;
import org.picocontainer.defaults.CachingComponentAdapter;
import org.picocontainer.defaults.CachingComponentAdapterFactory;
import org.picocontainer.defaults.ComponentAdapterFactory;
import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
import org.picocontainer.defaults.DefaultComponentAdapterFactory;
import org.picocontainer.defaults.DefaultPicoContainer;
import org.picocontainer.defaults.DuplicateComponentKeyRegistrationException;
import org.picocontainer.defaults.InstanceComponentAdapter;
import org.picocontainer.defaults.LifecycleVisitor;
import org.picocontainer.defaults.VerifyingVisitor;
import com.jdon.util.Debug;
/**
* modify the method getComponentInstance of DefaultPicoContainer of picocontainer
*
* @author <a href="mailto:banqiao@jdon.com">banq</a>
*
*/
public class JdonPicoContainer implements MutablePicoContainer, Serializable {
public final static String module = JdonPicoContainer.class.getName();
private Map componentKeyToAdapterCache = new HashMap();
private Map componentKeyToInstanceCache = new HashMap();
private ComponentAdapterFactory componentAdapterFactory;
private PicoContainer parent;
private List componentAdapters = new ArrayList();
// Keeps track of instantiation order.
private List orderedComponentAdapters = new ArrayList();
private boolean started = false;
private boolean disposed = false;
private HashSet children = new HashSet();
/**
* Creates a new container with a custom ComponentAdapterFactory and a parent container.
* <p/>
* <em>
* Important note about caching: If you intend the components to be cached, you should pass
* in a factory that creates {@link CachingComponentAdapter} instances, such as for example
* {@link CachingComponentAdapterFactory}. CachingComponentAdapterFactory can delegate to
* other ComponentAdapterFactories.
* </em>
*
* @param componentAdapterFactory the factory to use for creation of ComponentAdapters.
* @param parent the parent container (used for component dependency lookups).
*/
public JdonPicoContainer(ComponentAdapterFactory componentAdapterFactory, PicoContainer parent) {
if(componentAdapterFactory == null) throw new NullPointerException("componentAdapterFactory");
this.componentAdapterFactory = componentAdapterFactory;
this.parent = parent == null ? null : new ImmutablePicoContainer(parent);
}
/**
* Creates a new container with a (caching) {@link DefaultComponentAdapterFactory}
* and a parent container.
*/
public JdonPicoContainer(PicoContainer parent) {
this(new DefaultComponentAdapterFactory(), parent);
}
/**
* Creates a new container with a custom ComponentAdapterFactory and no parent container.
*
* @param componentAdapterFactory the ComponentAdapterFactory to use.
*/
public JdonPicoContainer(ComponentAdapterFactory componentAdapterFactory) {
this(componentAdapterFactory, null);
}
/**
* Creates a new container with a (caching) {@link DefaultComponentAdapterFactory} and no parent container.
*/
public JdonPicoContainer() {
this(new DefaultComponentAdapterFactory(), null);
}
public Collection getComponentAdapters() {
return Collections.unmodifiableList(componentAdapters);
}
public final ComponentAdapter getComponentAdapter(Object componentKey) throws AmbiguousComponentResolutionException {
ComponentAdapter adapter = (ComponentAdapter) componentKeyToAdapterCache.get(componentKey);
if (adapter == null && parent != null) {
adapter = parent.getComponentAdapter(componentKey);
}
return adapter;
}
public ComponentAdapter getComponentAdapterOfType(Class componentType) {
// See http://jira.codehaus.org/secure/ViewIssue.jspa?key=PICO-115
ComponentAdapter adapterByKey = getComponentAdapter(componentType);
if (adapterByKey != null) {
return adapterByKey;
}
List found = getComponentAdaptersOfType(componentType);
if (found.size() == 1) {
return ((ComponentAdapter) found.get(0));
} else if (found.size() == 0) {
if (parent != null) {
return parent.getComponentAdapterOfType(componentType);
} else {
return null;
}
} else {
Class[] foundClasses = new Class[found.size()];
for (int i = 0; i < foundClasses.length; i++) {
ComponentAdapter componentAdapter = (ComponentAdapter) found.get(i);
foundClasses[i] = componentAdapter.getComponentImplementation();
}
throw new AmbiguousComponentResolutionException(componentType, foundClasses);
}
}
public List getComponentAdaptersOfType(Class componentType) {
if(componentType == null) {
return Collections.EMPTY_LIST;
}
List found = new ArrayList();
for (Iterator iterator = getComponentAdapters().iterator(); iterator.hasNext();) {
ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
found.add(componentAdapter);
}
}
return found;
}
/**
* {@inheritDoc}
* This method can be used to override the ComponentAdapter created by the {@link ComponentAdapterFactory}
* passed to the constructor of this container.
*/
public ComponentAdapter registerComponent(ComponentAdapter componentAdapter) throws DuplicateComponentKeyRegistrationException {
Object componentKey = componentAdapter.getComponentKey();
if (componentKeyToAdapterCache.containsKey(componentKey)) {
throw new DuplicateComponentKeyRegistrationException(componentKey);
}
componentAdapters.add(componentAdapter);
componentKeyToAdapterCache.put(componentKey, componentAdapter);
return componentAdapter;
}
public ComponentAdapter unregisterComponent(Object componentKey) {
ComponentAdapter adapter = (ComponentAdapter) componentKeyToAdapterCache.remove(componentKey);
componentAdapters.remove(adapter);
orderedComponentAdapters.remove(adapter);
return adapter;
}
/**
* {@inheritDoc}
* The returned ComponentAdapter will be an {@link InstanceComponentAdapter}.
*/
public ComponentAdapter registerComponentInstance(Object component) throws PicoRegistrationException {
return registerComponentInstance(component.getClass(), component);
}
/**
* {@inheritDoc}
* The returned ComponentAdapter will be an {@link InstanceComponentAdapter}.
*/
public ComponentAdapter registerComponentInstance(Object componentKey, Object componentInstance) throws PicoRegistrationException {
if (componentInstance instanceof MutablePicoContainer) {
MutablePicoContainer pc = (MutablePicoContainer) componentInstance;
Object contrivedKey = new Object();
String contrivedComp = "";
pc.registerComponentInstance(contrivedKey, contrivedComp);
try {
if (this.getComponentInstance(contrivedKey) != null) {
throw new PicoRegistrationException("Cannot register a container to itself. The container is already implicitly registered.");
}
} finally {
pc.unregisterComponent(contrivedKey);
}
}
ComponentAdapter componentAdapter = new InstanceComponentAdapter(componentKey, componentInstance);
registerComponent(componentAdapter);
return componentAdapter;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -