org.apache.brooklyn.util.core.ResourceUtils Java Examples

The following examples show how to use org.apache.brooklyn.util.core.ResourceUtils. 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: JmxSupport.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** returns URL for accessing the java agent, throwing if not applicable;
 * prefers on classpath where it should be, but will fall back to taking from maven hosted
 * (known problem in Eclipse where JARs are not always copied)
 */
public String getJmxAgentJarUrl() {
    MavenArtifact artifact = getJmxAgentJarMavenArtifact();
    if (artifact==null)
        throw new IllegalStateException("Either JMX is not enabled or there is an error in the configuration (JMX mode "+getJmxAgentMode()+" does not support agent JAR)");
    String jar = "classpath://" + artifact.getFilename();
    if (ResourceUtils.create(this).doesUrlExist(jar))
        return jar;

    String result = MavenRetriever.localUrl(artifact);
    if (warnedAboutNotOnClasspath) {
        log.debug("JMX JAR for "+artifact+" is not on the classpath; taking from "+result);
    } else {
        log.warn("JMX JAR for "+artifact+" is not on the classpath; taking from "+result+" (subsequent similar messages will be logged at debug)");
        warnedAboutNotOnClasspath = true;
    }
    return result;
}
 
Example #2
Source File: PostgreSqlNodeChefImplFromScratch.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
protected void postStartCustom() {
    // now run the creation script
    String creationScript;
    String creationScriptUrl = entity().getConfig(PostgreSqlNode.CREATION_SCRIPT_URL);
    if (creationScriptUrl != null) {
        creationScript = ResourceUtils.create(entity()).getResourceAsString(creationScriptUrl);
    } else {
        creationScript = entity().getConfig(PostgreSqlNode.CREATION_SCRIPT_CONTENTS);
    }
    entity().executeScript(creationScript);

    // and finally connect sensors
    entity().connectSensors();
    super.postStartCustom();
}
 
Example #3
Source File: UrlsExternalConfigSupplier.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public UrlsExternalConfigSupplier(ManagementContext managementContext, String name, Map<String, String> config) throws IOException {
    super(managementContext, name);
    this.config = config;
    resourceUtils = ResourceUtils.create(
            managementContext.getCatalogClassLoader(), 
            this, 
            UrlsExternalConfigSupplier.class.getSimpleName()+"("+getName()+")");

    Map<String, String> missing = Maps.newLinkedHashMap();
    for (Map.Entry<String, String> entry : config.entrySet()) {
        String target = entry.getValue();
        if (!resourceUtils.doesUrlExist(target)) {
            missing.put(entry.getKey(), entry.getValue());
        }
    }
    if (missing.size() > 0) {
        throw new IllegalStateException("URLs for external config '"+getName()+"' not found: "+missing);
    }
}
 
Example #4
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 #5
Source File: LocationConfigUtils.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the given file(s) in-order, returning the contents of the first file that can be read.
 * Returns the file contents, or null if none of the files can be read.
 *  
 * @param files             list of file paths
 */
private static String getFileContents(Iterable<String> files) {
    Iterator<String> fi = files.iterator();
    while (fi.hasNext()) {
        String file = fi.next();
        if (groovyTruth(file)) {
            try {
                // see comment above
                String result = ResourceUtils.create().getResourceAsString(file);
                if (result!=null) return result;
                log.debug("Invalid file "+file+" ; " + (!fi.hasNext() ? "no more files to try" : "trying next file")+" (null)");
            } catch (Exception e) {
                Exceptions.propagateIfFatal(e);
                log.debug("Invalid file "+file+" ; " + (!fi.hasNext() ? "no more files to try" : "trying next file"), e);
            }
        }
    }
    return null;
}
 
Example #6
Source File: SshTasks.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** task to install a file given a url, where the url is resolved remotely first then locally */
public static TaskFactory<?> installFromUrl(final ResourceUtils utils, final Map<String, ?> props, final SshMachineLocation location, final String url, final String destPath) {
    return new TaskFactory<TaskAdaptable<?>>() {
        @Override
        public TaskAdaptable<?> newTask() {
            return Tasks.<Void>builder().displayName("installing "+Urls.getBasename(url)).description("installing "+url+" to "+destPath).body(new Runnable() {
                @Override
                public void run() {
                    int result = location.installTo(utils, props, url, destPath);
                    if (result!=0) 
                        throw new IllegalStateException("Failed to install '"+url+"' to '"+destPath+"' at "+location+": exit code "+result);
                }
            }).build();
        }
    };
}
 
