Hello,

attached the patch for the new logging service. Please have a look at it
if it is ok this way (if it is please let me know I will raise a JIRA
issue then and will attach the patch). If there are any improvements on
it I will change that.

Cheers,
Thomas

Guillaume Nodet wrote:
> Forwarding to the dev list ...
> 
> I think you may want to take a look at how the
> JdbcAuditor, DotViewService or StatisticsService
> are implemented.  They all inherit the
> o.a.s.jbi.management.BaseSystemService
> abstract class.  They come in different flavous wrt configuration however.
> I would recommend to look at the StatisticsService, which can be configured
> that way:
> 
>   <sm:container ...>
>     <sm:services>
>       <sm:statistics .. />
>     </sm:services>
>   </sm:container>
> 
> This way, the service is automatically registered in JMX and has its own
> lifecycle (which is tied to the container), so that you can stop / start
> the
> service from jmx.
> 
> On 1/11/07, Thomas TERMIN <[EMAIL PROTECTED]> wrote:
>> What I want to do is to implement a MBean which configure the log4j
>> system periodicaly with a scheduler. But before I will look if there is
>> a log4j.xml or log4j.properties in the conf directory if there is
>> nothing in it then I assume that there is no log4j system and don't
>> reconfigure log4j (I will give you a better explanation later ;-) ).
>>
>> The Problem what I have is to register a MBean in conf/servicemix.xml.
>> How do I have to do this? I tried this with the spring MBeanExporter but
>> it doesn't work for me.
>>
>> Cheers,
>> Thomas
>>
>> >
>> > Btw, if you don't mind, i'd rather have such discussion on
>> > servicemix-dev / servicemix-users ;-)
>> No problem at all! If you open the thread...
>>
>> >
>> >>
>> >> Cheers,
>> >> Thomas
>> >>
>> >> Guillaume Nodet wrote:
>> >> > Did you implement something useful ?  Would you
>> >> > consider giving it back to ServiceMix ?
>> >> >
>> >> > On 10/20/06, Thomas TERMIN <[EMAIL PROTECTED]> wrote:
>> >> >> Sorry I did not mean a servicemix component. I use allways the word
>> >> >> component ;-) since I started working with servicemix. What you
>> >> said is
>> >> >> exactly what I meant. So I will have a look on it!
>> >> >>
>> >> >> Thanks,
>> >> >> Thomas
>> >> >>
>> >> >> Guillaume Nodet wrote:
>> >> >> > I would rather use a ServiceMix service instead of
>> >> >> > a component, as this is more related to management /
>> >> >> > configuration than a component if I understand you
>> >> >> > correctly.  ... and use a timer to reload the log4j config.
>> >> >> > But iirc, log4j already has this feature, we just need to
>> >> >> > enable it.
>> >> >> >
>> >> >> > On 10/20/06, Thomas TERMIN <[EMAIL PROTECTED]> wrote:
>> >> >> >> Hello Guillaume,
>> >> >> >>
>> >> >> >> We would need a log4j Component where you can change the debug
>> >> >> level at
>> >> >> >> runtime. I would implement a MBean which initialise the log4j
>> >> >> system at
>> >> >> >> startup and also have a scheduler which looks if the
>> log4j.xml has
>> >> >> >> changed and then reinitialise the log4j system.
>> >> >> >>
>> >> >> >> If I would provide you a patch would you accept this in
>> servicemix?
>> >> >> >>
>> >> >> >> Cheers,
>> >> >> >> Thomas Termin
>> >> >> >>
>> >> >> >
>> >> >> >
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
> 
> 

