001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.xnet.provider.jsse;
022:
023: import java.io.PrintStream;
024: import java.security.AccessController;
025: import java.security.PrivilegedAction;
026:
027: /**
028: * This class provides debug logging for JSSE provider implementation
029: * TODO: Use java.util.logging
030: */
031: public class Logger {
032:
033: public static class Stream extends PrintStream {
034: private final String prefix;
035: private static int indent = 0;
036:
037: public Stream(String name) {
038: super (System.err);
039: prefix = name + "[" + Thread.currentThread().getName()
040: + "] ";
041: }
042:
043: public void print(String msg) {
044: for (int i = 0; i < indent; i++) {
045: super .print(" ");
046: }
047: super .print(msg);
048: }
049:
050: public void newIndent() {
051: indent++;
052: }
053:
054: public void endIndent() {
055: indent--;
056: }
057:
058: public void println(String msg) {
059: print(prefix);
060: super .println(msg);
061: }
062:
063: public void print(byte[] data) {
064: printAsHex(16, " ", "", data, 0, data.length);
065: }
066:
067: public void print(byte[] data, int offset, int len) {
068: printAsHex(16, " ", "", data, offset, len);
069: }
070:
071: public void printAsHex(int perLine, String prefix,
072: String delimiter, byte[] data) {
073: printAsHex(perLine, prefix, delimiter, data, 0, data.length);
074: }
075:
076: public void printAsHex(int perLine, String prefix,
077: String delimiter, byte[] data, int offset, int len) {
078: String line = "";
079: for (int i = 0; i < len; i++) {
080: String tail = Integer.toHexString(
081: 0x00ff & data[i + offset]).toUpperCase();
082: if (tail.length() == 1) {
083: tail = "0" + tail;
084: }
085: line += prefix + tail + delimiter;
086:
087: if (((i + 1) % perLine) == 0) {
088: super .println(line);
089: line = "";
090: }
091: }
092: super .println(line);
093: }
094: }
095:
096: private static String[] names;
097:
098: static {
099: try {
100: names = AccessController
101: .doPrivileged(new PrivilegedAction<String[]>() {
102: public String[] run() {
103: return System.getProperty("jsse", "")
104: .split(",");
105: }
106: });
107: } catch (Exception e) {
108: names = new String[0];
109: }
110: }
111:
112: public static Stream getStream(String name) {
113: for (int i = 0; i < names.length; i++) {
114: if (names[i].equals(name)) {
115: return new Stream(name);
116: }
117: }
118: return null;
119: }
120: }
|