/** * The inverse of {@link #isEmpty(CharSequence)}. * * @param s the String to check * @return true if non empty, false otherwise. * * @see #isEmpty(CharSequence) * @see #isBlank(CharSequence) * @see #isNonBlank(CharSequence) */ public static boolean isNonEmpty(CharSequence s) { return !isEmpty(s); }
/** * The inverse of {@link #isBlank(CharSequence)}. * * @param s the String to check * @return true if non blank, false otherwise. * * @see #isEmpty(CharSequence) * @see #isNonEmpty(CharSequence) * @see #isBlank(CharSequence) */ public static boolean isNonBlank(CharSequence s) { return !isBlank(s); }
/** * Returns a sequence of chained commands that runs until one of them fails (i.e. joined by '&&') * This currently runs as a subshell (so exits are swallowed) but behaviour may be changed imminently. * (Use {@link #chainGroup(Collection)} or {@link #chainSubshell(Collection)} to be clear.) */ public static String chain(Collection<String> commands) { return "( " + Strings.join(commands, " && ") + " )"; }
private String toSimpleName(String name) { String simpleName = name.substring(name.lastIndexOf(".")+1); if (Strings.isBlank(simpleName)) simpleName = name.trim(); return Strings.makeValidFilename(simpleName); }
public String getClassLoaderDir(String resourceInThatDir) { resourceInThatDir = Strings.removeFromStart(resourceInThatDir, "/"); URL url = getLoader().getResource(resourceInThatDir); if (url==null) throw new NoSuchElementException("Resource ("+resourceInThatDir+") not found"); String urls = url.toString(); boolean isJar = urls.startsWith("jar:"); urls = Strings.removeFromStart(urls, "jar:"); if (!urls.startsWith("file:")) throw new IllegalStateException("Resource ("+resourceInThatDir+") not on file system (at "+urls+")"); urls = Strings.removeFromStart(urls, "file:"); urls = Strings.removeFromStart(urls, "//"); int i = urls.indexOf(resourceInThatDir); if (i==-1) throw new IllegalStateException("Resource path ("+resourceInThatDir+") not in url substring ("+urls+")"); urls = urls.substring(0, i); if (isJar) { urls = Strings.removeFromEnd(urls, "/"); if (!urls.endsWith("!")) throw new IllegalStateException("Context class url mismatch, is jar but does not have ! separator ("+urls+")"); urls = Strings.removeFromEnd(urls, "!"); if (!new File(urls).exists()) throw new IllegalStateException("Context class url substring ("+urls+") not found on filesystem"); } return urls; }
/** returns a simplified name of the class, just the simple name if it seems useful, else the full name */ public static String simpleClassName(Class<?> t) { int arrayCount = 0; while (t.isArray()) { arrayCount++; t = t.getComponentType(); } Class<?> ct = componentType(t); String result = ct.getSimpleName(); if (Strings.isBlank(result) || result.length()<=4) { if (ct.isPrimitive()) { // TODO unbox } else { result = ct.getName(); } } return result+Strings.repeat("[]", arrayCount); }
protected Collection<JcloudsLocationCustomizer> getCustomizers(ConfigBag setup) { JcloudsLocationCustomizer customizer = setup.get(JCLOUDS_LOCATION_CUSTOMIZER); Collection<JcloudsLocationCustomizer> customizers = setup.get(JCLOUDS_LOCATION_CUSTOMIZERS); String customizerType = setup.get(JCLOUDS_LOCATION_CUSTOMIZER_TYPE); List<JcloudsLocationCustomizer> result = new ArrayList<JcloudsLocationCustomizer>(); if (customizer != null) result.add(customizer); if (customizers != null) result.addAll(customizers); if (Strings.isNonBlank(customizerType)) { try { Class<?> customizerClazz = getClass().getClassLoader().loadClass(customizerType); JcloudsLocationCustomizer customizerByType = (JcloudsLocationCustomizer) customizerClazz.getConstructor().newInstance(); result.add(customizerByType); } catch (Exception e) { throw new IllegalStateException("Failed to load customizer "+customizerType+" for location "+this); } } return result; }
public String getFilename() { return artifactId+"-"+ (Strings.isNonEmpty(getCustomFileNameAfterArtifactMarker()) ? getCustomFileNameAfterArtifactMarker()+"-" : "")+ version+ (Strings.isNonEmpty(getClassifierFileNameMarker()) ? "-"+getClassifierFileNameMarker() : "")+ (Strings.isNonEmpty(getExtension()) ? "."+getExtension() : ""); }
if (Strings.isBlank(locations)) { if (app != null) { System.err.println("Locations parameter not supplied: assuming localhost"); .webconsole(!noConsole) .shutdownOnExit(!noShutdownOnExit) .locations(Strings.isBlank(locations) ? ImmutableList.<String>of() : ImmutableList.of(locations)); if (noConsoleSecurity) { launcher.installSecurityFilter(false); if (Strings.isNonEmpty(bindAddress)) { InetAddress ip = Networking.getInetAddressWithFixedName(bindAddress); launcher.bindAddress(ip);
@Override protected void preStart() { String reverse = getConfig(REVERSE_LOOKUP_NETWORK); if (Strings.isBlank(reverse)) reverse = getAttribute(ADDRESS); setAttribute(REVERSE_LOOKUP_CIDR, new Cidr(reverse + "/24")); String reverseLookupDomain = Joiner.on('.').join(Iterables.skip(Lists.reverse(Lists.newArrayList(Splitter.on('.').split(reverse))), 1)) + ".in-addr.arpa"; setAttribute(REVERSE_LOOKUP_DOMAIN, reverseLookupDomain); Map<?, ?> flags = MutableMap.builder() .put("name", "Address tracker") .put("sensorsToTrack", ImmutableSet.of(getConfig(HOSTNAME_SENSOR))) .build(); policy = new AbstractMembershipTrackingPolicy(flags) { @Override protected void onEntityChange(Entity member) { added(member); } @Override protected void onEntityAdded(Entity member) { if (Strings.isNonBlank(member.getAttribute(getConfig(HOSTNAME_SENSOR)))) added(member); // Ignore, unless hostname set } @Override protected void onEntityRemoved(Entity member) { removed(member); } }; // For any entities that have already come up for (Entity member : entities.getMembers()) { if (Strings.isNonBlank(member.getAttribute(getConfig(HOSTNAME_SENSOR)))) added(member); // Ignore, unless hostname set } addPolicy(policy); policy.setGroup(entities); }
/** * Returns a unique valid java identifier name based on the input. * * Translated as per {@link #makeValidJavaName(String)} but with {@link String#hashCode()} * appended where necessary to guarantee uniqueness. * * @see #makeValidJavaName(String) */ public static String makeValidUniqueJavaName(String s) { String name = makeValidJavaName(s); if (isEmpty(s) || IS_JAVA_IDENTIFIER_PART.matchesAllOf(s) || CharMatcher.is('\'').matchesNoneOf(s)) { return name; } else { return name + "_" + s.hashCode(); } }
/** creates the command for running knife. * in some cases knife may be added multiple times, * and in that case the parameter here tells which time it is being added, * on a single run. */ protected String buildKnifeCommand(int knifeCommandIndex) { MutableList<String> words = new MutableList<String>(); words.add(knifeExecutable()); words.addAll(initialKnifeParameters()); words.addAll(knifeParameters()); String x = knifeConfigFileOption(); if (Strings.isNonBlank(x)) words.add(knifeConfigFileOption()); return Strings.join(words, " "); }
@Deprecated public static String fixLocation(String locationId) { if (locationId.startsWith("/v1/locations/")) { log.warn("REST API using legacy URI syntax for location: "+locationId); locationId = Strings.removeFromStart(locationId, "/v1/locations/"); } return locationId; }
public LdapSecurityProvider(ManagementContext mgmt) { StringConfigMap properties = mgmt.getConfig(); ldapUrl = properties.getConfig(BrooklynWebConfig.LDAP_URL); Strings.checkNonEmpty(ldapUrl, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_URL); ldapRealm = properties.getConfig(BrooklynWebConfig.LDAP_REALM); Strings.checkNonEmpty(ldapRealm, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_REALM); }
@Override protected void onEntityAdded(Entity member) { if (Strings.isNonBlank(member.getAttribute(getConfig(HOSTNAME_SENSOR)))) added(member); // Ignore, unless hostname set } @Override
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> List<CatalogItemSummary> getCatalogItemSummariesMatchingRegexFragment(Predicate<CatalogItem<T>> type, String regex, String fragment) { List filters = new ArrayList(); filters.add(type); if (Strings.isNonEmpty(regex)) filters.add(CatalogPredicates.xml(StringPredicates.containsRegex(regex))); if (Strings.isNonEmpty(fragment)) filters.add(CatalogPredicates.xml(StringPredicates.containsLiteralCaseInsensitive(fragment))); return ImmutableList.copyOf(Iterables.transform( brooklyn().getCatalog().getCatalogItems(Predicates.and(filters)), TO_CATALOG_ITEM_SUMMARY)); }
@Deprecated public static String fixLocation(String locationId) { if (locationId.startsWith("/v1/locations/")) { log.warn("REST API using legacy URI syntax for location: "+locationId); locationId = Strings.removeFromStart(locationId, "/v1/locations/"); } return locationId; }
public LdapSecurityProvider(ManagementContext mgmt) { StringConfigMap properties = mgmt.getConfig(); ldapUrl = properties.getConfig(BrooklynWebConfig.LDAP_URL); Strings.checkNonEmpty(ldapUrl, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_URL); ldapRealm = properties.getConfig(BrooklynWebConfig.LDAP_REALM); Strings.checkNonEmpty(ldapRealm, "LDAP security provider configuration missing required property "+BrooklynWebConfig.LDAP_REALM); }
/** throws IllegalArgument if string not empty; cf. guava Preconditions.checkXxxx */ public static void checkNonEmpty(CharSequence s, String message) { if (isEmpty(s)) throw new IllegalArgumentException(message); }
public void setName(String name) { if (Strings.isBlank(name)) { throw new IllegalArgumentException("Invalid name "+(name == null ? "null" : "'"+name+"'")+"; name must be non-empty and not just white space"); } this.name = name; this.simpleName = toSimpleName(name); snapshotValid.set(false); }