View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.audit;
35  
36  import info.magnolia.objectfactory.Components;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import org.apache.commons.lang.StringUtils;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  /**
47   * Class for auditory logging, it's optional to define it, configured in mgnl-beans.properties.
48   *
49   * @version $Id$
50   */
51  public class AuditLoggingManager {
52  
53      private List<LogConfiguration> logConfigurations = new ArrayList<LogConfiguration>();
54  
55      private String defaultSeparator = ", ";
56  
57      private static Logger applog = LoggerFactory.getLogger(AuditLoggingManager.class);
58  
59      public static AuditLoggingManager getInstance() {
60          try {
61              return Components.getComponent(AuditLoggingManager.class);
62          } catch (Exception e) {
63              // if not defined skip and return null
64              applog.info("Class AuditLoggingManager not defined");
65              return null;
66          }
67      }
68  
69      public void addLogConfigurations(LogConfiguration action) {
70          this.logConfigurations.add(action);
71      }
72  
73      public List<LogConfiguration> getLogConfigurations() {
74          return logConfigurations;
75      }
76  
77      public void setLogConfigurations(List<LogConfiguration> logConfigurations) {
78          this.logConfigurations = logConfigurations;
79      }
80  
81      public LogConfiguration getLogConfiguration(String action) {
82          Iterator<LogConfiguration> iterator = this.logConfigurations.iterator();
83          while (iterator.hasNext()) {
84              final LogConfiguration trail = iterator.next();
85              if (StringUtils.equals(trail.getName(), action)) {
86                  return trail;
87              }
88          }
89          return null;
90      }
91  
92      /**
93       * AuditLogging is active when at least one of log configurations is active.
94       */
95      public boolean isAuditLoggingActive() {
96          Iterator<LogConfiguration> iterator = this.logConfigurations.iterator();
97          while (iterator.hasNext()) {
98              final LogConfiguration trail = iterator.next();
99              if (trail.isActive()) {
100                 return true;
101             }
102         }
103         return false;
104     }
105 
106     public void log(String action, String[] data) {
107         StringBuilder message = new StringBuilder();
108         LogConfiguration trail = this.getLogConfiguration(action);
109         if (trail == null) {
110             applog.trace("Can't get log configuration");
111         } else {
112             String separator = defaultSeparator;
113             if (!StringUtils.isEmpty(trail.getSeparator())) {
114                 separator = trail.getSeparator();
115             }
116             message.append(separator).append(action);
117             if (trail.isActive()) {
118                 for (int i = 0; i < data.length; i++) {
119                     if (StringUtils.isNotEmpty(data[i])) {
120                         message.append(separator).append(data[i]);
121                     }
122 
123                 }
124                 org.apache.log4j.Logger.getLogger(trail.getLogName()).log(LoggingLevel.AUDIT_TRAIL, message.toString());
125             }
126         }
127     }
128 
129     public String getDefaultSeparator() {
130         return defaultSeparator;
131     }
132 
133     public void setDefaultSeparator(String defaultSeparator) {
134         this.defaultSeparator = defaultSeparator;
135     }
136 }