Search in sources :

Example 21 with DataStoreException

use of org.apache.sis.storage.DataStoreException in project sis by apache.

the class Store method close.

/**
 * Closes this data store and releases any underlying resources.
 *
 * @throws DataStoreException if an error occurred while closing this data store.
 */
@Override
public synchronized void close() throws DataStoreException {
    object = null;
    final Closeable in = input(source);
    // Cleared first in case of failure.
    source = null;
    if (in != null)
        try {
            in.close();
        } catch (IOException e) {
            throw new DataStoreException(e);
        }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Closeable(java.io.Closeable) IOException(java.io.IOException)

Example 22 with DataStoreException

use of org.apache.sis.storage.DataStoreException in project tika by apache.

the class GeographicInformationParser method parse.

@Override
public void parse(InputStream inputStream, ContentHandler contentHandler, Metadata metadata, ParseContext parseContext) throws IOException, SAXException, TikaException {
    metadata.set(Metadata.CONTENT_TYPE, geoInfoType);
    DataStore dataStore = null;
    DefaultMetadata defaultMetadata = null;
    XHTMLContentHandler xhtmlContentHandler = new XHTMLContentHandler(contentHandler, metadata);
    TemporaryResources tmp = TikaInputStream.isTikaInputStream(inputStream) ? null : new TemporaryResources();
    try {
        TikaInputStream tikaInputStream = TikaInputStream.get(inputStream, tmp);
        File file = tikaInputStream.getFile();
        dataStore = DataStores.open(file);
        defaultMetadata = new DefaultMetadata(dataStore.getMetadata());
        if (defaultMetadata != null)
            extract(xhtmlContentHandler, metadata, defaultMetadata);
    } catch (UnsupportedStorageException e) {
        throw new TikaException("UnsupportedStorageException", e);
    } catch (DataStoreException e) {
        throw new TikaException("DataStoreException", e);
    } finally {
        if (tmp != null) {
            tmp.dispose();
        }
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) TikaException(org.apache.tika.exception.TikaException) DataStore(org.apache.sis.storage.DataStore) DefaultMetadata(org.apache.sis.metadata.iso.DefaultMetadata) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) File(java.io.File) UnsupportedStorageException(org.apache.sis.storage.UnsupportedStorageException)

Example 23 with DataStoreException

use of org.apache.sis.storage.DataStoreException in project sis by apache.

the class AbstractProvider method probeContent.

/**
 * Returns {@link ProbeResult#SUPPORTED} if the given storage appears to be supported by the data store.
 * Returning {@code SUPPORTED} from this method does not guarantee that reading or writing will succeed,
 * only that there appears to be a reasonable chance of success based on a brief inspection of the storage
 * header.
 *
 * @return {@link ProbeResult#SUPPORTED} if the given storage seems to be readable as a XML file.
 * @throws DataStoreException if an I/O or SQL error occurred.
 */
@Override
public ProbeResult probeContent(final StorageConnector connector) throws DataStoreException {
    /*
         * Usual case. This includes InputStream, DataInput, File, Path, URL, URI.
         */
    final ByteBuffer buffer = connector.getStorageAs(ByteBuffer.class);
    if (buffer != null) {
        if (buffer.remaining() < HEADER.length) {
            return ProbeResult.INSUFFICIENT_BYTES;
        }
        // Quick check for "<?xml " header.
        for (int i = 0; i < HEADER.length; i++) {
            if (buffer.get(i) != HEADER[i]) {
                return ProbeResult.UNSUPPORTED_STORAGE;
            }
        }
        // Now check for a more accurate MIME type.
        buffer.position(HEADER.length);
        final ProbeResult result = new MimeTypeDetector(types) {

            @Override
            int read() {
                if (buffer.hasRemaining()) {
                    return buffer.get();
                }
                insufficientBytes = (buffer.limit() != buffer.capacity());
                return -1;
            }
        }.probeContent();
        buffer.position(0);
        return result;
    }
    /*
         * We should enter in this block only if the user gave us explicitely a Reader.
         * A common case is a StringReader wrapping a String object.
         */
    final Reader reader = connector.getStorageAs(Reader.class);
    if (reader != null)
        try {
            // Quick check for "<?xml " header.
            reader.mark(HEADER.length + READ_AHEAD_LIMIT);
            for (int i = 0; i < HEADER.length; i++) {
                if (reader.read() != HEADER[i]) {
                    reader.reset();
                    return ProbeResult.UNSUPPORTED_STORAGE;
                }
            }
            // Now check for a more accurate MIME type.
            final ProbeResult result = new MimeTypeDetector(types) {

                private int remaining = READ_AHEAD_LIMIT;

                @Override
                int read() throws IOException {
                    return (--remaining >= 0) ? IOUtilities.readCodePoint(reader) : -1;
                }
            }.probeContent();
            reader.reset();
            return result;
        } catch (IOException e) {
            throw new DataStoreException(e);
        }
    return ProbeResult.UNSUPPORTED_STORAGE;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Reader(java.io.Reader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ProbeResult(org.apache.sis.storage.ProbeResult)

Example 24 with DataStoreException

use of org.apache.sis.storage.DataStoreException in project sis by apache.

the class NetcdfStoreProvider method createByReflection.

/**
 * Creates a new netCDF decoder as a wrapper around the UCAR library. This decoder is used only when we can
 * not create our embedded netCDF decoder. This method uses reflection for creating the wrapper, in order
 * to keep the UCAR dependency optional.
 *
 * @param  input      the netCDF file object of filename string from which to read data.
 * @param  isUCAR     {@code true} if {@code input} is an instance of the UCAR {@link ucar.nc2.NetcdfFile} object,
 *                    or {@code false} if it is the filename as a {@code String}.
 * @param  geomlib    the library for geometric objects, or {@code null} for the default.
 * @param  listeners  where to send the warnings.
 * @return the {@link DecoderWrapper} instance for the given input, or {@code null} if the input type is not recognized.
 * @throws IOException if an error occurred while opening the netCDF file.
 * @throws DataStoreException if a logical error (other than I/O) occurred.
 */
private static Decoder createByReflection(final Object input, final boolean isUCAR, final GeometryLibrary geomlib, final WarningListeners<DataStore> listeners) throws IOException, DataStoreException {
    ensureInitialized(true);
    /*
         * Get the appropriate constructor for the isUCAR argument. This constructor will be null
         * if the above code failed to load the UCAR library. Otherwise, instantiate the wrapper.
         */
    final Constructor<? extends Decoder> constructor;
    final Class<?> expectedType;
    if (isUCAR) {
        constructor = createFromUCAR;
        expectedType = netcdfFileClass;
    } else {
        constructor = createFromPath;
        expectedType = String.class;
    }
    if (constructor == null || !expectedType.isInstance(input)) {
        return null;
    }
    try {
        return constructor.newInstance(input, geomlib, listeners);
    } catch (InvocationTargetException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof IOException)
            throw (IOException) cause;
        if (cause instanceof DataStoreException)
            throw (DataStoreException) cause;
        if (cause instanceof RuntimeException)
            throw (RuntimeException) cause;
        if (cause instanceof Error)
            throw (Error) cause;
        // Should never happen actually.
        throw new UndeclaredThrowableException(cause);
    } catch (ReflectiveOperationException e) {
        // Should never happen (shall be verified by the JUnit tests).
        throw new AssertionError(e);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 25 with DataStoreException

use of org.apache.sis.storage.DataStoreException in project sis by apache.

the class NetcdfStoreProvider method decoder.

/**
 * Creates a decoder for the given input. This method invokes
 * {@link StorageConnector#closeAllExcept(Object)} after the decoder has been created.
 *
 * @param  listeners  where to send the warnings.
 * @param  connector  information about the input (file, input stream, <i>etc.</i>)
 * @return the decoder for the given input, or {@code null} if the input type is not recognized.
 * @throws IOException if an error occurred while opening the netCDF file.
 * @throws DataStoreException if a logical error (other than I/O) occurred.
 */
static Decoder decoder(final WarningListeners<DataStore> listeners, final StorageConnector connector) throws IOException, DataStoreException {
    final GeometryLibrary geomlib = connector.getOption(OptionKey.GEOMETRY_LIBRARY);
    Decoder decoder;
    Object keepOpen;
    final ChannelDataInput input = connector.getStorageAs(ChannelDataInput.class);
    if (input != null)
        try {
            decoder = new ChannelDecoder(input, connector.getOption(OptionKey.ENCODING), geomlib, listeners);
            keepOpen = input;
        } catch (DataStoreException e) {
            final String path = connector.getStorageAs(String.class);
            if (path != null)
                try {
                    decoder = createByReflection(path, false, geomlib, listeners);
                    keepOpen = path;
                } catch (IOException | DataStoreException s) {
                    e.addSuppressed(s);
                }
            throw e;
        }
    else {
        keepOpen = connector.getStorage();
        decoder = createByReflection(keepOpen, true, geomlib, listeners);
    }
    connector.closeAllExcept(keepOpen);
    return decoder;
}
Also used : GeometryLibrary(org.apache.sis.setup.GeometryLibrary) ChannelDecoder(org.apache.sis.internal.netcdf.impl.ChannelDecoder) DataStoreException(org.apache.sis.storage.DataStoreException) Decoder(org.apache.sis.internal.netcdf.Decoder) ChannelDecoder(org.apache.sis.internal.netcdf.impl.ChannelDecoder) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput)

Aggregations

DataStoreException (org.apache.sis.storage.DataStoreException)39 IOException (java.io.IOException)16 DataStore (org.apache.sis.storage.DataStore)8 Path (java.nio.file.Path)6 DataStoreContentException (org.apache.sis.storage.DataStoreContentException)5 FeatureSet (org.apache.sis.storage.FeatureSet)5 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)4 IllegalNameException (org.apache.sis.storage.IllegalNameException)4 Resource (org.apache.sis.storage.Resource)4 BackingStoreException (org.apache.sis.util.collection.BackingStoreException)4 FactoryException (org.opengis.util.FactoryException)4 Reader (java.io.Reader)3 UncheckedIOException (java.io.UncheckedIOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 URISyntaxException (java.net.URISyntaxException)3 ByteBuffer (java.nio.ByteBuffer)3 ProbeResult (org.apache.sis.storage.ProbeResult)3 BufferedReader (java.io.BufferedReader)2 Closeable (java.io.Closeable)2 File (java.io.File)2