Java Code Examples for org.apache.brooklyn.util.os.Os#writeToTempFile()

The following examples show how to use org.apache.brooklyn.util.os.Os#writeToTempFile() . 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: CliTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private File generateSimpleBomFile(String itemName, String itemVersion) {
    String catalogContents = Joiner.on("\n").join(
            "brooklyn.catalog:",
            "  id: "+itemName,
            "  version: "+itemVersion,
            "  itemType: entity",
            "  item:",
            "    services:",
            "    - type: org.apache.brooklyn.entity.stock.BasicApplication");
    File bomFile = Os.writeToTempFile(new ByteArrayInputStream(catalogContents.getBytes()), "testAddToCatalog", ".bom");
    filesToDelete.add(bomFile);
    return bomFile;
}
 
Example 2
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected File writeTempFile(InputStream contents) {
    File tempFile = Os.writeToTempFile(contents, localTempDir, "sshcopy", "data");
    tempFile.setReadable(false, false);
    tempFile.setReadable(true, true);
    tempFile.setWritable(false);
    tempFile.setExecutable(false);
    return tempFile;
}
 
Example 3
Source File: ResourceUtilsTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteStreamToTempFile() throws Exception {
    File tempFileLocal = Os.writeToTempFile(new ByteArrayInputStream("mycontents".getBytes()), "resourceutils-test", ".txt");
    try {
        List<String> lines = Files.readLines(tempFileLocal, Charsets.UTF_8);
        assertEquals(lines, ImmutableList.of("mycontents"));
    } finally {
        tempFileLocal.delete();
    }
}
 
Example 4
Source File: WebAppContextProvider.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * Serve given WAR at the given pathSpec; if not yet started, it is simply remembered until start;
 * if server already running, the context for this WAR is started.
 * @return the context created and added as a handler (and possibly already started if server is
 * started, so be careful with any changes you make to it!)
 */
public WebAppContext get(ManagementContext managementContext, Map<String, Object> attributes, boolean ignoreFailures) {
    checkNotNull(managementContext, "managementContext");
    checkNotNull(attributes, "attributes");
    boolean isRoot = pathSpec.isEmpty();

    final WebAppContext context = new WebAppContext();
    // use a unique session ID to prevent interference with other web apps on same server (esp for localhost);
    // note however this is only run for the legacy launcher
    // TODO would be nice if the various karaf startups rename the session cookie property (from JSESSIONID)
    // as the default is likely to conflict with other java-based servers (esp on localhost);
    // this can be done e.g. on ServletContext.getSessionCookieConfig(), but will be needed for REST and for JS (static) bundles
    // low priority however, if you /etc/hosts a localhost-brooklyn and use that it will stop conflicting
    context.setInitParameter(SessionHandler.__SessionCookieProperty, SessionHandler.__DefaultSessionCookie + "_" + "BROOKLYN" + Identifiers.makeRandomId(6));
    context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    context.setAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT, managementContext);
    for (Map.Entry<String, Object> attributeEntry : attributes.entrySet()) {
        context.setAttribute(attributeEntry.getKey(), attributeEntry.getValue());
    }

    try {
        final CustomResourceLocator locator = new CustomResourceLocator(managementContext.getConfig(), ResourceUtils.create(this));
        final InputStream resource = locator.getResourceFromUrl(warUrl);
        final String warName = isRoot ? "ROOT" : ("embedded-" + pathSpec);
        File tmpWarFile = Os.writeToTempFile(resource, warName, ".war");
        context.setWar(tmpWarFile.getAbsolutePath());
    } catch (Exception e) {
        LOG.warn("Failed to deploy webapp " + pathSpec + " from " + warUrl
                + (ignoreFailures ? "; launching run without WAR" : " (rethrowing)")
                + ": " + Exceptions.collapseText(e));
        if (!ignoreFailures) {
            throw new IllegalStateException("Failed to deploy webapp " + pathSpec + " from " + warUrl + ": " + Exceptions.collapseText(e), e);
        }
        LOG.debug("Detail on failure to deploy webapp: " + e, e);
        context.setWar("/dev/null");
    }

    context.setContextPath("/" + pathSpec);
    context.setParentLoaderPriority(true);

    return context;
}
 
Example 5
Source File: Osgis.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private static String cacheFile(String url) {
    InputStream in = getUrlStream(url);
    File cache = Os.writeToTempFile(in, "bundle-cache", "jar");
    return cache.toURI().toString();
}
 
Example 6
Source File: ResourceUtilsTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@BeforeClass(alwaysRun=true)
public void setUp() throws Exception {
    utils = ResourceUtils.create(this, "mycontext");
    tempFile = Os.writeToTempFile(new ByteArrayInputStream(tempFileContents.getBytes()), "resourceutils-test", ".txt");
}
 
Example 7
Source File: HttpService.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public HttpService start() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), ROOT_WAR_PATH);

    try {
        if (httpsEnabled) {
            //by default the server is configured with a http connector, this needs to be removed since we are going
            //to provide https
            for (Connector c: server.getConnectors()) {
                server.removeConnector(c);
            }

            InputStream keyStoreStream = ResourceUtils.create(this).getResourceFromUrl(SERVER_KEYSTORE);
            KeyStore keyStore;
            try {
                keyStore = SecureKeys.newKeyStore(keyStoreStream, "password");
            } finally {
                keyStoreStream.close();
            }

            // manually create like seen in XMLs at http://www.eclipse.org/jetty/documentation/current/configuring-ssl.html
            SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setKeyStore(keyStore);
            sslContextFactory.setTrustAll(true);
            sslContextFactory.setKeyStorePassword("password");

            HttpConfiguration sslHttpConfig = new HttpConfiguration();
            sslHttpConfig.setSecureScheme("https");
            sslHttpConfig.setSecurePort(actualPort);

            ServerConnector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(sslHttpConfig));
            httpsConnector.setPort(actualPort);

            server.addConnector(httpsConnector);
        }

        addShutdownHook();

        File tmpWarFile = Os.writeToTempFile(
                ResourceUtils.create(this).getResourceFromUrl(ROOT_WAR_URL), 
                "TestHttpService", 
                ".war");
        
        WebAppContext context = new WebAppContext();
        context.setWar(tmpWarFile.getAbsolutePath());
        context.setContextPath("/");
        context.setParentLoaderPriority(true);

        if (securityHandler.isPresent()) {
            context.setSecurityHandler(securityHandler.get());
        }

        server.setHandler(context);
        server.start();

        log.info("Started test HttpService at "+getUrl());
        
    } catch (Exception e) {
        try {
            shutdown();
        } catch (Exception e2) {
            log.warn("Error shutting down HttpService while recovering from earlier error (re-throwing earlier error)", e2);
            throw e;
        }
    }

    return this;
}