View Javadoc

1   // 
2   // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.modules.views.logging;
29  
30  import java.util.ArrayList;
31  import java.util.Enumeration;
32  import java.util.List;
33  
34  import org.apache.log4j.Appender;
35  import org.apache.log4j.LogManager;
36  import org.apache.log4j.Logger;
37  import org.objectledge.context.Context;
38  import org.objectledge.parameters.RequestParameters;
39  import org.objectledge.pipeline.ProcessingException;
40  import org.objectledge.templating.TemplatingContext;
41  import org.objectledge.web.mvc.builders.PolicyProtectedBuilder;
42  import org.objectledge.web.mvc.security.PolicySystem;
43  
44  /***
45   * 
46   *
47   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
48   * @version $Id: EditLogger.java,v 1.4 2005/07/26 12:13:30 rafal Exp $
49   */
50  public class EditLogger
51      extends PolicyProtectedBuilder
52  {
53  
54      /***
55       * Creates new EditLogger instance.
56       * 
57       * @param context Context component.
58       * @param policySystemArg PolicySystem component.
59       */
60      public EditLogger(Context context, PolicySystem policySystemArg)
61      {
62          super(context, policySystemArg);
63      }
64  
65      /***
66       * {@inheritDoc}
67       */
68      @Override
69      public void process(TemplatingContext templatingContext)
70          throws ProcessingException
71      {
72          RequestParameters requestParameters = RequestParameters.getRequestParameters(context);
73          String id = requestParameters.get("id");
74          Logger logger;
75          if(id.equals("root"))
76          {
77              logger = LogManager.getRootLogger();
78          }
79          else
80          {
81              if(LogManager.exists(id) == null)
82              {
83                  throw new ProcessingException("invalid logger id "+id);
84              }
85              logger = LogManager.getLogger(id);
86          }
87          templatingContext.put("logger", logger);
88          List<Appender> appenders = new ArrayList<Appender>();
89          getAppenders(logger, appenders);
90          List<Appender> inheritedAppenders = new ArrayList<Appender>();
91          getInheritedAppenders(logger, inheritedAppenders);
92          inheritedAppenders.removeAll(appenders);
93          templatingContext.put("appenders", appenders);
94          templatingContext.put("inheritedAppenders", inheritedAppenders);
95      }        
96  
97      /***
98       * Returns appenders attached to the logger.
99       * 
100      * @param logger the logger.
101      * @param appenders attached to the logger.
102      */
103     private void getAppenders(Logger logger, List<Appender> appenders)
104     {
105         Enumeration<Appender> appenderEnumeration = logger.getAllAppenders();
106         while(appenderEnumeration.hasMoreElements())
107         {
108             appenders.add(appenderEnumeration.nextElement());
109         }
110     }
111     
112     /***
113      * Returns appenders inherited by the logger.
114      * 
115      * @param logger the logger.
116      * @param appenders inherited by the logger.
117      */
118     private void getInheritedAppenders(Logger logger, List<Appender> appenders)
119     {
120         getAppenders(logger, appenders);
121         if(logger.getAdditivity() && logger.getParent() != null)
122         {
123             getInheritedAppenders((Logger)logger.getParent(), appenders);
124         }
125     }
126 }