Coverage Report - argos.logging.Console
 
Classes in this File Line Coverage Branch Coverage Complexity
Console
0%
0/109
0%
0/20
0
 
 1  
 /*
 2  
 Copyright (c) 2006, University of Tromsø
 3  
 All rights reserved.
 4  
 
 5  
 Redistribution and use in source and binary forms, with or without 
 6  
 modification, are permitted provided that the following conditions are met:
 7  
 
 8  
  * Redistributions of source code must retain the above copyright notice, this list 
 9  
    of conditions and the following disclaimer.
 10  
 
 11  
  * Redistributions in binary form must reproduce the above copyright notice, this 
 12  
    list of conditions and the following disclaimer in the documentation and/or other 
 13  
    materials provided with the distribution.
 14  
 
 15  
  * Neither the name of the University of Tromsø nor the names of its contributors may 
 16  
    be used to endorse or promote products derived from this software without specific 
 17  
    prior written permission.
 18  
 
 19  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 20  
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 21  
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 22  
 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 23  
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 24  
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 25  
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 26  
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 27  
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 28  
 DAMAGE.
 29  
 */
 30  
 
 31  
 package argos.logging;
 32  
 
 33  
 import java.io.PrintStream;
 34  
 import java.util.ArrayList;
 35  
 import java.util.List;
 36  
 
 37  
 import javax.management.MBeanNotificationInfo;
 38  
 import javax.management.Notification;
 39  
 
 40  
 import argos.annotation.Description;
 41  
 import argos.annotation.InstrumentThisClassOnly;
 42  
 import argos.annotation.NotificationSender;
 43  
 import argos.annotation.RemoveInstrumentation;
 44  
 import argos.proxy.NotificationProxy;
 45  
 
 46  
 @InstrumentThisClassOnly
 47  
 @Description("Console output")
 48  
 public class Console extends PrintStream {
 49  0
         public static final Console console = new Console();
 50  
 
 51  
         public static final String OUTPUT = "Output";
 52  
         
 53  
         @NotificationSender public NotificationProxy proxy;
 54  
         
 55  
         private PrintStream error;
 56  
         private List<String> strings;
 57  
         private boolean exiting;
 58  
         private long sequence;
 59  
         
 60  
         private Console() {
 61  0
                 super(System.out);
 62  0
                 error = System.err;
 63  
                 
 64  0
                 sequence = 0;
 65  
                 
 66  0
                 strings = new ArrayList<String>();
 67  0
                 strings.add(" INFO org.mortbay.log - Logging to org.slf4j.impl.SimpleLogger");
 68  0
                 strings.add(" INFO org.mortbay.log - Started SocketConnector @");
 69  0
                 strings.add(" INFO org.mortbay.log - jetty-6.1.1");
 70  0
                 strings.add("::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog");
 71  0
                 strings.add("::INFO:  jetty-6.1.1");
 72  0
                 strings.add("::INFO:  Started SocketConnector @");
 73  0
                 strings.add("INFO:  jsp: init");
 74  0
                 strings.add("INFO:  default: init");
 75  0
                 strings.add(":INFO:  default: destroy");
 76  0
                 strings.add("Creating new servlet engine config file: /WEB-INF/server-config.wsdd");
 77  0
                 strings.add("[JavaUtils] Warning: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.");
 78  0
                 strings.add("[EngineConfigurationFactoryServlet] Warning: Unable to load/create servlet engine config file, attempting internal default (from jar).");
 79  0
                 strings.add("started and ready to accept connections on port");
 80  0
                 strings.add(" - (485682) shutdown at");
 81  0
                 strings.add("Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart)");
 82  0
                 strings.add("log4j:WARN");
 83  
                 
 84  0
                 System.setOut(this);
 85  0
                 exiting = false;
 86  0
         }
 87  
         
 88  
         @Override
 89  
     @RemoveInstrumentation
 90  
     public void write(byte[] buf, int off, int len) {
 91  0
                 if(exiting) {
 92  0
                         return;
 93  
                 }
 94  0
             String s = new String(buf);
 95  0
             if(!contains(new String(buf))) {
 96  0
                     if(s.indexOf("] Severe: ") == -1 && s.indexOf("] Warning: ") == -1) {
 97  0
                         super.write(buf, off, len);
 98  
                 }
 99  
                     else {
 100  0
                             error.write(buf, off, len);
 101  
                     }
 102  0
                     send(new String(buf, off, len));
 103  
             }
 104  0
     }
 105  
     
 106  
     private boolean contains(String s) {
 107  0
             for(String s2 : strings) {
 108  0
                     if(s.indexOf(s2) != -1) {
 109  0
                             return true;
 110  
                     }
 111  
             }
 112  0
             return false;
 113  
     }
 114  
     
 115  
     @Override
 116  
     @RemoveInstrumentation
 117  
     public void println() {
 118  0
             byte[] ln = {'\r', '\n'};
 119  0
             write(ln, 0, ln.length);
 120  0
     }
 121  
     
 122  
     @Override
 123  
     @RemoveInstrumentation
 124  
     public void print(Object o) {
 125  0
             String s = o.toString();
 126  0
             write(s.getBytes(), 0, s.length());
 127  0
     }
 128  
     
 129  
     @Override
 130  
     @RemoveInstrumentation
 131  
     public void print(String s) {
 132  0
             write(s.getBytes(), 0, s.length());
 133  0
     }
 134  
     
 135  
     @Override
 136  
     @RemoveInstrumentation
 137  
     public void print(boolean b) {
 138  
             String s;
 139  0
             if(b) {
 140  0
                     s = "true";
 141  
             }
 142  
             else {
 143  0
                     s= "false";
 144  
             }
 145  0
             write(s.getBytes(), 0, s.length());
 146  0
     }
 147  
     
 148  
     @Override
 149  
     @RemoveInstrumentation
 150  
     public void print(char c) {
 151  0
             String s = Character.toString(c);
 152  0
             write(s.getBytes(), 0, s.length());
 153  0
     }
 154  
     
 155  
     @Override
 156  
     @RemoveInstrumentation
 157  
     public void print(char[] c) {
 158  0
             String s = new String(c);
 159  0
             write(s.getBytes(), 0, s.length());
 160  0
     }
 161  
     
 162  
     @Override
 163  
     @RemoveInstrumentation
 164  
     public void print(double d) {
 165  0
             String s = Double.toString(d);
 166  0
             write(s.getBytes(), 0, s.length());
 167  0
     }
 168  
     
 169  
     @Override
 170  
     @RemoveInstrumentation
 171  
     public void print(float f) {
 172  0
             String s = Float.toString(f);
 173  0
             write(s.getBytes(), 0, s.length());
 174  0
     }
 175  
     
 176  
     @Override
 177  
     @RemoveInstrumentation
 178  
     public void print(int i) {
 179  0
             String s = Integer.toString(i);
 180  0
             write(s.getBytes(), 0, s.length());
 181  0
     }
 182  
     
 183  
     @Override
 184  
     @RemoveInstrumentation
 185  
     public void print(long l) {
 186  0
             String s = Long.toString(l);
 187  0
             write(s.getBytes(), 0, s.length());
 188  0
     }
 189  
     
 190  
     @Override
 191  
     @RemoveInstrumentation
 192  
     public void println(Object o) {
 193  0
             print(o);
 194  0
             println();
 195  0
             byte[] ln = {'o'};
 196  0
             write(ln, 0, ln.length);
 197  0
     }
 198  
     
 199  
     @Override
 200  
     @RemoveInstrumentation
 201  
     public void println(String s) {
 202  0
             if(s == null) {
 203  0
                     println("null");
 204  
             }
 205  0
             else if(!contains(s)) {
 206  0
                     print(s);
 207  0
                     println();
 208  
             }
 209  0
     }
 210  
     
 211  
     @Override
 212  
     @RemoveInstrumentation
 213  
     public void println(boolean b) {
 214  0
             print(b);
 215  0
             println();
 216  0
     }
 217  
     
 218  
     @Override
 219  
     @RemoveInstrumentation
 220  
     public void println(char c) {
 221  0
             print(c);
 222  0
             println();
 223  0
     }
 224  
     
 225  
     @Override
 226  
     @RemoveInstrumentation
 227  
     public void println(char[] c) {
 228  0
             print(c);
 229  0
             println();
 230  0
     }
 231  
     
 232  
     @Override
 233  
     @RemoveInstrumentation
 234  
     public void println(double d) {
 235  0
             print(d);
 236  0
             println();
 237  0
     }
 238  
     
 239  
     @Override
 240  
     @RemoveInstrumentation
 241  
     public void println(float f) {
 242  0
             print(f);
 243  0
             println();
 244  0
     }
 245  
     
 246  
     @Override
 247  
     @RemoveInstrumentation
 248  
     public void println(int i) {
 249  0
             print(i);
 250  0
             println();
 251  0
     }
 252  
     
 253  
     @Override
 254  
     @RemoveInstrumentation
 255  
     public void println(long l) {
 256  0
             print(l);
 257  0
             println();
 258  0
     }
 259  
     
 260  
     @RemoveInstrumentation
 261  
     public void setExiting() {
 262  0
             exiting = true;
 263  0
     }
 264  
     
 265  
     public MBeanNotificationInfo[] getNotificationInfo() {
 266  0
                 MBeanNotificationInfo[] not = new MBeanNotificationInfo[1];
 267  0
                 not[0] = new MBeanNotificationInfo(new String[]{OUTPUT}, 
 268  
                                 Notification.class.getClass().getName(), "Emitted Something is written to console.");
 269  0
                 return not;
 270  
         }
 271  
     
 272  
     private void send(String text) {
 273  0
                 Notification not = new Notification(OUTPUT, Notification.class.getClass().getName(), 
 274  
                                 sequence++, text);
 275  0
                 if(proxy != null) {
 276  0
                         proxy.send(not);
 277  
                 }
 278  0
         }
 279  
 }