001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.management.mbean;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.LoggingLevel;
021import org.apache.camel.api.management.ManagedNotification;
022import org.apache.camel.api.management.ManagedNotifications;
023import org.apache.camel.api.management.ManagedResource;
024import org.apache.camel.api.management.NotificationSender;
025import org.apache.camel.api.management.NotificationSenderAware;
026import org.apache.camel.api.management.mbean.ManagedTracerMBean;
027import org.apache.camel.processor.interceptor.Tracer;
028import org.apache.camel.spi.ManagementStrategy;
029import org.apache.camel.util.ObjectHelper;
030
031/**
032 * @version 
033 */
034@ManagedResource(description = "Managed Tracer")
035@ManagedNotifications(@ManagedNotification(name = "javax.management.Notification", 
036    description = "Fine grained trace events", 
037    notificationTypes = {"TraceNotification"}))
038public class ManagedTracer implements NotificationSenderAware, ManagedTracerMBean {
039    private final CamelContext camelContext;
040    private final Tracer tracer;
041    private JMXNotificationTraceEventHandler jmxTraceHandler;
042
043    public ManagedTracer(CamelContext camelContext, Tracer tracer) {
044        this.camelContext = camelContext;
045        this.tracer = tracer;
046        jmxTraceHandler = new JMXNotificationTraceEventHandler(tracer);
047        tracer.addTraceHandler(jmxTraceHandler);
048    }
049
050    public void init(ManagementStrategy strategy) {
051        // do nothing
052    }
053
054    public CamelContext getContext() {
055        return camelContext;
056    }
057
058    public Tracer getTracer() {
059        return tracer;
060    }
061
062    public String getCamelId() {
063        return camelContext.getName();
064    }
065
066    public String getCamelManagementName() {
067        return camelContext.getManagementName();
068    }
069
070    public boolean getEnabled() {
071        return tracer.isEnabled();
072    }
073
074    public void setEnabled(boolean enabled) {
075        tracer.setEnabled(enabled);
076    }
077
078    public String getDestinationUri() {
079        return tracer.getDestinationUri();
080    }
081
082    public void setDestinationUri(String uri) {
083        if (ObjectHelper.isEmpty(uri)) {
084            tracer.setDestinationUri(null);
085        } else {
086            tracer.setDestinationUri(uri);
087        }
088    }
089
090    public String getLogName() {
091        return tracer.getLogName();
092    }
093
094    public boolean getUseJpa() {
095        return tracer.isUseJpa();
096    }
097
098    public void setLogName(String logName) {
099        tracer.setLogName(logName);
100    }
101
102    public String getLogLevel() {
103        return tracer.getLogLevel().name();
104    }
105
106    public void setLogLevel(String logLevel) {
107        tracer.setLogLevel(LoggingLevel.valueOf(logLevel));
108    }
109
110    public boolean getLogStackTrace() {
111        return tracer.isLogStackTrace();
112    }
113
114    public void setLogStackTrace(boolean logStackTrace) {
115        tracer.setLogStackTrace(logStackTrace);
116    }
117
118    public boolean getTraceInterceptors() {
119        return tracer.isTraceInterceptors();
120    }
121
122    public void setTraceInterceptors(boolean traceInterceptors) {
123        tracer.setTraceInterceptors(traceInterceptors);
124    }
125
126    public boolean getTraceExceptions() {
127        return tracer.isTraceExceptions();
128    }
129
130    public void setTraceExceptions(boolean traceExceptions) {
131        tracer.setTraceExceptions(traceExceptions);
132    }
133
134    public boolean getTraceOutExchanges() {
135        return tracer.isTraceOutExchanges();
136    }
137
138    public void setTraceOutExchanges(boolean traceOutExchanges) {
139        tracer.setTraceOutExchanges(traceOutExchanges);
140    }
141
142    public boolean getFormatterShowBody() {
143        if (tracer.getDefaultTraceFormatter() == null) {
144            return false;
145        }
146        return tracer.getDefaultTraceFormatter().isShowBody();
147    }
148
149    public void setFormatterShowBody(boolean showBody) {
150        if (tracer.getDefaultTraceFormatter() == null) {
151            return;
152        }
153        tracer.getDefaultTraceFormatter().setShowBody(showBody);
154    }
155
156    public boolean getFormatterShowBodyType() {
157        if (tracer.getDefaultTraceFormatter() == null) {
158            return false;
159        }
160        return tracer.getDefaultTraceFormatter().isShowBodyType();
161    }
162
163    public void setFormatterShowBodyType(boolean showBodyType) {
164        if (tracer.getDefaultTraceFormatter() == null) {
165            return;
166        }
167        tracer.getDefaultTraceFormatter().setShowBodyType(showBodyType);
168    }
169
170    public boolean getFormatterShowOutBody() {
171        if (tracer.getDefaultTraceFormatter() == null) {
172            return false;
173        }
174        return tracer.getDefaultTraceFormatter().isShowOutBody();
175    }
176
177    public void setFormatterShowOutBody(boolean showOutBody) {
178        if (tracer.getDefaultTraceFormatter() == null) {
179            return;
180        }
181        tracer.getDefaultTraceFormatter().setShowOutBody(showOutBody);
182    }
183
184    public boolean getFormatterShowOutBodyType() {
185        if (tracer.getDefaultTraceFormatter() == null) {
186            return false;
187        }
188        return tracer.getDefaultTraceFormatter().isShowOutBodyType();
189    }
190
191    public void setFormatterShowOutBodyType(boolean showOutBodyType) {
192        if (tracer.getDefaultTraceFormatter() == null) {
193            return;
194        }
195        tracer.getDefaultTraceFormatter().setShowOutBodyType(showOutBodyType);
196    }
197
198    public boolean getFormatterShowBreadCrumb() {
199        if (tracer.getDefaultTraceFormatter() == null) {
200            return false;
201        }
202        return tracer.getDefaultTraceFormatter().isShowBreadCrumb();
203    }
204
205    public void setFormatterShowBreadCrumb(boolean showBreadCrumb) {
206        if (tracer.getDefaultTraceFormatter() == null) {
207            return;
208        }
209        tracer.getDefaultTraceFormatter().setShowBreadCrumb(showBreadCrumb);
210    }
211
212    public boolean getFormatterShowExchangeId() {
213        if (tracer.getDefaultTraceFormatter() == null) {
214            return false;
215        }
216        return tracer.getDefaultTraceFormatter().isShowExchangeId();
217    }
218
219    public void setFormatterShowExchangeId(boolean showExchangeId) {
220        if (tracer.getDefaultTraceFormatter() == null) {
221            return;
222        }
223        tracer.getDefaultTraceFormatter().setShowExchangeId(showExchangeId);
224    }
225
226    public boolean getFormatterShowHeaders() {
227        if (tracer.getDefaultTraceFormatter() == null) {
228            return false;
229        }
230        return tracer.getDefaultTraceFormatter().isShowHeaders();
231    }
232
233    public void setFormatterShowHeaders(boolean showHeaders) {
234        if (tracer.getDefaultTraceFormatter() == null) {
235            return;
236        }
237        tracer.getDefaultTraceFormatter().setShowHeaders(showHeaders);
238    }
239
240    public boolean getFormatterShowOutHeaders() {
241        if (tracer.getDefaultTraceFormatter() == null) {
242            return false;
243        }
244        return tracer.getDefaultTraceFormatter().isShowOutHeaders();
245    }
246
247    public void setFormatterShowOutHeaders(boolean showOutHeaders) {
248        if (tracer.getDefaultTraceFormatter() == null) {
249            return;
250        }
251        tracer.getDefaultTraceFormatter().setShowOutHeaders(showOutHeaders);
252    }
253
254    public boolean getFormatterShowProperties() {
255        if (tracer.getDefaultTraceFormatter() == null) {
256            return false;
257        }
258        return tracer.getDefaultTraceFormatter().isShowProperties();
259    }
260
261    public void setFormatterShowProperties(boolean showProperties) {
262        if (tracer.getDefaultTraceFormatter() == null) {
263            return;
264        }
265        tracer.getDefaultTraceFormatter().setShowProperties(showProperties);
266    }
267
268    public boolean getFormatterShowNode() {
269        if (tracer.getDefaultTraceFormatter() == null) {
270            return false;
271        }
272        return tracer.getDefaultTraceFormatter().isShowNode();
273    }
274
275    public void setFormatterShowNode(boolean showNode) {
276        if (tracer.getDefaultTraceFormatter() == null) {
277            return;
278        }
279        tracer.getDefaultTraceFormatter().setShowNode(showNode);
280    }
281
282    public boolean getFormatterShowExchangePattern() {
283        if (tracer.getDefaultTraceFormatter() == null) {
284            return false;
285        }
286        return tracer.getDefaultTraceFormatter().isShowExchangePattern();
287    }
288
289    public void setFormatterShowExchangePattern(boolean showExchangePattern) {
290        if (tracer.getDefaultTraceFormatter() == null) {
291            return;
292        }
293        tracer.getDefaultTraceFormatter().setShowExchangePattern(showExchangePattern);
294    }
295
296    public boolean getFormatterShowException() {
297        if (tracer.getDefaultTraceFormatter() == null) {
298            return false;
299        }
300        return tracer.getDefaultTraceFormatter().isShowException();
301    }
302
303    public void setFormatterShowException(boolean showException) {
304        if (tracer.getDefaultTraceFormatter() == null) {
305            return;
306        }
307        tracer.getDefaultTraceFormatter().setShowException(showException);
308    }
309
310    public boolean getFormatterShowRouteId() {
311        if (tracer.getDefaultTraceFormatter() == null) {
312            return false;
313        }
314        return tracer.getDefaultTraceFormatter().isShowRouteId();
315    }
316
317    public void setFormatterShowRouteId(boolean showRouteId) {
318        if (tracer.getDefaultTraceFormatter() == null) {
319            return;
320        }
321        tracer.getDefaultTraceFormatter().setShowRouteId(showRouteId);
322    }
323
324    public int getFormatterBreadCrumbLength() {
325        if (tracer.getDefaultTraceFormatter() == null) {
326            return 0;
327        }
328        return tracer.getDefaultTraceFormatter().getBreadCrumbLength();
329    }
330
331    public void setFormatterBreadCrumbLength(int breadCrumbLength) {
332        if (tracer.getDefaultTraceFormatter() == null) {
333            return;
334        }
335        tracer.getDefaultTraceFormatter().setBreadCrumbLength(breadCrumbLength);
336    }
337
338    public boolean getFormatterShowShortExchangeId() {
339        if (tracer.getDefaultTraceFormatter() == null) {
340            return false;
341        }
342        return tracer.getDefaultTraceFormatter().isShowShortExchangeId();
343    }
344
345    public void setFormatterShowShortExchangeId(boolean showShortExchangeId) {
346        if (tracer.getDefaultTraceFormatter() == null) {
347            return;
348        }
349        tracer.getDefaultTraceFormatter().setShowShortExchangeId(showShortExchangeId);
350    }
351
352    public int getFormatterNodeLength() {
353        if (tracer.getDefaultTraceFormatter() == null) {
354            return 0;
355        }
356        return tracer.getDefaultTraceFormatter().getNodeLength();
357    }
358
359    public void setFormatterNodeLength(int nodeLength) {
360        if (tracer.getDefaultTraceFormatter() == null) {
361            return;
362        }
363        tracer.getDefaultTraceFormatter().setNodeLength(nodeLength);
364    }
365
366    public int getFormatterMaxChars() {
367        if (tracer.getDefaultTraceFormatter() == null) {
368            return 0;
369        }
370        return tracer.getDefaultTraceFormatter().getMaxChars();
371    }
372
373    public void setFormatterMaxChars(int maxChars) {
374        if (tracer.getDefaultTraceFormatter() == null) {
375            return;
376        }
377        tracer.getDefaultTraceFormatter().setMaxChars(maxChars);
378    }
379    
380    public boolean isJmxTraceNotifications() {
381        return this.tracer.isJmxTraceNotifications();
382    }
383
384    public void setJmxTraceNotifications(boolean jmxTraceNotifications) {
385        this.tracer.setJmxTraceNotifications(jmxTraceNotifications);
386    }
387
388    public int getTraceBodySize() {
389        return this.tracer.getTraceBodySize();
390    }
391
392    public void setTraceBodySize(int traceBodySize) {
393        this.tracer.setTraceBodySize(traceBodySize);
394    }
395
396    @Override
397    public void setNotificationSender(NotificationSender sender) {
398        jmxTraceHandler.setNotificationSender(sender);
399    }
400
401}