Example usage for java.net URL getPath

List of usage examples for java.net URL getPath

Introduction

In this page you can find the example usage for java.net URL getPath.

Prototype

public String getPath() 

Source Link

Document

Gets the path part of this URL .

Usage

From source file:com.netflix.config.ClasspathPropertiesConfiguration.java

private static void loadResources(ClassLoader loader, String resourceName) throws Exception {
    Enumeration<URL> resources = loader.getResources(resourceName);
    boolean loadedResources = false;
    while (resources.hasMoreElements()) {
        URL from = resources.nextElement();
        /* Properties props = loadProperties(from);
        String configName = getConfigName(props, from);
        containerConfiguration.addConfiguration(
              new PropertiesConfiguration(from), configName); */
        ConfigurationManager.loadPropertiesFromResources(from.getPath());
        log.debug("Added properties from:" + from + from.getPath());
        loadedResources = true;/*from ww  w. j  a va  2  s  .c  o  m*/
    }
    if (!loadedResources) {
        log.debug("Did not find any properties resource in the classpath with name:"
                + propertiesResourceRelativePath);
    }
}

From source file:gov.nih.nci.cacisweb.util.CaCISUtil.java

/**
 * /*from   w  w  w. j  ava2 s. c o m*/
 * @param property
 * @param defaultValue
 * @return
 * @throws PropertyFileLoadException
 */
public static void setProperty(String propertyName, String propertyValue) throws CaCISWebException {

    URL propsUrl = CaCISUtil.class.getClassLoader().getResource(CaCISWebConstants.COM_PROPERTIES_FILE_NAME);
    File propertiesFile = new File(propsUrl.getPath());

    if (properties == null || propertiesFile.lastModified() > lastModified) {
        properties = new Properties();

        try {
            properties.load(propsUrl.openStream());
            properties.setProperty(propertyName, propertyValue);
            FileOutputStream fileOutputStream = new FileOutputStream(new File(propsUrl.getPath()));
            properties.store(fileOutputStream, "Updated Property: " + propertyName);
        } catch (FileNotFoundException ex) {
            throw new CaCISWebException(
                    String.format("The properties file %s does not exist or is not readable|writable",
                            propertiesFile.getAbsolutePath()),
                    ex);
        } catch (IOException ex) {
            throw new CaCISWebException(String.format(
                    "An error was encountered while attempting to read|write the properties file %s",
                    propertiesFile.getAbsolutePath()), ex);
        }
    }
}

From source file:gov.nih.nci.cacisweb.util.CaCISUtil.java

/**
 * /*from w ww. ja  v a  2s.  c  o  m*/
 * @param property
 * @param defaultValue
 * @return
 * @throws PropertyFileLoadException
 */
public static String getProperty(String property, String defaultValue) throws CaCISWebException {

    URL propsUrl = CaCISUtil.class.getClassLoader().getResource(CaCISWebConstants.COM_PROPERTIES_FILE_NAME);
    File propertiesFile = new File(propsUrl.getPath());

    if (properties == null || propertiesFile.lastModified() > lastModified) {
        properties = new Properties();

        try {
            properties.load(propsUrl.openStream());
        } catch (FileNotFoundException ex) {
            throw new CaCISWebException(
                    String.format("The properties file %s does not exist or is not readable",
                            propertiesFile.getAbsolutePath()),
                    ex);
        } catch (IOException ex) {
            throw new CaCISWebException(
                    String.format("An error was encountered while attempting to read the properties file %s",
                            propertiesFile.getAbsolutePath()),
                    ex);
        }
    }

    log.debug(property + " = " + properties.getProperty(property, defaultValue));

    return properties.getProperty(property, defaultValue);
}

From source file:com.linecorp.armeria.testing.server.webapp.WebAppContainerTest.java

/**
 * Returns the doc-base directory of the test web application.
 *///w  w  w.java2 s. co  m
public static File webAppRoot() {
    URL url = WebAppContainerTest.class.getProtectionDomain().getCodeSource().getLocation();
    File f;
    try {
        f = new File(url.toURI());
    } catch (URISyntaxException ignored) {
        f = new File(url.getPath());
    }

    final File buildDir;
    if (f.isDirectory()) {
        // f is: testing/build/resources/main
        buildDir = f.getParentFile().getParentFile();
    } else {
        // f is: testing/build/libs/armeria-testing-*.jar
        assert f.isFile();
        buildDir = f.getParentFile().getParentFile();
    }

    assert buildDir.getPath().endsWith("testing" + File.separatorChar + "build");

    final File webAppRoot = new File(buildDir.getParentFile(),
            "src" + File.separatorChar + "main" + File.separatorChar + "webapp");
    assert webAppRoot.isDirectory();
    return webAppRoot;
}

From source file:com.eryansky.common.utils.io.FileUtils.java

