1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.xnet.provider.jsse; 19 20 import java.io.PrintStream; 21 import java.security.AccessController; 22 import java.security.PrivilegedAction; 23 24 /** 25 * This class provides debug logging for JSSE provider implementation 26 * TODO: Use java.util.logging 27 */ 28 public class Logger { 29 30 public static class Stream extends PrintStream { 31 private final String prefix; 32 private static int indent = 0; 33 34 public Stream(String name) { 35 super(System.err); 36 prefix = name + "["+Thread.currentThread().getName()+"] "; 37 } 38 39 @Override 40 public void print(String msg) { 41 for (int i=0; i<indent; i++) { 42 super.print(" "); 43 } 44 super.print(msg); 45 } 46 47 public void newIndent() { 48 indent ++; 49 } 50 51 public void endIndent() { 52 indent --; 53 } 54 55 @Override 56 public void println(String msg) { 57 print(prefix); 58 super.println(msg); 59 } 60 61 public void print(byte[] data) { 62 printAsHex(16, " ", "", data, 0, data.length); 63 } 64 65 public void print(byte[] data, int offset, int len) { 66 printAsHex(16, " ", "", data, offset, len); 67 } 68 69 public void printAsHex(int perLine, 70 String prefix, 71 String delimiter, 72 byte[] data) { 73 printAsHex(perLine, prefix, delimiter, data, 0, data.length); 74 } 75 76 public void printAsHex(int perLine, 77 String prefix, 78 String delimiter, 79 byte[] data, int offset, int len) { 80 String line = ""; 81 for (int i=0; i<len; i++) { 82 String tail = 83 Integer.toHexString(0x00ff & data[i+offset]).toUpperCase(); 84 if (tail.length() == 1) { 85 tail = "0" + tail; 86 } 87 line += prefix + tail + delimiter; 88 89 if (((i+1)%perLine) == 0) { 90 super.println(line); 91 line = ""; 92 } 93 } 94 super.println(line); 95 } 96 } 97 98 private static String[] names; 99 100 static { 101 try { 102 names = AccessController 103 .doPrivileged(new PrivilegedAction<String[]>() { 104 public String[] run() { 105 return System.getProperty("jsse", "").split(","); 106 } 107 }); 108 } catch (Exception e) { 109 names = new String[0]; 110 } 111 } 112 113 public static Stream getStream(String name) { 114 for (int i=0; i<names.length; i++) { 115 if (names[i].equals(name)) { 116 return new Stream(name); 117 } 118 } 119 return null; 120 } 121 } 122 123