View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.remote.file;
17  
18  import org.jdiagnose.DiagnosticMessage;
19  import org.jdiagnose.RemoteResult;
20  import org.jdiagnose.ResultInfo;
21  import org.jdiagnose.remote.ExceptionListener;
22  import org.jdiagnose.remote.RemoteResultException;
23  import org.jdiagnose.remote.RemoteResultListener;
24  import org.jdiagnose.remote.RemoteResultSender;
25  import org.jdiagnose.remote.StdErrExceptionListener;
26  
27  /***
28   * @author jmccrindle
29   */
30  public class CsvSender implements RemoteResultSender, RemoteResultListener {
31  
32      private Emitter emitter = null;
33      private ExceptionListener exceptionListener = StdErrExceptionListener.DEFAULT_STD_ERR_EXCEPTION_LISTENER;
34      
35      public CsvSender() {
36          
37      }
38      
39      protected String encode(String in) {
40          StringBuffer out = new StringBuffer(in.length());
41          boolean controlCharFound = false;
42          for(int i = 0; i < in.length(); i++) {
43              char c = in.charAt(i);
44              switch (c) {
45                  case ',': controlCharFound = true; break;
46                  case '"': out.append('"'); controlCharFound = true; break;
47              }
48              switch (c) {
49                  case '\r': 
50                  case '\n': out.append(' '); break;
51                  default: out.append(c); 
52              }
53              
54          }
55          if(controlCharFound) {
56              return '"' + out.toString() + '"';
57          } else {
58              return out.toString();
59          }
60      }
61  
62      /* (non-Javadoc)
63       * @see org.jdiagnose.remote.RemoteResultSender#send(org.jdiagnose.RemoteResult)
64       */
65      public void send(RemoteResult result) throws RemoteResultException {
66          StringBuffer buffer = new StringBuffer();
67          buffer.append(encode(result.getGuid()));
68          buffer.append(",");
69          buffer.append(encode(result.getAgent()));
70          buffer.append(",");
71          buffer.append(encode(result.getHost()));
72          buffer.append(",");
73          buffer.append(Long.toString(result.getSequenceNumber()));
74          buffer.append(",");
75          ResultInfo resultInfo = result.getResultInfo();
76          buffer.append(encode(resultInfo.getName()));
77          buffer.append(",");
78          buffer.append(encode(resultInfo.getState().toString()));
79          buffer.append(",");
80          buffer.append(Long.toString(resultInfo.getStartTime()));
81          buffer.append(",");
82          buffer.append(Long.toString(resultInfo.getFinishTime()));
83          buffer.append(",");
84          buffer.append(Long.toString(resultInfo.getDuration()));
85          buffer.append(",");
86          DiagnosticMessage message = resultInfo.getMessage();
87          buffer.append(",");
88          if(message != null) {
89              buffer.append(encode(message.getSummary()));
90          }
91          emitter.emit(buffer);
92      }
93  
94      public void setEmitter(Emitter emitter) {
95          this.emitter = emitter;
96      }
97  
98      /* (non-Javadoc)
99       * @see org.jdiagnose.remote.RemoteResultListener#onRemoteResult(org.jdiagnose.RemoteResult)
100      */
101     public void onRemoteResult(RemoteResult remoteResult) {
102         try {
103             this.send(remoteResult);
104         } catch (RemoteResultException e) {
105             exceptionListener.onException(e);
106         }
107     }
108 }