logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c...@apache.org
Subject cvs commit: jakarta-log4j/tests/src/java/org/apache/log4j/rolling/helpers FileNamePatternParserTestCase.java
Date Tue, 13 May 2003 22:32:49 GMT
ceki        2003/05/13 15:32:49

  Modified:    src/java/org/apache/log4j/rolling
                        SlidingWindowRollingPolicy.java
  Added:       src/java/org/apache/log4j/rolling/helpers
                        IdentityTokenConverter.java TokenConverter.java
                        FileNamePatternParser.java
                        IntegerTokenConverter.java
               tests/src/java/org/apache/log4j/rolling/helpers
                        FileNamePatternParserTestCase.java
  Log:
  Work in progress on rolling
  
  Revision  Changes    Path
  1.1                  jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/IdentityTokenConverter.java
  
  Index: IdentityTokenConverter.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  package org.apache.log4j.rolling.helpers;
  
  /**
   * @author Ceki
   *
   */
  public class IdentityTokenConverter extends TokenConverter {
  
    final String str; 
    
    public IdentityTokenConverter(String str) {
     super(TokenConverter.IDENTITY);
     this.str = str; 
    }
    
    public String convert() {
      return str;
    }
  
  }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/TokenConverter.java
  
  Index: TokenConverter.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling.helpers;
  
  
  /**
   *
   * The TokenConverter offer some basic functionality for more specific token converters.

   * It basically sets up the chained architecture.
   * 
   * @author Ceki
   *
   */
  public class TokenConverter {
    /**
     * @author Ceki
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    protected class IndentityTokenConverter {
  
    }
    static final int IDENTITY = 0;
    static final int INTEGER = 1;
    static final int DATE = 1;
    int type;
    TokenConverter next;
  
    TokenConverter(int t) {
      type = t;
    }
  
    public TokenConverter getNext() {
      return next;
    }
  
    public void setNext(TokenConverter next) {
      this.next = next;
    }
   
    public int getType() {
      return type;
    }
  
    public void setType(int i) {
      type = i;
    }
  
  }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/FileNamePatternParser.java
  
  Index: FileNamePatternParser.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling.helpers;
  
  import org.apache.log4j.Logger;
  
  
  /**
   *
   * This class helps parse file name patterns. Given a number or a date it returns
   * a file name according to the file name pattern.
   *
   * This class is not intended to be used directly but sub-classed or wrapped.
   *
   * @author Ceki G&uuml;lc&uuml;
   *
   */
  class FileNamePatternParser {
    static Logger logger = Logger.getLogger(FileNamePatternParser.class);
    String pattern;
    TokenConverter headTokenConverter;
  
    protected FileNamePatternParser(String pattern) {
      if (pattern == null) {
        throw new IllegalArgumentException(
          "The argument to constrcutor cannot be null. ");
      }
  
      this.pattern = pattern;
      parse();
    }
  
    void parse() {
      int lastIndex = 0;
  
      TokenConverter tc = null;
      int len = pattern.length();
  
      while (true) {
        int i = pattern.indexOf('%', lastIndex);
  
        if (i == -1) {
          String remainingStr = pattern.substring(lastIndex);
          System.out.println("adding the identity token");
          addTokenConverter(tc, new IdentityTokenConverter(remainingStr));
  
          break;
        } else {
          // test for degenerate case where the '%' character is at the end.
          if (i == (len - 1)) {
            String remainingStr = pattern.substring(lastIndex);
            addTokenConverter(tc, new IdentityTokenConverter(remainingStr));
  
            break;
          }
  
          System.out.println("2adding the identity token");
          tc =
            addTokenConverter(
              tc, new IdentityTokenConverter(pattern.substring(lastIndex, i)));
  
          // At this stage, we can suppose that i < len -1
          char nextChar = pattern.charAt(i + 1);
  
          switch (nextChar) {
          case 'i':
            tc = addTokenConverter(tc, new IntegerTokenConverter());
            lastIndex = i + 2;
  
            break; // break from switch statement 
  
          case '%':
            tc = addTokenConverter(tc, new IdentityTokenConverter("%"));
            lastIndex = i + 2;
  
            break;
  
          default:
            throw new IllegalArgumentException(
              "The pattern[" + pattern
              + "] does not contain a valid specifer at position " + (i + 1));
          }
        }
      }
    }
  
    TokenConverter addTokenConverter(
      TokenConverter tc, TokenConverter newTokenConverter) {
      if (tc == null) {
        tc = headTokenConverter = newTokenConverter;
      } else {
        tc.next = newTokenConverter;
        tc = newTokenConverter;
      }
  
      return tc;
    }
  
    protected String convert(int i) {
      TokenConverter p = headTokenConverter;
      StringBuffer buf = new StringBuffer();
  
      while (p != null) {
        switch (p.getType()) {
        case TokenConverter.IDENTITY:
          buf.append(((IdentityTokenConverter) p).convert());
  
          break;
  
        case TokenConverter.INTEGER:
          buf.append(((IntegerTokenConverter) p).convert(i));
  
          break;
  
        default:
          logger.error(
            "Encountered an unknown TokenConverter type for pattern [" + pattern
            + "].");
        }
  
        p = p.getNext();
      }
  
      return buf.toString();
    }
  }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/IntegerTokenConverter.java
  
  Index: IntegerTokenConverter.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling.helpers;
  
  
  /**
   * @author Ceki
   *
   */
  class IntegerTokenConverter extends TokenConverter {
    public IntegerTokenConverter() {
      super(TokenConverter.INTEGER);
    }
  
    public String convert(int i) {
      return Integer.toString(i);
    }
  }
  
  
  
  1.3       +8 -0      jakarta-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java
  
  Index: SlidingWindowRollingPolicy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SlidingWindowRollingPolicy.java	5 May 2003 20:42:57 -0000	1.2
  +++ SlidingWindowRollingPolicy.java	13 May 2003 22:32:49 -0000	1.3
  @@ -119,6 +119,10 @@
       return activeFileName;
     }
   
  +  public String getFileNamePattern() {
  +    return fileNamePattern;
  +  }
  +
    
     public int getMaxIndex() {
       return maxIndex;
  @@ -126,6 +130,10 @@
   
     public int getMinIndex() {
       return minIndex;
  +  }
  +
  +  public void setFileNamePattern(String fnp) {
  +    fileNamePattern = fnp;
     }
   
     public void setMaxIndex(int maxIndex) {
  
  
  
  1.1                  jakarta-log4j/tests/src/java/org/apache/log4j/rolling/helpers/FileNamePatternParserTestCase.java
  
  Index: FileNamePatternParserTestCase.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling.helpers;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * @author Ceki
   *
   */
  public class FileNamePatternParserTestCase extends TestCase {
    /**
     * Constructor for FileNamePatternParserTestCase.
     * @param arg0
     */
    public FileNamePatternParserTestCase(String arg0) {
      super(arg0);
    }
  
    public void test1() {
      //System.out.println("Testing [t]");
      FileNamePatternParser pp = new FileNamePatternParser("t");
      assertEquals("t", pp.convert(3));
  
      //System.out.println("Testing [foo]");
      pp = new FileNamePatternParser("foo");
      assertEquals("foo", pp.convert(3));
  
      //System.out.println("Testing [foo%]");
      pp = new FileNamePatternParser("foo%");
      assertEquals("foo%", pp.convert(3));
  
      pp = new FileNamePatternParser("%ifoo");
      assertEquals("3foo", pp.convert(3));
  
      pp = new FileNamePatternParser("foo%ixixo");
      assertEquals("foo3xixo", pp.convert(3));
      
      pp = new FileNamePatternParser("foo%i.log");
      assertEquals("foo3.log", pp.convert(3));
  
      pp = new FileNamePatternParser("foo.%i.log");
      assertEquals("foo.3.log", pp.convert(3));
      
      pp = new FileNamePatternParser("%ifoo%");
      assertEquals("3foo%", pp.convert(3));
  
      pp = new FileNamePatternParser("%ifoo%%");
      assertEquals("3foo%", pp.convert(3));
  
      pp = new FileNamePatternParser("%%foo");
      assertEquals("%foo", pp.convert(3));
     
    }
  
  
    public void test2() {
      System.out.println("Testing [foo%ibar%i]");
      FileNamePatternParser pp = new FileNamePatternParser("foo%ibar%i");
      assertEquals("foo3bar3", pp.convert(3));
  
      ///pp = new FileNamePatternParser("%%foo");
      //assertEquals("%foo", pp.convert(3));
    }
  
    public static Test suite() {
      TestSuite suite = new TestSuite();
      suite.addTest(new FileNamePatternParserTestCase("test1"));
      suite.addTest(new FileNamePatternParserTestCase("test2"));
     
      return suite;
    }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message