Index: core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
===================================================================
--- core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java	(Revision 0)
+++ core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java	(Revision 0)
@@ -0,0 +1,212 @@
+package org.apache.servicemix.jbi.logging;
+
+import org.apache.servicemix.jbi.management.BaseSystemService;
+import org.apache.servicemix.jbi.management.OperationInfoHelper;
+import org.apache.servicemix.jbi.management.AttributeInfoHelper;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.InitializingBean;
+
+import javax.jbi.JBIException;
+import javax.management.MBeanOperationInfo;
+import javax.management.JMException;
+import javax.management.MBeanAttributeInfo;
+import java.util.Timer;
+import java.net.URL;
+import java.net.MalformedURLException;
+
+/**
+ *
+ *
+ * @org.apache.xbean.XBean element="logService"
+ *
+ * TODO add methods to change one or more specific LogLevels at runtime
+ */
+public class LogService extends BaseSystemService implements InitializingBean, LogServiceMBean
+{
+  private boolean autoStart = true;
+  private boolean initialized = false;
+  private int refreshPeriod = 60; // 60sec
+  private URL configFileUrl = null;
+  private String configUrl = "file:conf/log4j.xml";
+  private LogTask logTask = null;
+  // timer in daemon mode
+  private Timer timer = null;
+  private static Logger logger = Logger.getLogger(LogService.class);
+
+  public void afterPropertiesSet() throws Exception {
+    if (this.container == null) {
+      throw new IllegalArgumentException("container should not be null");
+    }
+    init(getContainer());
+    if (autoStart) {
+      start();
+    }
+  }
+
+  public JBIContainer getContainer() {
+        return container;
+  }
+
+  public void setContainer(JBIContainer container) {
+   this.container = container;
+  }
+
+  public String getDescription() {
+    return "Log4j Service which periodicaly scan the config file";
+  }
+
+  /**
+   *
+   * @param seconds Refresh period for the log4j system.
+   */
+  public void setRefreshPeriod (int seconds)
+  {
+    this.refreshPeriod = seconds;
+    try
+    {
+      if (isStarted())
+      {
+        stop();
+        if (autoStart)
+          start();
+      }
+    }
+    catch (JBIException ex)
+    {
+      logger.error("Error occured!", ex);
+    }
+  }
+
+  /**
+   *
+   * @return returns the time in seconds for the refresh period
+   */
+  public int getRefreshPeriod() {
+    return this.refreshPeriod;
+  }
+
+  /**
+   * set new location for log4j config
+   * @param url Location for log4j config file example: file:conf/log4j.xml
+   */
+  public void setConfigUrl(String url)
+  {
+    this.configUrl = url;
+    try
+    {
+      if (isStarted())
+      {
+        stop();
+        if (autoStart)
+        {
+          start();
+        }
+      }
+    }
+    catch (JBIException ex)
+    {
+      logger.error("Error occured!", ex);
+    }
+
+  }
+
+  public String getConfigUrl()
+  {
+    return this.configUrl;
+  }
+
+  public void setAutoStart(boolean autoStart)
+  {
+    this.autoStart = autoStart;
+  }
+
+  public boolean getAutoStart()
+  {
+    return this.autoStart;
+  }
+
+  /**
+   * reconfigure the log4j system if something has changed
+   * in the config file
+   */
+  public void reconfigureLogSystem()
+  {
+    if (logger.isDebugEnabled())
+    {
+      logger.debug("try to reconfigure the log4j system");
+    }
+    if (logTask != null)
+    {
+      logTask.reconfigure();
+    }
+  }
+
+  protected Class getServiceMBean() {
+    return LogServiceMBean.class;
+  }
+
+  public void start() throws JBIException
+  {
+    setup();
+    super.start();
+  }
+
+  public void stop() throws JBIException
+  {
+    if (logTask != null)
+    {
+      logTask.cancel();
+      logTask = null;
+    }
+    if (timer != null)
+    {
+      timer.cancel();
+      timer = null;
+    }
+    initialized = false;
+    super.stop();
+  }
+
+  public void setup() throws JBIException
+  {
+    if (!initialized)
+    {
+      if (configUrl != null)
+      {
+        try
+        {
+          configFileUrl = new URL(configUrl);
+        }
+        catch (MalformedURLException e)
+        {
+          configFileUrl = null;
+        }
+      }
+      if (configFileUrl != null)
+      {
+        // daemon mode
+        timer = new Timer(true);
+        logTask = new LogTask(configFileUrl);
+        logTask.run();
+        timer.schedule(logTask, 1000 * refreshPeriod, 1000 * refreshPeriod);
+        initialized = true;
+      }
+    }
+  }
+
+  public MBeanOperationInfo[] getOperationInfos() throws JMException
+  {
+    OperationInfoHelper helper = new OperationInfoHelper();
+    helper.addOperation(getObjectToManage(), "reconfigureLogSystem", 0, "Reconfigure the log4j system");
+    return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos());
+  }
+
+  public MBeanAttributeInfo[] getAttributeInfos() throws JMException
+  {
+    AttributeInfoHelper helper = new AttributeInfoHelper();
+    helper.addAttribute(getObjectToManage(), "configUrl", "the url for the log4j.xml config file");
+    helper.addAttribute(getObjectToManage(), "refreshPeriod", "schedule time for scanning the log4j config file");
+    return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos());
+  }
+}
Index: core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
===================================================================
--- core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java	(Revision 0)
+++ core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java	(Revision 0)
@@ -0,0 +1,51 @@
+package org.apache.servicemix.jbi.logging;
+
+import org.apache.log4j.xml.DOMConfigurator;
+import org.apache.log4j.Logger;
+
+import java.util.TimerTask;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class LogTask extends TimerTask
+{
+  private static Logger logger = Logger.getLogger(LogTask.class);
+  private URL url;
+  private long lastConfigured = -1;
+
+  public LogTask(URL url)
+  {
+    this.url = url;
+  }
+
+  public void run()
+  {
+    reconfigure();
+  }
+
+  /**
+   * reconfigure the log4j system if something has changed
+   *
+   * TODO might be good to check if the content type is text so that
+   * you can also can do the same with log4j.properties
+   */
+  public void reconfigure()
+  {
+    try
+    {
+      URLConnection conn = url.openConnection();
+      long lastModified = conn.getLastModified();
+      boolean xml = "application/xml".equals(conn.getContentType());
+      if (lastConfigured < lastModified && conn != null && url != null && xml)
+      {
+        DOMConfigurator.configure(url);
+        lastConfigured = System.currentTimeMillis();
+        logger.info("log4j system reconfigured");
+      }
+    }
+    catch (Exception ex)
+    {
+      logger.error(ex);
+    }
+  }
+}
Index: core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
===================================================================
--- core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java	(Revision 0)
+++ core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java	(Revision 0)
@@ -0,0 +1,18 @@
+package org.apache.servicemix.jbi.logging;
+
+import javax.jbi.JBIException;
+
+/**
+ * Created by
+ * User: tte
+ * Date: 11.01.2007
+ * Time: 13:48:10
+ */
+public interface LogServiceMBean
+{
+  public void setRefreshPeriod (int seconds);
+  public int getRefreshPeriod();
+  public void setConfigUrl(String url);
+  public String getConfigUrl();
+  public void reconfigureLogSystem();
+}
Index: core/servicemix-core/pom.xml
===================================================================
--- core/servicemix-core/pom.xml	(Revision 495566)
+++ core/servicemix-core/pom.xml	(Arbeitskopie)
@@ -43,7 +43,6 @@
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
-      <optional>true</optional>
     </dependency>
     <dependency>
       <groupId>xml-apis</groupId>
@@ -111,7 +110,7 @@
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
-      <scope>runtime</scope>
+      <!--<scope>runtime</scope>-->
     </dependency>
 
     <dependency>
Index: distributions/apache-servicemix/src/main/release/conf/servicemix.xml
===================================================================
--- distributions/apache-servicemix/src/main/release/conf/servicemix.xml	(Revision 495566)
+++ distributions/apache-servicemix/src/main/release/conf/servicemix.xml	(Arbeitskopie)
@@ -100,4 +100,6 @@
   
   <sm:dotViewService container="#jbi" autoStart="true" />
 
+  <sm:logService container="#jbi" autoStart="true" refreshPeriod="60" />
+
 </beans>

Reply via email to