@Override public String get(String key) { String target = config.get(key); if (target == null) { throw new IllegalArgumentException("Unknown key '"+key+"' for "+toString()); } return resourceUtils.getResourceAsString(target); } }
protected void execGroovyScript(ResourceUtils utils, GroovyClassLoader loader, String script) { log.debug("Running the user provided script: {}", script); String content = utils.getResourceAsString(script); GroovyShell shell = new GroovyShell(loader); shell.evaluate(content); }
@VisibleForTesting static List<String> getIpAddressWebsites() { String file = new ResourceUtils(LocalhostExternalIpLoader.class) .getResourceAsString("classpath://org/apache/brooklyn/location/geo/external-ip-address-resolvers.txt"); Iterable<String> lines = Splitter.on('\n') .omitEmptyStrings() .trimResults() .split(file); List<String> urls = Lists.newArrayList(Iterables.filter(lines, Predicates.not(StringPredicates.startsWith("#")))); Collections.shuffle(urls); return urls; }
public static String generateScriptString(Date generationTime, Collection<HostGeoInfo> hosts) { String template = ResourceUtils.create(GeoscalingScriptGenerator.class).getResourceAsString(PHP_SCRIPT_TEMPLATE_RESOURCE); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss 'UTC'"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String datestamp = sdf.format(generationTime); String declarations = getHostsDeclaration(hosts); return template .replaceAll(DATESTAMP_MARKER, datestamp) .replace(HOSTS_DECLARATIONS_MARKER, declarations); }
@SuppressWarnings("unchecked") public static Iterable<String> getDefaultTypes() { String input = new ResourceUtils(null).getResourceAsString(CONFIG_URL); return (List) Yamls.getAt(input, ImmutableList.of("defaultTypes")); }
protected String getScript(ImplementationArtifact artifact) { // Trying to get the CSAR file based on the artifact reference. If it fails, then we try to get the // content of the script from any resources String artifactRef = artifact.getArtifactRef(); Optional<Path> csarPath = getCsarPath(artifact.getArchiveName(), artifact.getArchiveVersion()); if(!csarPath.isPresent()) { return new ResourceUtils(this).getResourceAsString(artifactRef); } return new ResourceUtils(this).getResourceAsString(csarPath.get().getParent().toString() + expandedFolder + artifactRef); }
public static CompoundTransformer loadTransformer(ResourceUtils resources, String transformationsFileUrl) { if (Strings.isBlank(transformationsFileUrl)) { return CompoundTransformer.NOOP; } else { String contents = resources.getResourceAsString(transformationsFileUrl); return CompoundTransformerLoader.load(contents); } }
@Test public void testDataUrl() throws Exception { assertEquals(utils.getResourceAsString("data:,hello"), "hello"); assertEquals(utils.getResourceAsString("data:,hello%20world"), "hello world"); // above is correct. below are not valid ... but we accept them anyway assertEquals(utils.getResourceAsString("data:hello"), "hello"); assertEquals(utils.getResourceAsString("data://hello"), "hello"); assertEquals(utils.getResourceAsString("data:hello world"), "hello world"); assertEquals(utils.getResourceAsString(Urls.asDataUrlBase64("hello world")), "hello world"); String longString = Identifiers.makeRandomId(256); for (int a=32; a<128; a++) longString += (char)a; assertEquals(utils.getResourceAsString(Urls.asDataUrlBase64(longString)), longString); }
private void populateInitialFromUri(BasicBrooklynCatalog catalog, String catalogUrl) { log.debug("Loading initial catalog from {}", catalogUrl); try { String contents = new ResourceUtils(this).getResourceAsString(catalogUrl); catalog.reset(MutableList.<CatalogItem<?,?>>of()); Object result = catalog.addItems(contents); log.debug("Loaded initial catalog from {}: {}", catalogUrl, result); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.warn("Error importing catalog from " + catalogUrl + ": " + e, e); } }
@Override public void run() { String fileContents = new ResourceUtils(this).getResourceAsString(filePath); Assert.assertTrue(fileContents.contains("stop")); } });
protected Builder xsltTransformerRecursiveCopyWithExtraRules(String ...rules) { String xsltTemplate = ResourceUtils.create(this).getResourceAsString("classpath://org/apache/brooklyn/core/mgmt/rebind/transformer/recursiveCopyWithExtraRules.xslt"); String xslt = TemplateProcessor.processTemplateContents(xsltTemplate, ImmutableMap.of("extra_rules", Strings.join(rules, "\n"))); return xsltTransformer(xslt); }
private String prepareServiceConfigurationXmlContent(String config, Map<String, String> configMap) { return TemplateProcessor.processTemplateContents( ResourceUtils.create().getResourceAsString(config().get(CustomService.SERVICE_CONFIG_TEMPLATE_URL)), this, ImmutableMap.<String,String>of("configKey", config)); }
protected Builder xsltTransformerFromXsltFreemarkerTemplateUrl(String templateUrl, Map<String,String> vars) { String xsltTemplate = ResourceUtils.create(this).getResourceAsString(templateUrl); String xslt = TemplateProcessor.processTemplateContents(xsltTemplate, vars); return xsltTransformer(xslt); } protected Builder xsltTransformerRecursiveCopyWithExtraRules(String ...rules) {
@Override public void run() { String fileContents = new ResourceUtils(this).getResourceAsString(filePath); Assert.assertTrue(fileContents.contains("checkRunning")); } });
protected Reader loadYaml(String url, String location) { String yaml = "location: "+location+"\n"+ new ResourceUtils(this).getResourceAsString(url); return new StringReader(yaml); }
@Test public void testScriptGeneration() { Date generationTime = new Date(0); String generatedScript = GeoscalingScriptGenerator.generateScriptString(generationTime, HOSTS); assertTrue(generatedScript.contains("1.2.3")); String expectedScript = ResourceUtils.create(this).getResourceAsString("org/apache/brooklyn/entity/dns/geoscaling/expectedScript.php"); assertEqualsNormalizedEol(generatedScript, expectedScript); //also make sure leading slash is allowed String expectedScript2 = ResourceUtils.create(this).getResourceAsString("org/apache/brooklyn/entity/dns/geoscaling/expectedScript.php"); assertEqualsNormalizedEol(generatedScript, expectedScript2); }
@Test public void testEncodeDecodeRsaPublicKey() throws Exception { String data = ResourceUtils.create(this).getResourceAsString("classpath://brooklyn/util/crypto/sample_rsa.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); }
@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); }
@Override public String call(ConfigBag parameters) { return ResourceUtils.create(entity()).getResourceAsString( parameters.get(FILENAME) ); } }
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())); }