Java ClassLoader Load getProperty(String propertyName)

Here you can find the source of getProperty(String propertyName)

Description

Get the specified property from the environment.

License

Open Source License

Parameter

Parameter Description
propertyName The name of property.

Return

A String containing the string value of the property.

Declaration

public static String getProperty(String propertyName) 

Method Source Code


//package com.java2s;
/* MOD_V2.0/*from  w  ww  .j  ava 2  s.c o m*/
* Copyright (c) 2012 OpenDA Association
* All rights reserved.
* 
* This file is part of OpenDA. 
* 
* OpenDA is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Lesser General Public License as 
* published by the Free Software Foundation, either version 3 of 
* the License, or (at your option) any later version. 
* 
* OpenDA is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
* GNU Lesser General Public License for more details. 
* 
* You should have received a copy of the GNU Lesser General Public License
* along with OpenDA.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;
import java.io.IOException;

import java.net.URL;

public class Main {
    /** Get the specified property from the environment. An empty string
     *  is returned if the argument environment variable does not exist,
     *  though if certain properties are not defined, then we
     *  make various attempts to determine them and then set them.
     *  See the javadoc page for java.util.System.getProperties() for
     *  a list of system properties.
     *  <p>The following properties are handled specially
     *  <dl>
     *  <dt> "ptolemy.ptII.dir"
     *  <dd> vergil usually sets the ptolemy.ptII.dir property to the
     *  value of $PTII.  However, if we are running under Web Start,
     *  then this property might not be set, in which case we look
     *  for "ptolemy/kernel/util/NamedObj.class" and set the
     *  property accordingly.
     *  <dt> "ptolemy.ptII.dirAsURL"
     *  <dd> Return $PTII as a URL.  For example, if $PTII was c:\ptII,
     *  then return file:/c:/ptII/.
     *  <dt> "user.dir"
     *  <dd> Return the canonical path name to the current working directory.
     *  This is necessary because under JDK1.4.1 System.getProperty()
     *  returns <code><b>c</b>:/<i>foo</i></code>
     *  whereas most of the other methods that operate
     *  on path names return <code><b>C</b>:/<i>foo</i></code>.
     *  </dl>
     *  @param propertyName The name of property.
     *  @return A String containing the string value of the property.
     */
    public static String getProperty(String propertyName) {
        // NOTE: getProperty() will probably fail in applets, which
        // is why this is in a try block.
        String property = null;
        try {
            property = System.getProperty(propertyName);
        } catch (SecurityException security) {
            if (!propertyName.equals("ptolemy.ptII.dir")) {
                throw new RuntimeException("Could not find '" + propertyName + "' System property", security);
            }
        }
        if (propertyName.equals("user.dir")) {
            try {
                File userDirFile = new File(property);
                return userDirFile.getCanonicalPath();
            } catch (IOException ex) {
                return property;
            }
        }
        if (property != null) {
            return property;
        }
        if (propertyName.equals("ptolemy.ptII.dirAsURL")) {
            // Return $PTII as a URL.  For example, if $PTII was c:\ptII,
            // then return file:/c:/ptII/
            File ptIIAsFile = new File(getProperty("ptolemy.ptII.dir"));

            try {
                URL ptIIAsURL = ptIIAsFile.toURI().toURL();
                return ptIIAsURL.toString();
            } catch (java.net.MalformedURLException malformed) {
                throw new RuntimeException("While trying to find '" + propertyName + "', could not convert '"
                        + ptIIAsFile + "' to a URL", malformed);
            }
        }

        if (propertyName.equals("ptolemy.ptII.dir")) {
            String namedObjPath = "ptolemy/kernel/util/NamedObj.class";
            String home = null;
            // PTII variable was not set
            URL namedObjURL = Thread.currentThread().getContextClassLoader().getResource(namedObjPath);

            if (namedObjURL != null) {
                String namedObjFileName = namedObjURL.getFile().toString();
                // FIXME: How do we get from a URL to a pathname?
                if (namedObjFileName.startsWith("file:")) {
                    // We get rid of either file:/ or file:\
                    namedObjFileName = namedObjFileName.substring(6);
                }
                String abnormalHome = namedObjFileName.substring(0,
                        namedObjFileName.length() - namedObjPath.length());

                // abnormalHome will have values like: "/C:/ptII/"
                // which cause no end of trouble, so we construct a File
                // and call toString().

                home = (new File(abnormalHome)).toString();

                // If we are running under Web Start, then strip off
                // the trailing "!"
                if (home.endsWith("!")) {
                    home = home.substring(0, home.length() - 1);
                }

                // Web Start
                String ptsupportJarName = File.separator + "DMptolemy" + File.separator + "RMptsupport.jar";
                if (home.endsWith(ptsupportJarName)) {
                    home = home.substring(0, home.length() - ptsupportJarName.length());
                }

                ptsupportJarName = File.separator + "ptolemy" + File.separator + "ptsupport.jar";
                if (home.endsWith(ptsupportJarName)) {
                    home = home.substring(0, home.length() - ptsupportJarName.length());
                }
            }

            if (home == null) {
                throw new RuntimeException(
                        "Could not find " + "'ptolemy.ptII.dir'" + " property.  Also tried loading '" + namedObjPath
                                + "' as a resource and working from that. " + "Vergil should be "
                                + "invoked with -Dptolemy.ptII.dir" + "=\"$PTII\"");
            }
            try {
                System.setProperty("ptolemy.ptII.dir", home);
            } catch (SecurityException security) {
                // Ignore, we are probably running as an applet or -sandbox
            }
            return home;
        }
        if (property == null) {
            return "";
        }

        return property;
    }
}

Related

  1. getMetadataSpecialSymbolsXml()
  2. getPath(final String fileName)
  3. getPath(String resoureLocation)
  4. getProhibitedProxyInterfaces()
  5. getProperties(String bundleName, String fileName)
  6. getReader(final String name, final String encoding)
  7. getRootPath()
  8. getSimpleName(String fqcn)
  9. getSourcesPath()