Search in sources :

Example 11 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project records-management by Alfresco.

the class PublicAPITestUtil method testPublicAPIConsistency.

/**
 * Check the consistency of the public API exposed from the given package. For each class in the package that is
 * annotated {@link AlfrescoPublicApi}, check that no exposed methods (or fields, constructors, etc.) use
 * non-public-API classes from Alfresco.
 *
 * @param basePackageName The package to check classes within.
 * @param knownBadReferences Any references that would cause this test to fail, but which we don't want to change.
 *            The keys should be public API classes within our code and the values should be the non-public-API
 *            class that is being referenced.
 */
public static void testPublicAPIConsistency(String basePackageName, SetMultimap<Class<?>, Class<?>> knownBadReferences) {
    Reflections reflections = new Reflections(basePackageName);
    Set<Class<?>> publicAPIClasses = reflections.getTypesAnnotatedWith(AlfrescoPublicApi.class, true);
    SetMultimap<Class<?>, Class<?>> referencedFrom = HashMultimap.create();
    Set<Class<?>> referencedClasses = new HashSet<>();
    for (Class<?> publicAPIClass : publicAPIClasses) {
        Set<Class<?>> referencedClassesFromClass = getReferencedClassesFromClass(publicAPIClass, new HashSet<>());
        referencedClassesFromClass.forEach(clazz -> referencedFrom.put(clazz, publicAPIClass));
        // Remove any references in knownBadReferences and error if an expected reference wasn't found.
        if (knownBadReferences.containsKey(publicAPIClass)) {
            for (Class<?> clazz : knownBadReferences.get(publicAPIClass)) {
                assertTrue("Supplied knownBadReferences expects " + clazz + " to be referenced by " + publicAPIClass + ", but no such error was found", referencedClassesFromClass.remove(clazz));
            }
        }
        referencedClasses.addAll(referencedClassesFromClass);
    }
    List<String> errorMessages = new ArrayList<>();
    for (Class<?> referencedClass : referencedClasses) {
        if (isInAlfresco(referencedClass) && !isPartOfPublicApi(referencedClass)) {
            Set<String> referencerNames = referencedFrom.get(referencedClass).stream().map(c -> c.getName()).collect(Collectors.toSet());
            errorMessages.add(referencedClass.getName() + " <- " + StringUtils.join(referencerNames, ", "));
        }
    }
    if (!errorMessages.isEmpty()) {
        System.out.println("Errors found:");
        System.out.println(StringUtils.join(errorMessages, "\n"));
    }
    assertEquals("Found references to non-public API classes from public API classes.", Collections.emptyList(), errorMessages);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) AlfrescoPublicApi(org.alfresco.api.AlfrescoPublicApi) TypeVariable(java.lang.reflect.TypeVariable) WildcardType(java.lang.reflect.WildcardType) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Reflections(org.reflections.Reflections) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) SetMultimap(com.google.common.collect.SetMultimap) Sets(com.google.common.collect.Sets) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) HashMultimap(com.google.common.collect.HashMultimap) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) Executable(java.lang.reflect.Executable) Method(java.lang.reflect.Method) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 12 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project beijingThirdPeriod by weidongcao.

the class TestHbase method main.

public static void main(String[] args) {
    SparkConf conf = new SparkConf().setAppName("aaa").setMaster("local");
    JavaSparkContext sc = new JavaSparkContext(conf);
    Random random = new Random();
    List<String> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        int[] aaa = new int[4];
        for (int j = 0; j < 4; j++) {
            aaa[j] = random.nextInt(900) + 100;
        }
        list.add(StringUtils.join(aaa, ","));
    }
    JavaRDD<String> originalRDD = sc.parallelize(list);
    originalRDD.foreach((VoidFunction<String>) s -> System.out.println(s));
    /*JavaPairRDD<RowkeyColumnSecondarySort, String> hfileRDD = originalRDD.flatMapToPair(
                new PairFlatMapFunction<String, RowkeyColumnSecondarySort, String>() {
                    @Override
                    public Iterable<Tuple2<RowkeyColumnSecondarySort, String>> call(String s) throws Exception {
                        String[] aa = s.split(",");

                    }
                }
        )*/
    sc.close();
}
Also used : List(java.util.List) SparkConf(org.apache.spark.SparkConf) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) Random(java.util.Random) VoidFunction(org.apache.spark.api.java.function.VoidFunction) StringUtils(org.apache.commons.lang3.StringUtils) JavaRDD(org.apache.spark.api.java.JavaRDD) ArrayList(java.util.ArrayList) Random(java.util.Random) ArrayList(java.util.ArrayList) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) SparkConf(org.apache.spark.SparkConf)

