001 /*
002 * $Id: JamonMonitorFactory.java,v 1.6 2014/04/23 14:45:48 oboehm Exp $
003 *
004 * Copyright (c) 2008 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 27.12.2008 by oliver (ob@oasd.de)
019 */
020 package patterntesting.runtime.monitor;
021
022 import java.util.*;
023 import java.util.concurrent.ConcurrentHashMap;
024
025 import org.aspectj.lang.Signature;
026
027 import patterntesting.runtime.util.SignatureHelper;
028
029 import com.jamonapi.*;
030
031 /**
032 * This is a thin wrapper around com.jamonapi.MonitorFactory to keep the
033 * ProfileStatistic class clean from com.jamonapi dependencies.
034 *
035 * @author <a href="boehm@javatux.de">oliver</a>
036 * @since 27.12.2008
037 * @version $Revision: 1.6 $
038 */
039 public final class JamonMonitorFactory {
040
041 private static final Map<String, MonitorFactoryInterface> monitorFactories = new ConcurrentHashMap<String, MonitorFactoryInterface>();
042
043 /** No need to instantiate it (utility class). */
044 private JamonMonitorFactory() {}
045
046 /**
047 * Reset.
048 * <p>
049 * TODO: This method will be removed in 1.6.
050 * </p>
051 *
052 * @deprecated use {@link #reset(ProfileMonitor)}
053 */
054 @Deprecated
055 public static void reset() {
056 MonitorFactory.reset();
057 for (MonitorFactoryInterface factory : monitorFactories.values()) {
058 factory.reset();
059 }
060 }
061
062 /**
063 * Reset.
064 *
065 * @param rootMonitor the root monitor
066 * @since 1.4.2
067 */
068 public static void reset(final ProfileMonitor rootMonitor) {
069 reset(rootMonitor.getLabel());
070 }
071
072 /**
073 * Reset.
074 *
075 * @param rootLabel the root label
076 * @since 1.4.2
077 */
078 public static synchronized void reset(final String rootLabel) {
079 MonitorFactoryInterface factory = monitorFactories.get(rootLabel);
080 factory.reset();
081 }
082
083 /**
084 * Start.
085 * <p>
086 * TODO: This method will be removed in 1.6.
087 * </p>
088 *
089 * @param label the label
090 * @return the profile monitor
091 * @deprecated use {@link #start(String, ProfileMonitor)}
092 */
093 @Deprecated
094 public static ProfileMonitor start(final String label) {
095 Monitor mon = MonitorFactory.start(label);
096 return new JamonMonitor(mon);
097 }
098
099 /**
100 * Start.
101 *
102 * @param label the label
103 * @param root the root
104 * @return the profile monitor
105 */
106 public static ProfileMonitor start(final String label, final ProfileMonitor root) {
107 return start(label, root.getLabel());
108 }
109
110 /**
111 * Start.
112 *
113 * @param label the label
114 * @param rootLabel the root label
115 * @return the profile monitor
116 */
117 public static ProfileMonitor start(final String label, final String rootLabel) {
118 MonitorFactoryInterface factory = monitorFactories.get(rootLabel);
119 if (factory == null) {
120 factory = new FactoryEnabled();
121 monitorFactories.put(rootLabel, factory);
122 }
123 Monitor mon = factory.start(label);
124 return new JamonMonitor(mon);
125 }
126
127 /**
128 * Adds the monitors.
129 *
130 * @param labels the labels
131 */
132 public static void addMonitors(final List<String> labels) {
133 for (String label : labels) {
134 addMonitor(label);
135 }
136 }
137
138 /**
139 * Adds the monitor.
140 *
141 * @param label the label
142 */
143 public static void addMonitor(final String label) {
144 MonitorFactory.start(label);
145 }
146
147 /**
148 * Gets the monitor.
149 * <p>
150 * TODO: This method will be removed in 1.6.
151 * </p>
152 *
153 * @param sig the signature
154 * @return the monitor
155 * @deprecated use {@link #getMonitor(Signature, ProfileMonitor)}
156 */
157 @Deprecated
158 public static ProfileMonitor getMonitor(final Signature sig) {
159 String label = SignatureHelper.getAsString(sig);
160 ProfileMonitor mon = start(label);
161 return mon;
162 }
163
164 /**
165 * Gets the monitor.
166 *
167 * @param sig the sig
168 * @param root the root
169 * @return the monitor
170 * @since 1.4.2
171 */
172 public static ProfileMonitor getMonitor(final Signature sig, final ProfileMonitor root) {
173 String label = SignatureHelper.getAsString(sig);
174 return getMonitor(label, root);
175 }
176
177 /**
178 * Gets the monitor.
179 *
180 * @param label the label
181 * @param root the root monitor
182 * @return the monitor
183 * @since 1.4.2
184 */
185 public static ProfileMonitor getMonitor(final String label, final ProfileMonitor root) {
186 return start(label, root);
187 }
188
189 /**
190 * Gets the monitors.
191 * <p>
192 * TODO: This method will be removed in 1.6.
193 * </p>
194 *
195 * @return the monitors
196 * @deprecated use {@link #getMonitors(ProfileMonitor)}
197 */
198 @Deprecated
199 public static ProfileMonitor[] getMonitors() {
200 return getMonitorsFrom(MonitorFactory.getFactory());
201 }
202
203 /**
204 * Gets the monitors.
205 *
206 * @param root the root
207 * @return the monitors
208 * @since 1.4.2
209 */
210 public static ProfileMonitor[] getMonitors(final ProfileMonitor root) {
211 return getMonitors(root.getLabel());
212 }
213
214 /**
215 * Gets the monitors.
216 *
217 * @param rootLabel the root label
218 * @return the monitors
219 * @since 1.4.2
220 */
221 public static ProfileMonitor[] getMonitors(final String rootLabel) {
222 MonitorFactoryInterface factory = monitorFactories.get(rootLabel);
223 if (factory == null) {
224 return new JamonMonitor[0];
225 } else {
226 return getMonitorsFrom(factory);
227 }
228 }
229
230 private static ProfileMonitor[] getMonitorsFrom(final MonitorFactoryInterface factory) {
231 MonitorComposite rootMonitor = factory.getRootMonitor();
232 Monitor[] monitors = rootMonitor.getMonitors();
233 if (monitors == null) {
234 return new JamonMonitor[0];
235 }
236 ProfileMonitor[] profMonitors = new JamonMonitor[monitors.length];
237 for (int i = 0; i < monitors.length; i++) {
238 profMonitors[i] = new JamonMonitor(monitors[i]);
239 }
240 return profMonitors;
241 }
242
243 }