This looks good, but you might want to take a look at the
ExcaliburComponentManagerCreator class. It takes care of a lot of
the grunt work of creating and initializing an ExcaliburCompentManager.
You would have something like the following:
(Note that if instrumentation is not needed, the fifth parameter can be
null.)
ExcaliburComponentManagerCreator ecmc;
ecmc = new ExcaliburComponentManagerCreator( null,
new File( "../conf/logkit.xml" ), new File(
"../conf/roles.xml" ),
new File( "../conf/components.xml"), new File(
"../conf/instrument.xml" ) );
ComponentManager componentManager = ecmc.getComponentManager();
LoggerManager loggerManager = ecmc.getLoggerManager();
From this point, you use the componentManager and loggerManager as you
always have.
Foo foo = (Foo)componentManager.lookup( Foo.ROLE );
Logger logger = loggerManager.getLoggerForCategory( "foo.category" );
etc...
At shutdown, all objects can be disposed by disposing the ecmc.
You should be able to simplify the code below quite a bit as you would not
need to manually load configuration objects and initialize the
component, role
and logger managers yourself.
Cheers,
Leif
Thorsten Mauch wrote:
>Hi All
>I just add to my Cocoon Aplication a JBoss Backend. In Cocoon i used
>a lot of components based on avalon. This componets i wanted to reuse
>in JBoss. For this purpose I wrote a simple MBean that creates a
>Component Manager and binds it to the JNDI name space.
>I'm not a Avalon nor a JMX Expert so maybe my solution is simple but
>it works for me.
>Maybe is usefull for other or maybe better ideas exist.
>
>As a demo i add my souce code and of if you want you can take
>it as conribution (off course after better testing ;)
>
>
>import org.jboss.system.ServiceMBeanSupport;
>
>import java.util.HashMap;
>import javax.naming.InitialContext;
>import javax.naming.Name;
>import javax.naming.NamingException;
>import org.jboss.naming.NonSerializableFactory;
>import javax.rmi.PortableRemoteObject;
>import javax.naming.Context;
>
>import
>org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
>import org.apache.avalon.framework.configuration.Configuration;
>
>import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
>import org.apache.avalon.excalibur.component.RoleManager;
>import org.apache.avalon.excalibur.component.DefaultRoleManager;
>import org.apache.avalon.framework.context.DefaultContext;
>import org.apache.avalon.framework.component.ComponentManager;
>
>import org.apache.log.Hierarchy;
>//import org.apache.log.Logger;
>import org.apache.log.LogTarget;
>import org.apache.log.output.io.FileTarget;
>import org.apache.log.format.ExtendedPatternFormatter;
>
>import java.io.File;
>import java.io.IOException;
>
>public class AvalonManager extends ServiceMBeanSupport implements
>AvalonManagerMBean{
> private String jndiName;
> private boolean stopped = true;
>
> private ExcaliburComponentManager componentManager;
> private String xconf="../avalon/xconf.xml";
> private String roles="../avalon/roles.xml";
> private String logfile="../avalon/default.log";
> private String logpattern="%7.7{priority} %5.5{time} [%8.8{category}] "
>+
> "(%{context}): %{message}\\n%{throwable}";;
>
> public String getXconf() {
> return xconf;
> }
> public void setXconf(String newXconf) {
> xconf = newXconf;
> }
> public void setRoles(String newRoles) {
> roles = newRoles;
> }
> public String getRoles() {
> return roles;
> }
> public void setLogFile(String newLog) {
> logfile = newLog;
> }
> public String getLogFile() {
> return logfile;
> }
>
> private ExcaliburComponentManager createComponentManager() {
> try{
> DefaultConfigurationBuilder builder = new
>DefaultConfigurationBuilder();
> Configuration sysconf ;
> ExcaliburComponentManager avalonmanager;
> org.apache.log.Logger logger;
>
>
> sysconf=builder.buildFromFile(getXconf());
>
>
> // create a filelogger
>
> logger=Hierarchy.getDefaultHierarchy().getLoggerFor("default");
> logger.setLogTargets( new LogTarget[] {new FileTarget(new
>File(getLogFile()),true,new ExtendedPatternFormatter(logpattern) )} );
> Configuration roleConfig;
> DefaultRoleManager rolemanager = new DefaultRoleManager();
> rolemanager.setLogger(logger);
>
> roleConfig = builder.buildFromFile(roles);
> rolemanager.configure(roleConfig);
>
> // create and configure the component manager
> componentManager = new ExcaliburComponentManager();
> componentManager.contextualize(new DefaultContext());
> componentManager.setLogger( logger);
>
> componentManager.setRoleManager(rolemanager);
> componentManager.configure(sysconf);
> componentManager.initialize();
>
>
> return componentManager;
> }
> catch(Exception e){
> log.error("Error while createing Avalon Component Manager",e);
> return null;
> }
>
> }
> public void setLogpattern(String newLogpattern) {
> logpattern = newLogpattern;
> }
> public String getLogpattern() {
> return logpattern;
> }
>
>
>
> public String getJndiName()
> {
> return jndiName;
> }
> public void setJndiName(String jndiName) throws NamingException
> {
> String oldName = this.jndiName;
> this.jndiName = jndiName;
> if( super.getState() == STARTED )
> {
> unbind(oldName);
> try
> {
> rebind();
> }
> catch(Exception e)
> {
> NamingException ne = new
> NamingException("Failed to update jndiName");
> ne.setRootCause(e);
> throw ne;
> }
> }
> }
>
> public void startService() throws Exception
> {
> stopped = false;
> rebind();
> }
> public void stopService()
> {
> stopped = true;
> unbind(jndiName);
> }
>
> private void rebind() throws NamingException
> {
> componentManager =createComponentManager();
> InitialContext rootCtx = new InitialContext();
> Name fullName = rootCtx.getNameParser("").parse(jndiName);
> log.info("fullName="+fullName);
> NonSerializableFactory.rebind(fullName,componentManager, true);
> }
> private void unbind(String jndiName)
> {
> try
> {
> InitialContext rootCtx = new InitialContext();
> rootCtx.unbind(jndiName);
> NonSerializableFactory.unbind(jndiName);
> }
> catch(NamingException e)
> {
> log.error("Failed to unbind cache", e);
> }
> }
>
>}
>
>--
>To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>