Example #7
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 #8
Source File: ServerResource.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public VersionSummary getVersion() {
    if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SERVER_STATUS, null))
        throw WebResourceUtils.forbidden("User '%s' is not authorized to perform this operation", Entitlements.getEntitlementContext().user());
    
    // TODO
    // * "build-metadata.properties" is probably the wrong name
    // * we should include brooklyn.version and a build timestamp in this file
    // * the authority for brooklyn should probably be core rather than brooklyn-rest-server
    InputStream input = ResourceUtils.create(this).getResourceFromUrl("classpath://build-metadata.properties");
    Properties properties = new Properties();
    String gitSha1 = null, gitBranch = null;
    try {
        properties.load(input);
        gitSha1 = properties.getProperty(BUILD_SHA_1_PROPERTY);
        gitBranch = properties.getProperty(BUILD_BRANCH_PROPERTY);
    } catch (IOException e) {
        log.error("Failed to load build-metadata.properties", e);
    }
    gitSha1 = BrooklynVersion.INSTANCE.getSha1FromOsgiManifest();

    FluentIterable<BrooklynFeatureSummary> features = FluentIterable.from(BrooklynVersion.getFeatures(mgmt()))
            .transform(BrooklynFeatureTransformer.FROM_FEATURE);

    return new VersionSummary(BrooklynVersion.get(), gitSha1, gitBranch, features.toList());
}
 
Example #9
Source File: Main.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected void computeAndSetApp(BrooklynLauncher launcher, ResourceUtils utils, GroovyClassLoader loader)
    throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
    if (app != null) {
        // Create the instance of the brooklyn app
        log.debug("Loading the user's application: {}", app);
   
        if (isYamlApp()) {
            log.debug("Loading application as YAML spec: {}", app);
            String content = utils.getResourceAsString(app);
            launcher.application(content);
        } else {
            EntitySpec<? extends Application> appSpec = loadApplicationFromClasspathOrParse(utils, loader, app);
            launcher.application(appSpec);
        }
    }
}
 
Example #10
Source File: VanillaSoftwareYamlTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** yaml variant of VanillaSoftwareProcessAndChildrenIntegrationTest */
@Test(groups="Integration")
public void testVanillaSoftwareYamlWithChildStartedAfter() {
    SimpleYamlLauncher l = new SimpleYamlLauncherForTests();
    try {
        Application app = l.launchAppYaml("vanilla-software-with-child-blueprint.yaml");
        log.info("started "+app);

        Entity p1 = Iterables.getOnlyElement( app.getChildren() );
        Long d1 = Long.parseLong( Strings.getFirstWordAfter(new ResourceUtils(this).getResourceAsString(Os.mergePaths(p1.getAttribute(SoftwareProcess.RUN_DIR), "DATE")), "utc") );
        
        Entity p2 = Iterables.getOnlyElement( p1.getChildren() );
        Long d2 = Long.parseLong( Strings.getFirstWordAfter(new ResourceUtils(this).getResourceAsString(Os.mergePaths(p2.getAttribute(SoftwareProcess.RUN_DIR), "DATE")), "utc") );
        Assert.assertTrue( d2-d1 > 2 && d2-d1 < 10, "p2 should have started 3s after parent, but it did not ("+(d2-d1)+"s difference" );
    } finally {
        l.destroyAll();
    }
    log.info("DONE");
}
 
Example #11
Source File: RebindJcloudsLocationLiveTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test(groups={"Live", "WIP"}, enabled=false)
public void testRebindsToJcloudsMachineWithInvalidTemplate() throws Exception {
    ResourceUtils resourceUtils = ResourceUtils.create(this);
    FileUtils.write(
            new File(mementoDir, "locations/briByOel"),
            resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-azure-parent-briByOel"));
    FileUtils.write(
            new File(mementoDir, "locations/VNapYjwp"),
            resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-azure-machine-VNapYjwp"));
    
    TestApplication newApp = rebind();
    
    JcloudsLocation loc = (JcloudsLocation) newApp.getManagementContext().getLocationManager().getLocation("briByOel");
    JcloudsSshMachineLocation machine = (JcloudsSshMachineLocation) newApp.getManagementContext().getLocationManager().getLocation("VNapYjwp");
    assertEquals(ImmutableSet.of(loc.getChildren()), ImmutableSet.of(machine));
}
 
Example #12
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 #13
Source File: JcloudsRebindStubUnitTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testHistoricLocationWithoutSemaphoresStops() throws Exception {
    ResourceUtils resourceUtils = ResourceUtils.create(this);
    FileUtils.write(
            new File(mementoDir, "locations/afy79330h5"),
            resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-no-semaphores-stubbed-parent-afy79330h5"));
    FileUtils.write(
            new File(mementoDir, "locations/l27nwbyisk"),
            resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-no-semaphores-stubbed-machine-l27nwbyisk"));

    rebind();
    
    JcloudsLocation jcloudsLoc = (JcloudsLocation) mgmt().getLocationManager().getLocation("afy79330h5");
    JcloudsSshMachineLocation machine = (JcloudsSshMachineLocation) mgmt().getLocationManager().getLocation("l27nwbyisk");
    
    jcloudsLoc.release(machine);
}
 
Example #14
Source File: CatalogScanOsgiTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
static void installJavaScanningMoreEntitiesV2(ManagementContext mgmt, Object context) throws FileNotFoundException {
    // scanning bundle functionality added in 0.12.0, relatively new compared to non-osgi scanning
    
    TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH);
    
    CampYamlLiteTest.installWithoutCatalogBom(mgmt, OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    
    BundleMaker bm = new BundleMaker(mgmt);
    File f = Os.newTempFile(context.getClass(), "jar");
    Streams.copy(ResourceUtils.create(context).getResourceFromUrl(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH), new FileOutputStream(f));
    f = bm.copyRemoving(f, MutableSet.of("catalog.bom"));
    f = bm.copyAdding(f, MutableMap.of(new ZipEntry("catalog.bom"),
        new ByteArrayInputStream( Strings.lines(
            "brooklyn.catalog:",
            "  scanJavaAnnotations: true").getBytes() ) ));
    
    ((ManagementContextInternal)mgmt).getOsgiManager().get().install(new FileInputStream(f)).checkNoError();
}
 
Example #15
Source File: CatalogOsgiVersionMoreEntityRebindTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected RegisteredType installWrappedMoreEntity() throws Exception {

        TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH);
        TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH);

        ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( 
            new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_OSGI_ENTITIES_URL) ).checkNoError();
        ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( 
            new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_URL) ).get();
        addCatalogItems(
            "brooklyn.catalog:",
            "  id: wrapped-more-entity",
            "  version: 1.0",
            "  item:",
            "    services:",
            "    - type: " + BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
        
        RegisteredType ci = Preconditions.checkNotNull( mgmt().getTypeRegistry().get("wrapped-more-entity") );
        return ci;
    }
 
Example #16
Source File: CatalogOsgiVersionMoreEntityTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testBrooklynManagedBundleInstall() throws Exception {

    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH);

    OsgiBundleInstallationResult br = ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( 
        new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V1_URL) ).get();
    Assert.assertEquals(br.getVersionedName().toString(), BROOKLYN_TEST_MORE_ENTITIES_SYMBOLIC_NAME_FULL+":"+"0.1.0");
    
    // bundle installed
    Map<String, ManagedBundle> bundles = ((ManagementContextInternal)mgmt()).getOsgiManager().get().getManagedBundles();
    Asserts.assertSize(bundles.keySet(), 1);
    Assert.assertTrue(bundles.keySet().contains( br.getMetadata().getId() ));
    
    // types installed
    RegisteredType t = mgmt().getTypeRegistry().get(BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
    Assert.assertNotNull(t);
    Assert.assertEquals(t.getContainingBundle(), br.getVersionedName().toString());
    
    // can deploy
    createAndStartApplication("services: [ { type: "+BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY+" } ]");
}
 
