sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1515621 - in /sis/branches/JDK7: application/sis-console/src/main/java/org/apache/sis/console/ application/sis-console/src/main/resources/org/apache/sis/console/ core/sis-utility/src/main/java/org/apache/sis/util/ storage/sis-storage/src/m...
Date Mon, 19 Aug 2013 21:40:00 GMT
Author: desruisseaux
Date: Mon Aug 19 21:39:59 2013
New Revision: 1515621

URL: http://svn.apache.org/r1515621
Log:
Connect our MIME type detection mechanism to the system-wide java.nio.Files.probeContentType(Path).
Make it available from the command-line.

Added:
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
  (with props)
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java
  (with props)
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java
  (with props)
    sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector
  (with props)
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
  (with props)
Modified:
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
    sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
    sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreRegistry.java
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreTest.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -35,6 +35,7 @@ import org.apache.sis.util.logging.Monol
  * <blockquote><table class="compact">
  * <tr><td>{@code help}     </td><td>Show a help overview.</td></tr>
  * <tr><td>{@code about}    </td><td>Show information about Apache
SIS and system configuration.</td></tr>
+ * <tr><td>{@code mime-type}</td><td>Show MIME type for the given
file.</td></tr>
  * <tr><td>{@code metadata} </td><td>Show metadata information for
the given file.</td></tr>
  * </table></blockquote>
  *
