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]>

Reply via email to