View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * Logging connection
23   *
24   * @author Daniel Sendula
25   */
26  public class LoggingConnection extends ConnectionWrapper {
27  
28      /** Wrapped input stream */
29      protected LoggingInputStream inputStream;
30  
31      /** Wrapped output stream */
32      protected LoggingOutputStream outputStream;
33  
34      /** Log output stream */
35      protected OutputStream logOutputStream;
36  
37      /** Is logggin switched on or not */
38      private boolean logging = true;
39  
40      /** Is log demporary or not */
41      private boolean temporaryLog = true;
42  
43      /**
44       * Constructor.
45       *
46       * @param connection wrapped connection
47       * @param logOutputStream log output stream
48       * @param directional is logging directional
49       * @param temporaryLog is log temporary log
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       * Retruns logged input stream
81       * @return logged input stream
82       */
83      public InputStream getInputStream() {
84          return inputStream;
85      }
86  
87      /**
88       * Returns logged output stream
89       * @return logged output stream
90       */
91      public OutputStream getOutputStream() {
92          return outputStream;
93      }
94  
95      /**
96       * Returns logger output stream. It is useful for
97       * making other log statements directly to it.
98       * @return logger output stream
99       */
100     public OutputStream getDebugOutputStream() {
101         return logOutputStream;
102     }
103 
104     /**
105      * Turns logging on or off
106      * @param logging set to <code>true</code> if logging is switched on
107      */
108     public void setLogging(boolean logging) {
109         this.logging = logging;
110         inputStream.setLogging(logging);
111         outputStream.setLogging(logging);
112     }
113 
114     /**
115      * Returns if logging is switched on or off
116      * @return <code>true<code> logging is switched on or off
117      */
118     public boolean isLogging() {
119         return logging;
120     }
121 
122     /**
123      * Sets flag if log is temporary or not
124      * @param temporaryLog is log temporary or not
125      */
126     public void setTemporaryLog(boolean temporaryLog) {
127         this.temporaryLog = temporaryLog;
128     }
129 
130     /**
131      * Returns is logging temporary or not
132      * @return is logging temporary or not
133      */
134     public boolean isTermporaryLog() {
135         return temporaryLog;
136     }
137 
138 }