Example #17
Source File: JmxSupport.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** installs files needed for JMX, to the runDir given in constructor, assuming the runDir has been created */
public void install() {
    if (EnumSet.of(JmxAgentModes.JMXMP_AND_RMI, JmxAgentModes.JMXMP, JmxAgentModes.JMX_RMI_CUSTOM_AGENT).contains(getJmxAgentMode())) {
        Tasks.setBlockingDetails("Copying JMX agent jar to server.");
        try {
            getMachine().get().copyTo(ResourceUtils.create(this).getResourceFromUrl(
                    getJmxAgentJarUrl()), getJmxAgentJarDestinationFilePath());
        } finally {
            Tasks.resetBlockingDetails();
        }
    }
    if (isSecure()) {
        getJmxSslSupport().install();
    }
}
 
Example #18
Source File: BasicBrooklynCatalog.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated since 1.0.0; Catalog annotation is deprecated.
 */
@Deprecated
@SuppressWarnings("unused")  // keep during 0.12.0 until we are decided we won't support this; search for this method name
// (note that this now could work after rebind since we have the OSGi cache)
private Collection<CatalogItemDtoAbstract<?, ?>> scanAnnotationsInBundle(ManagementContext mgmt, ManagedBundle containingBundle) {
    CatalogDto dto = CatalogDto.newNamedInstance("Bundle "+containingBundle.getVersionedName().toOsgiString()+" Scanned Catalog", "All annotated Brooklyn entities detected in bundles", "scanning-bundle-"+containingBundle.getVersionedName().toOsgiString());
    CatalogDo subCatalog = new CatalogDo(dto);
    // need access to a JAR to scan this
    String url = null;
    File f = ((ManagementContextInternal)mgmt).getOsgiManager().get().getBundleFile(containingBundle);
    if (f!=null) {
        url = "file//:"+f.getAbsolutePath();
    }
    if (url==null) {
        url = containingBundle.getUrl();
    }
    if (url==null) {
        // should now always be available 
        throw new IllegalArgumentException("Error preparing to scan "+containingBundle.getVersionedName()+": no URL available");
    }
    // org.reflections requires the URL to be "file:" containg ".jar"
    File fJar = Os.newTempFile(containingBundle.getVersionedName().toOsgiString(), ".jar");
    try {
        Streams.copy(ResourceUtils.create().getResourceFromUrl(url), new FileOutputStream(fJar));
        subCatalog.addToClasspath(new String[] { "file:"+fJar.getAbsolutePath() });
        Collection<CatalogItemDtoAbstract<?, ?>> result = scanAnnotationsInternal(mgmt, subCatalog, MutableMap.of("version", containingBundle.getSuppliedVersionString()), containingBundle);
        return result;
    } catch (FileNotFoundException e) {
        throw Exceptions.propagateAnnotated("Error extracting "+url+" to scan "+containingBundle.getVersionedName(), e);
    } finally {
        fJar.delete();
    }
}
 
Example #19
Source File: JmxSupport.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected synchronized void init() {
    if (isJmx!=null)
        return;

    if (Boolean.FALSE.equals(entity.getConfig(USE_JMX))) {
        isJmx = false;
        return;
    }
    isJmx = true;
    jmxAgentMode = entity.getConfig(JMX_AGENT_MODE);
    if (jmxAgentMode==null) jmxAgentMode = JmxAgentModes.AUTODETECT;

    isSecure = entity.getConfig(JMX_SSL_ENABLED);
    if (isSecure==null) isSecure = false;

    if (jmxAgentMode==JmxAgentModes.AUTODETECT) {
        if (isSecure()) {
            jmxAgentMode = JmxAgentModes.JMXMP;
        } else {
            jmxAgentMode = JmxAgentModes.JMXMP_AND_RMI;
            if (!ResourceUtils.create(this).doesUrlExist(getJmxAgentJarUrl())) {
                // can happen e.g. if eclipse build
                log.warn("JMX agent JAR not found ("+getJmxAgentJarUrl()+") when auto-detecting JMX settings for "+entity+"; " +
                        "likely cause is an incomplete build (e.g. from Eclipse; run a maven build then retry in the IDE); "+
                        "reverting to NONE (use built-in Java JMX support, which will not go through firewalls)");
                jmxAgentMode = JmxAgentModes.NONE;
            }
        }

        entity.config().set(JMX_AGENT_MODE, jmxAgentMode);
    }

    if (isSecure && jmxAgentMode!=JmxAgentModes.JMXMP) {
        String msg = "JMX SSL is specified, but it requires JMXMP which is disabled, when configuring "+entity;
        log.warn(msg);
        throw new IllegalStateException(msg);
    }
}
 
Example #20
Source File: CleanOrphanedLocationsIntegrationTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private String copyResourcePathToTempPath(String resourcePath) {
    BundleMaker bm = new BundleMaker(ResourceUtils.create(this));
    bm.setDefaultClassForLoading(CleanOrphanedLocationsIntegrationTest.class);
    File jar = bm.createJarFromClasspathDir(resourcePath);
    File output = Os.newTempDir("brooklyn-test-resouce-from-"+resourcePath);
    try {
        ArchiveUtils.extractZip(new ZipFile(jar), output.getAbsolutePath());
        return output.getAbsolutePath();
    } catch (Exception e) {
        throw Exceptions.propagate(e);
    }
}
 
Example #21
Source File: JmxSupportTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public void testJmxmpJarExistence() {
    app = TestApplication.Factory.newManagedInstanceForTests();
    app.config().set(JmxSupport.JMX_AGENT_MODE, JmxAgentModes.JMXMP);
    JmxSupport support = new JmxSupport(app, null);
    
    Assert.assertEquals(support.getJmxAgentJarMavenArtifact().getArtifactId(),
            "brooklyn-jmxmp-agent");
    
    Assert.assertTrue(ResourceUtils.create(this).doesUrlExist(support.getJmxAgentJarUrl()), support.getJmxAgentJarUrl());
    Assert.assertTrue(support.getJmxAgentJarUrl().contains("-shaded-"), support.getJmxAgentJarUrl());
}
 
Example #22
Source File: JavaWebAppWithDslYamlRebindIntegrationTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups={"Integration", "WIP", "Broken"})
public void testJavaWebWithMemberSpecRebind() throws Exception {
    Reader input = Streams.reader(new ResourceUtils(this).getResourceFromUrl("test-java-web-app-spec-and-db-with-function.yaml"));
    AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input);

    Assembly assembly = at.getInstantiator().newInstance().instantiate(at, platform);
    final Application app = (Application) mgmt().getEntityManager().getEntity(assembly.getId());

    Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), app);
    for (Task<?> t: tasks) t.blockUntilEnded();
    Dumper.dumpInfo(app);
    
    Application app2 = rebind(app);
    Assert.assertEquals(app2.getChildren().size(), 2);
}
 
Example #23
Source File: AbstractYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Beta
public static String loadYaml(Object loadContext, String yamlFileName, String ...extraLines) throws Exception {
    ResourceUtils ru = new ResourceUtils(loadContext);
    if (!ru.doesUrlExist(yamlFileName)) {
        if (ru.doesUrlExist(Urls.mergePaths(loadContext.getClass().getPackage().getName().replace('.', '/'), yamlFileName))) {
            // look in package-specific folder if not found at root
            yamlFileName = Urls.mergePaths(loadContext.getClass().getPackage().getName().replace('.', '/'), yamlFileName);
        }
    }
    String input = ru.getResourceAsString(yamlFileName).trim();
    StringBuilder builder = new StringBuilder(input);
    for (String l: extraLines)
        builder.append("\n").append(l);
    return builder.toString();
}
 
Example #24
Source File: CatalogOsgiYamlEntityTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeepCatalogItemCanLoadResources() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_PATH);

    String symbolicNameInner = "my.catalog.app.id.inner";
    String symbolicNameFiller = "my.catalog.app.id.filler";
    String symbolicNameOuter = "my.catalog.app.id.outer";
    addCatalogItems(
        "brooklyn.catalog:",
        "  version: " + TEST_VERSION,
        "  items:",
        "  - id: " + symbolicNameInner,
        "    name: My Catalog App",
        "    brooklyn.libraries:",
        "    - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
        "    item: " + SIMPLE_ENTITY_TYPE,
        "  - id: " + symbolicNameFiller,
        "    name: Filler App",
        "    brooklyn.libraries:",
        "    - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_URL,
        "    item: " + symbolicNameInner,
        "  - id: " + symbolicNameOuter,
        "    item: " + symbolicNameFiller);

    String yaml = "name: " + symbolicNameOuter + "\n" +
        "services: \n" +
        "  - serviceType: "+ver(symbolicNameOuter);
    Entity app = createAndStartApplication(yaml);
    Entity entity = app.getChildren().iterator().next();

    final String catalogBom = ResourceUtils.create(entity).getResourceAsString("classpath://" + MORE_ENTITIES_POM_PROPERTIES_PATH);
    assertTrue(catalogBom.contains("artifactId=brooklyn-test-osgi-more-entities"));

    deleteCatalogEntity(symbolicNameOuter);
    deleteCatalogEntity(symbolicNameFiller);
    deleteCatalogEntity(symbolicNameInner);
}
 
Example #25
Source File: ArchiveUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Deploys an archive file to a remote machine and extracts the contents.
 * <p>
 * Copies the archive file from the given URL to a file in a temporary directory and extracts
 * the contents in the destination directory. For Java archives of type {@code .jar},
 * {@code .war} or {@code .ear} the file is simply copied.
 * 
 * @return true if the archive is downloaded AND unpacked; false if it is downloaded but not unpacked; 
 * throws if there was an error downloading or, for known archive types, unpacking.
 *
 * @see #deploy(String, SshMachineLocation, String)
 * @see #deploy(Map, String, SshMachineLocation, String, String, String)
 * @see #install(SshMachineLocation, String, String, int)
 */
public static boolean deploy(ResourceUtils resolver, Map<String, ?> props, String archiveUrl, SshMachineLocation machine, String destDir, boolean keepArchiveAfterUnpacking, String optionalTmpDir, String optionalDestFile) {
    String destFile = optionalDestFile;
    if (destFile==null) destFile = Urls.getBasename(Preconditions.checkNotNull(archiveUrl, "archiveUrl"));
    if (Strings.isBlank(destFile)) 
        throw new IllegalStateException("Not given filename and cannot infer archive type from '"+archiveUrl+"'");
    
    String tmpDir = optionalTmpDir;
    if (tmpDir==null) tmpDir=Preconditions.checkNotNull(destDir, "destDir");
    if (props==null) props = MutableMap.of();
    String destPath = Os.mergePaths(tmpDir, destFile);

    // Use the location mutex to prevent package manager locking issues
    machine.acquireMutex("installing", "installing archive");
    try {
        int result = install(resolver, props, machine, archiveUrl, destPath, NUM_RETRIES_FOR_COPYING);
        if (result != 0) {
            throw new IllegalStateException(format("Unable to install archive %s to %s", archiveUrl, machine));
        }

        // extract, now using task if available
        MutableList<String> commands = MutableList.copyOf(installCommands(destFile))
                .appendAll(extractCommands(destFile, tmpDir, destDir, false, keepArchiveAfterUnpacking));
        if (DynamicTasks.getTaskQueuingContext()!=null) {
            result = DynamicTasks.queue(SshTasks.newSshExecTaskFactory(machine, commands.toArray(new String[0])).summary("extracting archive").requiringExitCodeZero()).get();
        } else {
            result = machine.execCommands(props, "extracting content", commands);
        }
        if (result != 0) {
            throw new IllegalStateException(format("Failed to expand archive %s on %s", archiveUrl, machine));
        }
        return ArchiveType.of(destFile)!=ArchiveType.UNKNOWN;
    } finally {
        machine.releaseMutex("installing");
    }
}
 
