Search in sources :

Example 11 with ResourceLoaderAware

use of org.apache.lucene.analysis.util.ResourceLoaderAware in project lucene-solr by apache.

the class SolrResourceLoader method newInstance.

public <T> T newInstance(String cName, Class<T> expectedType, String[] subPackages, Class[] params, Object[] args) {
    Class<? extends T> clazz = findClass(cName, expectedType, subPackages);
    if (clazz == null) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Can not find class: " + cName + " in " + classLoader);
    }
    T obj = null;
    try {
        Constructor<? extends T> constructor = null;
        try {
            constructor = clazz.getConstructor(params);
            obj = constructor.newInstance(args);
        } catch (NoSuchMethodException e) {
            //look for a zero arg constructor if the constructor args do not match
            try {
                constructor = clazz.getConstructor();
                obj = constructor.newInstance();
            } catch (NoSuchMethodException e1) {
                throw e;
            }
        }
    } catch (Error err) {
        log.error("Loading Class " + cName + " (" + clazz.getName() + ") triggered serious java error: " + err.getClass().getName(), err);
        throw err;
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error instantiating class: '" + clazz.getName() + "'", e);
    }
    if (!live) {
        if (obj instanceof SolrCoreAware) {
            assertAwareCompatibility(SolrCoreAware.class, obj);
            waitingForCore.add((SolrCoreAware) obj);
        }
        if (obj instanceof ResourceLoaderAware) {
            assertAwareCompatibility(ResourceLoaderAware.class, obj);
            waitingForResources.add((ResourceLoaderAware) obj);
        }
        if (obj instanceof SolrInfoBean) {
            //TODO: Assert here?
            infoMBeans.add((SolrInfoBean) obj);
        }
    }
    return obj;
}
Also used : SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware) SolrException(org.apache.solr.common.SolrException) NoInitialContextException(javax.naming.NoInitialContextException) NamingException(javax.naming.NamingException) SolrException(org.apache.solr.common.SolrException) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException)

Example 12 with ResourceLoaderAware

use of org.apache.lucene.analysis.util.ResourceLoaderAware in project lucene-solr by apache.

the class AnalyzerFactoryTask method createAnalysisPipelineComponent.

/**
   * Instantiates the given analysis factory class after pulling params from
   * the given stream tokenizer, then stores the result in the appropriate
   * pipeline component list.
   *
   * @param stok stream tokenizer from which to draw analysis factory params
   * @param clazz analysis factory class to instantiate
   */
@SuppressWarnings("fallthrough")
private void createAnalysisPipelineComponent(StreamTokenizer stok, Class<? extends AbstractAnalysisFactory> clazz) {
    Map<String, String> argMap = new HashMap<>();
    boolean parenthetical = false;
    try {
        WHILE_LOOP: while (stok.nextToken() != StreamTokenizer.TT_EOF) {
            switch(stok.ttype) {
                case ',':
                    {
                        if (parenthetical) {
                            // Do nothing
                            break;
                        } else {
                            // Finished reading this analysis factory configuration
                            break WHILE_LOOP;
                        }
                    }
                case '(':
                    {
                        if (parenthetical) {
                            throw new RuntimeException("Line #" + lineno(stok) + ": Unexpected opening parenthesis.");
                        }
                        parenthetical = true;
                        break;
                    }
                case ')':
                    {
                        if (parenthetical) {
                            parenthetical = false;
                        } else {
                            throw new RuntimeException("Line #" + lineno(stok) + ": Unexpected closing parenthesis.");
                        }
                        break;
                    }
                case StreamTokenizer.TT_WORD:
                    {
                        if (!parenthetical) {
                            throw new RuntimeException("Line #" + lineno(stok) + ": Unexpected token '" + stok.sval + "'");
                        }
                        String argName = stok.sval;
                        stok.nextToken();
                        if (stok.ttype != ':') {
                            throw new RuntimeException("Line #" + lineno(stok) + ": Missing ':' after '" + argName + "' param to " + clazz.getSimpleName());
                        }
                        stok.nextToken();
                        String argValue = stok.sval;
                        switch(stok.ttype) {
                            case StreamTokenizer.TT_NUMBER:
                                {
                                    argValue = Double.toString(stok.nval);
                                    // Drop the ".0" from numbers, for integer arguments
                                    argValue = TRAILING_DOT_ZERO_PATTERN.matcher(argValue).replaceFirst("");
                                // Intentional fall-through
                                }
                            case '"':
                            case '\'':
                            case StreamTokenizer.TT_WORD:
                                {
                                    argMap.put(argName, argValue);
                                    break;
                                }
                            case StreamTokenizer.TT_EOF:
                                {
                                    throw new RuntimeException("Unexpected EOF: " + stok.toString());
                                }
                            default:
                                {
                                    throw new RuntimeException("Line #" + lineno(stok) + ": Unexpected token: " + stok.toString());
                                }
                        }
                    }
            }
        }
        if (!argMap.containsKey("luceneMatchVersion")) {
            argMap.put("luceneMatchVersion", Version.LATEST.toString());
        }
        final AbstractAnalysisFactory instance;
        try {
            instance = clazz.getConstructor(Map.class).newInstance(argMap);
        } catch (Exception e) {
            throw new RuntimeException("Line #" + lineno(stok) + ": ", e);
        }
        if (instance instanceof ResourceLoaderAware) {
            Path baseDir = Paths.get(getRunData().getConfig().get("work.dir", "work"));
            if (!Files.isDirectory(baseDir)) {
                baseDir = Paths.get(".");
            }
            ((ResourceLoaderAware) instance).inform(new FilesystemResourceLoader(baseDir));
        }
        if (CharFilterFactory.class.isAssignableFrom(clazz)) {
            charFilterFactories.add((CharFilterFactory) instance);
        } else if (TokenizerFactory.class.isAssignableFrom(clazz)) {
            tokenizerFactory = (TokenizerFactory) instance;
        } else if (TokenFilterFactory.class.isAssignableFrom(clazz)) {
            tokenFilterFactories.add((TokenFilterFactory) instance);
        }
    } catch (RuntimeException e) {
        if (e.getMessage().startsWith("Line #")) {
            throw (e);
        } else {
            throw new RuntimeException("Line #" + lineno(stok) + ": ", e);
        }
    } catch (Throwable t) {
        throw new RuntimeException("Line #" + lineno(stok) + ": ", t);
    }
}
Also used : Path(java.nio.file.Path) FilesystemResourceLoader(org.apache.lucene.analysis.util.FilesystemResourceLoader) TokenizerFactory(org.apache.lucene.analysis.util.TokenizerFactory) HashMap(java.util.HashMap) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware) AbstractAnalysisFactory(org.apache.lucene.analysis.util.AbstractAnalysisFactory)

