KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > io > rotate > OrRotateStrategy


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output.io.rotate;
9
10 import java.io.File JavaDoc;
11
12 /**
13  * Hierarchical Rotation stragety.
14  * This object is initialised with several rotation strategy objects.
15  * The <code>isRotationNeeded</code> method checks the first rotation
16  * strategy object. If a rotation is needed, this result is returned.
17  * If not the next rotation strategy object is asked and so on.
18  *
19  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
20  */

21 public class OrRotateStrategy
22         implements RotateStrategy {
23     private RotateStrategy[] m_strategies;
24
25     /**
26      * The rotation strategy used. This marker is required for the reset()
27      * method.
28      */

29     private int m_usedRotation = -1;
30
31     /**
32      * Constructor
33      */

34     public OrRotateStrategy(final RotateStrategy[] strategies) {
35         this.m_strategies = strategies;
36     }
37
38     /**
39      * reset.
40      */

41     public void reset() {
42         if (-1 != m_usedRotation) {
43             m_strategies[m_usedRotation].reset();
44             m_usedRotation = -1;
45         }
46     }
47
48     /**
49      * check if now a log rotation is neccessary.
50      * This object is initialised with several rotation strategy objects.
51      * The <code>isRotationNeeded</code> method checks the first rotation
52      * strategy object. If a rotation is needed, this result is returned.
53      * If not the next rotation strategy object is asked and so on.
54      *
55      * @param data the last message written to the log system
56      * @return boolean return true if log rotation is neccessary, else false
57      */

58     public boolean isRotationNeeded(final String JavaDoc data, final File JavaDoc file) {
59         m_usedRotation = -1;
60
61         if (null != m_strategies) {
62             final int length = m_strategies.length;
63             for (int i = 0; i < length; i++) {
64                 if (true == m_strategies[i].isRotationNeeded(data, file)) {
65                     m_usedRotation = i;
66                     return true;
67                 }
68             }
69         }
70
71         return false;
72     }
73 }
74
75
Popular Tags