1. /*
  2. * @(#)NamingUtils.java 1.18 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.naming.cosnaming;
  8. import java.io.*;
  9. import org.omg.CosNaming.NameComponent;
  10. public class NamingUtils {
  11. // Do not instantiate this class
  12. private NamingUtils() {};
  13. /**
  14. * Debug flag which must be true for debug streams to be created and
  15. * dprint output to be generated.
  16. */
  17. public static boolean debug = false;
  18. /**
  19. * Prints the message to the debug stream if debugging is enabled.
  20. * @param msg the debug message to print.
  21. */
  22. public static void dprint(String msg) {
  23. if (debug && debugStream != null)
  24. debugStream.println(msg);
  25. }
  26. /**
  27. * Prints the message to the error stream (System.err is default).
  28. * @param msg the error message to print.
  29. */
  30. public static void errprint(String msg) {
  31. if (errStream != null)
  32. errStream.println(msg);
  33. else
  34. System.err.println(msg);
  35. }
  36. /**
  37. * Prints the stacktrace of the supplied exception to the error stream.
  38. * @param e any Java exception.
  39. */
  40. public static void printException(java.lang.Exception e) {
  41. if (errStream != null)
  42. e.printStackTrace(errStream);
  43. else
  44. e.printStackTrace();
  45. }
  46. /**
  47. * Create a debug print stream to the supplied log file.
  48. * @param logFile the file to which debug output will go.
  49. * @exception IOException thrown if the file cannot be opened for output.
  50. */
  51. public static void makeDebugStream(File logFile)
  52. throws java.io.IOException {
  53. // Create an outputstream for debugging
  54. java.io.OutputStream logOStream =
  55. new java.io.FileOutputStream(logFile);
  56. java.io.DataOutputStream logDStream =
  57. new java.io.DataOutputStream(logOStream);
  58. debugStream = new java.io.PrintStream(logDStream);
  59. // Emit first message
  60. debugStream.println("Debug Stream Enabled.");
  61. }
  62. /**
  63. * Create a error print stream to the supplied file.
  64. * @param logFile the file to which error messages will go.
  65. * @exception IOException thrown if the file cannot be opened for output.
  66. */
  67. public static void makeErrStream(File errFile)
  68. throws java.io.IOException {
  69. if (debug) {
  70. // Create an outputstream for errors
  71. java.io.OutputStream errOStream =
  72. new java.io.FileOutputStream(errFile);
  73. java.io.DataOutputStream errDStream =
  74. new java.io.DataOutputStream(errOStream);
  75. errStream = new java.io.PrintStream(errDStream);
  76. dprint("Error stream setup completed.");
  77. }
  78. }
  79. /**
  80. * A utility method that takes Array of NameComponent and converts
  81. * into a directory structured name in the format of /id1.kind1/id2.kind2..
  82. * This is used mainly for Logging.
  83. */
  84. static String getDirectoryStructuredName( NameComponent[] name ) {
  85. StringBuffer directoryStructuredName = new StringBuffer("/");
  86. for( int i = 0; i < name.length; i++ ) {
  87. directoryStructuredName.append( name[i].id + "." + name[i].kind );
  88. }
  89. return directoryStructuredName.toString( );
  90. }
  91. /**
  92. * The debug printstream.
  93. */
  94. public static java.io.PrintStream debugStream;
  95. /**
  96. * The error printstream.
  97. */
  98. public static java.io.PrintStream errStream;
  99. }