001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.logging;
018
019 import java.net.URL;
020 import java.net.URLConnection;
021 import java.util.TimerTask;
022
023 import org.apache.log4j.Logger;
024 import org.apache.log4j.xml.DOMConfigurator;
025
026 public class LogTask extends TimerTask {
027
028 private static final Logger LOG = Logger.getLogger(LogTask.class);
029
030 private URL url;
031
032 private long lastConfigured = -1;
033
034 public LogTask(URL url) {
035 this.url = url;
036 }
037
038 public void run() {
039 reconfigure();
040 }
041
042 /**
043 * reconfigure the log4j system if something has changed
044 *
045 * TODO might be good to check if the content type is text so that you can
046 * also can do the same with log4j.properties
047 */
048 public void reconfigure() {
049 try {
050 URLConnection conn = url.openConnection();
051 conn.connect();
052 long lastModified = conn.getLastModified();
053 boolean xml = "application/xml".equals(conn.getContentType());
054 conn.getInputStream().close();
055 if (lastConfigured < lastModified && url != null && xml) {
056 DOMConfigurator.configure(url);
057 lastConfigured = System.currentTimeMillis();
058 LOG.info("Logging system reconfigured using file: " + url.toString());
059 }
060 } catch (Exception ex) {
061 LOG.error(ex);
062 }
063 }
064 }