incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: incubator-ftpserver/src/java/org/apache/ftpserver FtpLogFactory.java
Date Thu, 06 Oct 2005 05:54:09 GMT
rana_b      2005/10/05 22:54:09

  Added:       src/java/org/apache/ftpserver FtpLogFactory.java
  Log:
  custom log factory to wrap the original log factory
  
  Revision  Changes    Path
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/FtpLogFactory.java
  
  Index: FtpLogFactory.java
  ===================================================================
  //$Id: FtpLogFactory.java,v 1.1 2005/10/06 05:54:09 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed 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.ftpserver;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Method;
  import java.lang.reflect.Proxy;
  import java.util.Vector;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * Custom log factory to add other log implementations.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public
  class FtpLogFactory extends LogFactory {
      
      private LogFactory m_original;
      private Vector m_logs;
      
      
      /**
       * Constructor - pass the original log factory.
       */
      public FtpLogFactory(LogFactory original) {
          m_original = original;
          m_logs = new Vector();
      }
      
      /**
       * Add log object.
       */
      public void addLog(Log log) {
          m_logs.add(log);
      }
      
      /**
       * Remove log object.
       */
      public void removeLog(Log log) {
          m_logs.remove(log);
      }
      
      /**
       * Get log object.
       */
      public Log getInstance(Class clazz) {
          Log olog = m_original.getInstance(clazz);
          return createProxyLog(olog);
      }
      
      /**
       * Get log object.
       */
      public Log getInstance(String name) {
          Log olog = m_original.getInstance(name);
          return createProxyLog(olog);
      }
      
      /**
       * Get attribute.
       */
      public Object getAttribute(String arg) {
          return m_original.getAttribute(arg);
      }
      
      /**
       * Get all the attribute names.
       */
      public String[] getAttributeNames() {
          return m_original.getAttributeNames();
      }
      
      /**
       * Set attribute.
       */
      public void setAttribute(String arg, Object val) {
          m_original.setAttribute(arg, val);
      }
      
      /**
       * Remove attribute.
       */
      public void removeAttribute(String arg) {
          m_original.removeAttribute(arg);
      }
      
      /**
       * Release log factory.
       */
      public void release() {
          m_original.release();
          m_logs.clear();
      }
      
      /**
       * Create a proxy log object to redirect log message.
       */
      private Log createProxyLog(final Log original) {
          InvocationHandler handler = new InvocationHandler() {
              public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
{
                  Object retVal = m.invoke(original, args);
                  for(int i=m_logs.size(); --i>=0;) {
                      Log log = (Log)m_logs.get(i);
                      m.invoke(log, args);
                  }
                  return retVal;
              }
          };
          
          return (Log)Proxy.newProxyInstance(getClass().getClassLoader(), 
                                             new Class[] {Log.class}, 
                                             handler);
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message