Java Code Examples for org.apache.brooklyn.util.core.ResourceUtils#getResourceFromUrl()

The following examples show how to use org.apache.brooklyn.util.core.ResourceUtils#getResourceFromUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChefLiveTestSupport.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
public synchronized static String installBrooklynChefHostedConfig() {
    if (defaultConfigFile!=null) return defaultConfigFile;
    File tempDir = Files.createTempDir();
    ResourceUtils r = ResourceUtils.create(ChefServerTasksIntegrationTest.class);
    for (String f: new String[] { "knife.rb", "brooklyn-tests.pem", "brooklyn-validator.pem" }) {
        InputStream in = r.getResourceFromUrl("classpath:///org/apache/brooklyn/entity/chef/hosted-chef-brooklyn-credentials/"+f);
        try {
            FileUtil.copyTo(in, new File(tempDir, f));
        } finally {
            Streams.closeQuietly(in);
        }
    }
    File knifeConfig = new File(tempDir, "knife.rb");
    defaultConfigFile = knifeConfig.getPath();
    return defaultConfigFile;
}
 
Example 2
Source File: BrooklynWebServer.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private String getLocalKeyStorePath(String keystoreUrl) {
    ResourceUtils res = ResourceUtils.create(this);
    res.checkUrlExists(keystoreUrl, BrooklynWebConfig.KEYSTORE_URL.getName());
    if (new File(keystoreUrl).exists()) {
        return keystoreUrl;
    } else {
        InputStream keystoreStream;
        try {
            keystoreStream = res.getResourceFromUrl(keystoreUrl);
        } catch (Exception e) {
            Exceptions.propagateIfFatal(e);
            throw new IllegalArgumentException("Unable to access URL: "+keystoreUrl, e);
        }
        File tmp = Os.newTempFile("brooklyn-keystore", "ks");
        tmp.deleteOnExit();
        try {
            FileUtil.copyTo(keystoreStream, tmp);
        } finally {
            Streams.closeQuietly(keystoreStream);
        }
        return tmp.getAbsolutePath();
    }
}
 
Example 3
Source File: MyEntityImpl.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void install() {
    String resourceName = "/"+MyEntityApp.class.getName().replace(".", "/")+".class";
    ResourceUtils r = ResourceUtils.create(this);
    if (r.getResourceFromUrl(resourceName) == null) 
        throw new IllegalStateException("Cannot find resource "+resourceName);
    String tmpFile = "/tmp/brooklyn-test-MyEntityApp-"+Identifiers.makeRandomId(6)+".class";
    int result = getMachine().installTo(resourceName, tmpFile);
    if (result!=0) throw new IllegalStateException("Cannot install "+resourceName+" to "+tmpFile);
    String saveAs = "classes/"+MyEntityApp.class.getPackage().getName().replace(".", "/")+"/"+MyEntityApp.class.getSimpleName()+".class";
    newScript(INSTALLING).
        failOnNonZeroResultCode().
        body.append(
            "curl -L \"file://"+tmpFile+"\" --create-dirs -o "+saveAs+" || exit 9"
        ).execute();
}
 
Example 4
Source File: CustomResourceLocator.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream locate(String url, ConfigMap config, ResourceUtils r) {
    String cp = System.getProperty("java.class.path");
    int cpi = cp.indexOf(classpathSuffixToSearchFor);
    if (cpi==-1) return null;
    String path = cp.substring(0, cpi);
    int lps = path.lastIndexOf(File.pathSeparatorChar);
    if (lps>=0) path = path.substring(lps+1);
    path = path + classpathSuffixToUse;
    log.debug("Looking for "+url+" in revised location "+path);
    InputStream result = r.getResourceFromUrl(path);
    log.info("Using "+url+" from revised location "+path);
    return result;
}
 
