KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > callflow > AgentImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * AgentImpl.java
26  * $Id: AgentImpl.java,v 1.27.4.1 2006/09/11 20:11:50 harpreet Exp $
27  * $Date: 2006/09/11 20:11:50 $
28  * $Revision: 1.27.4.1 $
29  */

30
31 package com.sun.enterprise.admin.monitor.callflow;
32
33 import java.util.UUID JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import com.sun.enterprise.web.connector.extension.GrizzlyConfig;
42
43 import com.sun.enterprise.admin.common.constant.AdminConstants;
44
45 /**
46  * This class implements a call flow agent, which collects call flow data
47  * and sends it to data store, for later querying and analysis.
48  *
49  * It is possible to filter the requests for which call flow data is gathered,
50  * based on caller host IP address, and caller id (user name).
51  *
52  * @author Ram Jeyaraman, Harpreet Singh, Nazrul Islam, Siraj Ghaffar
53  * @date March 21, 2005
54  **/

55 public class AgentImpl implements Agent {
56     
57     /** Static definitions. */
58     
59     private static final Logger JavaDoc logger =
60             Logger.getLogger(AdminConstants.kLoggerName);
61     
62     private static final String JavaDoc SYSTEM_PROPERTY =
63             "com.sun.enterprise.callflow.trace";
64     
65     static class RequestData {
66         
67         private RequestType requestType;
68         private long requestStartTime;
69         private long requestStartTimeMillis;
70         private String JavaDoc callerIPAddress;
71         private String JavaDoc remoteUser;
72         
73         RequestType getRequestType() {
74             return requestType;
75         }
76         
77         void setRequestType(RequestType requestType) {
78             this.requestType = requestType;
79         }
80         
81         long getRequestStartTime() {
82             return requestStartTime;
83         }
84         
85         void setRequestStartTime(long requestStartTime) {
86             this.requestStartTime = requestStartTime;
87         }
88         
89         long getRequestStartTimeMillis() {
90             return requestStartTimeMillis;
91         }
92         
93         void setRequestStartTimeMillis(long requestStartTimeMillis) {
94             this.requestStartTimeMillis = requestStartTimeMillis;
95         }
96         
97         String JavaDoc getCallerIPAddress() {
98             return callerIPAddress;
99         }
100         
101         void setCallerIPAddress(String JavaDoc callerIPAddress) {
102             this.callerIPAddress = callerIPAddress;
103         }
104         
105         String JavaDoc getRemoteUser() {
106             return remoteUser;
107         }
108         
109         void setRemoteUser(String JavaDoc remoteUser) {
110             this.remoteUser = remoteUser;
111         }
112     }
113     
114     // Non-synchronized Stack implementation.
115
static class FlowStack<E> extends LinkedList JavaDoc<E> {
116         
117         void push(E e) {
118             addFirst(e);
119         }
120         
121         E pop() {
122             return removeFirst();
123         }
124     }
125     
126     public static class ThreadLocalState implements ThreadLocalData {
127         
128         private String JavaDoc requestId;
129         private boolean initialized;
130         private boolean storeData;
131         
132         // The following attributes are used primarily by the log manager
133
// to log these values into the log message. ThreadId is not cached
134
// at this point, because the log manager does not require it.
135
private String JavaDoc methodName;
136         private String JavaDoc componentType;
137         private String JavaDoc applicationName;
138         private String JavaDoc moduleName;
139         private String JavaDoc componentName;
140         private String JavaDoc transactionId;
141         private String JavaDoc securityId;
142         
143         // RequestStart data holder.
144
private RequestData requestData;
145         
146         // To handle nested container trap point invocations.
147
private FlowStack<ContainerTypeOrApplicationType> flowStack;
148         
149         ThreadLocalState() {
150             requestId = UUID.randomUUID().toString();
151             requestData = new RequestData();
152             flowStack = new FlowStack<ContainerTypeOrApplicationType>();
153         }
154         
155         public String JavaDoc getRequestId() {
156             return requestId;
157         }
158         
159         boolean getInitialized() {
160             return initialized;
161         }
162         
163         void setInitialized(boolean initialized) {
164             this.initialized = initialized;
165         }
166         
167         boolean getStoreData() {
168             return storeData;
169         }
170         
171         void setStoreData(boolean storeData) {
172             this.storeData = storeData;
173         }
174         
175         public String JavaDoc getMethodName() {
176             return methodName;
177         }
178         
179         void setMethodName(String JavaDoc methodName) {
180             this.methodName = methodName;
181         }
182         
183         public String JavaDoc getApplicationName() {
184             return applicationName;
185         }
186         
187         void setApplicationName(String JavaDoc applicationName) {
188             this.applicationName = applicationName;
189         }
190         
191         public String JavaDoc getModuleName() {
192             return moduleName;
193         }
194         
195         void setModuleName(String JavaDoc moduleName) {
196             this.moduleName = moduleName;
197         }
198         
199         public String JavaDoc getComponentName() {
200             return componentName;
201         }
202         
203         void setComponentName(String JavaDoc componentName) {
204             this.componentName = componentName;
205         }
206         
207         public String JavaDoc getComponentType() {
208             return componentType;
209         }
210         
211         void setComponentType(String JavaDoc componentType) {
212             this.componentType = componentType;
213         }
214         
215         public String JavaDoc getTransactionId() {
216             return transactionId;
217         }
218         
219         void setTransactionId(String JavaDoc transactionId) {
220             this.transactionId = transactionId;
221         }
222         
223         public String JavaDoc getSecurityId() {
224             return securityId;
225         }
226         
227         void setSecurityId(String JavaDoc securityId) {
228             this.securityId = securityId;
229         }
230         
231         RequestData getRequestData() {
232             return requestData;
233         }
234         
235         FlowStack<ContainerTypeOrApplicationType> getFlowStack() {
236             return flowStack;
237         }
238     }
239     
240     // Thread local object.
241
private static final ThreadLocal JavaDoc<ThreadLocalState> threadLocal =
242             new ThreadLocal JavaDoc() {
243         protected ThreadLocalState initialValue() {
244             return new ThreadLocalState();
245         }
246     };
247     
248     /** Instance variables. */
249     
250     private boolean storeData;
251     private int dataWriterThreadCount;
252     
253     private String JavaDoc callerIPAddressFilter;
254     private String JavaDoc callerPrincipalFilter;
255     
256     private AsyncHandler asyncHandler = new AsyncHandler();
257     private DbAccessObject dbAccessObject = DbAccessObjectImpl.getInstance();
258     
259     private boolean traceOn; // generates flow sequence trace debug messages.
260

261     private List JavaDoc<Listener> listeners =
262             java.util.Collections.synchronizedList(new ArrayList JavaDoc<Listener>());
263     
264     public AgentImpl() {
265         if (System.getProperty(SYSTEM_PROPERTY, "false").equals("true")) {
266             traceOn = true;
267         }
268     }
269     
270     /** Call flow trap points. */
271     
272     public void requestStart(RequestType requestType) {
273         try {
274             if (!this.storeData)
275                 return;
276             threadLocal.remove(); // sanity check
277
ThreadLocalState tls = threadLocal.get();
278             tls.getFlowStack().clear();
279             if (traceOn) {
280                 logger.log(Level.INFO, tls.getRequestId() + ", requestStart()");
281             }
282             // NOTE: The request start trap point
283
// data is written out during the first startTime trap point.
284
tls.getRequestData().setRequestType(requestType);
285             tls.getRequestData().setRequestStartTime(System.nanoTime());
286             tls.getRequestData().
287                     setRequestStartTimeMillis(System.currentTimeMillis());
288         } catch (Exception JavaDoc e) {
289             logger.log(
290                     Level.WARNING,
291                     "callflow.request_start_operation_failed", e);
292         }
293     }
294     
295     public void addRequestInfo(RequestInfo requestInfo, String JavaDoc value) {
296         try {
297             ThreadLocalState tls = threadLocal.get();
298             if (traceOn) {
299                 logger.log(
300                         Level.INFO, tls.getRequestId() + ", addRequestInfo()");
301             }
302             if (tls.getInitialized()) {
303                 logger.log(
304                         Level.FINE,
305                         "callflow.add_request_info_disallowed");
306                 return;
307             }
308             if (requestInfo == RequestInfo.CALLER_IP_ADDRESS) {
309                 tls.getRequestData().setCallerIPAddress(value);
310             } else if (requestInfo == RequestInfo.REMOTE_USER) {
311                 tls.getRequestData().setRemoteUser(value);
312             }
313         } catch (Exception JavaDoc e) {
314             logger.log(
315                     Level.WARNING,
316                     "callflow.add_request_info_operation_failed", e);
317         }
318     }
319     
320     /**
321      * Called only from startTime method. startTime is the first trap point
322      * that is called after all the request information such as callerIPAddress
323      * and remoteUser is supplied by the container.
324      *
325      * Upon being called, this method initializes the request thread local
326      * state. Even though this method may be called several times by various
327      * startTime invocations, only the first call for this request succeeds in
328      * initializing the thread local state.
329      *
330      * Note, the trap point call sequence has a specific order:
331      *
332      * {
333      * requestStart, addRequestInfo*,
334      * (startTime, (webMethodStart|ejbMethodStart))*,
335      * ((ejbMethodEnd|webMethodEnd), endTime)*,
336      * requestEnd
337      * }
338      *
339      * The startTime is the first trap point to be called after all the
340      * request information is supplied via addRequestInfo method.
341      */

342     private void initialize() {
343         
344         ThreadLocalState tls = threadLocal.get();
345         if (tls.getInitialized()) {
346             return;
347         }
348         
349         // Check filters.
350

351         RequestData requestData = tls.getRequestData();
352         String JavaDoc callerIPAddress = requestData.getCallerIPAddress();
353         String JavaDoc remoteUser = requestData.getRemoteUser();
354         boolean filtered = false;
355         
356         if ((this.callerIPAddressFilter != null) &&
357                 !(this.callerIPAddressFilter.equals(callerIPAddress))) {
358             filtered = true;
359         }
360         
361         if ((this.callerPrincipalFilter != null) &&
362                 !(this.callerPrincipalFilter.equals(remoteUser))) {
363             filtered = true;
364         }
365         
366         // Allow storing the data, iff the call is not filtered and
367
// storeData flag is turned on.
368
tls.setStoreData(!filtered && storeData);
369         
370         // Create asychronous data asyncHandler iff there is atleast one thread
371
// whose storeData flag is set.
372
if (!filtered && storeData) {
373             synchronized (asyncHandler) {
374                 if (dataWriterThreadCount == 0) {
375                     this.asyncHandler.enable();
376                 }
377                 dataWriterThreadCount++;
378             }
379         }
380         
381         // Note, this flag has to be set before the delayed write.
382
// Otherwise, this will result in unterminated recursion,
383
// due to the startTime() method call.
384
tls.setInitialized(true);
385         
386         // Notify listeners.
387

388         boolean listenersPresent = false;
389         long otherStartTime = System.nanoTime();
390         if (listeners.isEmpty() == false) {
391             listenersPresent = true;
392             synchronized (listeners) {
393                 for (Listener listener : listeners) {
394                     listener.requestStart(
395                             tls.getRequestId(), requestData.getRequestType(),
396                             callerIPAddress, remoteUser);
397                 }
398             }
399         }
400         long otherEndTime = System.nanoTime();
401         
402         // Store data (delayed write).
403

404         if (tls.getStoreData()) {
405             storeRequestStartData(
406                     tls.getRequestId(), requestData.getRequestStartTime(),
407                     requestData.getRequestStartTimeMillis(),
408                     requestData.getRequestType(), callerIPAddress, remoteUser,
409                     tls.getFlowStack(), listenersPresent, otherStartTime,
410                     otherEndTime);
411         }
412     }
413     
414     public void requestEnd() {
415         
416         try {
417             if (!this.storeData)
418                 return;
419             
420             ThreadLocalState tls = threadLocal.get();
421             
422             if (traceOn) {
423                 logger.log(Level.INFO, tls.getRequestId() + ", requestEnd()");
424             }
425             
426             // Notify listeners.
427

428             boolean listenersPresent = false;
429             long otherStartTime = System.nanoTime();
430             if (listeners.isEmpty() == false) {
431                 listenersPresent = true;
432                 synchronized (listeners) {
433                     for (Listener listener : listeners) {
434                         listener.requestEnd(tls.getRequestId());
435                     }
436                 }
437             }
438             long otherEndTime = System.nanoTime();
439             
440             // Store data.
441

442             if (tls.getStoreData()) {
443                 storeRequestEndData(
444                         tls.getRequestId(), tls.getFlowStack(),
445                         listenersPresent, otherStartTime, otherEndTime);
446             }
447             
448             // Check if there are any more data writer threads. If this is the
449
// last one, no need for the async data asyncHandler thread.
450

451             if (tls.getStoreData()) {
452                 synchronized (asyncHandler) {
453                     dataWriterThreadCount--;
454                     if (dataWriterThreadCount == 0) {
455                         this.asyncHandler.disable();
456                     }
457                 }
458             }
459             
460         } catch (Exception JavaDoc e) {
461             logger.log(
462                     Level.WARNING,
463                     "callflow.request_end_operation_failed", e);
464         } finally {
465             threadLocal.remove();
466         }
467     }
468     
469     public void startTime(ContainerTypeOrApplicationType type) {
470         try {
471             ThreadLocalState tls = threadLocal.get();
472             if (traceOn) {
473                 logger.log(Level.INFO, tls.getRequestId() + ", startTime()");
474             }
475             // Note: In the natural callflow sequence, startTime() is the first
476
// method that is called after all the addRequestInfo() calls are
477
// complete. So, we use the very first startTime() operation to
478
// do the initialization, filtering, et cetera.
479
initialize(); // idempotent
480
if (tls.getStoreData()) {
481                 storeStartTimeData(
482                         tls.getRequestId(), type, tls.getFlowStack());
483             }
484         } catch (Exception JavaDoc e) {
485             logger.log(
486                     Level.WARNING,
487                     "callflow.start_time_operation_failed", e);
488         }
489     }
490     
491     public void endTime() {
492         try {
493             ThreadLocalState tls = threadLocal.get();
494             if (traceOn) {
495                 logger.log(Level.INFO, tls.getRequestId() + ", endTime()");
496             }
497             if (tls.getStoreData()) {
498                 storeEndTimeData(tls.getRequestId(), tls.getFlowStack());
499             }
500         } catch (Exception JavaDoc e) {
501             logger.log(
502                     Level.WARNING,
503                     "callflow.end_time_operation_failed", e);
504         }
505     }
506     
507     public void ejbMethodStart(CallFlowInfo info) {
508         
509         try {
510             if (!this.storeData)
511                 return;
512             
513             ThreadLocalState tls = threadLocal.get();
514             
515             if (traceOn) {
516                 logger.log(
517                         Level.INFO, tls.getRequestId() + ", ejbMethodStart()");
518             }
519             
520             String JavaDoc requestId = tls.getRequestId();
521             String JavaDoc methodName = info.getMethod().toString();
522             ComponentType componentType = info.getComponentType();
523             String JavaDoc applicationName = info.getApplicationName();
524             String JavaDoc moduleName = info.getModuleName();
525             String JavaDoc componentName = info.getComponentName();
526             String JavaDoc transactionId = info.getTransactionId();
527             String JavaDoc securityId = info.getCallerPrincipal();
528             
529             // Notify listeners.
530

531             boolean listenersPresent = false;
532             long otherStartTime = System.nanoTime();
533             if (listeners.isEmpty() == false) {
534                 listenersPresent = true;
535                 synchronized (listeners) {
536                     for (Listener listener : listeners) {
537                         listener.ejbMethodStart(
538                                 requestId, methodName, applicationName,
539                                 moduleName, componentName, componentType,
540                                 securityId, transactionId);
541                     }
542                 }
543             }
544             long otherEndTime = System.nanoTime();
545             
546             // Store data.
547

548             if (tls.getStoreData()) {
549                 storeMethodStartData(
550                         tls.getRequestId(), info.getMethod().toString(),
551                         info.getComponentType(), info.getApplicationName(),
552                         info.getModuleName(), info.getComponentName(),
553                         Thread.currentThread().getName(), info.getTransactionId(),
554                         info.getCallerPrincipal(), tls.getFlowStack(),
555                         ContainerTypeOrApplicationType.EJB_APPLICATION,
556                         listenersPresent, otherStartTime, otherEndTime);
557             }
558             
559             // Update thread local state.
560

561             tls.setMethodName(methodName);
562             tls.setApplicationName(applicationName);
563             tls.setModuleName(moduleName);
564             tls.setComponentName(componentName);
565             tls.setComponentType(componentType.toString());
566             tls.setTransactionId(transactionId);
567             tls.setSecurityId(securityId);
568             
569         } catch (Exception JavaDoc e) {
570             logger.log(
571                     Level.WARNING,
572                     "callflow.ejb_method_start_operation_failed", e);
573         }
574     }
575     
576     public void ejbMethodEnd(CallFlowInfo info) {
577         
578         try {
579             if (!this.storeData)
580                 return;
581             
582             ThreadLocalState tls = threadLocal.get();
583             
584             if (traceOn) {
585                 logger.log(Level.INFO, tls.getRequestId() + ", ejbMethodEnd()");
586             }
587             
588             // Notify listeners.
589

590             boolean listenersPresent = false;
591             long otherStartTime = System.nanoTime();
592             if (listeners.isEmpty() == false) {
593                 listenersPresent = true;
594                 synchronized (listeners) {
595                     for (Listener listener : listeners) {
596                         listener.ejbMethodEnd(
597                                 tls.getRequestId(), info.getException());
598                     }
599                 }
600             }
601             long otherEndTime = System.nanoTime();
602             
603             // Store data.
604

605             if (tls.getStoreData()) {
606                 storeMethodEndData(
607                         tls.getRequestId(), info.getException(), tls.getFlowStack(),
608                         listenersPresent, otherStartTime, otherEndTime);
609             }
610             
611             // Clear relevant thread local state.
612

613             tls.setMethodName(null);
614             tls.setApplicationName(null);
615             tls.setModuleName(null);
616             tls.setComponentName(null);
617             tls.setComponentType(null);
618             tls.setTransactionId(null);
619             tls.setSecurityId(null);
620             
621         } catch (Exception JavaDoc e) {
622             logger.log(
623                     Level.WARNING,
624                     "callflow.ejb_method_end_operation_failed", e);
625         }
626     }
627     
628     public void webMethodStart(
629             String JavaDoc methodName, String JavaDoc applicationName, String JavaDoc moduleName,
630             String JavaDoc componentName, ComponentType componentType,
631             String JavaDoc callerPrincipal) {
632         
633         try {
634             if (!this.storeData)
635                 return;
636             
637             ThreadLocalState tls = threadLocal.get();
638             
639             if (traceOn) {
640                 logger.log(
641                         Level.INFO, tls.getRequestId() + ", webMethodStart()");
642             }
643             
644             // Notify listeners.
645

646             boolean listenersPresent = false;
647             long otherStartTime = System.nanoTime();
648             if (listeners.isEmpty() == false) {
649                 listenersPresent = true;
650                 synchronized (listeners) {
651                     for (Listener listener : listeners) {
652                         listener.webMethodStart(
653                                 tls.getRequestId(), methodName, applicationName,
654                                 moduleName, componentName, componentType,
655                                 callerPrincipal);
656                     }
657                 }
658             }
659             long otherEndTime = System.nanoTime();
660             
661             // Store data.
662

663             if (tls.getStoreData()) {
664                 storeMethodStartData(
665                         tls.getRequestId(), methodName, componentType,
666                         applicationName, moduleName,
667                         componentName, Thread.currentThread().getName(),
668                         null, callerPrincipal, tls.getFlowStack(),
669                         ContainerTypeOrApplicationType.WEB_APPLICATION,
670                         listenersPresent, otherStartTime, otherEndTime);
671             }
672             
673             // Update thread local state.
674

675             tls.setMethodName(methodName);
676             tls.setApplicationName(applicationName);
677             tls.setModuleName(moduleName);
678             tls.setComponentName(componentName);
679             tls.setComponentType(componentType.toString());
680             tls.setTransactionId(null);
681             tls.setSecurityId(callerPrincipal);
682             
683         } catch (Exception JavaDoc e) {
684             logger.log(
685                     Level.WARNING,
686                     "callflow.web_method_start_operation_failed", e);
687         }
688     }
689     
690     public void webMethodEnd(Throwable JavaDoc exception) {
691         
692         try {
693             if (!this.storeData)
694                 return;
695             
696             ThreadLocalState tls = threadLocal.get();
697             
698             if (traceOn) {
699                 logger.log(Level.INFO, tls.getRequestId() + ", webMethodEnd()");
700             }
701             
702             // Notify listeners.
703

704             boolean listenersPresent = false;
705             long otherStartTime = System.nanoTime();
706             if (listeners.isEmpty() == false) {
707                 listenersPresent = true;
708                 synchronized (listeners) {
709                     for (Listener listener : listeners) {
710                         listener.webMethodEnd(tls.getRequestId(), exception);
711                     }
712                 }
713             }
714             long otherEndTime = System.nanoTime();
715             
716             // Store data.
717

718             if (tls.getStoreData()) {
719                 storeMethodEndData(
720                         tls.getRequestId(), exception, tls.getFlowStack(),
721                         listenersPresent, otherStartTime, otherEndTime);
722             }
723             
724             // Clear relevant thread local state.
725

726             tls.setMethodName(null);
727             tls.setApplicationName(null);
728             tls.setModuleName(null);
729             tls.setComponentName(null);
730             tls.setComponentType(null);
731             tls.setTransactionId(null);
732             tls.setSecurityId(null);
733             
734         } catch (Exception JavaDoc e) {
735             logger.log(
736                     Level.WARNING,
737                     "callflow.web_method_end_operation_failed", e);
738         }
739     }
740     
741     // Private methods.
742

743     /**
744      * Note: This method is called lazily from startTime/initialize().
745      */

746     private void storeRequestStartData(
747             String JavaDoc requestId, long timeStamp, long timeStampMillis,
748             RequestType requestType, String JavaDoc callerIPAddress,
749             String JavaDoc remoteUser,
750             FlowStack<ContainerTypeOrApplicationType> flowStack,
751             boolean listenersPresent, long otherStartTime, long otherEndTime) {
752         if (requestType == RequestType.REMOTE_WEB) {
753             flowStack.push(ContainerTypeOrApplicationType.WEB_CONTAINER);
754         } else if (requestType == RequestType.REMOTE_EJB) {
755             // Remote EJBs first enter via the ORB layer.
756
flowStack.push(ContainerTypeOrApplicationType.ORB_CONTAINER);
757         } else { // Timer EJB or MDB
758
flowStack.push(ContainerTypeOrApplicationType.EJB_CONTAINER);
759         }
760         asyncHandler.handleRequestStart(
761                 requestId, timeStamp, timeStampMillis,
762                 requestType, callerIPAddress, remoteUser);
763         asyncHandler.handleStartTime(requestId, timeStamp, flowStack.peek());
764         if (listenersPresent) {
765             flowStack.push(ContainerTypeOrApplicationType.OTHER);
766             asyncHandler.handleStartTime(
767                     requestId, otherStartTime,
768                     ContainerTypeOrApplicationType.OTHER);
769             asyncHandler.handleEndTime(
770                     requestId, otherEndTime, flowStack.pop());
771         }
772     }
773     
774     private void storeRequestEndData(
775             String JavaDoc requestId,
776             FlowStack<ContainerTypeOrApplicationType> flowStack,
777             boolean listenersPresent, long otherStartTime, long otherEndTime) {
778         asyncHandler.handleEndTime(
779                 requestId, System.nanoTime(), flowStack.pop());
780         if (listenersPresent) {
781             flowStack.push(ContainerTypeOrApplicationType.OTHER);
782             asyncHandler.handleStartTime(
783                     requestId, otherStartTime,
784                     ContainerTypeOrApplicationType.OTHER);
785             asyncHandler.handleEndTime(
786                     requestId, otherEndTime, flowStack.pop());
787         }
788         asyncHandler.handleRequestEnd(requestId, System.nanoTime());
789     }
790     
791     private void storeMethodStartData(
792             String JavaDoc requestId, String JavaDoc methodName,
793             ComponentType componentType, String JavaDoc applicationName,
794             String JavaDoc moduleName, String JavaDoc componentName, String JavaDoc threadId,
795             String JavaDoc transactionId, String JavaDoc securityId,
796             FlowStack<ContainerTypeOrApplicationType> flowStack,
797             ContainerTypeOrApplicationType appType,
798             boolean listenersPresent, long otherStartTime, long otherEndTime) {
799         asyncHandler.handleEndTime(
800                 requestId, System.nanoTime(), flowStack.peek());
801         if (listenersPresent) {
802             flowStack.push(ContainerTypeOrApplicationType.OTHER);
803             asyncHandler.handleStartTime(
804                     requestId, otherStartTime,
805                     ContainerTypeOrApplicationType.OTHER);
806             asyncHandler.handleEndTime(
807                     requestId, otherEndTime, flowStack.pop());
808         }
809         asyncHandler.handleMethodStart(
810                 requestId, System.nanoTime(), methodName, componentType,
811                 applicationName, moduleName, componentName, threadId,
812                 transactionId, securityId);
813         flowStack.push(appType);
814         asyncHandler.handleStartTime(requestId, System.nanoTime(), appType);
815     }
816     
817     private void storeMethodEndData(
818             String JavaDoc requestId, Throwable JavaDoc exception,
819             FlowStack<ContainerTypeOrApplicationType> flowStack,
820             boolean listenersPresent, long otherStartTime, long otherEndTime) {
821         asyncHandler.handleEndTime(
822                 requestId, System.nanoTime(), flowStack.pop());
823         asyncHandler.handleMethodEnd(requestId, System.nanoTime(), exception);
824         if (listenersPresent) {
825             flowStack.push(ContainerTypeOrApplicationType.OTHER);
826             asyncHandler.handleStartTime(
827                     requestId, otherStartTime,
828                     ContainerTypeOrApplicationType.OTHER);
829             asyncHandler.handleEndTime(
830                     requestId, otherEndTime, flowStack.pop());
831         }
832         asyncHandler.handleStartTime(
833                 requestId, System.nanoTime(), flowStack.peek());
834     }
835     
836     private void storeStartTimeData(
837             String JavaDoc requestId,
838             ContainerTypeOrApplicationType type,
839             FlowStack<ContainerTypeOrApplicationType> flowStack) {
840         asyncHandler.handleEndTime(
841                 requestId, System.nanoTime(), flowStack.peek());
842         flowStack.push(type);
843         asyncHandler.handleStartTime(requestId, System.nanoTime(), type);
844     }
845     
846     private void storeEndTimeData(
847             String JavaDoc requestId,
848             FlowStack<ContainerTypeOrApplicationType> flowStack) {
849         asyncHandler.handleEndTime(
850                 requestId, System.nanoTime(), flowStack.pop());
851         asyncHandler.handleStartTime(
852                 requestId, System.nanoTime(), flowStack.peek());
853     }
854     
855     /** Data accessors. */
856     
857     public ThreadLocalData getThreadLocalData() {
858         return (ThreadLocalData) threadLocal.get();
859     }
860     
861     /** Support for notification. */
862     
863     public void registerListener(Listener listener) {
864         if (listeners.contains(listener) == false) {
865             listeners.add(listener);
866         }
867     }
868     
869     public void unregisterListener(Listener listener) {
870         listeners.remove(listener);
871     }
872     
873     /** API to support AMX MBean calls. */
874     
875     public synchronized void setEnable(boolean enable) {
876         if (enable) {
877             if (this.storeData == false){
878                 boolean result = this.dbAccessObject.enable();
879                 if (result) {
880                     enableGrizzly(true);
881                     logger.log(Level.INFO, "callflow.enable_succeeded");
882                 } else {
883                     logger.log(Level.SEVERE, "callflow.enable_failed");
884                     throw new RuntimeException JavaDoc("Callflow Enable Failed");
885                 }
886             }
887         } else {
888             if (this.storeData == true) {
889                 this.callerIPAddressFilter = null;
890                 this.callerPrincipalFilter = null;
891                 enableGrizzly (false);
892                 this.dbAccessObject.disable();
893                 logger.log(Level.INFO, "callflow.disable_succeeded");
894             }
895         }
896         this.storeData = enable;
897     }
898     
899     public synchronized boolean isEnabled() {
900         return this.storeData;
901     }
902     
903     public void enableGrizzly(boolean enable) {
904         List JavaDoc<GrizzlyConfig> grizzlies = GrizzlyConfig.getGrizzlyConfigInstances();
905         for (GrizzlyConfig grizzly: grizzlies){
906             grizzly.setEnableCallFlow(true);
907         }
908         
909     }
910     public void setCallerIPFilter(String JavaDoc ipAddress) {
911         this.callerIPAddressFilter = ipAddress;
912         if ((callerIPAddressFilter != null) &&
913                 (callerIPAddressFilter.equals(""))) {
914             callerIPAddressFilter = null;
915         }
916     }
917     
918     public String JavaDoc getCallerIPFilter() {
919         return this.callerIPAddressFilter;
920     }
921     
922     public void setCallerPrincipalFilter(String JavaDoc callerPrincipal) {
923         this.callerPrincipalFilter = callerPrincipal;
924         if ((callerPrincipalFilter != null) &&
925                 (callerPrincipalFilter.equals(""))) {
926             callerPrincipalFilter = null;
927         }
928     }
929     
930     public String JavaDoc getCallerPrincipalFilter() {
931         return this.callerPrincipalFilter;
932     }
933     
934     public void clearData() {
935         if (this.storeData == false){
936             dbAccessObject.clearData();
937         } else {
938             logger.log(
939                     Level.WARNING,
940                     "callflow.turn_off_callflow_before_clearData");
941         }
942     }
943     
944     public boolean deleteRequestIds(String JavaDoc[] requestIds) {
945         return dbAccessObject.deleteRequestIds(requestIds);
946     }
947     
948     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getRequestInformation() {
949         return dbAccessObject.getRequestInformation();
950     }
951     
952     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getCallStackForRequest(String JavaDoc requestId) {
953         return dbAccessObject.getCallStackInformation(requestId);
954     }
955     
956     public java.util.Map JavaDoc<String JavaDoc, String JavaDoc> getPieInformation(String JavaDoc requestID) {
957         return dbAccessObject.getPieInformation(requestID);
958     }
959 }
960
Popular Tags