/** * @return null if var is null or empty string, otherwise return var */ public static String emptyToNull(String var) { if (isNonEmpty(var)) { return var; } else { return null; } }
@Override public String toString() { // give display name plus id, or job and tags plus id; some jobs have been extended to include nice tostrings return "Task["+ (Strings.isNonEmpty(displayName) ? displayName : (job + (tags!=null && !tags.isEmpty() ? ";"+tags : "")) ) + "]@"+getId(); }
/** creates a private temp file which will be deleted on exit; * either prefix or ext may be null; * if ext is non-empty and not > 4 chars and not starting with a ., then a dot will be inserted; * if either name part is too long it will be shortened to prevent filesystem errors */ public static File newTempFile(String prefix, String ext) { String sanitizedPrefix = (Strings.isNonEmpty(prefix) ? Strings.makeValidFilename(prefix) + "-" : ""); if (sanitizedPrefix.length()>101) sanitizedPrefix = sanitizedPrefix.substring(0, 100)+"--"; String extWithPrecedingSeparator = (Strings.isNonEmpty(ext) ? ext.startsWith(".") || ext.length()>4 ? ext : "."+ext : ""); if (extWithPrecedingSeparator.length()>13) sanitizedPrefix = sanitizedPrefix.substring(0, 12)+"--"; try { File tempFile = File.createTempFile(sanitizedPrefix, extWithPrecedingSeparator, new File(tmp())); tempFile.deleteOnExit(); return tempFile; } catch (IOException e) { throw Exceptions.propagate(e); } }
/** as {@link #ReflectionScanner(Iterable, String, Predicate, ClassLoader...)} using the prefix as the base for the filter */ public ReflectionScanner( final Iterable<URL> urlsToScan, final String optionalPrefix, final ClassLoader ...classLoaders) { this(urlsToScan, optionalPrefix, Strings.isNonEmpty(optionalPrefix) ? new FilterBuilder.Include(FilterBuilder.prefix(optionalPrefix)) : null, classLoaders); }
public String getFilename() { return artifactId+"-"+ (Strings.isNonEmpty(getCustomFileNameAfterArtifactMarker()) ? getCustomFileNameAfterArtifactMarker()+"-" : "")+ version+ (Strings.isNonEmpty(getClassifierFileNameMarker()) ? "-"+getClassifierFileNameMarker() : "")+ (Strings.isNonEmpty(getExtension()) ? "."+getExtension() : ""); }
public static RangeSet<Integer> portRulesToRanges(Collection<String> portRules) { RangeSet<Integer> result = TreeRangeSet.create(); for (String portRule : portRules) { if (portRule.contains("-")) { String[] fromTo = portRule.split("-"); checkArgument(fromTo.length == 2, "Invalid port range '%s'", portRule); checkArgument(Strings.countOccurrences(portRule, '-') == 1, "Invalid port range '%s'", portRule); checkArgument(Strings.isNonEmpty(fromTo[0]), "Invalid port range '%s'", portRule); checkArgument(Strings.isNonEmpty(fromTo[1]), "Invalid port range '%s'", portRule); result.add(closedRange(fromTo[0], fromTo[1])); } else { result.add(closedRange(portRule, portRule)); } } return result; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T,SpecT> List<CatalogItemSummary> getCatalogItemSummariesMatchingRegexFragment( Predicate<RegisteredType> type, String regex, String fragment, boolean allVersions) { List<Predicate<RegisteredType>> filters = new ArrayList(); filters.add(type); if (Strings.isNonEmpty(regex)) filters.add(RegisteredTypePredicates.stringRepresentationMatches(StringPredicates.containsRegex(regex))); if (Strings.isNonEmpty(fragment)) filters.add(RegisteredTypePredicates.stringRepresentationMatches(StringPredicates.containsLiteralIgnoreCase(fragment))); if (!allVersions) filters.add(RegisteredTypePredicates.isBestVersion(mgmt())); filters.add(RegisteredTypePredicates.entitledToSee(mgmt())); ImmutableList<RegisteredType> sortedItems = FluentIterable.from(brooklyn().getTypeRegistry().getMatching(Predicates.and(filters))) .toSortedList(RegisteredTypes.RegisteredTypeNameThenBestFirstComparator.INSTANCE); return Lists.transform(sortedItems, toCatalogItemSummary(ui)); }
(e instanceof IllegalArgumentException && Strings.isNonEmpty(e.getMessage())) ? e.getMessage() : ""+e) );
return isTrue(expected) == (null == actual || Strings.isEmpty(actual.toString())); case NOT_EMPTY: return isTrue(expected) == ((null != actual && Strings.isNonEmpty(actual.toString()))); case MATCHES: Pattern matchesPattern = Pattern.compile(expected.toString());
@Override public void apply(EntityLocal entity) { WindowsPerformanceCounterFeed.Builder builder = WindowsPerformanceCounterFeed.builder() .period(period) .entity(entity); for (Map<String, String> sensorConfig : sensors) { String name = sensorConfig.get("name"); String sensorType = sensorConfig.get("sensorType"); Class<?> clazz; try { clazz = Strings.isNonEmpty(sensorType) ? ((EntityInternal)entity).getManagementContext().getCatalog().getRootClassLoader().loadClass(sensorType) : String.class; } catch (ClassNotFoundException e) { throw new IllegalStateException("Could not load type "+sensorType+" for sensor "+name, e); } builder.addSensor(sensorConfig.get("counter"), Sensors.newSensor(clazz, name, sensorConfig.get("description"))); } entity.addFeed(builder.build()); } }
else if (classLoaders.length>0 && classLoaders[0]!=null) setUrls( ClasspathHelper.forPackage(Strings.isNonEmpty(optionalPrefix) ? optionalPrefix : "", asClassLoaderVarArgs(classLoaders[0])));
@Override public void prepare() { // Check if we should create a download resolver? String downloadUrl = getEntity().config().get(SoftwareProcess.DOWNLOAD_URL); if (Strings.isNonEmpty(downloadUrl)) { resolver = Entities.newDownloader(this); String formatString = getArchiveNameFormat(); if (Strings.isNonEmpty(formatString)) { setExpandedInstallDir(Os.mergePaths(getInstallDir(), resolver.getUnpackedDirectoryName(String.format(formatString, getVersion())))); } else { setExpandedInstallDir(getInstallDir()); } } }
public void isBlankOrEmpty() { assertTrue(Strings.isEmpty(null)); assertTrue(Strings.isEmpty("")); assertFalse(Strings.isEmpty(" \t ")); assertFalse(Strings.isEmpty("abc")); assertFalse(Strings.isEmpty(" abc ")); assertFalse(Strings.isNonEmpty(null)); assertFalse(Strings.isNonEmpty("")); assertTrue(Strings.isNonEmpty(" \t ")); assertTrue(Strings.isNonEmpty("abc")); assertTrue(Strings.isNonEmpty(" abc ")); assertTrue(Strings.isBlank(null)); assertTrue(Strings.isBlank("")); assertTrue(Strings.isBlank(" \t ")); assertFalse(Strings.isBlank("abc")); assertFalse(Strings.isBlank(" abc ")); assertFalse(Strings.isNonBlank(null)); assertFalse(Strings.isNonBlank("")); assertFalse(Strings.isNonBlank(" \t ")); assertTrue(Strings.isNonBlank("abc")); assertTrue(Strings.isNonBlank(" abc ")); }
@Override public void init() { super.init(); registerLocationResolver(); // Check for override of the Marathon URL on the cluster entity String marathonUrl = getMesosCluster().config().get(MARATHON_URL); if (Strings.isNonEmpty(marathonUrl)) { sensors().set(MesosFramework.FRAMEWORK_URL, marathonUrl); sensors().set(Attributes.MAIN_URI, URI.create(marathonUrl)); } }
throw new IllegalArgumentException("Keystore password is required and non-empty if keystore is specified."); sslContextFactory.setKeyStorePassword(keystorePassword); if (Strings.isNonEmpty(keystoreCertAlias)) sslContextFactory.setCertAlias(keystoreCertAlias); } else {
@Test(groups = "Live", dataProvider = "virtualMachineData") protected void testOperatingSystemProvider(String imageId, String provider, String region, String description) throws Exception { log.info("Testing CouchDB on {}{} using {} ({})", new Object[] { provider, Strings.isNonEmpty(region) ? ":" + region : "", description, imageId }); Map<String, String> properties = MutableMap.of("imageId", imageId); Location testLocation = app.getManagementContext().getLocationRegistry() .getLocationManaged(provider + (Strings.isNonEmpty(region) ? ":" + region : ""), properties); CouchDBNode couchdb = app.createAndManageChild(EntitySpec.create(CouchDBNode.class) .configure("httpPort", "12345+") .configure("clusterName", "TestCluster")); app.start(ImmutableList.of(testLocation)); EntityAsserts.assertAttributeEqualsEventually(couchdb, Startable.SERVICE_UP, true); JcouchdbSupport jcouchdb = new JcouchdbSupport(couchdb); jcouchdb.jcouchdbTest(); } }
@Test(groups = "Live", dataProvider = "virtualMachineData") protected void testOperatingSystemProvider(String imageId, String provider, String region, String description) throws Exception { log.info("Testing Cassandra on {}{} using {} ({})", new Object[] { provider, Strings.isNonEmpty(region) ? ":" + region : "", description, imageId }); Map<String, String> properties = MutableMap.of("imageId", imageId); testLocation = app.getManagementContext().getLocationRegistry() .getLocationManaged(provider + (Strings.isNonEmpty(region) ? ":" + region : ""), properties); cassandra = app.createAndManageChild(EntitySpec.create(CassandraNode.class) .configure("thriftPort", "9876+") .configure("clusterName", "TestCluster")); app.start(ImmutableList.of(testLocation)); EntityAsserts.assertAttributeEqualsEventually(cassandra, CassandraNode.SERVICE_UP, true); AstyanaxSample astyanax = new AstyanaxSample(cassandra); astyanax.astyanaxTest(); } }
@Override public void customize() { newScript(CUSTOMIZING) .body.append( // create app-specific dirs "mkdir logs contexts webapps", // link to the binary directories; silly that we have to do this but jetty has only one notion of "jetty.home" // (jetty.run is used only for writing the pid file, not for looking up webapps or even for logging) format("for x in start.jar bin contrib modules lib extras; do ln -s %s/$x $x ; done", getExpandedInstallDir()), // copy config files across format("for x in etc resources; do cp -r %s/$x $x ; done", getExpandedInstallDir()) ) .execute(); // Copy configuration XML files across String destinationBrooklynConfig = Os.mergePathsUnix(getRunDir(), "etc/jetty-brooklyn.xml"); copyTemplate("classpath://org/apache/brooklyn/entity/webapp/jetty/jetty-brooklyn.xml", destinationBrooklynConfig); String customConfigTemplateUrl = getConfigXmlTemplateUrl(); if (Strings.isNonEmpty(customConfigTemplateUrl)) { String destinationConfigFile = Os.mergePathsUnix(getRunDir(), "etc/jetty-custom.xml"); copyTemplate(customConfigTemplateUrl, destinationConfigFile); } getEntity().deployInitialWars(); }
@Test(groups = "Live", dataProvider = "virtualMachineData") protected void testOperatingSystemProvider(String imageId, String provider, String region, String description) throws Exception { log.info("Testing Solr on {}{} using {} ({})", new Object[] { provider, Strings.isNonEmpty(region) ? ":" + region : "", description, imageId }); Map<String, String> properties = MutableMap.of("imageId", imageId); testLocation = app.getManagementContext().getLocationRegistry() .getLocationManaged(provider + (Strings.isNonEmpty(region) ? ":" + region : ""), properties); solr = app.createAndManageChild(EntitySpec.create(SolrServer.class) .configure(SolrServer.SOLR_CORE_CONFIG, ImmutableMap.of("example", "classpath://solr/example.tgz"))); app.start(ImmutableList.of(testLocation)); EntityAsserts.assertAttributeEqualsEventually(solr, Startable.SERVICE_UP, true); SolrJSupport client = new SolrJSupport(solr, "example"); Iterable<SolrDocument> results = client.getDocuments(); assertTrue(Iterables.isEmpty(results)); client.addDocument(MutableMap.<String, Object>of("id", "1", "description", "first")); client.addDocument(MutableMap.<String, Object>of("id", "2", "description", "second")); client.addDocument(MutableMap.<String, Object>of("id", "3", "description", "third")); client.commit(); results = client.getDocuments(); assertEquals(Iterables.size(results), 3); } }
if (Strings.isNonEmpty(wrapperParent.getDisplayName())) { wrappedChild.displayName(wrapperParent.getDisplayName());