/** as {@link #walk(String...))} and {@link #walk(Iterable)} */ public static Function<Maybe<JsonElement>, Maybe<JsonElement>> walkM(final String... elements) { return walkM(Arrays.asList(elements)); }
@Override public void connectSensors() { super.connectSensors(); connectServiceUpIsRunning(); HostAndPort hostAndPort = BrooklynAccessUtils.getBrooklynAccessibleAddress(this, sensors().get(DOCKER_REGISTRY_PORT)); sensors().set(Attributes.MAIN_URI, URI.create("https://" + hostAndPort + "/v2")); httpFeed = HttpFeed.builder() .entity(this) .period(Duration.seconds(3)) .baseUri(getAttribute(Attributes.MAIN_URI)) .poll(new HttpPollConfig<Boolean>(Attributes.SERVICE_UP) .onSuccess(Functions.constant(true)) .onFailureOrException(Functions.constant(false))) .poll(new HttpPollConfig<List<String>>(DOCKER_REGISTRY_CATALOG) .suburl("/_catalog") .onSuccess(Functionals.chain( HttpValueFunctions.jsonContents(), JsonFunctions.walk("repositories"), JsonFunctions.forEach(JsonFunctions.cast(String.class)))) .onFailureOrException(Functions.constant(Collections.<String>emptyList()))) .build(); }
@Override public T apply(Maybe<JsonElement> input) { if (input.isAbsent()) return defaultValue; return cast(expected).apply(input.get()); } };
@Override public void connectSensors() { super.connectSensors(); HttpFeed.Builder httpFeedBuilder = HttpFeed.builder() .entity(this) .period(2000, TimeUnit.MILLISECONDS) .baseUri(sensors().get(FRAMEWORK_URL)) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MARATHON_APPLICATIONS) .suppressDuplicates(true) .suburl("/v2/apps/") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("apps"), JsonFunctions.forEach(JsonFunctions.<String>getPath("id")))) .onFailureOrException(Functions.constant(Arrays.asList(new String[0])))) .poll(HttpPollConfig.forSensor(MARATHON_VERSION) .suppressDuplicates(true) .suburl("/v2/info/") .onSuccess(HttpValueFunctions.jsonContents("version", String.class)) .onFailureOrException(Functions.constant(""))) .poll(HttpPollConfig.forSensor(SERVICE_UP) .suppressDuplicates(true) .suburl("/ping") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(Boolean.FALSE))); httpFeed = httpFeedBuilder.build(); }
public boolean apply(JsonElement input) { return input.getAsJsonObject().get("id").getAsString().equals(id); }}), JsonFunctions.walk("slaves")), JsonFunctions.walkM("resources", "mem"), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll(HttpPollConfig.forSensor(CPU_AVAILABLE) public boolean apply(JsonElement input) { return input.getAsJsonObject().get("id").getAsString().equals(id); }}), JsonFunctions.walk("slaves")), JsonFunctions.walkM("resources", "cpus"), JsonFunctions.castM(Double.class))) .onFailureOrException(Functions.constant(-1d))) .poll(HttpPollConfig.forSensor(DISK_AVAILABLE) public boolean apply(JsonElement input) { return input.getAsJsonObject().get("id").getAsString().equals(id); }}), JsonFunctions.walk("slaves")), JsonFunctions.walkM("resources", "disk"), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll(HttpPollConfig.forSensor(MEMORY_USED) public boolean apply(JsonElement input) { return input.getAsJsonObject().get("id").getAsString().equals(id); }}), JsonFunctions.walk("slaves")), JsonFunctions.walkM("used_resources", "mem"), JsonFunctions.castM(Long.class)))
/** as {@link #walkM(Iterable)} taking a series of strings (dot separators not respected here) */ public static Function<JsonElement, JsonElement> walk(final String... elements) { return walk(Arrays.asList(elements)); }
@Override public String stopApplication(String id) { Optional<String> result = MesosUtils.httpDelete(this, Urls.mergePaths("v2/apps", id)); if (!result.isPresent()) { throw new IllegalStateException("Failed to stop Marathon task"); } else { LOG.debug("Success deleting Marathon task"); JsonElement json = JsonFunctions.asJson().apply(result.get()); String deployment = json.getAsJsonObject().get("deploymentId").getAsString(); return deployment; } }
@Test public void testGetPathWithNullReturnsNull(){ Integer obj = (Integer) JsonFunctions.getPath("$.europe.uk.edinburgh.population").apply(null); Assert.assertNull(obj); }
/** as {@link #walk(String...))} but if any element is not found it simply returns null */ public static Function<JsonElement, JsonElement> walkN(final String... elements) { return walkN(Arrays.asList(elements)); }
@Test(expectedExceptions=IllegalStateException.class) public void testCastMWhenAbsent() { // Maybe<JsonElement> m = JsonFunctions.walkM("europe", "spain", "barcelona").apply( Maybe.of( europeMap()) ); Maybe<JsonElement> m = Maybe.absent(); JsonFunctions.castM(String.class).apply(m); }
@Override public T apply(Maybe<JsonElement> input) { if (input.isAbsent()) return defaultValue; return cast(expected).apply(input.get()); } }
/** as {@link #walkM(Iterable)} taking a single string consisting of a dot separated path */ public static Function<JsonElement, JsonElement> walk(String elementOrDotSeparatedElements) { return walk( Splitter.on('.').split(elementOrDotSeparatedElements) ); }