Example 5
Source File: SshMachineLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Installs the given URL at the indicated destination path.
 * <p>
 * Attempts to curl the source URL on the remote machine,
 * then if that fails, loads locally (from classpath or file) and transfers.
 * <p>
 * Use {@link ArchiveUtils} to handle directories and their contents properly.
 *
 * TODO allow s3://bucket/file URIs for AWS S3 resources
 * TODO use PAX-URL style URIs for maven artifacts
 * TODO use subtasks here for greater visibility?; deprecate in favour of SshTasks.installFromUrl?
 *
 * @param utils A {@link ResourceUtils} that can resolve the source URLs
 * @param url The source URL to be installed
 * @param destPath The file to be created on the destination
 *
 * @see ArchiveUtils#deploy(String, SshMachineLocation, String)
 * @see ArchiveUtils#deploy(String, SshMachineLocation, String, String)
 * @see ResourceUtils#getResourceFromUrl(String)
 */
public int installTo(ResourceUtils utils, Map<String,?> props, String url, String destPath) {
    LOG.debug("installing {} to {} on {}, attempting remote curl", new Object[] { url, destPath, this });

    try {
        PipedInputStream insO = new PipedInputStream(); OutputStream outO = new PipedOutputStream(insO);
        PipedInputStream insE = new PipedInputStream(); OutputStream outE = new PipedOutputStream(insE);
        StreamGobbler sgsO = new StreamGobbler(insO, null, LOG); sgsO.setLogPrefix("[curl @ "+address+":stdout] ").start();
        StreamGobbler sgsE = new StreamGobbler(insE, null, LOG); sgsE.setLogPrefix("[curl @ "+address+":stdout] ").start();
        Map<String, ?> sshProps = MutableMap.<String, Object>builder().putAll(props).put("out", outO).put("err", outE).build();
        int result = execScript(sshProps, "copying remote resource "+url+" to server",  ImmutableList.of(
                BashCommands.INSTALL_CURL, // TODO should hold the 'installing' mutex
                "mkdir -p `dirname '"+destPath+"'`",
                "curl "+url+" -L --silent --insecure --show-error --fail --connect-timeout 60 --max-time 600 --retry 5 -o '"+destPath+"'"));
        sgsO.close();
        sgsE.close();
        if (result != 0) {
            LOG.debug("installing {} to {} on {}, curl failed, attempting local fetch and copy", new Object[] { url, destPath, this });
            try {
                Tasks.setBlockingDetails("retrieving resource "+url+" for copying across");
                InputStream stream = utils.getResourceFromUrl(url);
                Tasks.setBlockingDetails("copying resource "+url+" to server");
                result = copyTo(props, stream, destPath);
            } finally {
                Tasks.setBlockingDetails(null);
            }
        }
        if (result == 0) {
            LOG.debug("installing {} complete; {} on {}", new Object[] { url, destPath, this });
        } else {
            LOG.warn("installing {} failed; {} on {}: {}", new Object[] { url, destPath, this, result });
        }
        return result;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 6
Source File: OsgiStandaloneTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadAbsoluteWindowsResourceWithInstalledOSGi() {
    //Felix installs an additional URL to the system classloader
    //which throws an IllegalArgumentException when passed a
    //windows path. See ExtensionManager.java static initializer.
    String context = "mycontext";
    String dummyPath = "C:\\dummypath";
    ResourceUtils utils = ResourceUtils.create(this, context);
    try {
        utils.getResourceFromUrl(dummyPath);
        Assert.fail("Non-reachable, should throw an exception for non-existing resource.");
    } catch (RuntimeException e) {
        Assert.assertTrue(e.getMessage().startsWith("Error getting resource '"+dummyPath+"' for "+context));
    }
}
 
Example 7
Source File: HighstateTest.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
private String getTestYaml() {
    final ResourceUtils resourceUtils = ResourceUtils.create();
    final InputStream yaml = resourceUtils.getResourceFromUrl("classpath://test-highstate.yaml");
    return Streams.readFullyStringAndClose(yaml);
}