/** 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(); }
/** 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; }
/** Fails-fast if value of the given key is null or unresolveable. */ public static String getRequiredUrlConfig(Entity entity, ConfigKey<String> urlKey) { String url = entity.getConfig(urlKey); Preconditions.checkNotNull(url, "Key %s on %s should not be null", urlKey, entity); if (!ResourceUtils.create(entity).doesUrlExist(url)) { throw new IllegalStateException(String.format("Key %s on %s contains unavailable URL %s", urlKey, entity, url)); } return url; }
@Override public boolean apply(@Nullable String resource, @Nullable BrooklynObject context) { return !Strings.isBlank(resource) && new ResourceUtils(context).doesUrlExist(resource); }
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); } }
/** 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; }
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));
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(); }
@Test(groups="Integration") // runs without internet but doesn't assert what it should, and can take a long time, so integration public void testBadExampleWar() { String url = BrooklynMavenArtifacts.localUrl("example", "brooklyn-example-GOODBYE-world-sql-webapp", "war"); Assert.assertFalse(ResourceUtils.create(this).doesUrlExist(url), "should not exist: "+url); }
if (new ResourceUtils(this).doesUrlExist(catalogUrl)) { populateInitialFromUri(catalog, catalogUrl); return;
} else { jmxAgentMode = JmxAgentModes.JMXMP_AND_RMI; if (!ResourceUtils.create(this).doesUrlExist(getJmxAgentJarUrl())) {
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()); }
public void testJmxrmiJarExistence() { app = TestApplication.Factory.newManagedInstanceForTests(); JmxSupport support = new JmxSupport(app, null); app.config().set(JmxSupport.JMX_AGENT_MODE, JmxAgentModes.JMX_RMI_CUSTOM_AGENT); Assert.assertEquals(support.getJmxAgentJarMavenArtifact().getArtifactId(), "brooklyn-jmxrmi-agent"); Assert.assertTrue(ResourceUtils.create(this).doesUrlExist(support.getJmxAgentJarUrl()), support.getJmxAgentJarUrl()); }
/** not much fun without a REST server. * but TODO we should make it so a different REST endpoint could be configured. * or better, use node js launchers in that project (likely to come with a new ui.) * <p> * relative path to webapp assumes brooklyn-server has been checked out at the same level as brooklyn-ui; * see {@link BrooklynRestApiLauncher#findJsguiWebappInSource()} */ public static Server startJavascriptWithoutRest() throws Exception { WebAppContext context = new WebAppContext( BrooklynRestApiLauncher.findJsguiWebappInSource().isPresent() ? BrooklynRestApiLauncher.findJsguiWebappInSource().get() : ResourceUtils.create(BrooklynJavascriptGuiLauncher.class).doesUrlExist("classpath://brooklyn.war") ? Os.writeToTempFile(ResourceUtils.create(BrooklynJavascriptGuiLauncher.class).getResourceFromUrl("classpath://brooklyn.war"), "brooklyn", "war").getAbsolutePath() : throwingReturning("could not find jsgui war or source", "missing-brooklyn.war"), "/"); context.setSecurityHandler(new NopSecurityHandler()); Server server = new Server(new InetSocketAddress(Networking.LOOPBACK, Networking.nextAvailablePort(FAVOURITE_PORT))); BrooklynRestApiLauncher.initJaasLoginService(server); server.setHandler(context); server.start(); log.info("JS GUI server started (no REST) at http://localhost:"+((NetworkConnector)server.getConnectors()[0]).getLocalPort()+"/"); return server; }
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)")); }