List of usage examples for org.apache.commons.lang StringUtils removeStart
public static String removeStart(String str, String remove)
Removes a substring only if it is at the begining of a source string, otherwise returns the source string.
From source file:org.apache.ranger.plugin.policyengine.RangerPolicyRepository.java
private List<? extends RangerPolicy.RangerPolicyItem> normalizeAndPrunePolicyItems( List<? extends RangerPolicy.RangerPolicyItem> policyItems, final String componentType) { if (CollectionUtils.isNotEmpty(policyItems)) { final String prefix = componentType + AbstractServiceStore.COMPONENT_ACCESSTYPE_SEPARATOR; List<RangerPolicy.RangerPolicyItem> itemsToPrune = null; for (RangerPolicy.RangerPolicyItem policyItem : policyItems) { List<RangerPolicy.RangerPolicyItemAccess> policyItemAccesses = policyItem.getAccesses(); if (CollectionUtils.isNotEmpty(policyItemAccesses)) { List<RangerPolicy.RangerPolicyItemAccess> accessesToPrune = null; for (RangerPolicy.RangerPolicyItemAccess access : policyItemAccesses) { String accessType = access.getType(); if (StringUtils.startsWith(accessType, prefix)) { String newAccessType = StringUtils.removeStart(accessType, prefix); access.setType(newAccessType); } else if (accessType.contains(AbstractServiceStore.COMPONENT_ACCESSTYPE_SEPARATOR)) { if (accessesToPrune == null) { accessesToPrune = new ArrayList<RangerPolicy.RangerPolicyItemAccess>(); }/*ww w . j a v a 2 s.c om*/ accessesToPrune.add(access); } } if (accessesToPrune != null) { policyItemAccesses.removeAll(accessesToPrune); } if (policyItemAccesses.isEmpty() && !policyItem.getDelegateAdmin()) { if (itemsToPrune == null) { itemsToPrune = new ArrayList<RangerPolicy.RangerPolicyItem>(); } itemsToPrune.add(policyItem); } } } if (itemsToPrune != null) { policyItems.removeAll(itemsToPrune); } } return policyItems; }
From source file:org.apache.rya.accumulo.mr.merge.CopyTool.java
public static void main(final String[] args) { final String log4jConfiguration = System.getProperties().getProperty("log4j.configuration"); if (StringUtils.isNotBlank(log4jConfiguration)) { final String parsedConfiguration = StringUtils.removeStart(log4jConfiguration, "file:"); final File configFile = new File(parsedConfiguration); if (configFile.exists()) { DOMConfigurator.configure(parsedConfiguration); } else {/* w w w. j av a2 s.com*/ BasicConfigurator.configure(); } } log.info("Starting Copy Tool"); Thread.setDefaultUncaughtExceptionHandler( (thread, throwable) -> log.error("Uncaught exception in " + thread.getName(), throwable)); final CopyTool copyTool = new CopyTool(); final int returnCode = copyTool.setupAndRun(args); log.info("Finished running Copy Tool"); System.exit(returnCode); }
From source file:org.apache.rya.accumulo.mr.merge.MergeTool.java
public static void main(final String[] args) { final String log4jConfiguration = System.getProperties().getProperty("log4j.configuration"); if (StringUtils.isNotBlank(log4jConfiguration)) { final String parsedConfiguration = StringUtils.removeStart(log4jConfiguration, "file:"); final File configFile = new File(parsedConfiguration); if (configFile.exists()) { DOMConfigurator.configure(parsedConfiguration); } else {/*from www.j a v a 2 s.co m*/ BasicConfigurator.configure(); } } log.info("Starting Merge Tool"); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread thread, final Throwable throwable) { log.error("Uncaught exception in " + thread.getName(), throwable); } }); final int returnCode = setupAndRun(args); log.info("Finished running Merge Tool"); System.exit(returnCode); }
From source file:org.apache.rya.accumulo.mr.merge.util.ToolConfigUtils.java
/** * Gets the set of user arguments from the user's config and/or their extra supplied * command line arguments. This weeds out all the automatically generated parameters created * from initializing a {@link Configuration} object and should only give back a set of arguments * provided directly by the user./* w w w. ja v a2 s .co m*/ * @param conf the {@link Configuration} provided. * @param args the extra arguments from the command line. * @return a {@link Set} of argument strings. * @throws IOException */ public static Set<String> getUserArguments(final Configuration conf, final String[] args) throws IOException { String[] filteredArgs = new String[] {}; if (Arrays.asList(args).contains("-conf")) { // parse args new GenericOptionsParser(conf, args); final List<String> commandLineArgs = new ArrayList<>(); for (final String arg : args) { if (arg.startsWith("-D")) { commandLineArgs.add(arg); } } filteredArgs = commandLineArgs.toArray(new String[0]); } else { filteredArgs = args; } // Get the supplied config name from the resource string. // No real easy way of getting the name. // So, pulling it off the list of resource names in the Configuration's toString() method // where it should be the last one. final String confString = conf.toString(); final String resourceString = StringUtils.removeStart(confString, "Configuration: "); final List<String> resourceNames = Arrays.asList(StringUtils.split(resourceString, ", ")); final String configFilename = resourceNames.get(resourceNames.size() - 1); final Set<String> toolArgsSet = new HashSet<>(); final File file = new File(configFilename); // Check that the last resource name is the actual user's config by seeing if it's a file // on the system, the other resources seem to be contained in jars and so should fail here which // should happen if no config is supplied. if (file.exists()) { XMLConfiguration configuration = null; try { configuration = new XMLConfiguration(configFilename); toolArgsSet.addAll(getConfigArguments(configuration)); } catch (final ConfigurationException e) { log.error("Unable to load configuration file.", e); } } toolArgsSet.addAll(Arrays.asList(filteredArgs)); return Collections.unmodifiableSet(toolArgsSet); }
From source file:org.apache.rya.api.domain.VarNameUtils.java
/** * Removes the constant prefix from a string if it exists. * @param name the name string to strip the constant prefix from. * @return the string with the constant prefix removed. Otherwise returns * the original string./*w w w .j a v a 2s . c om*/ */ public static @Nullable String removeConstant(@Nullable final String name) { if (isConstant(name)) { String removed = StringUtils.removeStart(name, CONSTANT_PREFIX); if (name.equals(removed)) { removed = StringUtils.removeStart(name, LEGACY_CONSTANT_PREFIX); } return removed; } return name; }
From source file:org.apache.rya.api.domain.VarNameUtils.java
/** * Removes the anonymous prefix from a string if it exists. * @param name the name string to strip the anonymous prefix from. * @return the string with the anonymous prefix removed. Otherwise returns * the original string.//from www . j a v a2 s. c o m */ public static @Nullable String removeAnonymous(@Nullable final String name) { if (isAnonymous(name)) { String removed = StringUtils.removeStart(name, ANONYMOUS_PREFIX); if (name.equals(removed)) { removed = StringUtils.removeStart(name, LEGACY_ANONYMOUS_PREFIX); } } return name; }
From source file:org.apache.rya.export.client.MergeDriverClient.java
public static void main(final String[] args) throws ParseException, MergeConfigurationException, UnknownHostException, MergerException, java.text.ParseException, SailException, AccumuloException, AccumuloSecurityException, InferenceEngineException, RepositoryException, MalformedQueryException, UpdateExecutionException { final String log4jConfiguration = System.getProperties().getProperty("log4j.configuration"); if (StringUtils.isNotBlank(log4jConfiguration)) { final String parsedConfiguration = StringUtils.removeStart(log4jConfiguration, "file:"); final File configFile = new File(parsedConfiguration); if (configFile.exists()) { DOMConfigurator.configure(parsedConfiguration); } else {//from w w w .jav a2s . c o m BasicConfigurator.configure(); } } final MergeConfigurationCLI config = new MergeConfigurationCLI(args); try { configuration = config.createConfiguration(); } catch (final MergeConfigurationException e) { LOG.error("Configuration failed.", e); } final boolean useTimeSync = configuration.getUseNtpServer(); Optional<Long> offset = Optional.absent(); if (useTimeSync) { final String tomcat = configuration.getChildTomcatUrl(); final String ntpHost = configuration.getNtpServerHost(); try { offset = Optional .<Long>fromNullable(TimeUtils.getNtpServerAndMachineTimeDifference(ntpHost, tomcat)); } catch (final IOException e) { LOG.error("Unable to get time difference between time server: " + ntpHost + " and the server: " + tomcat, e); } } final StatementStoreFactory storeFactory = new StatementStoreFactory(configuration); try { final RyaStatementStore parentStore = storeFactory.getParentStatementStore(); final RyaStatementStore childStore = storeFactory.getChildStatementStore(); LOG.info("Starting Merge Tool"); if (configuration.getParentDBType() == ACCUMULO && configuration.getChildDBType() == ACCUMULO) { final AccumuloRyaStatementStore childAStore = (AccumuloRyaStatementStore) childStore; final AccumuloRyaStatementStore parentAStore = (AccumuloRyaStatementStore) parentStore; //do map reduce merging. //TODO: Run Merger } else { if (configuration.getMergePolicy() == TIMESTAMP) { final TimestampPolicyMergeConfiguration timeConfig = (TimestampPolicyMergeConfiguration) configuration; final Long timeOffset; if (offset.isPresent()) { timeOffset = offset.get(); } else { timeOffset = 0L; } final MemoryTimeMerger merger = new MemoryTimeMerger(parentStore, childStore, new VisibilityStatementMerger(), timeConfig.getToolStartTime(), configuration.getParentRyaInstanceName(), timeOffset); merger.runJob(); } } } catch (final Exception e) { LOG.error("Something went wrong creating a Rya Statement Store connection.", e); } Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread thread, final Throwable throwable) { LOG.error("Uncaught exception in " + thread.getName(), throwable); } }); LOG.info("Finished running Merge Tool"); System.exit(1); }
From source file:org.apache.rya.indexing.accumulo.freetext.AccumuloFreeTextIndexer.java
private Set<String> unrollWildcard(final String string, final boolean reverse) throws IOException { final Scanner termTableScan = getScanner(getFreeTextTermTablename(conf)); final Set<String> unrolledTerms = new HashSet<String>(); Text queryTerm;//from w ww.j av a 2s. c o m if (reverse) { final String t = StringUtils.removeStart(string, "*").toLowerCase(); queryTerm = ColumnPrefixes.getRevTermListColFam(t); } else { final String t = StringUtils.removeEnd(string, "*").toLowerCase(); queryTerm = ColumnPrefixes.getTermListColFam(t); } // perform query and read results termTableScan.setRange(Range.prefix(queryTerm)); for (final Entry<Key, Value> e : termTableScan) { final String term = ColumnPrefixes.removePrefix(e.getKey().getRow()).toString(); if (reverse) { unrolledTerms.add(StringUtils.reverse(term)); } else { unrolledTerms.add(term); } } if (unrolledTerms.isEmpty()) { // put in a placeholder term that will never be in the index. unrolledTerms.add("\1\1\1"); } return unrolledTerms; }
From source file:org.apache.rya.mongodb.document.util.DisjunctiveNormalFormConverter.java
/** * Creates a document visibility boolean expression string into Disjunctive * Normal Form (DNF). Expressions use this format in DNF:<pre> * (P1 & P2 & P3 ... Pn) | (Q1 & Q2 ... Qm) ... * </pre>/*from w w w. j ava 2 s.c o m*/ * @param documentVisibility the {@link DocumentVisibility}. * @return a new {@link DocumentVisibility} with its expression in DNF. */ public static DocumentVisibility convertToDisjunctiveNormalForm(final DocumentVisibility documentVisibility) { // Find all the terms used in the expression final List<String> terms = findNodeTerms(documentVisibility.getParseTree(), documentVisibility.getExpression()); // Create an appropriately sized truth table that has the correct 0's // and 1's in place based on the number of terms. // This size should be [numberOfTerms][2 ^ numberOfTerms]. final byte[][] truthTable = createTruthTableInputs(terms); // Go through each row in the truth table. // If the row has a 1 for the term then create an Authorization for it // and test if it works. // If the row passes then that means all the terms that were a 1 and // were used can be AND'ed together to pass the expression. // All the rows that pass can be OR'd together. // Disjunction Normal Form: (P1 & P2 & P3 ... Pn) | (Q1 & Q2 ... Qm) ... final List<List<String>> termRowsThatPass = new ArrayList<>(); for (final byte[] row : truthTable) { final List<String> termRowToCheck = new ArrayList<>(); // If the truth table input is a 1 then include the corresponding // term that it matches. for (int i = 0; i < row.length; i++) { final byte entry = row[i]; if (entry == 1) { termRowToCheck.add(terms.get(i)); } } final List<String> authList = new ArrayList<>(); for (final String auth : termRowToCheck) { String formattedAuth = auth; formattedAuth = StringUtils.removeStart(formattedAuth, "\""); formattedAuth = StringUtils.removeEnd(formattedAuth, "\""); authList.add(formattedAuth); } final Authorizations auths = new Authorizations(authList.toArray(new String[0])); final boolean hasAccess = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, documentVisibility, false); if (hasAccess) { boolean alreadyCoveredBySimplerTerms = false; // If one 'AND' group is (A&C) and another is (A&B&C) then we // can drop (A&B&C) since it is already covered by simpler terms // (it's a subset) for (final List<String> existingTermRowThatPassed : termRowsThatPass) { alreadyCoveredBySimplerTerms = termRowToCheck.containsAll(existingTermRowThatPassed); if (alreadyCoveredBySimplerTerms) { break; } } if (!alreadyCoveredBySimplerTerms) { termRowsThatPass.add(termRowToCheck); } } } // Rebuild the term rows that passed as a document visibility boolean // expression string. final StringBuilder sb = new StringBuilder(); boolean isFirst = true; final boolean hasMultipleGroups = termRowsThatPass.size() > 1; for (final List<String> termRowThatPassed : termRowsThatPass) { if (isFirst) { isFirst = false; } else { sb.append("|"); } if (hasMultipleGroups && termRowThatPassed.size() > 1) { sb.append("("); } sb.append(Joiner.on("&").join(termRowThatPassed)); if (hasMultipleGroups && termRowThatPassed.size() > 1) { sb.append(")"); } } log.trace(sb.toString()); final DocumentVisibility dnfDv = new DocumentVisibility(sb.toString()); return dnfDv; }
From source file:org.apache.sentry.provider.db.service.persistent.SentryStore.java
public SentryStore(Configuration conf) throws SentryNoSuchObjectException, SentryAccessDeniedException, SentryConfigurationException, IOException { commitSequenceId = 0;/*from w w w . j a va 2 s .c o m*/ this.conf = conf; Properties prop = new Properties(); prop.putAll(ServerConfig.SENTRY_STORE_DEFAULTS); String jdbcUrl = conf.get(ServerConfig.SENTRY_STORE_JDBC_URL, "").trim(); Preconditions.checkArgument(!jdbcUrl.isEmpty(), "Required parameter " + ServerConfig.SENTRY_STORE_JDBC_URL + " missing"); String user = conf.get(ServerConfig.SENTRY_STORE_JDBC_USER, ServerConfig.SENTRY_STORE_JDBC_USER_DEFAULT) .trim(); //Password will be read from Credential provider specified using property // CREDENTIAL_PROVIDER_PATH("hadoop.security.credential.provider.path" in sentry-site.xml // it falls back to reading directly from sentry-site.xml char[] passTmp = conf.getPassword(ServerConfig.SENTRY_STORE_JDBC_PASS); String pass = null; if (passTmp != null) { pass = new String(passTmp); } else { throw new SentryConfigurationException("Error reading " + ServerConfig.SENTRY_STORE_JDBC_PASS); } String driverName = conf.get(ServerConfig.SENTRY_STORE_JDBC_DRIVER, ServerConfig.SENTRY_STORE_JDBC_DRIVER_DEFAULT); prop.setProperty(ServerConfig.JAVAX_JDO_URL, jdbcUrl); prop.setProperty(ServerConfig.JAVAX_JDO_USER, user); prop.setProperty(ServerConfig.JAVAX_JDO_PASS, pass); prop.setProperty(ServerConfig.JAVAX_JDO_DRIVER_NAME, driverName); for (Map.Entry<String, String> entry : conf) { String key = entry.getKey(); if (key.startsWith(ServerConfig.SENTRY_JAVAX_JDO_PROPERTY_PREFIX) || key.startsWith(ServerConfig.SENTRY_DATANUCLEUS_PROPERTY_PREFIX)) { key = StringUtils.removeStart(key, ServerConfig.SENTRY_DB_PROPERTY_PREFIX); prop.setProperty(key, entry.getValue()); } } boolean checkSchemaVersion = conf .get(ServerConfig.SENTRY_VERIFY_SCHEM_VERSION, ServerConfig.SENTRY_VERIFY_SCHEM_VERSION_DEFAULT) .equalsIgnoreCase("true"); if (!checkSchemaVersion) { prop.setProperty("datanucleus.autoCreateSchema", "true"); prop.setProperty("datanucleus.fixedDatastore", "false"); } // Disallow operations outside of transactions prop.setProperty("datanucleus.NontransactionalRead", "false"); prop.setProperty("datanucleus.NontransactionalWrite", "false"); pmf = JDOHelper.getPersistenceManagerFactory(prop); verifySentryStoreSchema(conf, checkSchemaVersion); // Kick off the thread that cleans orphaned privileges (unless told not to) privCleaner = this.new PrivCleaner(); if (conf.get(ServerConfig.SENTRY_STORE_ORPHANED_PRIVILEGE_REMOVAL, ServerConfig.SENTRY_STORE_ORPHANED_PRIVILEGE_REMOVAL_DEFAULT).equalsIgnoreCase("true")) { privCleanerThread = new Thread(privCleaner); privCleanerThread.start(); } }