1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  package org.abstracthorizon.danube.support.logging;
14  
15  import org.abstracthorizon.danube.connection.Connection;
16  import org.abstracthorizon.danube.connection.ConnectionWrapper;
17  
18  import java.io.InputStream;
19  import java.io.OutputStream;
20  
21  
22  
23  
24  
25  
26  public class LoggingConnection extends ConnectionWrapper {
27  
28      
29      protected LoggingInputStream inputStream;
30  
31      
32      protected LoggingOutputStream outputStream;
33  
34      
35      protected OutputStream logOutputStream;
36  
37      
38      private boolean logging = true;
39  
40      
41      private boolean temporaryLog = true;
42  
43      
44  
45  
46  
47  
48  
49  
50  
51      public LoggingConnection(Connection connection, OutputStream logOutputStream, boolean directional, boolean temporaryLog) {
52          super(connection);
53          this.logOutputStream = logOutputStream;
54          this.temporaryLog = temporaryLog;
55          InputStream originalInputStream = (InputStream)connection.adapt(InputStream.class);
56          OutputStream originalOutputStream = (OutputStream)connection.adapt(OutputStream.class);
57          if (directional) {
58              inputStream = new DirectionalLoggingInputStream(originalInputStream, logOutputStream);
59              outputStream = new DirectionalLoggingOutputStream(originalOutputStream, logOutputStream);
60          } else {
61              inputStream = new LoggingInputStream(originalInputStream, logOutputStream);
62              outputStream = new LoggingOutputStream(originalOutputStream, logOutputStream);
63          }
64      }
65  
66      @SuppressWarnings("unchecked")
67      @Override
68      public <T> T adapt(Class<T> cls) {
69          if (cls == InputStream.class) {
70              return (T)getInputStream();
71          } else if (cls == OutputStream.class) {
72              return (T)getOutputStream();
73          } else if (cls == LoggingInputStream.class) {
74              return (T)this;
75          }
76          return super.adapt(cls);
77      }
78  
79      
80  
81  
82  
83      public InputStream getInputStream() {
84          return inputStream;
85      }
86  
87      
88  
89  
90  
91      public OutputStream getOutputStream() {
92          return outputStream;
93      }
94  
95      
96  
97  
98  
99  
100     public OutputStream getDebugOutputStream() {
101         return logOutputStream;
102     }
103 
104     
105 
106 
107 
108     public void setLogging(boolean logging) {
109         this.logging = logging;
110         inputStream.setLogging(logging);
111         outputStream.setLogging(logging);
112     }
113 
114     
115 
116 
117 
118     public boolean isLogging() {
119         return logging;
120     }
121 
122     
123 
124 
125 
126     public void setTemporaryLog(boolean temporaryLog) {
127         this.temporaryLog = temporaryLog;
128     }
129 
130     
131 
132 
133 
134     public boolean isTermporaryLog() {
135         return temporaryLog;
136     }
137 
138 }