Hi Folks.

I have searched the archives for information on how to register Logback to 
the JMX server in WebSphere and did not find any information.
I did some research and found that IBM has two MBean servers. I posted 
information to the users mailing list on how to get WebSphere to use the 
platform server.

I have also done some research and development on how to get Logback to 
register to the JSR160RMI connector.

I modified the JMXConfiguratorAction class to look for a system property 
called ch.qos.logback.WebSphere to be set to true.
If the property is set to true, it will register the MBean to the MBean 
server at the JSR160RMI connector instead of the platform MBean server.

I have tested this at the level of a proof of concept. I would really like 
to see this feature be added to the code for Logback.
I have included the source code as part of this post. 

Please feel free to contact me at dale.chap...@medavie.bluecross.ca if you 
have any questions or comments about this.

Thanks!



packagech.qos.logback.classic.joran.action;

importjava.lang.management.ManagementFactory;

importjavax.management.MBeanServer;
importjavax.management.ObjectName;

importorg.xml.sax.Attributes;

importcom.ibm.websphere.management.AdminServiceFactory;
importcom.ibm.websphere.management.MBeanFactory;
importcom.ibm.websphere.management.RuntimeCollaborator;
importcom.ibm.websphere.management.UserMBeanCollaborator;
importcom.ibm.websphere.management.exception.AdminException;

importch.qos.logback.classic.LoggerContext;
importch.qos.logback.classic.jmx.JMXConfigurator;
importch.qos.logback.classic.jmx.MBeanUtil;
importch.qos.logback.core.joran.action.Action;
importch.qos.logback.core.joran.spi.ActionException;
importch.qos.logback.core.joran.spi.InterpretationContext;
importch.qos.logback.core.util.OptionHelper;

publicclassJMXConfiguratorAction extendsAction {

 staticfinalString OBJECT_NAME_ATTRIBUTE_NAME= "objectName";
 staticfinalString CONTEXT_NAME_ATTRIBUTE_NAME= "contextName";
 staticfinalcharJMX_NAME_SEPARATOR= ',';
 
 @Override
 publicvoidbegin(InterpretationContext ec, String name, Attributes 
attributes)
     throwsActionException {
   addInfo("begin");


   String contextName = context.getName();
   String contextNameAttributeVal = attributes
   .getValue(CONTEXT_NAME_ATTRIBUTE_NAME);
   if(!OptionHelper.isEmpty(contextNameAttributeVal)) {
     contextName = contextNameAttributeVal;
   }

   String objectNameAsStr;
   String objectNameAttributeVal = attributes
       .getValue(OBJECT_NAME_ATTRIBUTE_NAME);
   if(OptionHelper.isEmpty(objectNameAttributeVal)) {
     objectNameAsStr = MBeanUtil.getObjectNameFor(contextName,
         JMXConfigurator.class);
   } else{
     objectNameAsStr = objectNameAttributeVal;
   }

   ObjectName objectName = MBeanUtil.string2ObjectName(context, this,
       objectNameAsStr);
   if(objectName == null) {
     addError("Failed construct ObjectName for ["+objectNameAsStr+"]");
     return;
   }
 
   String websphereFlag = System.getProperty("ch.qos.logback.WebSphere");
 
   if(websphereFlag == null|| !websphereFlag.equalsIgnoreCase("true")){
 
           MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
           if(!MBeanUtil.isRegistered(mbs, objectName)) {
             // register only of the named JMXConfigurator has not been 
previously
             // registered. Unregistering an MBean within invocation of 
itself
             // caused jconsole to throw an NPE. (This occurs when the 
reload* method
             // unregisters the 
             JMXConfigurator jmxConfigurator = 
newJMXConfigurator((LoggerContext) context, mbs,
                 objectName);
             try{ 
               mbs.registerMBean(jmxConfigurator, objectName);
             } catch(Exception e) {
               addError("Failed to create mbean", e);
             }
           }
   } else{
       try{
                       MBeanFactory mbfactory = 
AdminServiceFactory.getMBeanFactory();
                       MBeanServer mbs = mbfactory.getMBeanServer();
                       RuntimeCollaborator logbackCollaborator = 
newUserMBeanCollaborator(newJMXConfigurator((LoggerContext) context, mbs, 
objectName ));
                       mbfactory.activateMBean("LogbackMBean", 
logbackCollaborator, "logbackMBeanId", "LogbackMBean.xml");
                       System.out.println("LOGBACK registration with IBM 
MBeanServer complete");
               } catch(AdminException e) {
                       System.out.println("LOGBACK registration with IBM 
MBeanServer failed");
                       e.printStackTrace();
               }
   }
 }

 @Override
 publicvoidend(InterpretationContext ec, String name) 
throwsActionException {

 }

}


Dale Chapman,
Architect - MAAX Support
Medavie
506-867-4430
dale.chap...@medavie.bluecross.ca-----------------------------------------------------------------------
This communication, including any attached documentation, is intended only for 
the person or entity to which it is addressed, and may contain confidential, 
personal, and/or privileged information. Any unauthorized disclosure, copying, 
or taking action on the contents is strictly prohibited. If you have received 
this message in error, please contact us immediately so we may correct our 
records. Please then delete or destroy the original transmission and any 
subsequent reply. Thank you.

La présente communication, y compris toute pièce qui y a été jointe, est 
destinée uniquement à la personne ou à l’entité à laquelle elle a été adressée, 
et contient des renseignements à caractère confidentiel et personnel. Toute 
diffusion ou reproduction non autorisée ou toute intervention entreprise 
relativement à son contenu est strictement interdite. Si vous avez reçu ce 
message par erreur, veuillez nous le signaler immédiatement afin que nous 
puissions effectuer la correction à nos dossiers. Veuillez par la suite 
supprimer ou détruire le contenu de la transmission originale ainsi que toute 
réponse ultérieure. Merci.
-----------------------------------------------------------------------
_______________________________________________
logback-dev mailing list
logback-dev@qos.ch
http://mailman.qos.ch/mailman/listinfo/logback-dev

Reply via email to