You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Thorsten Mauch <ma...@imkenberg.de> on 2002/10/12 00:24:51 UTC

Jboss-JMX Excalibur Manager Binding

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Jboss-JMX Excalibur Manager Binding

Posted by Leif Mortenson <le...@tanukisoftware.com>.
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:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>  
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Jboss-JMX Excalibur Manager Binding

Posted by Peter Donald <pe...@apache.org>.
On Sat, 12 Oct 2002 10:24, Peter Royal wrote:
> On Friday, October 11, 2002, at 08:01  PM, Peter Donald wrote:
> >> Nice piece of glue! Thanks for sharing.
> >>
> >> Where in CVS should this go? Alongside ECM?
> >
> > +1
> >
> > Though we may want to use an optional exclude so that you can build ECM
> > without JMX but you will get a warning. ie
> >
> > "ECM will not be built with JMX support. Please set jmx.jar property
> > if you
> > need JMX support"
>
> Actually upon closer inspection it extends JBoss classes.. from a
> license standpoint can we have code that subclasses LGPL under APL?

Hmmm .. nope. They would be considered extensions of LGPL work rather than 
users of LGPL work which means we can't host em ;(

-- 
Cheers,

Peter Donald
He strains to hear a whisper who refuses to hear a shout.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Jboss-JMX Excalibur Manager Binding

Posted by Peter Royal <pr...@apache.org>.
On Friday, October 11, 2002, at 08:01  PM, Peter Donald wrote:
>> Nice piece of glue! Thanks for sharing.
>>
>> Where in CVS should this go? Alongside ECM?
>
> +1
>
> Though we may want to use an optional exclude so that you can build ECM
> without JMX but you will get a warning. ie
>
> "ECM will not be built with JMX support. Please set jmx.jar property 
> if you
> need JMX support"

Actually upon closer inspection it extends JBoss classes.. from a 
license standpoint can we have code that subclasses LGPL under APL?
-pete
-- 
peter royal -> proyal@apache.org


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Jboss-JMX Excalibur Manager Binding

Posted by Peter Donald <pe...@apache.org>.
On Sat, 12 Oct 2002 09:53, Peter Royal wrote:
> On Friday, October 11, 2002, at 06:24  PM, Thorsten Mauch wrote:
> > 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 ;)
>
> Nice piece of glue! Thanks for sharing.
>
> Where in CVS should this go? Alongside ECM?

+1

Though we may want to use an optional exclude so that you can build ECM 
without JMX but you will get a warning. ie 

"ECM will not be built with JMX support. Please set jmx.jar property if you 
need JMX support"

-- 
Cheers,

Peter Donald
----------------------------------------
Whatever you do will be insignificant, 
but it is very important that you do it. 
                              --Gandhi
---------------------------------------- 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Jboss-JMX Excalibur Manager Binding

Posted by Peter Royal <pr...@apache.org>.
On Friday, October 11, 2002, at 06:24  PM, Thorsten Mauch wrote:
> 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 ;)

Nice piece of glue! Thanks for sharing.

Where in CVS should this go? Alongside ECM?
-pete
-- 
peter royal -> proyal@apache.org


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>