1. /*
  2. * @(#)file DefaultPaths.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.13
  5. * @(#)lastedit 03/12/19
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. */
  10. package com.sun.jmx.snmp.defaults;
  11. // java import
  12. //
  13. import java.io.File;
  14. import java.io.BufferedReader;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.util.StringTokenizer;
  18. /**
  19. * This class represents a set of default directories used by Java DMK.
  20. *
  21. * <p><b>This API is a Sun Microsystems internal API and is subject
  22. * to change without notice.</b></p>
  23. * @since 1.5
  24. */
  25. public class DefaultPaths {
  26. private static final String INSTALL_PATH_RESOURCE_NAME = "com/sun/jdmk/defaults/install.path";
  27. // private constructor defined to "hide" the default public constructor
  28. private DefaultPaths() {
  29. }
  30. // PUBLIC STATIC METHODS
  31. //----------------------
  32. /**
  33. * Returns the installation directory for Java DMK.
  34. *
  35. * The default value of the installation directory is:
  36. * <CODE><base_dir> + File.separator + SUNWjdmk + File.separator + jdmk5.0 </CODE>
  37. *
  38. * @return Java DMK installation directory.
  39. */
  40. public static String getInstallDir() {
  41. if (installDir == null)
  42. return useRessourceFile();
  43. else
  44. return installDir;
  45. }
  46. /**
  47. * Returns the installation directory for Java DMK concatenated with dirname.
  48. *
  49. * The default value of the installation directory is:
  50. * <CODE><base_dir> + File.separator + SUNWjdmk + File.separator + jdmk5.0 </CODE>
  51. *
  52. * @param dirname The directory to be appended.
  53. *
  54. * @return Java DMK installation directory + <CODE>File.separator</CODE> + <CODE>dirname</CODE>.
  55. */
  56. public static String getInstallDir(String dirname) {
  57. if (installDir == null) {
  58. if (dirname == null) {
  59. return getInstallDir();
  60. } else {
  61. return getInstallDir() + File.separator + dirname;
  62. }
  63. } else {
  64. if (dirname == null) {
  65. return installDir;
  66. } else {
  67. return installDir + File.separator + dirname;
  68. }
  69. }
  70. }
  71. /**
  72. * Sets the installation directory for Java DMK.
  73. *
  74. * @param dirname The directory where Java DMK resides.
  75. */
  76. public static void setInstallDir(String dirname) {
  77. installDir = dirname;
  78. }
  79. /**
  80. * Returns the <CODE>etc</CODE> directory for Java DMK.
  81. * <P>
  82. * The default value of the <CODE>etc</CODE> directory is:
  83. * <UL>
  84. * <LI><CODE>DefaultPaths.getInstallDir("etc")</CODE>.
  85. * </UL>
  86. *
  87. * @return Java DMK <CODE>etc</CODE> directory.
  88. */
  89. public static String getEtcDir() {
  90. if (etcDir == null)
  91. return getInstallDir("etc");
  92. else
  93. return etcDir;
  94. }
  95. /**
  96. * Returns the <CODE>etc</CODE> directory for Java DMK concatenated with dirname.
  97. * <P>
  98. * The default value of the <CODE>etc</CODE> directory is:
  99. * <UL>
  100. * <LI><CODE>DefaultPaths.getInstallDir("etc")</CODE>.
  101. * </UL>
  102. *
  103. * @param dirname The directory to be appended.
  104. *
  105. * @return Java DMK <CODE>etc</CODE> directory + <CODE>File.separator</CODE> + <CODE>dirname</CODE>.
  106. */
  107. public static String getEtcDir(String dirname) {
  108. if (etcDir == null) {
  109. if (dirname == null) {
  110. return getEtcDir();
  111. } else {
  112. return getEtcDir() + File.separator + dirname;
  113. }
  114. } else {
  115. if (dirname == null) {
  116. return etcDir;
  117. } else {
  118. return etcDir + File.separator + dirname;
  119. }
  120. }
  121. }
  122. /**
  123. * Sets the <CODE>etc</CODE> directory for Java DMK.
  124. *
  125. * @param dirname The <CODE>etc</CODE> directory for Java DMK.
  126. */
  127. public static void setEtcDir(String dirname) {
  128. etcDir = dirname;
  129. }
  130. /**
  131. * Returns the <CODE>tmp</CODE> directory for the product.
  132. * <P>
  133. * The default value of the <CODE>tmp</CODE> directory is:
  134. * <UL>
  135. * <LI><CODE>DefaultPaths.getInstallDir("tmp")</CODE>.
  136. * </UL>
  137. *
  138. * @return Java DMK <CODE>tmp</CODE> directory.
  139. */
  140. public static String getTmpDir() {
  141. if (tmpDir == null)
  142. return getInstallDir("tmp");
  143. else
  144. return tmpDir;
  145. }
  146. /**
  147. * Returns the <CODE>tmp</CODE> directory for Java DMK concatenated with dirname.
  148. * <P>
  149. * The default value of the <CODE>tmp</CODE> directory is:
  150. * <UL>
  151. * <LI><CODE>DefaultPaths.getInstallDir("tmp")</CODE>.
  152. * </UL>
  153. *
  154. * @param dirname The directory to be appended.
  155. *
  156. * @return Java DMK <CODE>tmp</CODE> directory + <CODE>File.separator</CODE> + <CODE>dirname</CODE>.
  157. */
  158. public static String getTmpDir(String dirname) {
  159. if (tmpDir == null) {
  160. if (dirname == null) {
  161. return getTmpDir();
  162. } else {
  163. return getTmpDir() + File.separator + dirname;
  164. }
  165. } else {
  166. if (dirname == null) {
  167. return tmpDir;
  168. } else {
  169. return tmpDir + File.separator + dirname;
  170. }
  171. }
  172. }
  173. /**
  174. * Sets the <CODE>tmp</CODE> directory for the product
  175. *
  176. * @param dirname The <CODE>tmp</CODE> directory for Java DMK.
  177. */
  178. public static void setTmpDir(String dirname) {
  179. tmpDir = dirname;
  180. }
  181. // PRIVATE STATIC METHODS
  182. //-----------------------
  183. private static String useRessourceFile() {
  184. InputStream in = null;
  185. BufferedReader r = null;
  186. try {
  187. in =
  188. DefaultPaths.class.getClassLoader().getResourceAsStream(INSTALL_PATH_RESOURCE_NAME);
  189. if(in == null) return null;
  190. r = new BufferedReader(new InputStreamReader(in));
  191. installDir = r.readLine();
  192. }catch(Exception e) {
  193. }
  194. finally {
  195. try {
  196. if(in != null) in.close();
  197. if(r != null) r.close();
  198. }catch(Exception e) {}
  199. }
  200. return installDir;
  201. }
  202. // PRIVATE VARIABLES
  203. //------------------
  204. /**
  205. * Directories used by Java DMK.
  206. */
  207. private static String etcDir;
  208. private static String tmpDir;
  209. private static String installDir;
  210. }