Search in sources :

Example 66 with Closeable

use of java.io.Closeable in project lucene-solr by apache.

the class SolrSuggester method init.

/**
   * Uses the <code>config</code> and the <code>core</code> to initialize the underlying 
   * Lucene suggester
   * */
public String init(NamedList<?> config, SolrCore core) {
    LOG.info("init: " + config);
    // read the config
    name = config.get(NAME) != null ? (String) config.get(NAME) : DEFAULT_DICT_NAME;
    sourceLocation = (String) config.get(LOCATION);
    lookupImpl = (String) config.get(LOOKUP_IMPL);
    dictionaryImpl = (String) config.get(DICTIONARY_IMPL);
    String store = (String) config.get(STORE_DIR);
    if (lookupImpl == null) {
        lookupImpl = LookupFactory.DEFAULT_FILE_BASED_DICT;
        LOG.info("No " + LOOKUP_IMPL + " parameter was provided falling back to " + lookupImpl);
    }
    contextFilterQueryAnalyzer = new TokenizerChain(new StandardTokenizerFactory(Collections.EMPTY_MAP), null);
    // initialize appropriate lookup instance
    factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
    lookup = factory.create(config, core);
    if (lookup != null && lookup instanceof Closeable) {
        core.addCloseHook(new CloseHook() {

            @Override
            public void preClose(SolrCore core) {
                try {
                    ((Closeable) lookup).close();
                } catch (IOException e) {
                    LOG.warn("Could not close the suggester lookup.", e);
                }
            }

            @Override
            public void postClose(SolrCore core) {
            }
        });
    }
    // if store directory is provided make it or load up the lookup with its content
    if (store != null && !store.isEmpty()) {
        storeDir = new File(store);
        if (!storeDir.isAbsolute()) {
            storeDir = new File(core.getDataDir() + File.separator + storeDir);
        }
        if (!storeDir.exists()) {
            storeDir.mkdirs();
        } else if (getStoreFile().exists()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("attempt reload of the stored lookup from file " + getStoreFile());
            }
            try {
                lookup.load(new FileInputStream(getStoreFile()));
            } catch (IOException e) {
                LOG.warn("Loading stored lookup data failed, possibly not cached yet");
            }
        }
    }
    // dictionary configuration
    if (dictionaryImpl == null) {
        dictionaryImpl = (sourceLocation == null) ? DictionaryFactory.DEFAULT_INDEX_BASED_DICT : DictionaryFactory.DEFAULT_FILE_BASED_DICT;
        LOG.info("No " + DICTIONARY_IMPL + " parameter was provided falling back to " + dictionaryImpl);
    }
    dictionaryFactory = core.getResourceLoader().newInstance(dictionaryImpl, DictionaryFactory.class);
    dictionaryFactory.setParams(config);
    LOG.info("Dictionary loaded with params: " + config);
    return name;
}
Also used : CloseHook(org.apache.solr.core.CloseHook) TokenizerChain(org.apache.solr.analysis.TokenizerChain) SolrCore(org.apache.solr.core.SolrCore) Closeable(java.io.Closeable) StandardTokenizerFactory(org.apache.lucene.analysis.standard.StandardTokenizerFactory) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 67 with Closeable

use of java.io.Closeable in project lucene-solr by apache.

the class Suggester method init.

@Override
public String init(NamedList config, SolrCore core) {
    LOG.info("init: " + config);
    String name = super.init(config, core);
    threshold = config.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f : (Float) config.get(THRESHOLD_TOKEN_FREQUENCY);
    sourceLocation = (String) config.get(LOCATION);
    lookupImpl = (String) config.get(LOOKUP_IMPL);
    // support the old classnames without -Factory for config file backwards compatibility.
    if (lookupImpl == null || "org.apache.solr.spelling.suggest.jaspell.JaspellLookup".equals(lookupImpl)) {
        lookupImpl = JaspellLookupFactory.class.getName();
    } else if ("org.apache.solr.spelling.suggest.tst.TSTLookup".equals(lookupImpl)) {
        lookupImpl = TSTLookupFactory.class.getName();
    } else if ("org.apache.solr.spelling.suggest.fst.FSTLookup".equals(lookupImpl)) {
        lookupImpl = FSTLookupFactory.class.getName();
    }
    factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
    lookup = factory.create(config, core);
    core.addCloseHook(new CloseHook() {

        @Override
        public void preClose(SolrCore core) {
            if (lookup != null && lookup instanceof Closeable) {
                try {
                    ((Closeable) lookup).close();
                } catch (IOException e) {
                    LOG.warn("Could not close the suggester lookup.", e);
                }
            }
        }

        @Override
        public void postClose(SolrCore core) {
        }
    });
    String store = (String) config.get(STORE_DIR);
    if (store != null) {
        storeDir = new File(store);
        if (!storeDir.isAbsolute()) {
            storeDir = new File(core.getDataDir() + File.separator + storeDir);
        }
        if (!storeDir.exists()) {
            storeDir.mkdirs();
        } else {
            // attempt reload of the stored lookup
            try {
                lookup.load(new FileInputStream(new File(storeDir, factory.storeFileName())));
            } catch (IOException e) {
                LOG.warn("Loading stored lookup data failed", e);
            }
        }
    }
    return name;
}
Also used : CloseHook(org.apache.solr.core.CloseHook) FSTLookupFactory(org.apache.solr.spelling.suggest.fst.FSTLookupFactory) TSTLookupFactory(org.apache.solr.spelling.suggest.tst.TSTLookupFactory) JaspellLookupFactory(org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory) SolrCore(org.apache.solr.core.SolrCore) Closeable(java.io.Closeable) JaspellLookupFactory(org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory) FSTLookupFactory(org.apache.solr.spelling.suggest.fst.FSTLookupFactory) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 68 with Closeable

