View Javadoc

1   package org.e2etrace.trace;
2   
3   /*
4    * Copyright 2006 Gunther Popp
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.e2etrace.config.DefaultTraceConfig;
22  import org.e2etrace.config.ITraceConfig;
23  import org.e2etrace.config.PropertiesTraceConfig;
24  
25  
26  /**
27   * Base class for all trace session manager implementations.
28   * <p>
29   *
30   * The base class automatically loads a trace configuration if a property <code>
31   * e2etrace.configuration</code>
32   * has been defined. This property must contain the name of a valid e2etrace
33   * configuration file. For example, if
34   * <code>-De2etrace.configuration=e2etrace.properties</code> is passed on the
35   * command line of the application, the respective file will be looked up in the
36   * classpath and loaded into an instance of {@link PropertiesTraceConfig}. The
37   * base class applies the following rules to determine the correct configuration
38   * class for a file name:
39   * <p>
40   * <ul>
41   * <li>file name ends with <code>.properties</code>: Load file into
42   * {@link PropertiesTraceConfig}.</li>
43   * </ul>
44   * <p>
45   *
46   * If <code>e2etrace.configuration</code> has <em>not</em> been defined, a
47   * default trace configuration (see {@link org.e2etrace.config.DefaultTraceConfig}
48   * will be used for the trace session manager.
49   * <p>
50   *
51   * If tracing has been disabled in the trace config,
52   * <code>getCurrentSession</code> will always return a NOOP trace session (see
53   * {@link org.e2etrace.trace.NoopTraceSession}.
54   * <p>
55   * <p>
56   *
57   * @author Gunther Popp
58   */
59  public abstract class AbstractTraceSessionManager implements ITraceSessionManager {
60  
61    private static final Log log = LogFactory.getLog(AbstractTraceSessionManager.class);
62    private static final String CONFIG_PROPERTY = "e2etrace.configuration";
63  
64    private ITraceConfig config;
65    private ITraceSession noopSession;
66  
67    /**
68     * Constructor.
69     *
70     */
71    protected AbstractTraceSessionManager() {
72      this.config = initConfiguration();
73      this.noopSession = new NoopTraceSession();
74    }
75  
76    /**
77     * Helper method: Initialize the e2etrace configuration.
78     * <p>
79     *
80     * @return e2etrace configuration
81     */
82    private ITraceConfig initConfiguration() {
83      ITraceConfig initConfig = null;
84      String configFile;
85  
86      configFile = System.getProperty(CONFIG_PROPERTY);
87  
88      if (configFile != null) {
89        try {
90          if (configFile.endsWith(".properties")) {
91            PropertiesTraceConfig propConfig = new PropertiesTraceConfig();
92  
93            propConfig.loadConfigFile("/" + configFile);
94  
95            initConfig = propConfig;
96          }
97        } catch (Exception e) {
98          log
99              .error(
100                 "Error while initializing e2etrace configuration! Will use default configuration instead.",e);
101       }
102     }
103 
104     // If no valid configuration has been loaded at this point use the default
105     // configuration
106     if (initConfig == null) {
107       initConfig = new DefaultTraceConfig();
108     }
109 
110     return initConfig;
111 
112   }
113 
114   /**
115    * CALL-BACK: Request the current trace session from sub-classes.
116    * <p>
117    *
118    * This call-back is triggered for every invokation of
119    * <code>getCurrentSession</code>.
120    * <p>
121    *
122    * @return current (active) trace session
123    */
124   protected abstract ITraceSession requestCurrentSession();
125 
126   /**
127    * CALL-BACK: Forward a newly assigned current trace session to sub-classes.
128    * <p>
129    *
130    * This call-back is triggered for every invokation of
131    * <code>setCurrentSession</code>.
132    * <p>
133    *
134    * @param session new current (active) trace session
135    */
136   protected abstract void assignCurrentSession(ITraceSession session);
137 
138   /** {@inheritDoc} */
139   public final void setConfig(ITraceConfig config) {
140     this.config = config;
141 
142   }
143 
144   /** {@inheritDoc} */
145   public final ITraceConfig getConfig() {
146     return this.config;
147   }
148 
149   /** {@inheritDoc} */
150   public ITraceSession getCurrentSession() {
151     ITraceSession currentSession;
152 
153     // If tracing is enabled, retrieve the current trace session
154     if (getConfig().isTraceEnabled()) {
155       currentSession = requestCurrentSession();
156 
157       if( currentSession == null ) {
158         currentSession = this.noopSession;
159       }
160     } else {
161       currentSession = this.noopSession;
162     }
163 
164     return currentSession;
165   }
166 
167   /** {@inheritDoc} */
168   public void setCurrentSession(ITraceSession session) {
169     // Check if tracing has been disabled for this session
170     if (session.getRootStep() == null || !(getConfig().isTraceEnabledForId(session.getRootStep().getId()))) {
171       assignCurrentSession(this.noopSession);
172     } else {
173       session.setConfig(this.getConfig());
174       assignCurrentSession(session);
175     }
176 
177   }
178 
179 }