Example 13 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project custom-war-packager by jenkinsci.

the class JenkinsWarPatcher method replaceLib.

private void replaceLib(DependencyInfo lib, Map<String, String> versionOverrides) throws IOException, InterruptedException {
    if (lib.source == null) {
        throw new IOException("Source is not defined for " + lib);
    }
    File libsDir = getLibsDir();
    String effectiveVersion = versionOverrides.get(lib.artifactId);
    if (effectiveVersion == null) {
        if (!lib.source.isReleasedVersion()) {
            throw new IOException("Cannot resolve new version for library " + lib);
        }
        effectiveVersion = lib.source.version;
    }
    List<Path> paths = Files.find(libsDir.toPath(), 1, (path, basicFileAttributes) -> {
        // TODO: this matcher is a bit lame, it may suffer from false positives
        String fileName = String.valueOf(path.getFileName());
        if (fileName.matches(lib.artifactId + "-\\d+.*")) {
            return true;
        }
        return false;
    }).collect(Collectors.toList());
    if (paths.size() > 1) {
        throw new IOException("Bug in Jenkins WAR Packager, cannot find unique lib JAR for artifact " + lib.artifactId + ". Candidates: " + StringUtils.join(paths, ","));
    } else if (paths.size() == 1) {
        Path oldFile = paths.get(0);
        LOGGER.log(Level.INFO, "Replacing the existing library {0} by version {1}. Original File: {2}", new Object[] { lib.artifactId, effectiveVersion, oldFile.getFileName() });
        Files.delete(oldFile);
    } else {
        LOGGER.log(Level.INFO, "Adding new library {0} with version {1}", new Object[] { lib.artifactId, effectiveVersion });
    }
    File newJarFile = new File(libsDir, lib.artifactId + "-" + effectiveVersion + ".jar");
    MavenHelper.downloadArtifact(dstDir, lib, effectiveVersion, newJarFile);
}
Also used : Path(java.nio.file.Path) Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) Arrays(java.util.Arrays) TransformerException(javax.xml.transform.TransformerException) Enumeration(java.util.Enumeration) StreamResult(javax.xml.transform.stream.StreamResult) StringUtils(org.apache.commons.lang3.StringUtils) Level(java.util.logging.Level) HashSet(java.util.HashSet) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) DependencyInfo(io.jenkins.tools.warpackager.lib.config.DependencyInfo) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) GroovyHookInfo(io.jenkins.tools.warpackager.lib.config.GroovyHookInfo) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Config(io.jenkins.tools.warpackager.lib.config.Config) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) MavenHelper(io.jenkins.tools.warpackager.lib.util.MavenHelper) CheckForNull(javax.annotation.CheckForNull) InputStream(java.io.InputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 14 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project custom-war-packager by jenkinsci.

the class JenkinsWarPatcher method excludeLib.

private void excludeLib(DependencyInfo lib) throws IOException, InterruptedException {
    File libsDir = getLibsDir();
    List<Path> paths = Files.find(libsDir.toPath(), 1, (path, basicFileAttributes) -> {
        // TODO: this matcher is a bit lame, it may suffer from false positives
        String fileName = String.valueOf(path.getFileName());
        if (fileName.matches(lib.artifactId + "-\\d+.*")) {
            return true;
        }
        return false;
    }).collect(Collectors.toList());
    if (paths.size() > 1) {
        throw new IOException("Bug in Jenkins WAR Packager, cannot find unique lib JAR for artifact " + lib.artifactId + ". Candidates: " + StringUtils.join(paths, ","));
    } else if (paths.size() == 1) {
        Path oldFile = paths.get(0);
        LOGGER.log(Level.INFO, "Removing library {0}. Original File: {1}", new Object[] { lib.artifactId, oldFile.getFileName() });
        Files.delete(oldFile);
    } else {
        throw new IOException("Cannot remove library " + lib + ". It is missing in the WAR file");
    }
}
Also used : Path(java.nio.file.Path) Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) Arrays(java.util.Arrays) TransformerException(javax.xml.transform.TransformerException) Enumeration(java.util.Enumeration) StreamResult(javax.xml.transform.stream.StreamResult) StringUtils(org.apache.commons.lang3.StringUtils) Level(java.util.logging.Level) HashSet(java.util.HashSet) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) DependencyInfo(io.jenkins.tools.warpackager.lib.config.DependencyInfo) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) GroovyHookInfo(io.jenkins.tools.warpackager.lib.config.GroovyHookInfo) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Config(io.jenkins.tools.warpackager.lib.config.Config) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) MavenHelper(io.jenkins.tools.warpackager.lib.util.MavenHelper) CheckForNull(javax.annotation.CheckForNull) InputStream(java.io.InputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 15 with StringUtils.join

use of org.apache.commons.lang3.StringUtils.join in project Gemma by PavlidisLab.

the class SearchServiceImpl method characteristicSearchWithChildren.

/**
 * Search for the query in ontologies, including items that are associated with children of matching query terms.
 * That is, 'brain' should return entities tagged as 'hippocampus'. This method will return results only up to
 * MAX_CHARACTERISTIC_SEARCH_RESULTS. It can handle AND in searches, so Parkinson's AND neuron finds items tagged
 * with both of those terms. The use of OR is handled by the caller.
 *
 * @param classes Classes of characteristic-bound entities. For example, to get matching characteristics of
 *                ExpressionExperiments, pass ExpressionExperiments.class in this collection parameter.
 * @return SearchResults of CharacteristicObjects. Typically to be useful one needs to retrieve the 'parents'
 * (entities which have been 'tagged' with the term) of those Characteristics
 */
private Collection<SearchResult> characteristicSearchWithChildren(Collection<Class<?>> classes, String query) {
    StopWatch timer = this.startTiming();
    /*
         * The tricky part here is if the user has entered a boolean query. If they put in Parkinson's disease AND neuron,
         * then we want to eventually return entities that are associated with both. We don't expect to find single
         * characteristics that match both.
         *
         * But if they put in Parkinson's disease we don't want to do two queries.
         */
    List<String> subparts = Arrays.asList(query.split(" AND "));
    // we would have to first deal with the separate queries, and then apply the logic.
    Collection<SearchResult> allResults = new HashSet<>();
    SearchServiceImpl.log.info("Starting characteristic search: " + query + " for type=" + StringUtils.join(classes, ","));
    for (String rawTerm : subparts) {
        String trimmed = StringUtils.strip(rawTerm);
        if (StringUtils.isBlank(trimmed)) {
            continue;
        }
        Collection<SearchResult> subqueryResults = this.characteristicSearchTerm(classes, trimmed);
        if (allResults.isEmpty()) {
            allResults.addAll(subqueryResults);
        } else {
            // this is our Intersection operation.
            allResults.retainAll(subqueryResults);
            // aggregate the highlighted text.
            Map<SearchResult, String> highlights = new HashMap<>();
            for (SearchResult sqr : subqueryResults) {
                highlights.put(sqr, sqr.getHighlightedText());
            }
            for (SearchResult ar : allResults) {
                String k = highlights.get(ar);
                if (StringUtils.isNotBlank(k)) {
                    String highlightedText = ar.getHighlightedText();
                    if (StringUtils.isBlank(highlightedText)) {
                        ar.setHighlightedText(k);
                    } else {
                        ar.setHighlightedText(highlightedText + "," + k);
                    }
                }
            }
        }
        if (timer.getTime() > 1000) {
            SearchServiceImpl.log.info("Characteristic search for '" + rawTerm + "': " + allResults.size() + " hits retained so far; " + timer.getTime() + "ms");
            timer.reset();
            timer.start();
        }
    }
    return allResults;
}
Also used : StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)34 List (java.util.List)30 Collectors (java.util.stream.Collectors)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 HashMap (java.util.HashMap)15 Set (java.util.Set)14 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 IOException (java.io.IOException)13 HashSet (java.util.HashSet)11 Arrays (java.util.Arrays)10 Collections (java.util.Collections)10 Date (java.util.Date)9 File (java.io.File)6 StopWatch (org.apache.commons.lang3.time.StopWatch)6 InputStream (java.io.InputStream)5 java.util (java.util)5 Pair (org.apache.commons.lang3.tuple.Pair)5 Path (java.nio.file.Path)4