File Source: DMDircResourceManager.java

         /* 
    P/P   *  Method: com.dmdirc.util.resourcemanager.DMDircResourceManager__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.util.resourcemanager;
    24  
    25  import com.dmdirc.logger.ErrorLevel;
    26  import com.dmdirc.logger.Logger;
    27  
    28  import java.io.UnsupportedEncodingException;
    29  import java.net.URL;
    30  import java.net.URLDecoder;
    31  
    32  /**
    33   *
    34   * @author chris
    35   */
         /* 
    P/P   *  Method: void com.dmdirc.util.resourcemanager.DMDircResourceManager()
          */
    36  public class DMDircResourceManager {
    37  
    38      /**
    39       * Returns the working directory for the application.
    40       * 
    41       * @return Current working directory
    42       */
    43      public static synchronized String getCurrentWorkingDirectory() {
                 /* 
    P/P           *  Method: String getCurrentWorkingDirectory()
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
                  *    java.lang.ClassLoader:getResource(...)@45 != null
                  *    java.lang.ClassLoader:getResource(...)@51 != null
                  *    java.lang.String:lastIndexOf(...)@62 <= 232-2
                  *    java.lang.System:getProperty(...)@56 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    java.lang.String:substring(...)._tainted == 0
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@50: {0}, {1}
                  *    java.lang.String:equals(...)@53: {0}, {1}
                  *    java.lang.String:startsWith(...)@56: {0}, {1}
                  */
    44          String path = "";        
    45          final URL resource = Thread.currentThread().getContextClassLoader().
    46                          getResource("com/dmdirc/Main.class");
    47          
    48          final String protocol = resource.getProtocol();
    49          
    50          if ("file".equals(protocol)) {
    51              path = Thread.currentThread().
    52                      getContextClassLoader().getResource("").getPath();
    53          } else if ("jar".equals(protocol)) {
    54              final String tempPath = resource.getPath();
    55              
    56              if (System.getProperty("os.name").startsWith("Windows")) {
    57                  path = tempPath.substring(6, tempPath.length() - 23);
    58              } else {
    59                  path = tempPath.substring(5, tempPath.length() - 23);
    60              }
    61              
    62              path = path.substring(0, path.lastIndexOf('/') + 1);
    63          }
    64          
    65          try {
    66              path = URLDecoder.decode(path, "UTF-8");
    67          } catch (UnsupportedEncodingException ex) {
    68              Logger.userError(ErrorLevel.MEDIUM, "Unable to decode path");
    69              path = "";
    70          }
    71          return path;
    72      }
    73      
    74      /**
    75       * Determines if this instance of DMDirc is running from a jar or not.
    76       * 
    77       * @return True if this instance is running from a JAR, false otherwise
    78       */
    79      public static boolean isRunningFromJar() {
                 /* 
    P/P           *  Method: bool isRunningFromJar()
                  * 
                  *  Presumptions:
                  *    java.lang.ClassLoader:getResource(...)@80 != null
                  *    java.lang.Thread:currentThread(...)@80 != null
                  *    java.lang.Thread:getContextClassLoader(...)@80 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    80          final URL resource = Thread.currentThread().getContextClassLoader().
    81                          getResource("com/dmdirc/Main.class");
    82          return "jar".equals(resource.getProtocol());
    83      }
    84      
    85  }








SofCheck Inspector Build Version : 2.17854
DMDircResourceManager.java 2009-Jun-25 01:54:24
DMDircResourceManager.class 2009-Sep-02 17:04:17