Exemplo n.º 1
0
  /**
   * Utility method which loads the given properties file and returns a Properties object containing
   * the key,value pairs in that file. The properties files should be in the class path as this
   * method looks to the thread context class loader (TCL) to locate the resource. If the TCL is a
   * URLClassLoader the findResource(String) method is first tried. If this fails or the TCL is not
   * a URLClassLoader getResource(String) is tried.
   *
   * @param defaultsName - the name of the default properties file resource that will be used as the
   *     default Properties to the ctor of the propertiesName Properties instance.
   * @param propertiesName - the name of the properties file resource
   * @return the loaded properties file if found
   * @exception java.io.IOException thrown if the properties file cannot be found or loaded
   */
  static Properties loadProperties(String defaultsName, String propertiesName) throws IOException {
    Properties bundle = null;
    ClassLoader loader = SecurityActions.getContextClassLoader();
    URL defaultUrl = null;
    URL url = null;
    // First check for local visibility via a URLClassLoader.findResource
    if (loader instanceof URLClassLoader) {
      URLClassLoader ucl = (URLClassLoader) loader;
      defaultUrl = SecurityActions.findResource(ucl, defaultsName);
      url = SecurityActions.findResource(ucl, propertiesName);
      PicketBoxLogger.LOGGER.traceAttemptToLoadResource(propertiesName);
    }
    // Do a general resource search
    if (defaultUrl == null) {
      defaultUrl = loader.getResource(defaultsName);
      if (defaultUrl == null) {
        try {
          defaultUrl = new URL(defaultsName);
        } catch (MalformedURLException mue) {
          PicketBoxLogger.LOGGER.debugFailureToOpenPropertiesFromURL(mue);
          File tmp = new File(defaultsName);
          if (tmp.exists()) defaultUrl = tmp.toURI().toURL();
        }
      }
    }
    if (url == null) {
      url = loader.getResource(propertiesName);
      if (url == null) {
        try {
          url = new URL(propertiesName);
        } catch (MalformedURLException mue) {
          PicketBoxLogger.LOGGER.debugFailureToOpenPropertiesFromURL(mue);
          File tmp = new File(propertiesName);
          if (tmp.exists()) url = tmp.toURI().toURL();
        }
      }
    }
    if (url == null && defaultUrl == null) {
      String propertiesFiles = propertiesName + "/" + defaultsName;
      throw PicketBoxMessages.MESSAGES.unableToFindPropertiesFile(propertiesFiles);
    }

    Properties defaults = new Properties();
    if (defaultUrl != null) {
      InputStream is = null;
      try {
        is = defaultUrl.openStream();
        defaults.load(is);
        PicketBoxLogger.LOGGER.tracePropertiesFileLoaded(defaultsName, defaults.keySet());
      } catch (Throwable e) {
        PicketBoxLogger.LOGGER.debugFailureToLoadPropertiesFile(defaultsName, e);
      } finally {
        safeClose(is);
      }
    }

    bundle = new Properties(defaults);
    if (url != null) {
      InputStream is = null;
      try {
        is = SecurityActions.openStream(url);
      } catch (PrivilegedActionException e) {
        throw new IOException(e.getLocalizedMessage());
      }
      if (is != null) {
        try {
          bundle.load(is);
        } finally {
          safeClose(is);
        }
      } else {
        throw PicketBoxMessages.MESSAGES.unableToLoadPropertiesFile(propertiesName);
      }
      PicketBoxLogger.LOGGER.tracePropertiesFileLoaded(propertiesName, bundle.keySet());
    }

    return bundle;
  }