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.e2etrace.config.DefaultTraceConfig;
20  import org.e2etrace.config.ITraceConfig;
21  
22  /**
23   * Default-Implementation of a trace session.
24   * <p>
25   *
26   * The session uses a default trace configuration (see
27   * {@link org.e2etrace.config.DefaultTraceConfig}). This can be changed by
28   * <code>setConfig</code>. As soon as a trace session manager that extends
29   * <code>AbstractTraceSessionManager</code> is in control of the session, the
30   * trace configuration assigned to the session manager will automatically be
31   * forwarded to the trace session.
32   * <p>
33   *
34   * @author Gunther Popp
35   */
36  public class DefaultTraceSession implements ITraceSession {
37  
38    private ITraceStep root;
39    private ITraceStep current;
40    private ITraceStepFactory stepFactory;
41    private ITraceConfig tc;
42  
43    /**
44     * Default constructor.
45     * <p>
46     *
47     * @param sessionId id of the session. This id will be used for the root step
48     *          of the session. It will be prefixed by a dollar sign ($) (see
49     *          {@link TraceSessionRootStep} and {@link TraceSessionRootStepId})
50     * @param stepFactory Factory used for the concrete trace steps in the context
51     *          of this session
52     */
53    public DefaultTraceSession(String sessionId, ITraceStepFactory stepFactory) {
54      this.root = new TraceSessionRootStep(new TraceSessionRootStepId(sessionId));
55      this.current = this.root;
56      this.stepFactory = stepFactory;
57      this.tc = new DefaultTraceConfig();
58    }
59  
60    /** {@inheritDoc} */
61    public void enterStep(Class clazz, String method) {
62      if (this.tc.isTraceEnabled()) {
63        this.enterStep(new MethodTraceStepId(clazz, method));
64      }
65    }
66  
67    /** {@inheritDoc} */
68    public void leaveStep(Class clazz, String method) {
69      if (this.tc.isTraceEnabled()) {
70        this.leaveStep(new MethodTraceStepId(clazz, method));
71      }
72    }
73  
74    /** {@inheritDoc} */
75    public void enterStep(String id) {
76      if (this.tc.isTraceEnabled()) {
77        this.enterStep(new SimpleTraceStepId(id));
78      }
79    }
80  
81    /** {@inheritDoc} */
82    public void leaveStep(String id) {
83      if (this.tc.isTraceEnabled()) {
84        this.leaveStep(new SimpleTraceStepId(id));
85      }
86    }
87  
88    /** {@inheritDoc} */
89    public void enterStep(ITraceStepId id) {
90      if (this.tc.isTraceEnabledForId(id)) {
91        ITraceStep step;
92  
93        step = this.stepFactory.newInstance(id);
94  
95        // Add the new step as child to the current step
96        this.current.addChild(step);
97  
98        // Now, the new trace step is the current step
99        this.current = step;
100 
101       // Start time measuring for the new trace step
102       step.enter();
103     }
104 
105   }
106 
107   /** {@inheritDoc} */
108   public void leaveStep(ITraceStepId id) {
109     if (this.tc.isTraceEnabledForId(id)) {
110       ITraceStep step = this.current;
111 
112       // If the supplied id does not match the current trace step we
113       // probably missed a call to leaveStep()
114       if (!(this.current.getId().equals(id))) {
115         // Search for a matching trace step in all parents of the
116         // current trace step until we hit the root-step
117         step = current.getParent();
118         while (step != null && step != root) {
119           if (step.getId().equals(id)) {
120             break;
121           }
122 
123           step = step.getParent();
124 
125         }
126 
127       }
128 
129       if (step != null) {
130         // End time measuring
131         step.leave();
132       }
133 
134       if (step != root && step != null) {
135         // Return to the parent step
136         this.current = step.getParent();
137       }
138 
139     }
140 
141   }
142 
143   /** {@inheritDoc} */
144   public ITraceStep getRootStep() {
145     return this.root;
146   }
147 
148   /** {@inheritDoc} */
149   public long getDuration() {
150     long duration = 0L;
151     ITraceStep[] rootChildren = this.root.getChildren();
152 
153     // The root step itself always has a duration of 0, so the following
154     // call returns the accumulated duration of all childs of root.
155     for (int i = 0; i < rootChildren.length; i++) {
156       duration += rootChildren[i].getDuration();
157     }
158 
159     return duration;
160 
161   }
162 
163   /** {@inheritDoc} */
164   public void setConfig(ITraceConfig tc) {
165     this.tc = tc;
166 
167   }
168 
169   /** {@inheritDoc} */
170   public ITraceConfig getConfig() {
171     return this.tc;
172   }
173 
174   /** {@inheritDoc} */
175   public ITraceStep getCurrentStep() {
176     return this.current;
177   }
178 }