Author: gnodet
Date: Fri Jan 12 07:43:02 2007
New Revision: 495611

URL: http://svn.apache.org/viewvc?view=rev&rev=495611
Log:
SM-817: log4j service for changing log levels at runtime
Patch provided by Thomas Termin, thx a lot !

Added:
    
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/
    
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
   (with props)
    
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
   (with props)
    
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
   (with props)
Modified:
    incubator/servicemix/trunk/core/servicemix-core/pom.xml
    
incubator/servicemix/trunk/distributions/apache-servicemix/src/main/release/conf/servicemix.xml

Modified: incubator/servicemix/trunk/core/servicemix-core/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/pom.xml?view=diff&rev=495611&r1=495610&r2=495611
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/pom.xml (original)
+++ incubator/servicemix/trunk/core/servicemix-core/pom.xml Fri Jan 12 07:43:02 
2007
@@ -108,11 +108,6 @@
       <groupId>org.apache.xbean</groupId>
       <artifactId>xbean-spring</artifactId>
     </dependency>
-    <dependency>
-      <groupId>log4j</groupId>
-      <artifactId>log4j</artifactId>
-      <scope>runtime</scope>
-    </dependency>
 
     <dependency>
       <groupId>commons-collections</groupId>

Added: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java?view=auto&rev=495611
==============================================================================
--- 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
 (added)
+++ 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
 Fri Jan 12 07:43:02 2007
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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();
+                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();
+                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());
+    }
+}

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java?view=auto&rev=495611
==============================================================================
--- 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
 (added)
+++ 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
 Fri Jan 12 07:43:02 2007
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.logging;
+
+/**
+ * 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();
+}

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogServiceMBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java?view=auto&rev=495611
==============================================================================
--- 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
 (added)
+++ 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
 Fri Jan 12 07:43:02 2007
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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();
+            conn.connect();
+            long lastModified = conn.getLastModified();
+            boolean xml = "application/xml".equals(conn.getContentType());
+            conn.getInputStream().close();
+            if (lastConfigured < lastModified && url != null && xml) {
+                DOMConfigurator.configure(url);
+                lastConfigured = System.currentTimeMillis();
+                logger.info("log4j system reconfigured");
+            }
+        } catch (Exception ex) {
+            logger.error(ex);
+        }
+    }
+}

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: 
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/logging/LogTask.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
incubator/servicemix/trunk/distributions/apache-servicemix/src/main/release/conf/servicemix.xml
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/distributions/apache-servicemix/src/main/release/conf/servicemix.xml?view=diff&rev=495611&r1=495610&r2=495611
==============================================================================
--- 
incubator/servicemix/trunk/distributions/apache-servicemix/src/main/release/conf/servicemix.xml
 (original)
+++ 
incubator/servicemix/trunk/distributions/apache-servicemix/src/main/release/conf/servicemix.xml
 Fri Jan 12 07:43:02 2007
@@ -54,7 +54,7 @@
                 transactionManager="#transactionManager"
                 createJmxConnector="false"
                 depends-on="jndi">
-               
+
     <!-- Use a secured broker (see security.xml)
          and define the 3 standard flows --> 
     <sm:broker>
@@ -67,11 +67,11 @@
         </sm:flows>
       </sm:securedBroker>
     </sm:broker>
-    
+
     <sm:services>
       <sm:statistics statsInterval="10" dumpStats="true" />
     </sm:services>
-    
+
     <sm:executorFactory>
       <bean class="org.apache.servicemix.executors.impl.ExecutorFactoryImpl">
         <property name="defaultConfig">
@@ -97,7 +97,9 @@
       </bean>
     </audit:dataSource> 
   </audit:jdbcAuditor>
-  
+
   <sm:dotViewService container="#jbi" autoStart="true" />
+
+  <sm:logService container="#jbi" autoStart="true" refreshPeriod="60" />
 
 </beans>


Reply via email to