Example 13 with ResourceLoaderAware

use of org.apache.lucene.analysis.util.ResourceLoaderAware in project lucene-solr by apache.

the class TestAllAnalyzersHaveFactories method test.

public void test() throws Exception {
    List<Class<?>> analysisClasses = TestRandomChains.getClassesForPackage("org.apache.lucene.analysis");
    for (final Class<?> c : analysisClasses) {
        final int modifiers = c.getModifiers();
        if (// don't waste time with abstract classes
        Modifier.isAbstract(modifiers) || !Modifier.isPublic(modifiers) || c.isSynthetic() || c.isAnonymousClass() || c.isMemberClass() || c.isInterface() || testComponents.contains(c) || crazyComponents.contains(c) || oddlyNamedComponents.contains(c) || tokenFiltersWithoutFactory.contains(c) || // deprecated ones are typically back compat hacks
        c.isAnnotationPresent(Deprecated.class) || !(Tokenizer.class.isAssignableFrom(c) || TokenFilter.class.isAssignableFrom(c) || CharFilter.class.isAssignableFrom(c))) {
            continue;
        }
        Map<String, String> args = new HashMap<>();
        args.put("luceneMatchVersion", Version.LATEST.toString());
        if (Tokenizer.class.isAssignableFrom(c)) {
            String clazzName = c.getSimpleName();
            assertTrue(clazzName.endsWith("Tokenizer"));
            String simpleName = clazzName.substring(0, clazzName.length() - 9);
            assertNotNull(TokenizerFactory.lookupClass(simpleName));
            TokenizerFactory instance = null;
            try {
                instance = TokenizerFactory.forName(simpleName, args);
                assertNotNull(instance);
                if (instance instanceof ResourceLoaderAware) {
                    ((ResourceLoaderAware) instance).inform(loader);
                }
                assertSame(c, instance.create().getClass());
            } catch (IllegalArgumentException e) {
            // TODO: For now pass because some factories have not yet a default config that always works
            }
        } else if (TokenFilter.class.isAssignableFrom(c)) {
            String clazzName = c.getSimpleName();
            assertTrue(clazzName.endsWith("Filter"));
            String simpleName = clazzName.substring(0, clazzName.length() - (clazzName.endsWith("TokenFilter") ? 11 : 6));
            assertNotNull(TokenFilterFactory.lookupClass(simpleName));
            TokenFilterFactory instance = null;
            try {
                instance = TokenFilterFactory.forName(simpleName, args);
                assertNotNull(instance);
                if (instance instanceof ResourceLoaderAware) {
                    ((ResourceLoaderAware) instance).inform(loader);
                }
                Class<? extends TokenStream> createdClazz = instance.create(new KeywordTokenizer()).getClass();
                // only check instance if factory have wrapped at all!
                if (KeywordTokenizer.class != createdClazz) {
                    assertSame(c, createdClazz);
                }
            } catch (IllegalArgumentException e) {
            // TODO: For now pass because some factories have not yet a default config that always works
            }
        } else if (CharFilter.class.isAssignableFrom(c)) {
            String clazzName = c.getSimpleName();
            assertTrue(clazzName.endsWith("CharFilter"));
            String simpleName = clazzName.substring(0, clazzName.length() - 10);
            assertNotNull(CharFilterFactory.lookupClass(simpleName));
            CharFilterFactory instance = null;
            try {
                instance = CharFilterFactory.forName(simpleName, args);
                assertNotNull(instance);
                if (instance instanceof ResourceLoaderAware) {
                    ((ResourceLoaderAware) instance).inform(loader);
                }
                Class<? extends Reader> createdClazz = instance.create(new StringReader("")).getClass();
                // only check instance if factory have wrapped at all!
                if (StringReader.class != createdClazz) {
                    assertSame(c, createdClazz);
                }
            } catch (IllegalArgumentException e) {
            // TODO: For now pass because some factories have not yet a default config that always works
            }
        }
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) TokenizerFactory(org.apache.lucene.analysis.util.TokenizerFactory) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) CharFilterFactory(org.apache.lucene.analysis.util.CharFilterFactory) TokenFilterFactory(org.apache.lucene.analysis.util.TokenFilterFactory) StringReader(java.io.StringReader) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware) Tokenizer(org.apache.lucene.analysis.Tokenizer) MockTokenizer(org.apache.lucene.analysis.MockTokenizer) ReversePathHierarchyTokenizer(org.apache.lucene.analysis.path.ReversePathHierarchyTokenizer) TeeSinkTokenFilter(org.apache.lucene.analysis.sinks.TeeSinkTokenFilter) CachingTokenFilter(org.apache.lucene.analysis.CachingTokenFilter) MockGraphTokenFilter(org.apache.lucene.analysis.MockGraphTokenFilter) ValidatingTokenFilter(org.apache.lucene.analysis.ValidatingTokenFilter) MockRandomLookaheadTokenFilter(org.apache.lucene.analysis.MockRandomLookaheadTokenFilter) CrankyTokenFilter(org.apache.lucene.analysis.CrankyTokenFilter) MockTokenFilter(org.apache.lucene.analysis.MockTokenFilter) TokenFilter(org.apache.lucene.analysis.TokenFilter) MockHoleInjectingTokenFilter(org.apache.lucene.analysis.MockHoleInjectingTokenFilter)

