📄 containerimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.framework.internal;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.rapla.framework.Container;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaContextException;
import org.rapla.framework.RaplaException;
import org.rapla.framework.StartupEnvironment;
/** Base class for the ComponentContainers in Rapla.
* Containers are the RaplaMainContainer, the Client- and the Server-Service
*/
public class ContainerImpl extends AbstractLogEnabled implements Container, Disposable
{
protected Container m_parent;
protected RaplaContext m_context;
protected Configuration m_config;
protected List m_componentHandler = new ArrayList();
protected HashMap m_roleMap = new HashMap();
protected LogManagerAdapter m_loggerManager;
public ContainerImpl(RaplaContext parentContext, Configuration config) throws RaplaException {
m_config = config;
service( parentContext );
init( );
}
protected void init() throws RaplaException {
configure( m_config );
addContainerProvidedComponentInstance( Container.class.getName(), this );
addContainerProvidedComponentInstance( Logger.class.getName(), getLogger());
}
public StartupEnvironment getStartupEnvironment() {
try
{
return (StartupEnvironment)getContext().lookup( StartupEnvironment.ROLE);
}
catch ( RaplaContextException e )
{
throw new IllegalStateException(" Container not initialized with a startup environment");
}
}
private void service(final RaplaContext parent) throws RaplaContextException {
if (parent.has( "logger-manager" )) {
m_loggerManager = (LogManagerAdapter) parent.lookup("logger-manager");
} else {
final Logger logger;
if ( parent.has(Logger.class.getName() ) )
{
logger = (Logger) parent.lookup( Logger.class.getName());
}
else
{
logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
}
m_loggerManager = new LogManagerAdapter()
{
public Logger getLoggerForCategory(String categoryName)
{
return logger.getChildLogger( categoryName );
}
public Logger getDefaultLogger()
{
return logger;
}
};
}
enableLogging( m_loggerManager.getDefaultLogger());
if ( parent.has(Container.ROLE )) {
m_parent = (Container) parent.lookup( Container.ROLE);
}
m_context = new RaplaContext() {
public Object lookup(String role) throws RaplaContextException {
ComponentHandler handler = getHandler( role );
if ( handler != null ) {
return handler.get();
}
return parent.lookup( role);
}
public boolean has(String role) {
if (getHandler( role ) != null)
return true;
return parent.has( role );
}
ComponentHandler getHandler( String role) {
int hintSeperator = role.indexOf('/');
String roleName = role;
String hint = null;
if ( hintSeperator > 0 ) {
roleName = role.substring( 0, hintSeperator );
hint = role.substring( hintSeperator + 1 );
}
return ContainerImpl.this.getHandler( roleName, hint );
}
};
}
protected void configure( final Configuration config )
throws RaplaException
{
Map m_componentInfos = getComponentInfos();
final Configuration[] elements = config.getChildren();
for ( int i = 0; i < elements.length; i++ )
{
final Configuration element = elements[i];
final String id = element.getAttribute( "id", null );
if ( null == id )
{
// Only components with an id attribute are treated as components.
getLogger().debug( "Ignoring configuration for component, " + element.getName()
+ ", because the id attribute is missing." );
}
else
{
final String className;
final String[] roles;
if ( "component".equals( element.getName() ) )
{
try {
className = element.getAttribute( "class" );
Configuration[] roleConfigs = element.getChildren("roles");
roles = new String[ roleConfigs.length ];
for ( int j=0;j< roles.length;j++) {
roles[j] = roleConfigs[j].getValue();
}
} catch ( ConfigurationException ex) {
throw new RaplaException( ex);
}
}
else
{
String configName = element.getName();
final ComponentInfo roleEntry = (ComponentInfo) m_componentInfos.get( configName );
if ( null == roleEntry )
{
final String message = "No class found matching configuration name " +
"[name: " + element.getName() + ", location: " + element.getLocation() + "]";
getLogger().error( message );
continue;
}
roles = roleEntry.getRoles();
className = roleEntry.getClassname();
}
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "Configuration processed for: " + className );
}
Logger logger = m_loggerManager.getLoggerForCategory( id );
ComponentHandler handler =new ComponentHandler( element, className, logger);
for ( int j=0;j< roles.length;j++) {
String roleName = (roles[j]);
addHandler( roleName, id, handler );
}
}
}
}
protected Map getComponentInfos() {
return Collections.EMPTY_MAP;
}
synchronized public void addContainerProvidedComponentInstance(String role,Object component) {
addContainerProvidedComponentInstance( role, component.getClass().getName(),component);
}
synchronized public void addContainerProvidedComponentInstance(String roleName,String hint,Object component) {
addHandler( roleName, hint, new ComponentHandler(component));
}
synchronized public void addContainerProvidedComponent(String classname) {
addContainerProvidedComponent( classname, classname);
}
synchronized public void addContainerProvidedComponent(String role,String classname) {
addContainerProvidedComponent( new String[] {role}, classname, classname, null);
}
synchronized public void addContainerProvidedComponent(String role,String classname, Configuration config) {
addContainerProvidedComponent( new String[] {role}, classname, classname, config);
}
synchronized public void addContainerProvidedComponent(String role,String classname, String hint,Configuration config) {
addContainerProvidedComponent( new String[] {role}, classname, hint, config);
}
synchronized public void addContainerProvidedComponent(String[] roles,String classname,String hint, Configuration config) {
ComponentHandler handler = new ComponentHandler( config, classname, getLogger() );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -