Java Code Examples for org.apache.brooklyn.util.core.ResourceUtils#create()
The following examples show how to use
org.apache.brooklyn.util.core.ResourceUtils#create() .
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 |
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: RebindJcloudsLocationLiveTest.java From brooklyn-server with Apache License 2.0 | 6 votes |
@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 3
Source File: UrlsExternalConfigSupplier.java From brooklyn-server with Apache License 2.0 | 6 votes |
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: JcloudsRebindStubUnitTest.java From brooklyn-server with Apache License 2.0 | 6 votes |
@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 5
Source File: CliTest.java From brooklyn-server with Apache License 2.0 | 6 votes |
@Test public void testInvokeGroovyScript() throws Exception { File groovyFile = File.createTempFile("testinvokegroovy", "groovy"); try { String contents = CliTest.class.getCanonicalName()+".GROOVY_INVOKED.set(true);"; Files.write(contents.getBytes(), groovyFile); LaunchCommand launchCommand = new Main.LaunchCommand(); ResourceUtils resourceUtils = ResourceUtils.create(this); GroovyClassLoader loader = new GroovyClassLoader(CliTest.class.getClassLoader()); launchCommand.execGroovyScript(resourceUtils, loader, groovyFile.toURI().toString()); assertTrue(GROOVY_INVOKED.get()); } finally { groovyFile.delete(); GROOVY_INVOKED.set(false); } }
Example 6
Source File: MyEntityImpl.java From brooklyn-server with Apache License 2.0 | 6 votes |
@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 7
Source File: ArchiveUtils.java From brooklyn-server with Apache License 2.0 | 5 votes |
public static int install(ResourceUtils resolver, Map<String, ?> props, SshMachineLocation machine, String urlToInstall, String target, int numAttempts) { if (resolver==null) resolver = ResourceUtils.create(machine); Exception lastError = null; int retriesRemaining = numAttempts; int attemptNum = 0; do { attemptNum++; try { Tasks.setBlockingDetails("Installing "+urlToInstall+" at "+machine); // TODO would be nice to have this in a task (and the things within it!) return machine.installTo(resolver, props, urlToInstall, target); } catch (Exception e) { Exceptions.propagateIfFatal(e); lastError = e; String stack = StackTraceSimplifier.toString(e); if (stack.contains("net.schmizz.sshj.sftp.RemoteFile.write")) { log.warn("Failed to transfer "+urlToInstall+" to "+machine+", retryable error, attempt "+attemptNum+"/"+numAttempts+": "+e); continue; } log.warn("Failed to transfer "+urlToInstall+" to "+machine+", not a retryable error so failing: "+e); throw Exceptions.propagate(e); } finally { Tasks.resetBlockingDetails(); } } while (retriesRemaining --> 0); throw Exceptions.propagate(lastError); }
Example 8
Source File: CatalogOsgiLibraryTest.java From brooklyn-server with Apache License 2.0 | 5 votes |
protected void assertCanFindMessages(Entity entity) { ResourceUtils ru = ResourceUtils.create(entity); Iterable<URL> files = ru.getResources("org/apache/brooklyn/test/osgi/resources/message.txt"); if (!files.iterator().hasNext()) { Dumper.dumpInfo(entity); Assert.fail("Expected to find 'messages.txt'"); } }
Example 9
Source File: CatalogOsgiLibraryTest.java From brooklyn-server with Apache License 2.0 | 5 votes |
protected void assertCannotFindMessages(Entity entity) { ResourceUtils ru = ResourceUtils.create(entity); Iterable<URL> files = ru.getResources("org/apache/brooklyn/test/osgi/resources/message.txt"); if (files.iterator().hasNext()) { Dumper.dumpInfo(entity); Assert.fail("Expected NOT to find 'messages.txt'"); } }
Example 10
Source File: ArchiveUtilsTest.java From brooklyn-server with Apache License 2.0 | 5 votes |
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); machine = app.newLocalhostProvisioningLocation().obtain(); resourceUtils = ResourceUtils.create(ArchiveUtilsTest.class); destDir = Os.newTempDir(getClass().getSimpleName()); }
Example 11
Source File: OsgiStandaloneTest.java From brooklyn-server with Apache License 2.0 | 5 votes |
@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 12
Source File: ResourceUtilsTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
@BeforeClass(alwaysRun=true) public void setUp() throws Exception { utils = ResourceUtils.create(this, "mycontext"); tempFile = Os.writeToTempFile(new ByteArrayInputStream(tempFileContents.getBytes()), "resourceutils-test", ".txt"); }
Example 13
Source File: BundleMaker.java From brooklyn-server with Apache License 2.0 | 4 votes |
public BundleMaker(@Nonnull ManagementContext mgmt) { this(((ManagementContextInternal) mgmt).getOsgiManager().get().getFramework(), ResourceUtils.create()); }
Example 14
Source File: CliTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
private Object loadApplicationFromClasspathOrParse(String appName) throws Exception { LaunchCommand launchCommand = new Main.LaunchCommand(); ResourceUtils resourceUtils = ResourceUtils.create(this); GroovyClassLoader loader = new GroovyClassLoader(CliTest.class.getClassLoader()); return launchCommand.loadApplicationFromClasspathOrParse(resourceUtils, loader, appName); }
Example 15
Source File: AbstractSoftwareProcessDriver.java From brooklyn-server with Apache License 2.0 | 4 votes |
public AbstractSoftwareProcessDriver(EntityLocal entity, Location location) { this.entity = checkNotNull(entity, "entity"); this.location = checkNotNull(location, "location"); this.resource = ResourceUtils.create(entity); }
Example 16
Source File: WebAppContextProvider.java From brooklyn-server with Apache License 2.0 | 4 votes |
/** * 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 17
Source File: CatalogMakeOsgiBundleTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
@BeforeClass(alwaysRun = true) public void setUp() throws Exception { super.setUp(); bm = new BundleMaker( ((LocalManagementContext)mgmt()).getOsgiManager().get().getFramework(), ResourceUtils.create(this) ); }
Example 18
Source File: RebindJcloudsLocationLiveTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
@Test(groups={"Live", "Live-sanity"}) public void testRebindsToJcloudsWinrmMachineWithTemplateAndNode() throws Exception { // Populate the mementoDir with some old-style peristed state ResourceUtils resourceUtils = ResourceUtils.create(this); String origParentXml = resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-aws-winrm-parent-fKc0Ofyn"); String origMachineXml = resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-aws-winrm-machine-KYSryzW8"); File persistedParentFile = new File(mementoDir, "locations/fKc0Ofyn"); File persistedMachineFile = new File(mementoDir, "locations/KYSryzW8"); FileUtils.write( persistedParentFile, origParentXml); FileUtils.write( persistedMachineFile, origMachineXml); assertTrue(origMachineXml.contains("NodeMetadataImpl"), origMachineXml); // Rebind to the old-style persisted state, which includes the NodeMetadata and the Template objects. // Expect to parse that ok. TestApplication app2 = rebind(); JcloudsLocation loc2 = (JcloudsLocation) app2.getManagementContext().getLocationManager().getLocation("fKc0Ofyn"); JcloudsWinRmMachineLocation machine2 = (JcloudsWinRmMachineLocation) app2.getManagementContext().getLocationManager().getLocation("KYSryzW8"); assertEquals(ImmutableSet.copyOf(loc2.getChildren()), ImmutableSet.of(machine2)); String errmsg = "loc="+loc2.toVerboseString()+"; machine="+machine2.toVerboseString(); assertEquals(machine2.getId(), "KYSryzW8", errmsg); assertEquals(machine2.getJcloudsId(), "eu-central-1/i-372eda8a", errmsg); assertEquals(machine2.getAddress().getHostAddress(), "52.28.153.46", errmsg); assertEquals(machine2.getPort(), 5985, errmsg); // FIXME assertEquals(machine2.getAddress().getHostAddress(), HostAndPort.fromParts("ec2-52-28-153-46.eu-central-1.compute.amazonaws.com", 22), errmsg); assertEquals(machine2.getPrivateAddresses(), ImmutableSet.of("172.31.18.175"), errmsg); assertEquals(machine2.getPublicAddresses(), ImmutableSet.of("52.28.153.46"), errmsg); assertEquals(machine2.getPrivateAddress(), Optional.of("172.31.18.175"), errmsg); assertEquals(machine2.getHostname(), "ip-172-31-18-175", errmsg); // TODO would prefer the hostname that works inside and out assertNull(machine2.getOsDetails(), errmsg); // JcloudsWinRmMachineLocation never had OsDetails // Force it to be persisted again. Expect to pesist without the NodeMetadata and Template. app2.getManagementContext().getRebindManager().getChangeListener().onChanged(loc2); app2.getManagementContext().getRebindManager().getChangeListener().onChanged(machine2); RebindTestUtils.stopPersistence(app2); String newMachineXml = new String(java.nio.file.Files.readAllBytes(persistedMachineFile.toPath())); assertFalse(newMachineXml.contains("NodeMetadataImpl"), newMachineXml); // Rebind again, with the re-written persisted state. TestApplication app3 = rebind(); JcloudsLocation loc3 = (JcloudsLocation) app3.getManagementContext().getLocationManager().getLocation("fKc0Ofyn"); JcloudsWinRmMachineLocation machine3 = (JcloudsWinRmMachineLocation) app3.getManagementContext().getLocationManager().getLocation("KYSryzW8"); assertEquals(ImmutableSet.copyOf(loc3.getChildren()), ImmutableSet.of(machine3)); errmsg = "loc="+loc3.toVerboseString()+"; machine="+machine3.toVerboseString(); assertEquals(machine3.getId(), "KYSryzW8", errmsg); assertEquals(machine3.getJcloudsId(), "eu-central-1/i-372eda8a", errmsg); assertEquals(machine3.getAddress().getHostAddress(), "52.28.153.46", errmsg); assertEquals(machine3.getPort(), 5985, errmsg); assertEquals(machine3.getPrivateAddresses(), ImmutableSet.of("172.31.18.175"), errmsg); assertEquals(machine3.getPublicAddresses(), ImmutableSet.of("52.28.153.46"), errmsg); assertEquals(machine3.getPrivateAddress(), Optional.of("172.31.18.175"), errmsg); assertEquals(machine3.getHostname(), "ip-172-31-18-175", errmsg); // TODO would prefer the hostname that works inside and out assertNull(machine2.getOsDetails(), errmsg); // JcloudsWinRmMachineLocation never had OsDetails }
Example 19
Source File: RebindJcloudsLocationLiveTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
@Test(groups={"Live", "Live-sanity"}) public void testRebindsToJcloudsSshMachineWithTemplateAndNode() throws Exception { // Populate the mementoDir with some old-style peristed state ResourceUtils resourceUtils = ResourceUtils.create(this); String origParentXml = resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-aws-parent-lCYB3mTb"); String origMachineXml = resourceUtils.getResourceAsString("classpath://org/apache/brooklyn/location/jclouds/persisted-aws-machine-aKEcbxKN"); File persistedParentFile = new File(mementoDir, "locations/lCYB3mTb"); File persistedMachineFile = new File(mementoDir, "locations/aKEcbxKN"); FileUtils.write( persistedParentFile, origParentXml); FileUtils.write( persistedMachineFile, origMachineXml); assertTrue(origMachineXml.contains("AWSEC2TemplateOptions"), origMachineXml); assertTrue(origMachineXml.contains("NodeMetadataImpl"), origMachineXml); // Rebind to the old-style persisted state, which includes the NodeMetadata and the Template objects. // Expect to parse that ok. TestApplication app2 = rebind(); JcloudsLocation loc2 = (JcloudsLocation) app2.getManagementContext().getLocationManager().getLocation("lCYB3mTb"); JcloudsSshMachineLocation machine2 = (JcloudsSshMachineLocation) app2.getManagementContext().getLocationManager().getLocation("aKEcbxKN"); assertEquals(ImmutableSet.copyOf(loc2.getChildren()), ImmutableSet.of(machine2)); String errmsg = "loc="+loc2.toVerboseString()+"; machine="+machine2.toVerboseString(); assertNull(machine2.config().getLocalBag().getAllConfig().get("node"), errmsg); assertNull(machine2.config().getLocalBag().getAllConfig().get("template"), errmsg); assertEquals(machine2.getId(), "aKEcbxKN", errmsg); assertEquals(machine2.getJcloudsId(), "ap-southeast-1/i-56fd53f2", errmsg); assertEquals(machine2.getSshHostAndPort(), HostAndPort.fromParts("ec2-54-254-23-53.ap-southeast-1.compute.amazonaws.com", 22), errmsg); assertEquals(machine2.getPrivateAddresses(), ImmutableSet.of("10.144.66.5"), errmsg); assertEquals(machine2.getPublicAddresses(), ImmutableSet.of("54.254.23.53"), errmsg); assertEquals(machine2.getPrivateAddress(), Optional.of("10.144.66.5"), errmsg); assertEquals(machine2.getSubnetHostname(), "10.144.66.5", errmsg); assertEquals(machine2.getHostname(), "54.254.23.53", errmsg); assertEquals(machine2.getOsDetails().isWindows(), false, errmsg); assertEquals(machine2.getOsDetails().isLinux(), true, errmsg); assertEquals(machine2.getOsDetails().isMac(), false, errmsg); assertEquals(machine2.getOsDetails().getName(), "centos", errmsg); assertEquals(machine2.getOsDetails().getArch(), "x86_64", errmsg); assertEquals(machine2.getOsDetails().getVersion(), "6.5", errmsg); assertEquals(machine2.getOsDetails().is64bit(), true, errmsg); // Re-populates the @SetFromFlag fields from config machine2.configure(ImmutableMap.of()); // Force it to be persisted again. Expect to pesist without the NodeMetadata and Template. app2.getManagementContext().getRebindManager().getChangeListener().onChanged(loc2); app2.getManagementContext().getRebindManager().getChangeListener().onChanged(machine2); RebindTestUtils.stopPersistence(app2); String newMachineXml = new String(java.nio.file.Files.readAllBytes(persistedMachineFile.toPath())); assertFalse(newMachineXml.contains("AWSEC2TemplateOptions"), newMachineXml); assertFalse(newMachineXml.contains("NodeMetadataImpl"), newMachineXml); // Rebind again, with the re-written persisted state. TestApplication app3 = rebind(); JcloudsLocation loc3 = (JcloudsLocation) app3.getManagementContext().getLocationManager().getLocation("lCYB3mTb"); JcloudsSshMachineLocation machine3 = (JcloudsSshMachineLocation) app3.getManagementContext().getLocationManager().getLocation("aKEcbxKN"); assertEquals(ImmutableSet.copyOf(loc3.getChildren()), ImmutableSet.of(machine3)); errmsg = "loc="+loc3.toVerboseString()+"; machine="+machine3.toVerboseString(); assertEquals(machine3.getId(), "aKEcbxKN", errmsg); assertEquals(machine3.getJcloudsId(), "ap-southeast-1/i-56fd53f2", errmsg); assertEquals(machine3.getSshHostAndPort(), HostAndPort.fromParts("ec2-54-254-23-53.ap-southeast-1.compute.amazonaws.com", 22), errmsg); assertEquals(machine3.getPrivateAddresses(), ImmutableSet.of("10.144.66.5"), errmsg); assertEquals(machine3.getPublicAddresses(), ImmutableSet.of("54.254.23.53"), errmsg); assertEquals(machine3.getPrivateAddress(), Optional.of("10.144.66.5"), errmsg); assertEquals(machine3.getHostname(), "54.254.23.53", errmsg); // The VM is no longer running, so won't be able to infer OS Details. assertFalse(machine3.getOptionalOsDetails().isPresent(), errmsg); }
Example 20
Source File: HighstateTest.java From brooklyn-library with Apache License 2.0 | 4 votes |
private String getTestYaml() { final ResourceUtils resourceUtils = ResourceUtils.create(); final InputStream yaml = resourceUtils.getResourceFromUrl("classpath://test-highstate.yaml"); return Streams.readFullyStringAndClose(yaml); }