NamingUtils.java revision 672:2bb058ce572e
1/*
2 * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.naming.cosnaming;
27
28import java.io.*;
29import org.omg.CosNaming.NameComponent;
30
31
32public class NamingUtils {
33    // Do not instantiate this class
34    private NamingUtils() {};
35
36    /**
37     * Debug flag which must be true for debug streams to be created and
38     * dprint output to be generated.
39     */
40    public static boolean debug = false;
41
42    /**
43     * Prints the message to the debug stream if debugging is enabled.
44     * @param msg the debug message to print.
45     */
46    public static void dprint(String msg) {
47        if (debug && debugStream != null)
48            debugStream.println(msg);
49    }
50
51    /**
52     * Prints the message to the error stream (System.err is default).
53     * @param msg the error message to print.
54     */
55    public static void errprint(String msg) {
56        if (errStream != null)
57            errStream.println(msg);
58        else
59            System.err.println(msg);
60    }
61
62    /**
63     * Prints the stacktrace of the supplied exception to the error stream.
64     * @param e any Java exception.
65     */
66    public static void printException(java.lang.Exception e) {
67        if (errStream != null)
68            e.printStackTrace(errStream);
69        else
70            e.printStackTrace();
71    }
72
73    /**
74     * Create a debug print stream to the supplied log file.
75     * @param logFile the file to which debug output will go.
76     * @exception IOException thrown if the file cannot be opened for output.
77     */
78    public static void makeDebugStream(File logFile)
79        throws java.io.IOException {
80        // Create an outputstream for debugging
81        java.io.OutputStream logOStream =
82            new java.io.FileOutputStream(logFile);
83        java.io.DataOutputStream logDStream =
84            new java.io.DataOutputStream(logOStream);
85        debugStream = new java.io.PrintStream(logDStream);
86
87        // Emit first message
88        debugStream.println("Debug Stream Enabled.");
89    }
90
91    /**
92     * Create a error print stream to the supplied file.
93     * @param errFile the file to which error messages will go.
94     * @exception IOException thrown if the file cannot be opened for output.
95     */
96    public static void makeErrStream(File errFile)
97        throws java.io.IOException {
98        if (debug) {
99            // Create an outputstream for errors
100            java.io.OutputStream errOStream =
101                new java.io.FileOutputStream(errFile);
102            java.io.DataOutputStream errDStream =
103                new java.io.DataOutputStream(errOStream);
104            errStream = new java.io.PrintStream(errDStream);
105            dprint("Error stream setup completed.");
106        }
107    }
108
109
110    /**
111     * A utility method that takes Array of NameComponent and converts
112     * into a directory structured name in the format of /id1.kind1/id2.kind2..
113     * This is used mainly for Logging.
114     */
115    static String getDirectoryStructuredName( NameComponent[] name ) {
116        StringBuffer directoryStructuredName = new StringBuffer("/");
117        for( int i = 0; i < name.length; i++ ) {
118            directoryStructuredName.append( name[i].id + "." + name[i].kind );
119        }
120        return directoryStructuredName.toString( );
121    }
122
123    /**
124     * The debug printstream.
125     */
126    public static java.io.PrintStream debugStream;
127
128    /**
129     * The error printstream.
130     */
131    public static java.io.PrintStream errStream;
132}
133