protected String getFileContents(String file) { return ResourceUtils.create(this).getResourceAsString(file); } }
private static InputStream getUrlStream(String url) { return ResourceUtils.create(Osgis.class).getResourceFromUrl(url); }
@SuppressWarnings("unchecked") public static Iterable<String> getDefaultTypes() { String input = new ResourceUtils(null).getResourceAsString(CONFIG_URL); return (List) Yamls.getAt(input, ImmutableList.of("defaultTypes")); }
/** Fails-fast if value of the given URL is null or unresolveable. */ public static String checkRequiredUrl(Entity entity, String url) { Preconditions.checkNotNull(url, "url"); if (!ResourceUtils.create(entity).doesUrlExist(url)) { throw new IllegalStateException(String.format("URL %s on %s is unavailable", url, entity)); } return url; }
/** takes {@link #getResourceFromUrl(String)} and reads fully, into a string */ public String getResourceAsString(String url) { try { return Streams.readFullyStringAndClose(getResourceFromUrl(url)); } catch (Exception e) { log.debug("ResourceUtils got error reading "+url+(context==null?"":" "+context)+" (rethrowing): "+e); throw Throwables.propagate(e); } }
private void loadDefaultTypes() { // NullPointerException thrown in alien4cloud.security.AuthorizationUtil if admin auth is not granted. grantAdminAuth(); final Iterable<String> defaultTypes = ConfigLoader.getDefaultTypes(); try { for (String resource : defaultTypes) { // TODO: Check for cached location too. loadTypesFromUrl(resource); } } catch (Exception e) { throw Exceptions.propagate("Error loading default types " + Iterables.toString(defaultTypes), e); } final String brooklynTypes = "classpath://brooklyn/types/brooklyn-types.yaml"; LOG.info("Loading types from " + brooklynTypes); uploader.uploadSingleYaml(new ResourceUtils(this).getResourceFromUrl(brooklynTypes), "brooklyn-types"); }
private void installWar(WebAppContext context) { // here we run with the JS GUI, for convenience, if we can find it, else set up an empty dir // TODO pretty sure there is an option to monitor this dir and load changes to static content // NOTE: When running Brooklyn from an IDE (i.e. by launching BrooklynJavascriptGuiLauncher.main()) // you will need to ensure that the working directory is set to the brooklyn-ui repo folder. For IntelliJ, // set the 'Working directory' of the Run/Debug Configuration to $MODULE_DIR$/brooklyn-server/launcher. // For Eclipse, use the default option of ${workspace_loc:brooklyn-launcher}. // If the working directory is not set correctly, Brooklyn will be unable to find the jsgui .war // file and the 'gui not available' message will be shown. context.setWar(this.deployJsgui && findJsguiWebappInSource().isPresent() ? findJsguiWebappInSource().get() : ResourceUtils.create(this).doesUrlExist("classpath://brooklyn.war") ? Os.writeToTempFile(ResourceUtils.create(this).getResourceFromUrl("classpath://brooklyn.war"), "brooklyn", "war").getAbsolutePath() : createTempWebDirWithIndexHtml("Brooklyn REST API <p> (gui not available)")); }
public AbstractSoftwareProcessDriver(EntityLocal entity, Location location) { this.entity = checkNotNull(entity, "entity"); this.location = checkNotNull(location, "location"); this.resource = ResourceUtils.create(entity); }
@Override public String generateConfigFile(NginxDriver driver, NginxController nginx) { // Check template URL exists String templateUrl = driver.getEntity().getConfig(NginxController.SERVER_CONF_TEMPLATE_URL); ResourceUtils.create(this).checkUrlExists(templateUrl); // Check SSL configuration ProxySslConfig ssl = driver.getEntity().getConfig(NginxController.SSL_CONFIG); if (ssl != null && Strings.isEmpty(ssl.getCertificateDestination()) && Strings.isEmpty(ssl.getCertificateSourceUrl())) { throw new IllegalStateException("ProxySslConfig can't have a null certificateDestination and null certificateSourceUrl. One or both need to be set"); } // For mapping by URL Iterable<UrlMapping> mappings = ((NginxController) driver.getEntity()).getUrlMappings(); Multimap<String, UrlMapping> mappingsByDomain = LinkedHashMultimap.create(); for (UrlMapping mapping : mappings) { Collection<String> addrs = mapping.getAttribute(UrlMapping.TARGET_ADDRESSES); if (addrs != null && addrs.size() > 0) { mappingsByDomain.put(mapping.getDomain(), mapping); } } Map<String, Object> substitutions = MutableMap.<String, Object>builder() .putIfNotNull("ssl", ssl) .put("urlMappings", mappings) .put("domainMappings", mappingsByDomain) .build(); // Get template contents and process String contents = ResourceUtils.create(driver.getEntity()).getResourceAsString(templateUrl); return TemplateProcessor.processTemplateContents(contents, driver, substitutions); }
@Test(groups="Integration") public void testUtilsCommon() { ResourceUtils.create(this).checkUrlExists(BrooklynMavenArtifacts.localUrlForJar("brooklyn-utils-common")); }
protected String loadYaml(String yamlFileName, String ...extraLines) throws Exception { ResourceUtils ru = new ResourceUtils(this); if (!ru.doesUrlExist(yamlFileName)) { if (ru.doesUrlExist(Urls.mergePaths(getClass().getPackage().getName().replace('.', '/'), yamlFileName))) { // look in package-specific folder if not found at root yamlFileName = Urls.mergePaths(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(); }
@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")); }
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()) { Entities.dumpInfo(entity); Assert.fail("Expected NOT to find 'messages.txt'"); } }
@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); } }
if (resources.doesUrlExist(Urls.mergePaths(path, MANIFEST_PATH))) { InputStream min = resources.getResourceFromUrl(Urls.mergePaths(path, MANIFEST_PATH)); zout = new JarOutputStream(new FileOutputStream(f), new Manifest(min)); addUrlItemRecursively(zout, path, path, Predicates.not(Predicates.equalTo(MANIFEST_PATH)));
@Override public boolean apply(@Nullable String resource, @Nullable BrooklynObject context) { return !Strings.isBlank(resource) && new ResourceUtils(context).doesUrlExist(resource); }
/** * Creates a {@link ResourceUtils} object with the given context. * <p> * Uses the {@link ClassLoader} of the given {@code contextObject} for class * loading and the {@code contextMessage} string for error messages. * * @see ResourceUtils#create(ClassLoader, Object, String) * @see ResourceUtils#create(Object) */ public static final ResourceUtils create(Object contextObject, String contextMessage) { return new ResourceUtils(contextObject, contextMessage); }
@Override public void init() { super.init(); new ResourceUtils(this).checkUrlExists(Storm.STORM_CONFIG_TEMPLATE_URL.getDefaultValue());
/** returns the first available URL */ public Optional<String> firstAvailableUrl(String ...urls) { for (String url: urls) { if (doesUrlExist(url)) return Optional.of(url); } return Optional.absent(); }
/** @see #checkUrlExists(String, String) */ public String checkUrlExists(String url) { return checkUrlExists(url, null); }