Example #26
Source File: BashCommandsIntegrationTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@BeforeMethod(alwaysRun=true)
public void setUp() throws Exception {
    super.setUp();
    
    exec = new BasicExecutionContext(mgmt.getExecutionManager());
    
    destFile = Os.newTempFile(getClass(), "commoncommands-test-dest.txt");
    
    sourceNonExistantFile = new File("/this/does/not/exist/ERQBETJJIG1234");
    sourceNonExistantFileUrl = sourceNonExistantFile.toURI().toString();
    
    sourceFile1 = Os.newTempFile(getClass(), "commoncommands-test.txt");
    sourceFileUrl1 = sourceFile1.toURI().toString();
    Files.write("mysource1".getBytes(), sourceFile1);
    
    sourceFile2 = Os.newTempFile(getClass(), "commoncommands-test2.txt");
    sourceFileUrl2 = sourceFile2.toURI().toString();
    Files.write("mysource2".getBytes(), sourceFile2);

    localRepoEntityVersionPath = JavaClassNames.simpleClassName(this)+"-test-dest-"+Identifiers.makeRandomId(8);
    localRepoBasePath = new File(format("%s/.brooklyn/repository", System.getProperty("user.home")));
    localRepoEntityBasePath = new File(localRepoBasePath, localRepoEntityVersionPath);
    localRepoEntityFile = new File(localRepoEntityBasePath, localRepoFilename);
    localRepoEntityBasePath.mkdirs();
    Files.write("mylocal1".getBytes(), localRepoEntityFile);

    tmpSudoersFile = Os.newTempFile(getClass(), "sudoers" + Identifiers.makeRandomId(8));

    String sudoers = ResourceUtils.create(this).getResourceAsString("classpath://brooklyn/util/ssh/test_sudoers");
    Files.write(sudoers.getBytes(), tmpSudoersFile);
    
    loc = mgmt.getLocationManager().createLocation(LocalhostMachineProvisioningLocation.spec()).obtain();
}
 
Example #27
Source File: SecureKeysAndSignerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecodeDsaPublicKey() throws Exception {
    String data = ResourceUtils.create(this).getResourceAsString("classpath://brooklyn/util/crypto/sample_dsa.pem.pub");
    PublicKey key = AuthorizedKeysParser.decodePublicKey(data);
    String data2 = AuthorizedKeysParser.encodePublicKey(key);
    Assert.assertTrue(data.contains(data2), "Expected to find '"+data2+"' in '"+data+"'");
    PublicKey key2 = AuthorizedKeysParser.decodePublicKey(data2);
    Assert.assertEquals(key2, key);
}
 
Example #28
Source File: CatalogScanTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private synchronized void loadTheDefaultCatalog(boolean lookOnDiskForDefaultCatalog) {
    if (defaultCatalog!=null) return;
    BrooklynProperties props = BrooklynProperties.Factory.newEmpty();
    props.put(BrooklynServerConfig.BROOKLYN_CATALOG_URL.getName(),
        // if default catalog is picked up from the system, we might get random stuff from ~/.brooklyn/ instead of the default;
        // useful as an integration check that we default correctly, but irritating for people to use if they have such a catalog installed
        (lookOnDiskForDefaultCatalog ? "" :
            "data:,"+Urls.encode(new ResourceUtils(this).getResourceAsString("classpath:/brooklyn/default.catalog.bom"))));
    LocalManagementContext mgmt = newManagementContext(props);
    defaultCatalog = mgmt.getCatalog();
    log.info("ENTITIES loaded for DEFAULT: "+defaultCatalog.getCatalogItems(Predicates.alwaysTrue()));
}
 
Example #29
Source File: CassandraDatacenterTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testBigIntegerFormattedCorrectly() {
    Map<String, Object> substitutions = ImmutableMap.<String, Object>builder()
            .put("entity", new MockInputForTemplate())
            .put("driver", new MockInputForTemplate())
            .build();

    String templatedUrl = CassandraNode.CASSANDRA_CONFIG_TEMPLATE_URL.getDefaultValue();
    String url = TemplateProcessor.processTemplateContents(templatedUrl, ImmutableMap.of("entity", ImmutableMap.of("majorMinorVersion", "1.2")));
    String templateContents = new ResourceUtils(this).getResourceAsString(url);
    String processedTemplate = TemplateProcessor.processTemplateContents(templateContents, substitutions);
    Assert.assertEquals(processedTemplate.indexOf("775,808"), -1);
    Assert.assertTrue(processedTemplate.indexOf("-9223372036854775808") > 0);
}
 
Example #30
Source File: VanillaJavaAppTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
    if (BROOKLYN_THIS_CLASSPATH==null) {
        BROOKLYN_THIS_CLASSPATH = ResourceUtils.create(MAIN_CLASS).getClassLoaderDir();
    }
    app = TestApplication.Factory.newManagedInstanceForTests();
    loc = app.newLocalhostProvisioningLocation(MutableMap.of("address", "localhost"));
}