@SuppressWarnings("rawtypes")
public static String getAppPath(Class cls) {
    // ??//from w w w  .ja va  2 s . c om
    if (cls == null)
        throw new IllegalArgumentException("???");
    ClassLoader loader = cls.getClassLoader();
    // ????
    String clsName = cls.getName() + ".class";
    // ?
    Package pack = cls.getPackage();
    String path = "";
    // ?????
    if (pack != null) {
        String packName = pack.getName();
        // ??JavaJDK
        if (packName.startsWith("java.") || packName.startsWith("javax."))
            throw new IllegalArgumentException("????");
        // ??????
        clsName = clsName.substring(packName.length() + 1);
        // ?????????
        if (packName.indexOf(".") < 0)
            path = packName + "/";
        else {// ???????
            int start = 0, end = 0;
            end = packName.indexOf(".");
            while (end != -1) {
                path = path + packName.substring(start, end) + "/";
                start = end + 1;
                end = packName.indexOf(".", start);
            }
            path = path + packName.substring(start) + "/";
        }
    }
    // ClassLoadergetResource????
    java.net.URL url = loader.getResource(path + clsName);
    // URL??
    String realPath = url.getPath();
    // ?????"file:"
    int pos = realPath.indexOf("file:");
    if (pos > -1)
        realPath = realPath.substring(pos + 5);
    // ????
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    // JARJAR??
    if (realPath.endsWith("!"))
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    /*------------------------------------------------------------ 
     ClassLoadergetResourceutf-8?? 
      ??? 
      URLDecoderdecode? 
      ? 
    -------------------------------------------------------------*/
    try {
        realPath = java.net.URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    System.out.println("realPath----->" + realPath);
    return realPath;
}

From source file:mobi.jenkinsci.ci.client.JenkinsFormAuthHttpClient.java

public static String getUrl(final String baseUrl, final String path) throws MalformedURLException {
    if (path.startsWith("http")) {
        return path;
    }/*from   www.  ja va 2  s.  com*/

    final URL url = new URL(baseUrl);
    if (path.startsWith("/")) {
        return getDomainFromUrl(url) + path;
    } else {
        return getDomainFromUrl(url) + "/" + url.getPath() + (url.getPath().endsWith("/") ? "" : "/") + path;
    }
}

From source file:org.dataconservancy.dcs.integration.main.LineageIT.java

/**
 * Loads properties to set up the test, deposit a DU to establish a lineage, and retrieve the deposited DU.
 *
 * @throws IOException/*from  www  .  j ava 2s. co  m*/
 * @throws InterruptedException
 * @throws InvalidXmlException
 */
@BeforeClass
public static void loadProperties() throws IOException, InterruptedException, InvalidXmlException {
    // Load properties
    final URL defaultProps = LineageIT.class.getResource("/default.properties");
    assertNotNull("Could not resolve /default.properties from the classpath.", defaultProps);
    assertTrue("default.properties does not exist.", new File(defaultProps.getPath()).exists());
    props.load(defaultProps.openStream());
    baseUrl = interpolate(new StringBuilder("${dcs.baseurl}/"), 0, props).toString();
    lineageUrl = baseUrl + "lineage";

    // Establish a lineage by depositing a single object
    Dcp toDeposit = new Dcp();
    DcsDeliverableUnit du = new DcsDeliverableUnit();
    du.setId(duId);
    du.setTitle("Deliverable Unit Title");
    toDeposit.addEntity(du);

    // Perform the deposit
    DepositBuilder depositBuilder = depositClient.buildDeposit(toDeposit);
    String id = depositBuilder.execute();
    Thread.sleep(10000);
    DepositInfo info = depositClient.getDepositInfo(id);
    assertTrue(info.hasCompleted());
    assertTrue(info.isSuccessful());

    // Get the minted DCS id from the deposit feed, and retrieve the
    // deposited DU
    dcsDuId = getDcsIdFromDepositedId(duId, info);
    assertNotNull("Unable to determine deposited entity id", dcsDuId);
    DcsDeliverableUnit depositedDu = (DcsDeliverableUnit) getEntity(dcsDuId);
    assertNotNull(depositedDu);
    assertNotNull(depositedDu.getLineageId());
    lineageId = depositedDu.getLineageId();
}

From source file:org.wso2.identity.integration.test.util.Utils.java

public static void setSystemProperties(Class classIn) {
    URL resourceUrl = classIn.getResource(
            File.separator + "keystores" + File.separator + "products" + File.separator + "wso2carbon.jks");
    System.setProperty("javax.net.ssl.trustStore", resourceUrl.getPath());
    System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    System.setProperty("javax.net.ssl.trustStoreType", "JKS");
}

From source file:com.investoday.code.util.aliyun.api.gateway.util.HttpUtil.java

/**
 * ?Header// www  .  j  av a2s.c o  m
 *
 * @param headers              Http
 * @param appKey               APP KEY
 * @param appSecret            APP
 * @param method               Http
 * @param requestAddress       http://host+path+query
 * @param formParam            ??
 * @param signHeaderPrefixList ???Header?
 * @return Header
 * @throws MalformedURLException
 */
private static Map<String, String> initialBasicHeader(Map<String, String> headers, String appKey,
        String appSecret, String method, String requestAddress, Map formParam,
        List<String> signHeaderPrefixList) throws MalformedURLException {
    if (headers == null) {
        headers = new HashMap<String, String>();
    }

    URL url = new URL(requestAddress);
    StringBuilder stringBuilder = new StringBuilder();
    if (StringUtils.isNotBlank(url.getPath())) {
        stringBuilder.append(url.getPath());
    }
    if (StringUtils.isNotBlank(url.getQuery())) {
        stringBuilder.append("?");
        stringBuilder.append(url.getQuery());
    }

    headers.put(HttpHeader.HTTP_HEADER_USER_AGENT, Constants.USER_AGENT);
    //      headers.put(SystemHeader.X_CA_TIMESTAMP, String.valueOf(new Date().getTime())); 
    //headers.put(SystemHeader.X_CA_TIMESTAMP, Test11.getGMT()); //
    headers.put(SystemHeader.X_CA_NONCE, UUID.randomUUID().toString());
    headers.put(SystemHeader.X_CA_KEY, appKey);
    headers.put(SystemHeader.X_CA_SIGNATURE, SignUtil.sign(method, stringBuilder.toString(), headers, formParam,
            appSecret, signHeaderPrefixList));

    return headers;
}

From source file:com.alibaba.rocketmq.common.MixAll.java

public static String findClassPath(Class<?> c) {
    URL url = c.getProtectionDomain().getCodeSource().getLocation();
    return url.getPath();
}