1 /*
   2  * Copyright (c) 2002, 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 
  26 package com.sun.jmx.snmp.defaults;
  27 
  28 
  29 // java import
  30 //
  31 import java.io.File;
  32 import java.io.BufferedReader;
  33 import java.io.InputStream;
  34 import java.io.InputStreamReader;
  35 import java.util.StringTokenizer;
  36 
  37 /**
  38  * This class represents a set of default directories used by Java DMK.
  39  *
  40  * <p><b>This API is a Sun Microsystems internal API  and is subject
  41  * to change without notice.</b></p>
  42  * @since 1.5
  43  */
  44 public class DefaultPaths {
  45     private static final String INSTALL_PATH_RESOURCE_NAME = "com/sun/jdmk/defaults/install.path";
  46     // private constructor defined to "hide" the default public constructor
  47     private DefaultPaths() {
  48 
  49     }
  50 
  51     // PUBLIC STATIC METHODS
  52     //----------------------
  53 
  54     /**
  55      * Returns the installation directory for Java DMK.
  56      *
  57      * The default value of the installation directory is:
  58      * <CODE>&lt;base_dir&gt; + File.separator + SUNWjdmk + File.separator + jdmk5.0 </CODE>
  59      *
  60      * @return Java DMK installation directory.
  61      */
  62     public static String getInstallDir() {
  63         if (installDir == null)
  64             return useRessourceFile();
  65         else
  66             return installDir;
  67     }
  68 
  69     /**
  70      * Returns the installation directory for Java DMK concatenated with dirname.
  71      *
  72      * The default value of the installation directory is:
  73      * <CODE>&lt;base_dir&gt; + File.separator + SUNWjdmk + File.separator + jdmk5.0 </CODE>
  74      *
  75      * @param dirname The directory to be appended.
  76      *
  77      * @return Java DMK installation directory + <CODE>File.separator</CODE> + <CODE>dirname</CODE>.
  78      */
  79     public static String getInstallDir(String dirname) {
  80         if (installDir == null) {
  81             if (dirname == null) {
  82                 return getInstallDir();
  83             } else {
  84                 return getInstallDir() + File.separator + dirname;
  85             }
  86         } else {
  87             if (dirname == null) {
  88                 return installDir;
  89             } else {
  90                 return installDir + File.separator + dirname;
  91             }
  92         }
  93     }
  94 
  95     /**
  96      * Sets the installation directory for Java DMK.
  97      *
  98      * @param dirname The directory where Java DMK resides.
  99      */
 100     public static void setInstallDir(String dirname) {
 101         installDir = dirname;
 102     }
 103 
 104     /**
 105      * Returns the <CODE>etc</CODE> directory for Java DMK.
 106      * <P>
 107      * The default value of the <CODE>etc</CODE> directory is:
 108      * <UL>
 109      * <LI><CODE>DefaultPaths.getInstallDir("etc")</CODE>.
 110      * </UL>
 111      *
 112      * @return Java DMK <CODE>etc</CODE> directory.
 113      */
 114     public static String getEtcDir() {
 115         if (etcDir == null)
 116             return getInstallDir("etc");
 117         else
 118             return etcDir;
 119     }
 120 
 121     /**
 122      * Returns the <CODE>etc</CODE> directory for Java DMK concatenated with dirname.
 123      * <P>
 124      * The default value of the <CODE>etc</CODE> directory is:
 125      * <UL>
 126      * <LI><CODE>DefaultPaths.getInstallDir("etc")</CODE>.
 127      * </UL>
 128      *
 129      * @param dirname The directory to be appended.
 130      *
 131      * @return Java DMK <CODE>etc</CODE> directory + <CODE>File.separator</CODE> + <CODE>dirname</CODE>.
 132      */
 133     public static String getEtcDir(String dirname) {
 134         if (etcDir == null) {
 135             if (dirname == null) {
 136                 return getEtcDir();
 137             } else {
 138                 return getEtcDir() + File.separator + dirname;
 139             }
 140         } else {
 141             if (dirname == null) {
 142                 return etcDir;
 143             } else {
 144                 return etcDir + File.separator + dirname;
 145             }
 146         }
 147     }
 148 
 149     /**
 150      * Sets the <CODE>etc</CODE> directory for Java DMK.
 151      *
 152      * @param dirname The <CODE>etc</CODE> directory for Java DMK.
 153      */
 154     public static void setEtcDir(String dirname) {
 155         etcDir = dirname;
 156     }
 157 
 158     /**
 159      * Returns the <CODE>tmp</CODE> directory for the product.
 160      * <P>
 161      * The default value of the <CODE>tmp</CODE> directory is:
 162      * <UL>
 163      * <LI><CODE>DefaultPaths.getInstallDir("tmp")</CODE>.
 164      * </UL>
 165      *
 166      * @return Java DMK <CODE>tmp</CODE> directory.
 167      */
 168     public static String getTmpDir() {
 169          if (tmpDir == null)
 170             return getInstallDir("tmp");
 171         else
 172             return tmpDir;
 173     }
 174 
 175     /**
 176      * Returns the <CODE>tmp</CODE> directory for Java DMK concatenated with dirname.
 177      * <P>
 178      * The default value of the <CODE>tmp</CODE> directory is:
 179      * <UL>
 180      * <LI><CODE>DefaultPaths.getInstallDir("tmp")</CODE>.
 181      * </UL>
 182      *
 183      * @param dirname The directory to be appended.
 184      *
 185      * @return Java DMK <CODE>tmp</CODE> directory + <CODE>File.separator</CODE> + <CODE>dirname</CODE>.
 186      */
 187     public static String getTmpDir(String dirname) {
 188         if (tmpDir == null) {
 189             if (dirname == null) {
 190                 return getTmpDir();
 191             } else {
 192                 return getTmpDir() + File.separator + dirname;
 193             }
 194         } else {
 195             if (dirname == null) {
 196                 return tmpDir;
 197             } else {
 198                 return tmpDir + File.separator + dirname;
 199             }
 200         }
 201     }
 202 
 203     /**
 204      * Sets the <CODE>tmp</CODE> directory for the product
 205      *
 206      * @param dirname The <CODE>tmp</CODE> directory for Java DMK.
 207      */
 208     public static void setTmpDir(String dirname) {
 209         tmpDir = dirname;
 210     }
 211 
 212 
 213     // PRIVATE STATIC METHODS
 214     //-----------------------
 215 
 216     private static String useRessourceFile() {
 217         InputStream in = null;
 218         BufferedReader r = null;
 219         try {
 220             in =
 221                 DefaultPaths.class.getClassLoader().getResourceAsStream(INSTALL_PATH_RESOURCE_NAME);
 222             if(in == null) return null;
 223             r = new BufferedReader(new InputStreamReader(in));
 224             installDir = r.readLine();
 225         }catch(Exception e) {
 226         }
 227         finally {
 228             try {
 229                 if(in != null) in.close();
 230                 if(r != null) r.close();
 231             }catch(Exception e) {}
 232         }
 233         return installDir;
 234     }
 235 
 236     // PRIVATE VARIABLES
 237     //------------------
 238 
 239     /**
 240      * Directories used by Java DMK.
 241      */
 242     private static String etcDir;
 243     private static String tmpDir;
 244     private static String installDir;
 245 }