bloritsch 01/04/06 07:00:09
Modified: proposal/4.0/src/java/org/apache/framework/component
DefaultComponentManager.java
Added: proposal/4.0/src/java/org/apache/framework/component
DefaultRoleManager.java RoleManager.java
Removed: proposal/4.0/src/java/org/apache/framework/component
DefaultRoleInfo.java RoleInfo.java
Log:
Reworked ComponentManagement infrastructure a little bit.
Revision Changes Path
1.3 +45 -59
jakarta-avalon/proposal/4.0/src/java/org/apache/framework/component/DefaultComponentManager.java
Index: DefaultComponentManager.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/proposal/4.0/src/java/org/apache/framework/component/DefaultComponentManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultComponentManager.java 2001/04/05 19:38:43 1.2
+++ DefaultComponentManager.java 2001/04/06 14:00:05 1.3
@@ -6,7 +6,7 @@
* the LICENSE file. *
*****************************************************************************/
-package org.apache.framework.component;
+package org.apache.avalon.component;
import java.util.HashMap;
import java.util.Map;
@@ -15,22 +15,26 @@
import java.util.ArrayList;
import java.util.List;
-import org.apache.framework.context.Context;
-import org.apache.framework.context.Contextualizable;
-import org.apache.framework.configuration.Configurable;
-import org.apache.framework.configuration.Configuration;
-import org.apache.framework.configuration.ConfigurationException;
-import org.apache.framework.configuration.DefaultConfiguration;
-import org.apache.framework.lifecycle.Disposable;
-import org.apache.framework.lifecycle.Initializable;
-import org.apache.framework.logger.AbstractLoggable;
+import org.apache.avalon.ComponentManager;
+import org.apache.avalon.Component;
+import org.apache.avalon.ComponentManagerException;
+import org.apache.avalon.Context;
+import org.apache.avalon.Contextualizable;
+import org.apache.avalon.configuration.Configurable;
+import org.apache.avalon.configuration.Configuration;
+import org.apache.avalon.Composer;
+import org.apache.avalon.configuration.ConfigurationException;
+import org.apache.avalon.configuration.DefaultConfiguration;
+import org.apache.avalon.Disposable;
+import org.apache.avalon.Initializable;
+import org.apache.avalon.AbstractLoggable;
/**
* Default component manager for Avalon's components.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a>
- * @version CVS $Revision: 1.2 $ $Date: 2001/04/05 19:38:43 $
+ * @version CVS $Revision: 1.3 $ $Date: 2001/04/06 14:00:05 $
*/
public class DefaultComponentManager extends AbstractLoggable
implements ComponentManager, Configurable, Contextualizable, Disposable {
@@ -49,7 +53,7 @@
/** RoleInfos.
*/
- private RoleInfo roles;
+ private RoleManager roles;
/** Is the Manager disposed or not? */
private boolean disposed = false;
@@ -102,7 +106,7 @@
* case, the Role's FQN is appended with "Selector", and we return a
ComponentSelector.
*/
public Component lookup( String role )
- throws ComponentException {
+ throws ComponentManagerException {
if (disposed) throw new IllegalStateException("You cannot lookup components
on a disposed ComponentManager");
@@ -111,7 +115,7 @@
if ( role == null ) {
getLogger().error("ComponentManager Attempted to retrieve component
with null role.");
- throw new ComponentException("Attempted to retrieve component with null
role.");
+ throw new ComponentManagerException("Attempted to retrieve component
with null role.");
}
handler = (DefaultComponentHandler) this.componentHandlers.get(role);
@@ -122,14 +126,14 @@
Configuration config = new DefaultConfiguration("", "-");
try {
- componentClass =
this.getClass().getClassLoader().loadClass(roles.defaultClass(role));
+ componentClass =
this.getClass().getClassLoader().loadClass(roles.getDefaultClassNameForRole(role));
handler = new DefaultComponentHandler(componentClass, config, this,
this.context);
handler.setLogger(getLogger());
handler.init();
} catch (Exception e) {
getLogger().error("ComponentManager Could not find component for
role: " + role, e);
- throw new ComponentException("Could not find component for role: "
+ role, e);
+ throw new ComponentManagerException("Could not find component for
role: " + role, e);
}
this.componentHandlers.put(role, handler);
@@ -143,10 +147,10 @@
try {
component = handler.get();
} catch (Exception ee) {
- throw new ComponentException("Could not access the Component for
you", ee);
+ throw new ComponentManagerException("Could not access the Component
for you", ee);
}
} catch (Exception e) {
- throw new ComponentException("Could not access the Component for you",
e);
+ throw new ComponentManagerException("Could not access the Component for
you", e);
}
this.componentMapping.put(component, handler);
@@ -157,56 +161,38 @@
* Configure the ComponentManager.
*/
public void configure(Configuration conf) throws ConfigurationException {
- DefaultRoleInfo role_info = new DefaultRoleInfo();
+ DefaultRoleManager role_info = new DefaultRoleManager();
role_info.setLogger(getLogger());
role_info.configure(conf);
roles = role_info;
// Set components
- Configuration[] e = conf.getChildren("component");
+ Configuration[] e = conf.getChildren();
for (int i = 0; i < e.length; i++) {
- String type = e[i].getAttribute("type", "");
- String role = e[i].getAttribute("role", "");
- String className = e[i].getAttribute("class", "");
+ String type = e[i].getName(); // types are already trimmed
- if (! "".equals(type)) {
- role = roles.lookup(type);
- }
-
- if ("".equals(className)) {
- className = roles.defaultClass(role);
- }
-
- try {
- getLogger().debug("Adding component (" + role + " = " + className +
")");
- this.addComponent(role,
this.getClass().getClassLoader().loadClass(className),e[i]);
- } catch ( Exception ex ) {
- getLogger().error("Could not load class " + className, ex);
- throw new ConfigurationException("Could not get class " + className
- + " for role " + role, ex);
- }
- }
-
- Iterator r = roles.shorthandNames();
- while (r.hasNext()) {
- Configuration co = conf.getChild((String) r.next(), false);
-
- if (co != null) {
- String role = roles.lookup(co.getName());
- String className = co.getAttribute("class", "");
+ if (("role".equals(type) == false)) {
+ String role = e[i].getAttribute("role", "");
+ String className = e[i].getAttribute("class", "");
- if ("".equals(className)) {
- className = roles.defaultClass(role);
+ if ("".equals(role)) {
+ role = roles.getRoleForName(type);
}
- try {
- getLogger().debug("Adding component (" + role + " = " +
className + ")");
- this.addComponent(role,
this.getClass().getClassLoader().loadClass(className), co);
- } catch ( Exception ex ) {
- getLogger().error("Could not load class " + className, ex);
- throw new ConfigurationException("Could not get class " +
className
- + " for role " + role, ex);
+ if (role != null && ("".equals(role) == false)) {
+ if ("".equals(className)) {
+ className = roles.getDefaultClassNameForRole(role);
+ }
+
+ try {
+ getLogger().debug("Adding component (" + role + " = " +
className + ")");
+ this.addComponent(role,
this.getClass().getClassLoader().loadClass(className), e[i]);
+ } catch ( Exception ex ) {
+ getLogger().error("Could not load class " + className, ex);
+ throw new ConfigurationException("Could not get class " +
className
+ + " for role " + role + " on configuration element " +
e[i].getName(), ex);
+ }
}
}
}
@@ -230,13 +216,13 @@
* @param Configuration the configuration for this component.
*/
public void addComponent(String role, Class component, Configuration config)
- throws ComponentException {
+ throws ComponentManagerException {
try {
DefaultComponentHandler handler = new
DefaultComponentHandler(component, config, this, this.context);
handler.setLogger(getLogger());
this.componentHandlers.put(role, handler);
} catch (Exception e) {
- throw new ComponentException ("Could not set up Component for role: " +
role, e);
+ throw new ComponentManagerException ("Could not set up Component for
role: " + role, e);
}
}
1.1
jakarta-avalon/proposal/4.0/src/java/org/apache/framework/component/DefaultRoleManager.java
Index: DefaultRoleManager.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.avalon.component;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Collections;
import org.apache.avalon.configuration.Configurable;
import org.apache.avalon.configuration.Configuration;
import org.apache.avalon.AbstractLoggable;
import org.apache.avalon.configuration.ConfigurationException;
/**
* Default RoleManager implementation. It populates the RoleManager
* from a configuration file.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:ricardo@apache,org">Ricardo Rocha</a>
* @author <a href="mailto:giacomo@apache,org">Giacomo Pati</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/04/06 14:00:06 $
*/
public class DefaultRoleManager extends AbstractLoggable implements RoleManager,
Configurable {
private Map shorthands = new HashMap();
private Map classNames = new HashMap();
public final String getRoleForName(String shorthandName) {
getLogger().debug("looking up role " + shorthandName + ", returning " +
(String) this.shorthands.get(shorthandName));
return (String) this.shorthands.get(shorthandName);
}
public final String getDefaultClassNameForRole(String role) {
return (String) this.classNames.get(role);
}
protected final void addRole(String name, String shorthand, String
defaultClassName) {
this.shorthands.put(shorthand, name);
if (defaultClassName != null) {
this.classNames.put(name, defaultClassName);
}
}
public final void configure(Configuration conf) throws ConfigurationException {
Configuration[] roles = conf.getChildren("role");
for (int i = 0; i < roles.length; i++) {
String name = roles[i].getAttribute("name");
String shorthand = roles[i].getAttribute("shorthand");
String defaultClassName = roles[i].getAttribute("default-class", null);
this.addRole(name, shorthand, defaultClassName);
getLogger().debug("added Role " + name + " with shorthand " + shorthand
+ " for " + defaultClassName);
}
this.shorthands = Collections.unmodifiableMap(this.shorthands);
this.classNames = Collections.unmodifiableMap(this.classNames);
}
}
1.1
jakarta-avalon/proposal/4.0/src/java/org/apache/framework/component/RoleManager.java
Index: RoleManager.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.avalon.component;
import java.util.Iterator;
/**
* RoleManager Interface, use this to specify the Roles and how they
* correspond easy shorthand names.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:ricardo@apache,org">Ricardo Rocha</a>
* @author <a href="mailto:giacomo@apache,org">Giacomo Pati</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/04/06 14:00:07 $
*/
public interface RoleManager {
/**
* Find Role name based on shorthand name. Please note that if
* this returns <code>null</code> or an empty string, then the
* shorthand name is assumed to be a "reserved word". In other
* words, you should not try to instantiate a class from an empty
* role.
*/
public String getRoleForName(String shorthandName);
/**
* Get the default classname for a given role
*/
public String getDefaultClassNameForRole(String role);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]