use of java.io.Closeable in project jackrabbit-oak by apache.

the class RDBDocumentStoreJDBCTest method queryIteratorConsumedTest.

@Test
public void queryIteratorConsumedTest() throws SQLException {
    insertTestResource(this.getClass().getName() + "." + name.getMethodName());
    LogCustomizer customLogs = LogCustomizer.forLogger(RDBDocumentStoreJDBC.class.getName()).enable(Level.DEBUG).contains("Query on ").create();
    customLogs.starting();
    MyConnectionHandler ch = new MyConnectionHandler(super.rdbDataSource);
    RDBTableMetaData tmd = ((RDBDocumentStore) super.ds).getTable(Collection.NODES);
    List<QueryCondition> conditions = Collections.emptyList();
    try {
        Iterator<RDBRow> qi = jdbc.queryAsIterator(ch, tmd, null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions, Integer.MAX_VALUE, null);
        assertTrue(qi instanceof Closeable);
        assertEquals(1, ch.cnt.get());
        while (qi.hasNext()) {
            qi.next();
        }
        assertEquals(0, ch.cnt.get());
        assertEquals("should have a DEBUG level log entry", 1, customLogs.getLogs().size());
    } finally {
        customLogs.finished();
        customLogs = null;
    }
}
Also used : RDBTableMetaData(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.RDBTableMetaData) LogCustomizer(org.apache.jackrabbit.oak.commons.junit.LogCustomizer) Closeable(java.io.Closeable) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Test(org.junit.Test) AbstractDocumentStoreTest(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)

Example 69 with Closeable

use of java.io.Closeable in project jackrabbit-oak by apache.

the class RDBDocumentStoreJDBCTest method queryIteratorNotStartedTest.

@Test
public void queryIteratorNotStartedTest() throws SQLException {
    insertTestResource(this.getClass().getName() + "." + name.getMethodName());
    MyConnectionHandler ch = new MyConnectionHandler(super.rdbDataSource);
    RDBTableMetaData tmd = ((RDBDocumentStore) super.ds).getTable(Collection.NODES);
    List<QueryCondition> conditions = Collections.emptyList();
    Iterator<RDBRow> qi = jdbc.queryAsIterator(ch, tmd, null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions, Integer.MAX_VALUE, null);
    assertTrue(qi instanceof Closeable);
    assertEquals(1, ch.cnt.get());
    Utils.closeIfCloseable(qi);
    assertEquals(0, ch.cnt.get());
}
Also used : RDBTableMetaData(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.RDBTableMetaData) Closeable(java.io.Closeable) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Test(org.junit.Test) AbstractDocumentStoreTest(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)

Example 70 with Closeable

use of java.io.Closeable in project phoenix by apache.

the class Closeables method closeAllQuietly.

public static IOException closeAllQuietly(Iterable<? extends Closeable> iterable) {
    if (iterable == null)
        return null;
    LinkedList<IOException> exceptions = null;
    for (Closeable closeable : iterable) {
        IOException ioe = closeQuietly(closeable);
        if (ioe != null) {
            if (exceptions == null)
                exceptions = new LinkedList<IOException>();
            exceptions.add(ioe);
        }
    }
    IOException ex = MultipleCausesIOException.fromIOExceptions(exceptions);
    return ex;
}
Also used : Closeable(java.io.Closeable) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Aggregations

Closeable (java.io.Closeable)283 Test (org.junit.Test)100 IOException (java.io.IOException)99 ArrayList (java.util.ArrayList)33 File (java.io.File)32 URL (java.net.URL)18 HttpResponse (org.apache.http.HttpResponse)17 HttpClient (org.apache.http.client.HttpClient)17 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)16 Greeter (org.apache.cxf.greeter_control.Greeter)16 HashMap (java.util.HashMap)13 BasicGreeterService (org.apache.cxf.greeter_control.BasicGreeterService)13 VirtualFile (org.jboss.vfs.VirtualFile)12 InputStream (java.io.InputStream)11 PingMeFault (org.apache.cxf.greeter_control.PingMeFault)11 Path (java.nio.file.Path)10 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 Map (java.util.Map)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8