List of usage examples for org.apache.commons.lang3 StringUtils splitByWholeSeparator
public static String[] splitByWholeSeparator(final String str, final String separator)
Splits the provided text into an array, separator string specified.
The separator(s) will not be included in the returned String array.
From source file:com.norconex.commons.lang.map.Properties.java
/** * Reads a property list (key and element pairs) from the input * character stream in a simple line-oriented format. * If a key was stored with multiple values using a delimiter, * this method will split these values appropriately provided the * supplied delimiter is the same. If the key value was stored as a * single value, then this method behavior is the * exact same as the //from w ww . j a v a 2s. c o m * {@link Properties#load(Reader)} method. * @param reader the input character stream. * @param delimiter delimiter string to used to parse a multi value * key. * @throws IOException i/o problem * @see Properties#load(Reader) */ public synchronized void load(Reader reader, String delimiter) throws IOException { java.util.Properties p = new java.util.Properties(); p.load(reader); for (String key : p.stringPropertyNames()) { List<String> values = new ArrayList<String>(); String value = p.getProperty(key); if (value != null) { values.addAll(Arrays.asList(StringUtils.splitByWholeSeparator(value, delimiter))); } put(key, values); } p = null; }
From source file:com.streamsets.pipeline.lib.jdbc.multithread.util.OffsetQueryUtil.java
/** * This is the inverse of {@link #getSourceKeyOffsetsRepresentation(Map)}. * * @param offsets the String representation of source key offsets * @return the {@link Map} of the offsets reconstructed from the supplied representation *///from w w w . j a v a 2 s . c o m public static Map<String, String> getOffsetsFromSourceKeyRepresentation(String offsets) { final Map<String, String> offsetMap = new HashMap<>(); if (StringUtils.isNotBlank(offsets)) { for (String col : StringUtils.splitByWholeSeparator(offsets, OFFSET_KEY_COLUMN_SEPARATOR)) { final String[] parts = StringUtils.splitByWholeSeparator(col, OFFSET_KEY_COLUMN_NAME_VALUE_SEPARATOR, 2); if (parts.length != 2) { throw new IllegalArgumentException(String.format( "Invalid column offset of \"%s\" seen. Expected colName%svalue. Full offsets representation: %s", col, OFFSET_KEY_COLUMN_NAME_VALUE_SEPARATOR, offsets)); } offsetMap.put(parts[0], parts[1]); } } return offsetMap; }
From source file:com.datatorrent.stram.client.StramAppLauncher.java
/** * Submit application to the cluster and return the app id. * Sets the context class loader for application dependencies. * * @param appConfig// ww w. j a v a2 s. c om * @return ApplicationId * @throws Exception */ public ApplicationId launchApp(AppFactory appConfig) throws Exception { loadDependencies(); Configuration conf = propertiesBuilder.conf; conf.setEnum(StreamingApplication.ENVIRONMENT, StreamingApplication.Environment.CLUSTER); LogicalPlan dag = appConfig.createApp(propertiesBuilder); long hdfsTokenMaxLifeTime = conf.getLong(StramClientUtils.DT_HDFS_TOKEN_MAX_LIFE_TIME, conf.getLong( StramClientUtils.HDFS_TOKEN_MAX_LIFE_TIME, StramClientUtils.DELEGATION_TOKEN_MAX_LIFETIME_DEFAULT)); dag.setAttribute(LogicalPlan.HDFS_TOKEN_LIFE_TIME, hdfsTokenMaxLifeTime); long rmTokenMaxLifeTime = conf.getLong(StramClientUtils.DT_RM_TOKEN_MAX_LIFE_TIME, conf.getLong(YarnConfiguration.DELEGATION_TOKEN_MAX_LIFETIME_KEY, YarnConfiguration.DELEGATION_TOKEN_MAX_LIFETIME_DEFAULT)); dag.setAttribute(LogicalPlan.RM_TOKEN_LIFE_TIME, rmTokenMaxLifeTime); if (conf.get(StramClientUtils.KEY_TAB_FILE) != null) { dag.setAttribute(LogicalPlan.KEY_TAB_FILE, conf.get(StramClientUtils.KEY_TAB_FILE)); } else if (conf.get(StramUserLogin.DT_AUTH_KEYTAB) != null) { Path localKeyTabPath = new Path(conf.get(StramUserLogin.DT_AUTH_KEYTAB)); FileSystem fs = StramClientUtils.newFileSystemInstance(conf); try { Path destPath = new Path(StramClientUtils.getDTDFSRootDir(fs, conf), localKeyTabPath.getName()); if (!fs.exists(destPath)) { fs.copyFromLocalFile(false, false, localKeyTabPath, destPath); } dag.setAttribute(LogicalPlan.KEY_TAB_FILE, destPath.toString()); } finally { fs.close(); } } String tokenRefreshFactor = conf.get(StramClientUtils.TOKEN_ANTICIPATORY_REFRESH_FACTOR); if (tokenRefreshFactor != null && tokenRefreshFactor.trim().length() > 0) { dag.setAttribute(LogicalPlan.TOKEN_REFRESH_ANTICIPATORY_FACTOR, Double.parseDouble(tokenRefreshFactor)); } StramClient client = new StramClient(conf, dag); try { client.start(); LinkedHashSet<String> libjars = Sets.newLinkedHashSet(); String libjarsCsv = conf.get(LIBJARS_CONF_KEY_NAME); if (libjarsCsv != null) { String[] jars = StringUtils.splitByWholeSeparator(libjarsCsv, StramClient.LIB_JARS_SEP); libjars.addAll(Arrays.asList(jars)); } if (deployJars != null) { for (File deployJar : deployJars) { libjars.add(deployJar.getAbsolutePath()); } } client.setResources(libjars); client.setFiles(conf.get(FILES_CONF_KEY_NAME)); client.setArchives(conf.get(ARCHIVES_CONF_KEY_NAME)); client.setOriginalAppId(conf.get(ORIGINAL_APP_ID)); client.setQueueName(conf.get(QUEUE_NAME)); client.startApplication(); return client.getApplicationReport().getApplicationId(); } finally { client.stop(); } }
From source file:com.streamsets.pipeline.lib.jdbc.multithread.TableContextUtil.java
public static Timestamp getTimestampForOffsetValue(String offsetValue) { final String[] parts = StringUtils.splitByWholeSeparator(offsetValue, OFFSET_VALUE_NANO_SEPARATOR); Utils.checkState(parts.length <= 2, String.format(/*w w w .j av a2 s .c om*/ "offsetValue of %s invalid; should contain at most one occurence of nanos separator %s", offsetValue, OFFSET_VALUE_NANO_SEPARATOR)); final long millis = Long.parseLong(parts[0]); Timestamp timestamp = new Timestamp(millis); if (parts.length == 2) { final String nanosStr = parts[1]; if (StringUtils.isNotBlank(nanosStr) && StringUtils.isNumeric(nanosStr)) { final int nanos = Integer.parseInt(nanosStr) % JdbcUtil.NANOS_TO_MILLIS_ADJUSTMENT; // in a Timestamp, nanos also includes the millisecond portion, so we need to incorporate that final long nanosFromMillis = millis % 1000; timestamp.setNanos(((int) nanosFromMillis * JdbcUtil.NANOS_TO_MILLIS_ADJUSTMENT) + nanos); } } return timestamp; }
From source file:com.norconex.commons.lang.map.Properties.java
/** * Loads all of the properties represented by the XML document on the * specified input stream into this instance. * If a key was stored with multiple values using a delimiter, * this method will split these values appropriately provided the * supplied delimiter is the same. If the key value was stored as a * single value, then this method behavior is the * exact same as the /*from ww w .j a va 2 s. com*/ * {@link Properties#loadFromXML(InputStream)} method. * @param in in the input stream from which to read the XML document. * @param delimiter delimiter string to used to parse a multi value * key. * @throws IOException i/o problem */ public synchronized void loadFromXML(InputStream in, String delimiter) throws IOException { java.util.Properties p = new java.util.Properties(); p.loadFromXML(in); List<String> values = new ArrayList<String>(); for (String key : p.stringPropertyNames()) { String value = p.getProperty(key); if (value != null) { values.addAll(Arrays.asList(StringUtils.splitByWholeSeparator(value, delimiter))); } put(key, values); } p = null; }
From source file:com.netflix.genie.web.jpa.services.JpaJobPersistenceServiceImpl.java
private JobEntity toEntity(final String id, final com.netflix.genie.common.dto.JobRequest jobRequest, final com.netflix.genie.common.dto.JobMetadata jobMetadata, final Job job, final JobExecution jobExecution) { final JobEntity jobEntity = new JobEntity(); // Fields from the original Job Request jobEntity.setUniqueId(id);//from ww w . j a v a 2 s . c o m jobEntity.setName(jobRequest.getName()); jobEntity.setUser(jobRequest.getUser()); jobEntity.setVersion(jobRequest.getVersion()); jobEntity.setStatus(JobStatus.INIT); jobRequest.getDescription().ifPresent(jobEntity::setDescription); jobRequest.getMetadata() .ifPresent(metadata -> EntityDtoConverters.setJsonField(metadata, jobEntity::setMetadata)); JpaServiceUtils.setEntityMetadata(GenieObjectMapper.getMapper(), jobRequest, jobEntity); jobRequest.getCommandArgs().ifPresent(commandArgs -> jobEntity.setCommandArgs( Lists.newArrayList(StringUtils.splitByWholeSeparator(commandArgs, StringUtils.SPACE)))); jobRequest.getGroup().ifPresent(jobEntity::setGenieUserGroup); final FileEntity setupFile = jobRequest.getSetupFile().isPresent() ? this.createAndGetFileEntity(jobRequest.getSetupFile().get()) : null; if (setupFile != null) { jobEntity.setSetupFile(setupFile); } final List<CriterionEntity> clusterCriteria = Lists .newArrayListWithExpectedSize(jobRequest.getClusterCriterias().size()); for (final ClusterCriteria clusterCriterion : jobRequest.getClusterCriterias()) { clusterCriteria.add(new CriterionEntity(null, null, null, null, this.createAndGetTagEntities(clusterCriterion.getTags()))); } jobEntity.setClusterCriteria(clusterCriteria); jobEntity.setCommandCriterion(new CriterionEntity(null, null, null, null, this.createAndGetTagEntities(jobRequest.getCommandCriteria()))); jobEntity.setConfigs(this.createAndGetFileEntities(jobRequest.getConfigs())); jobEntity.setDependencies(this.createAndGetFileEntities(jobRequest.getDependencies())); jobEntity.setArchivingDisabled(jobRequest.isDisableLogArchival()); jobRequest.getEmail().ifPresent(jobEntity::setEmail); if (!jobRequest.getTags().isEmpty()) { jobEntity.setTags(this.createAndGetTagEntities(jobRequest.getTags())); } jobRequest.getCpu().ifPresent(jobEntity::setRequestedCpu); jobRequest.getMemory().ifPresent(jobEntity::setRequestedMemory); if (!jobRequest.getApplications().isEmpty()) { jobEntity.setRequestedApplications(jobRequest.getApplications()); } jobRequest.getTimeout().ifPresent(jobEntity::setRequestedTimeout); jobRequest.getGrouping().ifPresent(jobEntity::setGrouping); jobRequest.getGroupingInstance().ifPresent(jobEntity::setGroupingInstance); // Fields collected as metadata jobMetadata.getClientHost().ifPresent(jobEntity::setRequestApiClientHostname); jobMetadata.getUserAgent().ifPresent(jobEntity::setRequestApiClientUserAgent); jobMetadata.getNumAttachments().ifPresent(jobEntity::setNumAttachments); jobMetadata.getTotalSizeOfAttachments().ifPresent(jobEntity::setTotalSizeOfAttachments); jobMetadata.getStdErrSize().ifPresent(jobEntity::setStdErrSize); jobMetadata.getStdOutSize().ifPresent(jobEntity::setStdOutSize); // Fields a user cares about (job dto) job.getArchiveLocation().ifPresent(jobEntity::setArchiveLocation); job.getStarted().ifPresent(jobEntity::setStarted); job.getFinished().ifPresent(jobEntity::setFinished); jobEntity.setStatus(job.getStatus()); job.getStatusMsg().ifPresent(jobEntity::setStatusMsg); // Fields set by system as part of job execution jobEntity.setAgentHostname(jobExecution.getHostName()); jobExecution.getProcessId().ifPresent(jobEntity::setProcessId); jobExecution.getCheckDelay().ifPresent(jobEntity::setCheckDelay); jobExecution.getTimeout().ifPresent(jobEntity::setTimeout); jobExecution.getMemory().ifPresent(jobEntity::setMemoryUsed); // Flag to signal to rest of system that this job is V3. Temporary until everything moved to v4 jobEntity.setV4(false); return jobEntity; }
From source file:org.dbgl.util.StringRelatedUtils.java
public static String[] textAreaToStringArray(final String contents, final String del) { return StringUtils.splitByWholeSeparator(StringUtils.strip(contents, del), del); }
From source file:org.dbgl.util.StringRelatedUtils.java
public static String[] mountToStringArray(final String paths, final boolean quotes) { return quotes ? StringUtils.splitByWholeSeparator(paths, "\" \"") : StringUtils.split(paths, ' '); }
From source file:org.esigate.vars.DriverEsiVariablesTest.java
/** * 0000246: ESI variables are not available / replaced. http://www.esigate.org/mantisbt/view.php?id=246 * // w w w. java 2 s . co m * @throws IOException * @throws HttpErrorPage */ @SuppressWarnings("static-method") @Test public void testEsiVariablesCase1() throws IOException, HttpErrorPage { // Reset Driverfactory (used for default driver with $(PROVIDER)) Properties factoryProperties = new Properties(); factoryProperties.put("tested." + Parameters.REMOTE_URL_BASE.getName(), "http://localhost.mydomain.fr/"); DriverFactory.configure(factoryProperties); // Configuration Properties properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost.mydomain.fr/"); // Test case IncomingRequest request = TestUtils .createRequest("http://test.mydomain.fr/foobar/?test=esigate&test2=esigate2") .addHeader("Referer", "http://www.esigate.org") .addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 " + "(KHTML, like Gecko) Version/6.0.5 Safari/536.30.1") .addHeader("Accept-Language", "da, en-gb;q=0.8, en;q=0.7") .addCookie(new BasicClientCookie("test-cookie", "test-cookie-value")) .addCookie(new BasicClientCookie("test-cookie2", "test-cookie-value2")).build(); final StringBuilder expected = new StringBuilder(); addVariable(expected, "HTTP_ACCEPT_LANGUAGE", "da, en-gb;q=0.8, en;q=0.7"); addVariable(expected, "HTTP_ACCEPT_LANGUAGE{en}", "true"); addVariable(expected, "HTTP_ACCEPT_LANGUAGE{fr}", "false"); addVariable(expected, "QUERY_STRING{test}", "esigate"); addVariable(expected, "QUERY_STRING", "test=esigate&test2=esigate2"); addVariable(expected, "HTTP_REFERER", "http://www.esigate.org"); addVariable(expected, "PROVIDER{tested}", "http://localhost.mydomain.fr/"); addVariable(expected, "PROVIDER{missing}", ""); addVariable(expected, "PROVIDER", ""); addVariable(expected, "HTTP_HOST", "test.mydomain.fr"); addVariable(expected, "HTTP_USER_AGENT", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko)" + " Version/6.0.5 Safari/536.30.1"); addVariable(expected, "HTTP_USER_AGENT{browser}", "MOZILLA"); addVariable(expected, "HTTP_USER_AGENT{os}", "MAC"); addVariable(expected, "HTTP_COOKIE{test-cookie}", "test-cookie-value"); addVariable(expected, "HTTP_COOKIE{'test-cookie'}", "test-cookie-value"); addVariable(expected, "HTTP_COOKIE{missing}", ""); addVariable(expected, "QUERY_STRING{missing}", ""); addVariable(expected, "HTTP_USER_AGENT{version}", "5.0"); addVariable(expected, "HTTP_HEADER{Accept-Language}", "da, en-gb;q=0.8, en;q=0.7"); addVariable(expected, "HTTP_COOKIE", "test-cookie=test-cookie-value; test-cookie2=test-cookie-value2"); addVariable(expected, "QUERY_STRING{missing}|default-value", "default-value"); addVariable(expected, "QUERY_STRING{missing}|'default value'", "default value"); // Setup remote server (provider) response. IResponseHandler mockExecutor = new IResponseHandler() { @Override public HttpResponse execute(HttpRequest request) { StringBuilder content = new StringBuilder(); content.append("<esi:vars>"); String[] expectedArray = StringUtils.splitByWholeSeparator(expected.toString(), "<p>"); for (String expr : expectedArray) { addVariable(content, expr.substring(0, expr.indexOf(":"))); } content.append("</esi:vars>"); LOG.info("Backend response:\n" + content.toString()); return TestUtils.createHttpResponse() .entity(new StringEntity(content.toString(), ContentType.TEXT_HTML)).build(); } }; // Build driver and request. Driver driver = TestUtils.createMockDriver(properties, mockExecutor); CloseableHttpResponse response = TestUtils.driverProxy(driver, request, new EsiRenderer()); String entityContent = EntityUtils.toString(response.getEntity()); LOG.info("Esigate response: \n" + entityContent); String[] expectedArray = StringUtils.splitByWholeSeparator(expected.toString(), "<p>"); String[] resultArray = StringUtils.splitByWholeSeparator(entityContent, "<p>"); for (int i = 0; i < expectedArray.length; i++) { String varName = expectedArray[i].substring(0, expectedArray[i].indexOf(":")); Assert.assertEquals(varName, expectedArray[i], resultArray[i]); LOG.info("Success with variable {}", varName); } }
From source file:org.esigate.vars.DriverEsiVariablesTest.java
/** * 0000246: ESI variables are not available / replaced. http://www.esigate.org/mantisbt/view.php?id=246 * //from w w w .jav a2 s .co m * @throws IOException * @throws HttpErrorPage */ @SuppressWarnings("static-method") @Test public void testEsiVariablesCase2() throws IOException, HttpErrorPage { // Configuration Properties properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost.mydomain.fr/"); // Test case IncomingRequest request = TestUtils.createRequest("http://test.mydomain.fr/foobar/") .addHeader("User-Agent", "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)").build(); final StringBuilder expected = new StringBuilder(); addVariable(expected, "HTTP_ACCEPT_LANGUAGE{en}", "false"); addVariable(expected, "HTTP_ACCEPT_LANGUAGE{fr}", "false"); addVariable(expected, "HTTP_HOST", "test.mydomain.fr"); addVariable(expected, "HTTP_USER_AGENT", "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)"); addVariable(expected, "HTTP_USER_AGENT{browser}", "MSIE"); addVariable(expected, "HTTP_USER_AGENT{os}", "WIN"); addVariable(expected, "HTTP_ACCEPT_LANGUAGE", ""); addVariable(expected, "QUERY_STRING{test}", ""); addVariable(expected, "QUERY_STRING", ""); addVariable(expected, "HTTP_REFERER", ""); addVariable(expected, "HTTP_COOKIE{test-cookie}", ""); addVariable(expected, "HTTP_COOKIE", ""); addVariable(expected, "QUERY_STRING{missing}", ""); addVariable(expected, "HTTP_USER_AGENT{version}", "5.0"); // Setup remote server (provider) response. IResponseHandler mockExecutor = new IResponseHandler() { @Override public HttpResponse execute(HttpRequest request) { StringBuilder content = new StringBuilder(); content.append("<esi:vars>"); String[] expectedArray = StringUtils.splitByWholeSeparator(expected.toString(), "<p>"); for (String expr : expectedArray) { addVariable(content, expr.substring(0, expr.indexOf(": "))); } content.append("</esi:vars>"); LOG.info("Backend response:\n" + content.toString()); return TestUtils.createHttpResponse() .entity(new StringEntity(content.toString(), ContentType.TEXT_HTML)).build(); } }; // Build driver and request. Driver driver = TestUtils.createMockDriver(properties, mockExecutor); CloseableHttpResponse response = TestUtils.driverProxy(driver, request, new EsiRenderer()); String entityContent = EntityUtils.toString(response.getEntity()); LOG.info("Esigate response: \n" + entityContent); String[] expectedArray = StringUtils.splitByWholeSeparator(expected.toString(), "<p>"); String[] resultArray = StringUtils.splitByWholeSeparator(entityContent, "<p>"); for (int i = 0; i < expectedArray.length; i++) { String varName = expectedArray[i].substring(0, expectedArray[i].indexOf(":")); Assert.assertEquals(varName, expectedArray[i], resultArray[i]); LOG.info("Success with variable {}", varName); } }