@@ -147,9 +148,10 @@ public final class Command {
         } else {
             commandName = commandName.toLowerCase(Locale.US);
             switch (commandName) {
-                case "help":     command = new HelpSC    (commandIndex, args); break;
-                case "about":    command = new AboutSC   (commandIndex, args); break;
-                case "metadata": command = new MetadataSC(commandIndex, args); break;
+                case "help":      command = new HelpSC    (commandIndex, args); break;
+                case "about":     command = new AboutSC   (commandIndex, args); break;
+                case "mime-type": command = new MimeTypeSC(commandIndex, args); break;
+                case "metadata":  command = new MetadataSC(commandIndex, args); break;
                 default: throw new InvalidCommandException(Errors.format(
                             Errors.Keys.UnknownCommand_1, commandName), commandName);
             }

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -39,6 +39,7 @@ final class HelpSC extends SubCommand {
     private static final String[] COMMANDS = {
         "help",
         "about",
+        "mime-type",
         "metadata"
     };
 

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -39,7 +39,7 @@ import org.apache.sis.xml.XML;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.4
  * @module
  */
 final class MetadataSC extends SubCommand {
@@ -53,9 +53,7 @@ final class MetadataSC extends SubComman
     /**
      * Prints metadata information.
      *
-     * @todo NetCDF data store is hard-coded for now. Will need a dynamic mechanism in the
future.
-     *
-     * @throws DataStoreException If an error occurred while reading the NetCDF file.
+     * @throws DataStoreException If an error occurred while reading the file.
      * @throws JAXBException If an error occurred while producing the XML output.
      * @throws IOException Should never happen, since we are appending to a print writer.
      */

Added: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java?rev=1515621&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
(added)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+import java.util.EnumSet;
+import java.io.IOException;
+import org.apache.sis.storage.DataStores;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.util.resources.Errors;
+
+// Related to JDK7.
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.InvalidPathException;
+
+
+/**
+ * The "mime-type" subcommand.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ *
+ * @see Files#probeContentType(Path)
+ * @see DataStores#probeContentType(Object)
+ */
+final class MimeTypeSC extends SubCommand {
+    /**
+     * Creates the {@code "mime-type"} sub-command.
+     */
+    MimeTypeSC(final int commandIndex, final String... args) throws InvalidOptionException
{
+        super(commandIndex, args, EnumSet.of(Option.ENCODING, Option.HELP));
+    }
+
+    /**
+     * Prints mime-type information.
+     *
+     * @throws IOException If an error occurred while reading the file.
+     */
+    @Override
+    public int run() throws InvalidOptionException, IOException, DataStoreException {
+        if (hasUnexpectedFileCount(1, 1)) {
+            return Command.INVALID_ARGUMENT_EXIT_CODE;
+        }
+        final String file = files.get(0);
+        String type;
+        try {
+            type = Files.probeContentType(Paths.get(file));
+        } catch (InvalidPathException e) {
+            type = DataStores.probeContentType(file);
+        } catch (NoSuchFileException e) {
+            err.println(Errors.format(Errors.Keys.CanNotOpen_1, file));
+            return Command.IO_EXCEPTION_EXIT_CODE;
+        }
+        if (type != null) {
+            out.println(type);
+            out.flush();
+        }
+        return 0;
+    }
+}

Propchange: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands.properties
[ISO-8859-1] Mon Aug 19 21:39:59 2013
@@ -4,4 +4,5 @@ Usage=Usage: sis <command> [options] [fi
 
 help=Show a help overview.
 about=Show information about Apache SIS and system configuration.
+mime-type=Show MIME type for the given file.
 metadata=Show metadata information for the given file.

Modified: sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/resources/org/apache/sis/console/Commands_fr.properties
[ISO-8859-1] Mon Aug 19 21:39:59 2013
@@ -4,4 +4,5 @@ Usage=Usage: sis <commande> [options] [f
 
 help=Affiche un écran d\u2019aide.
 about=Affiche des informations à propos de Apache SIS et de la configuration du système.
+mime-type=Affiche le type MIME du fichier spécifié.
 metadata=Affiche les méta-données du fichier spécifié.

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java [UTF-8]
(original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java [UTF-8]
Mon Aug 19 21:39:59 2013
@@ -16,8 +16,8 @@
  */
 package org.apache.sis.util;
 
-import java.util.Set;
-import java.util.HashSet;
+import java.util.List;
+import java.util.ArrayList;
 import java.util.Locale;
 import java.sql.SQLException;
 import org.apache.sis.internal.util.LocalizedException;
@@ -30,7 +30,7 @@ import static org.apache.sis.util.CharSe
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.3 (derived from geotk-2.0)
- * @version 0.3
+ * @version 0.4
  * @module
  */
 public final class Exceptions extends Static {
@@ -124,34 +124,24 @@ public final class Exceptions extends St
      *         and no exception provide a message.
      */
     public static String formatChainedMessages(final Locale locale, String header, Throwable
cause) {
-        Set<CharSequence> done = null;
+        List<String> previousLines = null;
         String lineSeparator = null;
         StringBuilder buffer = null;
         while (cause != null) {
             final String message = trimWhitespaces(getLocalizedMessage(cause, locale));
             if (message != null && !message.isEmpty()) {
                 if (buffer == null) {
-                    done = new HashSet<>();
                     buffer = new StringBuilder(128);
+                    previousLines = new ArrayList<>(4);
                     lineSeparator = System.lineSeparator();
                     header = trimWhitespaces(header);
                     if (header != null && !header.isEmpty()) {
                         buffer.append(header);
-                        done.add(header);
-                        /*
-                         * The folowing is for avoiding to repeat the same message in the
-                         * common case where the header contains the exception class name
-                         * followed by the message, as in:
-                         *
-                         * FooException: InnerException: the inner message.
-                         */
-                        int s=0;
-                        while ((s=header.indexOf(':', s)) >= 0) {
-                            done.add(trimWhitespaces(header, ++s, header.length()));
-                        }
+                        previousLines.add(header);
                     }
                 }
-                if (done.add(message)) {
+                if (!contains(previousLines, message)) {
+                    previousLines.add(message);
                     if (buffer.length() != 0) {
                         buffer.append(lineSeparator);
                     }
@@ -172,4 +162,17 @@ public final class Exceptions extends St
         }
         return header;
     }
+
+    /**
+     * Returns {@code true} if a previous line contains the given exception message.
+     */
+    private static boolean contains(final List<String> previousLines, final String
message) {
+        for (int i=previousLines.size(); --i>=0;) {
+            final int p = previousLines.get(i).indexOf(message);
+            if (p >= 0) {
+                return true;
+            }
+        }
+        return false;
+    }
 }

Added: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java?rev=1515621&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.storage;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.spi.FileTypeDetector;
+import org.apache.sis.storage.DataStores;
+import org.apache.sis.storage.DataStoreException;
+
+
+/**
+ * A {@code java.nio.file} service to be registered for probing content type.
+ * The {@link #probeContentType(Path)} will be automatically invoked by
+ * {@link java.nio.file.Files#probeContentType(Path)}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+public class StoreTypeDetector extends FileTypeDetector {
+    /**
+     * Constructor for {@link java.util.ServiceLoader}.
+     */
+    public StoreTypeDetector() {
+    }
+
+    /**
+     * Probes the given file by delegating to {@link DataStores#probeContentType(Object)}.
+     *
+     * @param  path the path to the file to probe.
+     * @return The content type or {@code null} if the file type is not recognized.
+     * @throws IOException if an I/O error occurs while reading the file.
+     *
+     * @see java.nio.file.Files#probeContentType(Path)
+     */
+    @Override
+    public String probeContentType(final Path path) throws IOException {
+        try {
+            return DataStores.probeContentType(path);
+        } catch (DataStoreException e) {
+            final Throwable cause = e.getCause();
+            if (cause instanceof IOException) {
+                throw (IOException) cause;
+            }
+            throw new IOException(e);
+        }
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreTypeDetector.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreRegistry.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreRegistry.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreRegistry.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreRegistry.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -66,6 +66,19 @@ final class DataStoreRegistry {
     }
 
     /**
+     * Returns the MIME type of the storage file format, or {@code null} if unknown or not
applicable.
+     *
+     * @param  storage The input/output object as a URL, file, image input stream, <i>etc.</i>.
+     * @return The storage MIME type, or {@code null} if unknown or not applicable.
+     * @throws DataStoreException If an error occurred while opening the storage.
+     */
+    public String probeContentType(final Object storage) throws DataStoreException {
+        ArgumentChecks.ensureNonNull("storage", storage);
+        final ProbeProviderPair p = lookup(storage, false);
+        return (p != null) ? p.probe.getMimeType() : null;
+    }
+
+    /**
      * Creates a {@link DataStore} for reading the given storage.
      * The {@code storage} argument can be any of the following types:
      *
@@ -84,16 +97,28 @@ final class DataStoreRegistry {
      * @throws UnsupportedStorageException if no {@link DataStoreProvider} is found for a
given storage object.
      * @throws DataStoreException If an error occurred while opening the storage.
      */
-    public DataStore open(final Object storage) throws DataStoreException {
+    public DataStore open(final Object storage) throws UnsupportedStorageException, DataStoreException
{
         ArgumentChecks.ensureNonNull("storage", storage);
+        return lookup(storage, true).store;
+    }
+
+    /**
+     * Implementation of {@link #probeContentType(Object)} and {@link #open(Object)}.
+     *
+     * @param  storage The input/output object as a URL, file, image input stream, <i>etc.</i>.
+     * @param  open {@code true} for creating a {@link DataStore}, or {@code false} if not
needed.
+     * @throws UnsupportedStorageException if no {@link DataStoreProvider} is found for a
given storage object.
+     * @throws DataStoreException If an error occurred while opening the storage.
+     */
+    private ProbeProviderPair lookup(final Object storage, final boolean open) throws DataStoreException
{
         StorageConnector connector;
         if (storage instanceof StorageConnector) {
             connector = (StorageConnector) storage;
         } else {
             connector = new StorageConnector(storage);
         }
-        DataStoreProvider provider = null;
-        List<DataStoreProvider> deferred = null;
+        ProbeProviderPair selected = null;
+        List<ProbeProviderPair> deferred = null;
         try {
             /*
              * All usages of 'loader' and its 'providers' iterator must be protected in a
synchronized block,
@@ -101,19 +126,19 @@ final class DataStoreRegistry {
              * possible for less contention. In particular, the probeContent(connector) method
call may be costly.
              */
             final Iterator<DataStoreProvider> providers;
-            DataStoreProvider candidate;
+            DataStoreProvider provider;
             synchronized (loader) {
                 providers = loader.iterator();
-                candidate = providers.hasNext() ? providers.next() : null;
+                provider = providers.hasNext() ? providers.next() : null;
             }
-            while (candidate != null) {
-                final ProbeResult probe = candidate.probeContent(connector);
+            while (provider != null) {
+                final ProbeResult probe = provider.probeContent(connector);
                 if (probe.isSupported()) {
                     /*
                      * Stop at the first provider claiming to be able to read the storage.
                      * Do not iterate over the list of deferred providers (if any).
                      */
-                    provider = candidate;
+                    selected = new ProbeProviderPair(provider, probe);
                     deferred = null;
                     break;
                 }
@@ -126,7 +151,7 @@ final class DataStoreRegistry {
                     if (deferred == null) {
                         deferred = new LinkedList<>();
                     }
-                    deferred.add(candidate);
+                    deferred.add(new ProbeProviderPair(provider, probe));
                 } else if (ProbeResult.UNDETERMINED.equals(probe)) {
                     /*
                      * If a provider doesn't know whether it can open the given storage,
@@ -136,10 +161,10 @@ final class DataStoreRegistry {
                      *       provider.open(connector) in a try … catch block because it
may leave
                      *       the StorageConnector in an invalid state in case of failure.
                      */
-                    provider = candidate;
+                    selected = new ProbeProviderPair(provider, probe);
                 }
                 synchronized (loader) {
-                    candidate = providers.hasNext() ? providers.next() : null;
+                    provider = providers.hasNext() ? providers.next() : null;
                 }
             }
             /*
@@ -149,16 +174,16 @@ final class DataStoreRegistry {
              */
             if (deferred != null) {
 search:         while (!deferred.isEmpty() && connector.prefetch()) {
-                    for (final Iterator<DataStoreProvider> it=deferred.iterator();
it.hasNext();) {
-                        candidate = it.next();
-                        final ProbeResult probe = candidate.probeContent(connector);
-                        if (probe.isSupported()) {
-                            provider = candidate;
+                    for (final Iterator<ProbeProviderPair> it=deferred.iterator();
it.hasNext();) {
+                        final ProbeProviderPair p = it.next();
+                        p.probe = provider.probeContent(connector);
+                        if (p.probe.isSupported()) {
+                            selected = p;
                             break search;
                         }
-                        if (!ProbeResult.INSUFFICIENT_BYTES.equals(probe)) {
-                            if (ProbeResult.UNDETERMINED.equals(probe)) {
-                                provider = candidate; // To be used only if we don't find
a better match.
+                        if (!ProbeResult.INSUFFICIENT_BYTES.equals(p.probe)) {
+                            if (ProbeResult.UNDETERMINED.equals(p.probe)) {
+                                selected = p; // To be used only if we don't find a better
match.
                             }
                             it.remove(); // UNSUPPORTED_* or UNDETERMINED: do not try again
those providers.
                         }
@@ -172,16 +197,18 @@ search:         while (!deferred.isEmpty
              * shall avoid the UNDETERMINED value as much as possible (this value should
be used
              * only for RAW image format).
              */
-            if (provider != null) {
-                final DataStore data = provider.open(connector);
+            if (open && selected != null) {
+                selected.store = selected.provider.open(connector);
                 connector = null; // For preventing it to be closed.
-                return data;
             }
         } finally {
             if (connector != null && connector != storage) {
                 connector.closeAllExcept(null);
             }
         }
-        throw new UnsupportedStorageException(Errors.format(Errors.Keys.UnknownFormatFor_1,
connector.getStorageName()));
+        if (open && selected == null) {
+            throw new UnsupportedStorageException(Errors.format(Errors.Keys.UnknownFormatFor_1,
connector.getStorageName()));
+        }
+        return selected;
     }
 }

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStores.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -62,6 +62,33 @@ public final class DataStores extends St
     }
 
     /**
+     * Returns the registry, created when first needed.
+     */
+    private static DataStoreRegistry registry() {
+        DataStoreRegistry r = registry;
+        if (r == null) {
+            synchronized (DataStores.class) {
+                r = registry;
+                if (r == null) {
+                    registry = r = new DataStoreRegistry();
+                }
+            }
+        }
+        return r;
+    }
+
+    /**
+     * Returns the MIME type of the storage file format, or {@code null} if unknown or not
applicable.
+     *
+     * @param  storage The input/output object as a URL, file, image input stream, <i>etc.</i>.
+     * @return The storage MIME type, or {@code null} if unknown or not applicable.
+     * @throws DataStoreException If an error occurred while opening the storage.
+     */
+    public static String probeContentType(final Object storage) throws DataStoreException
{
+        return registry().probeContentType(storage);
+    }
+
+    /**
      * Creates a {@link DataStore} for the given storage.
      * The {@code storage} argument can be any of the following types:
      *
@@ -80,16 +107,7 @@ public final class DataStores extends St
      * @throws UnsupportedStorageException if no {@link DataStoreProvider} is found for a
given storage object.
      * @throws DataStoreException If an error occurred while opening the storage.
      */
-    public static DataStore open(final Object storage) throws DataStoreException {
-        DataStoreRegistry r = registry;
-        if (r == null) {
-            synchronized (DataStores.class) {
-                r = registry;
-                if (r == null) {
-                    registry = r = new DataStoreRegistry();
-                }
-            }
-        }
-        return r.open(storage);
+    public static DataStore open(final Object storage) throws UnsupportedStorageException,
DataStoreException {
+        return registry().open(storage);
     }
 }

Added: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java?rev=1515621&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.storage;
+
+
+/**
+ * A pair of {@link ProbeResult} and {@link DataStoreProvider},
+ * for internal usage by {@link DataStoreRegistry} only.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+final class ProbeProviderPair {
+    /**
+     * The provider to use for probing a file.
+     */
+    final DataStoreProvider provider;
+
+    /**
+     * The result of the call to {@link DataStoreProvider#probeContent(StorageConnector)}.
+     */
+    ProbeResult probe;
+
+    /**
+     * A data store created by the provider.
+     */
+    DataStore store;
+
+    /**
+     * Creates a new pair.
+     */
+    ProbeProviderPair(final DataStoreProvider provider, final ProbeResult probe) {
+        this.provider = provider;
+        this.probe    = probe;
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeProviderPair.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Added: sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector?rev=1515621&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -0,0 +1 @@
+org.apache.sis.internal.storage.StoreTypeDetector

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreTest.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreTest.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -46,7 +46,7 @@ public final strictfp class XMLStoreTest
     /**
      * The metadata to unmarshal.
      */
-    static final String XML =
+    public static final String XML =
             "<?xml version=\"1.0\" standalone=\"yes\"?>\n" +
             "<gmd:MD_Metadata\n" +
             "  xmlns:gmd = \"" + Namespaces.GMD + "\"\n"  +

Added: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java?rev=1515621&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.storage;
+
+import java.io.StringReader;
+import org.apache.sis.internal.storage.xml.XMLStoreTest;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link DataStores}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+@DependsOn(XMLStoreTest.class)
+public final strictfp class DataStoresTest extends TestCase {
+    /**
+     * Tests {@link DataStores#probeContentType(Object)}.
+     *
+     * @throws DataStoreException Should never happen.
+     */
+    @Test
+    public void testProbeContentType() throws DataStoreException {
+        final String type = DataStores.probeContentType(new StringReader(XMLStoreTest.XML));
+        assertEquals("application/vnd.iso.19139+xml", type);
+    }
+
+    /**
+     * Tests {@link DataStores#open(Object)}.
+     *
+     * @throws DataStoreException Should never happen.
+     */
+    @Test
+    public void testOpen() throws DataStoreException {
+        final DataStore store = DataStores.open(new StringReader(XMLStoreTest.XML));
+        assertFalse(store.getMetadata().getContacts().isEmpty());
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java?rev=1515621&r1=1515620&r2=1515621&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] Mon Aug 19 21:39:59 2013
@@ -38,6 +38,7 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.storage.xml.MimeTypeDetectorTest.class,
     org.apache.sis.internal.storage.xml.XMLStoreProviderTest.class,
     org.apache.sis.internal.storage.xml.XMLStoreTest.class,
+    org.apache.sis.storage.DataStoresTest.class,
     org.apache.sis.index.GeoHashCoderTest.class
 })
 public final strictfp class StorageTestSuite extends TestSuite {



Mime
View raw message