Example 14 with ResourceLoaderAware

use of org.apache.lucene.analysis.util.ResourceLoaderAware in project tika by apache.

the class AnalyzerDeserializer method buildTokenizerFactory.

private static TokenizerFactory buildTokenizerFactory(JsonElement map, String analyzerName) throws IOException {
    if (!(map instanceof JsonObject)) {
        throw new IllegalArgumentException("Expecting a map with \"factory\" string and " + "\"params\" map in tokenizer factory;" + " not: " + map.toString() + " in " + analyzerName);
    }
    JsonElement factoryEl = ((JsonObject) map).get(FACTORY);
    if (factoryEl == null || !factoryEl.isJsonPrimitive()) {
        throw new IllegalArgumentException("Expecting value for factory in char filter factory builder in:" + analyzerName);
    }
    String factoryName = factoryEl.getAsString();
    factoryName = factoryName.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName;
    JsonElement paramsEl = ((JsonObject) map).get(PARAMS);
    Map<String, String> params = mapify(paramsEl);
    String spiName = "";
    for (String s : TokenizerFactory.availableTokenizers()) {
        Class clazz = TokenizerFactory.lookupClass(s);
        if (clazz.getName().equals(factoryName)) {
            spiName = s;
            break;
        }
    }
    if (spiName.equals("")) {
        throw new IllegalArgumentException("A SPI class of type org.apache.lucene.analysis.util.TokenizerFactory with name" + "'" + factoryName + "' does not exist.");
    }
    try {
        TokenizerFactory tokenizerFactory = TokenizerFactory.forName(spiName, params);
        if (tokenizerFactory instanceof ResourceLoaderAware) {
            ((ResourceLoaderAware) tokenizerFactory).inform(new ClasspathResourceLoader(AnalyzerDeserializer.class));
        }
        return tokenizerFactory;
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("While working on " + analyzerName, e);
    }
}
Also used : TokenizerFactory(org.apache.lucene.analysis.util.TokenizerFactory) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) ClasspathResourceLoader(org.apache.lucene.analysis.util.ClasspathResourceLoader) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware)

Aggregations

ResourceLoaderAware (org.apache.lucene.analysis.util.ResourceLoaderAware)14 HashMap (java.util.HashMap)7 IOException (java.io.IOException)6 SolrException (org.apache.solr.common.SolrException)5 TokenFilterFactory (org.apache.lucene.analysis.util.TokenFilterFactory)4 TokenizerFactory (org.apache.lucene.analysis.util.TokenizerFactory)4 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Map (java.util.Map)3 AbstractAnalysisFactory (org.apache.lucene.analysis.util.AbstractAnalysisFactory)3 CharFilterFactory (org.apache.lucene.analysis.util.CharFilterFactory)3 ClasspathResourceLoader (org.apache.lucene.analysis.util.ClasspathResourceLoader)3 JsonArray (com.google.gson.JsonArray)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 LinkedList (java.util.LinkedList)2 NamingException (javax.naming.NamingException)2 NoInitialContextException (javax.naming.NoInitialContextException)2 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1