summaryrefslogtreecommitdiffstats
path: root/simple/simple-transport/src/main/java/org/simpleframework/transport/connect/SocketAnalyzer.java
blob: be6b95c752b19b29b6fe94dee247ea19526b1bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * SocketAnalyzer.java February 2012
 *
 * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.transport.connect;

import java.nio.channels.SelectableChannel;

import org.simpleframework.transport.trace.TraceAnalyzer;
import org.simpleframework.transport.trace.Trace;

/**
 * The <code>SocketAnalyzer</code> is used to wrap an analyzer object. 
 * Wrapping an analyzer in this way ensures that even if the analyzer 
 * is badly written there is little chance that it will affect the
 * operation of the server. All <code>Trace</code> objects returned
 * from this will catch all exceptions within the created trace.
 * 
 * @author Niall Gallagher
 */
class SocketAnalyzer implements TraceAnalyzer {
   
   /**
    * This is the analyzer that is used to create the trace objects.
    */
   private final TraceAnalyzer analyzer;
   
   /**
    * Constructor for the <code>SocketAnalyzer</code> object. This will
    * be given the analyzer that is to be used to create traces. This 
    * can be a null value, in which case the trace provided will be
    * a simple empty void that swallows all trace events.
    * 
    * @param analyzer the analyzer that is to be wrapped by this
    */
   public SocketAnalyzer(TraceAnalyzer analyzer) {
      this.analyzer = analyzer;
   }
 
   /**
    * This method is used to attach a trace to the specified channel.
    * Attaching a trace basically means associating events from that
    * trace with the specified socket. It ensures that the events 
    * from a specific channel can be observed in isolation.
    * 
    * @param channel this is the channel to associate with the trace
    * 
    * @return this returns a trace associated with the channel
    */
   public Trace attach(SelectableChannel channel) {
      Trace trace = null;
      
      if(analyzer != null) {
         trace = analyzer.attach(channel);
      }
      return new SocketTrace(trace);
   }
   
   /**
    * This is used to stop the analyzer and clear all trace information.
    * Stopping the analyzer is typically done when the server is stopped
    * and is used to free any resources associated with the analyzer. If
    * an analyzer does not hold information this method can be ignored.
    */
   public void stop() {
      if(analyzer != null) {
         analyzer.stop();
      }
   }
}