Author: desruisseaux
Date: Wed Jul 31 21:27:16 2013
New Revision: 1509040

URL: http://svn.apache.org/r1509040
Log:
Replace usage of the JDK7 java.nio.file.OpenOption by our own CodeList for the 
following reasons:
- While java.nio.file.OpenOptions are mostly the same than the DataStore 
OpenOption for now,
  we have no guaranteed that it will still be the case in the future.
- Sharing the same OpenOption between file and DataStore would allow to pass 
DataStore-specific
  OpenOption to java.nio.file, or java.nio.file-specific OpenOption to 
DataStores. It may not
  really hurt since java.nio.file ignores unknown options, but it nevertheless 
may increase the
  risk of confusion.
- java.nio.file is JDK7 specific while we still have a JDK6 branch.

Added:
    
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/OpenOption.java
   (with props)
Modified:
    
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java
    
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/NetcdfStoreProviderTest.java
    
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreProvider.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

Modified: 
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java?rev=1509040&r1=1509039&r2=1509040&view=diff
==============================================================================
--- 
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java
 [UTF-8] Wed Jul 31 21:27:16 2013
@@ -20,8 +20,6 @@ import java.util.Set;
 import java.util.Collections;
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.nio.file.OpenOption;
-import java.nio.file.StandardOpenOption;
 import java.lang.reflect.Method;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
@@ -36,6 +34,7 @@ import org.apache.sis.storage.DataStore;
 import org.apache.sis.storage.DataStoreProvider;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.OpenOption;
 import org.apache.sis.util.logging.WarningListeners;
 import org.apache.sis.util.ThreadSafe;
 
@@ -101,7 +100,7 @@ public class NetcdfStoreProvider extends
     /**
      * The open options of NetCDF files.
      */
-    private static final Set<OpenOption> OPTIONS = 
Collections.<OpenOption>singleton(StandardOpenOption.READ);
+    private static final Set<OpenOption> OPTIONS = 
Collections.<OpenOption>singleton(OpenOption.READ);
 
     /**
      * Creates a new provider.
@@ -126,7 +125,7 @@ public class NetcdfStoreProvider extends
         final ByteBuffer buffer = storage.getStorageAs(ByteBuffer.class);
         if (buffer != null) {
             if (buffer.remaining() < Integer.SIZE / Byte.SIZE) {
-                return null;
+                return Collections.singleton(OpenOption.UNKNOWN);
             }
             final int header = buffer.getInt(buffer.position());
             if ((header & 0xFFFFFF00) == ChannelDecoder.MAGIC_NUMBER) {

Modified: 
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/NetcdfStoreProviderTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/NetcdfStoreProviderTest.java?rev=1509040&r1=1509039&r2=1509040&view=diff
==============================================================================
--- 
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/NetcdfStoreProviderTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/NetcdfStoreProviderTest.java
 [UTF-8] Wed Jul 31 21:27:16 2013
@@ -18,7 +18,6 @@ package org.apache.sis.storage.netcdf;
 
 import java.util.Collections;
 import java.io.IOException;
-import java.nio.file.StandardOpenOption;
 import ucar.nc2.NetcdfFile;
 import org.opengis.wrapper.netcdf.IOTestCase;
 import org.apache.sis.internal.netcdf.TestCase;
@@ -26,6 +25,7 @@ import org.apache.sis.internal.netcdf.De
 import org.apache.sis.internal.netcdf.ucar.DecoderWrapper;
 import org.apache.sis.internal.netcdf.impl.ChannelDecoder;
 import org.apache.sis.internal.netcdf.impl.ChannelDecoderTest;
+import org.apache.sis.storage.OpenOption;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.test.DependsOn;
@@ -56,7 +56,7 @@ public final strictfp class NetcdfStoreP
     public void testCanOpenFromStream() throws DataStoreException {
         final StorageConnector c = new 
StorageConnector(IOTestCase.class.getResourceAsStream(NCEP));
         final NetcdfStoreProvider provider = new NetcdfStoreProvider();
-        assertEquals(Collections.singleton(StandardOpenOption.READ), 
provider.getOpenCapabilities(c));
+        assertEquals(Collections.singleton(OpenOption.READ), 
provider.getOpenCapabilities(c));
         c.closeAllExcept(null);
     }
 
@@ -71,7 +71,7 @@ public final strictfp class NetcdfStoreP
         final NetcdfFile file = open(NCEP);
         final StorageConnector c = new StorageConnector(file);
         final NetcdfStoreProvider provider = new NetcdfStoreProvider();
-        assertEquals(Collections.singleton(StandardOpenOption.READ), 
provider.getOpenCapabilities(c));
+        assertEquals(Collections.singleton(OpenOption.READ), 
provider.getOpenCapabilities(c));
         file.close();
     }
 

Modified: 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreProvider.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreProvider.java?rev=1509040&r1=1509039&r2=1509040&view=diff
==============================================================================
--- 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreProvider.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreProvider.java
 [UTF-8] Wed Jul 31 21:27:16 2013
@@ -17,8 +17,6 @@
 package org.apache.sis.storage;
 
 import java.util.Set;
-import java.nio.file.OpenOption;
-import java.nio.file.StandardOpenOption;
 import org.apache.sis.util.ThreadSafe;
 
 
@@ -63,23 +61,23 @@ public abstract class DataStoreProvider 
     }
 
     /**
-     * Returns a non-empty set if the given storage appears to be supported by 
the {@code DataStore}.
-     * Returning a non-empty set 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
-     * {@linkplain StorageConnector#getStorage() storage object} or contents.
-     *
-     * <p>If the given storage is supported, then the returned set shall 
contain at least one of the
-     * following values:</p>
+     * Indicates how the given storage can be opened.
+     * The set returned by this method will fall in one of the following cases:
      *
-     * <table class="sis">
-     *   <tr><th>Value</th>                                 
<th>Meaning</th></tr>
-     *   <tr><td>{@link StandardOpenOption#READ}</td>       <td>Can read data 
from the given storage.</td></tr>
-     *   <tr><td>{@link StandardOpenOption#WRITE}</td>      <td>Can overwrite 
existing data.</td></tr>
-     *   <tr><td>{@link StandardOpenOption#APPEND}</td>     <td>Can write new 
data.</td></tr>
-     *   <tr><td>{@link StandardOpenOption#CREATE_NEW}</td> <td>Can create a 
new storage at the given location.</td></tr>
-     * </table>
+     * <ul>
+     *   <li>If the {@code DataStore} managed by this provider can not open 
the given storage,
+     *       then this method returns an empty set.</li>
+     *   <li>Otherwise if this method does not have enough information for 
determining the
+     *       open capabilities, then this method returns a singleton 
containing only the
+     *       {@link OpenOption#UNKNOWN} value.</li>
+     *   <li>Otherwise this method returns a set containing at least one, and 
possibly many, of
+     *       {@link OpenOption#READ}, {@link OpenOption#WRITE WRITE}, {@link 
OpenOption#APPEND APPEND},
+     *       {@link OpenOption#CREATE CREATE} or implementation-specific 
values.</li>
+     * </ul>
      *
-     * Other values may be present at implementation choice.
+     * Note that the later case 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
+     * {@linkplain StorageConnector#getStorage() storage object} or contents.
      *
      * {@section Implementation note}
      * Implementations will typically check the first bytes of the stream for 
a "magic number" associated
@@ -90,10 +88,10 @@ public abstract class DataStoreProvider 
      *         final ByteBuffer buffer = 
storage.getStorageAs(ByteBuffer.class);
      *         if (buffer != null) {
      *             if (buffer.remaining() < Integer.SIZE / Byte.SIZE) {
-     *                 return null; // See notes below.
+     *                 return Collections.singleton(OpenOption.UNKNOWN);
      *             }
      *             if (buffer.getInt(buffer.position()) == MAGIC_NUMBER) {
-     *                 return EnumSet.of(StandardOpenOption.READ);
+     *                 return Collections.singleton(OpenOption.READ);
      *             }
      *         }
      *         return Collections.emptySet();
@@ -134,7 +132,7 @@ public abstract class DataStoreProvider 
     @Deprecated
     public Boolean canOpen(StorageConnector storage) throws DataStoreException 
{
         final Set<OpenOption> options = getOpenCapabilities(storage);
-        return (options == null) ? null : 
options.contains(StandardOpenOption.READ);
+        return (options == null) ? null : options.contains(OpenOption.READ);
     }
 
     /**

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=1509040&r1=1509039&r2=1509040&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] Wed Jul 31 21:27:16 2013
@@ -18,7 +18,6 @@ package org.apache.sis.storage;
 
 import java.util.Set;
 import java.util.ServiceLoader;
-import java.nio.file.OpenOption;
 import org.apache.sis.util.ThreadSafe;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
@@ -79,8 +78,7 @@ final class DataStoreRegistry {
      * </ul>
      *
      * @param  storage The input/output object as a URL, file, image input 
stream, <i>etc.</i>.
-     * @param  options The open options. Shall contain at least one element,
-     *                 typically {@link java.nio.file.StandardOpenOption#READ}.
+     * @param  options The open options. Shall contain at least one element, 
typically {@link OpenOption#READ}.
      * @return The object to use for reading geospatial data from the given 
storage.
      * @throws UnsupportedStorageException if no {@link DataStoreProvider} is 
found for a given storage object.
      * @throws DataStoreException If an error occurred while opening the 
storage.
@@ -91,6 +89,10 @@ final class DataStoreRegistry {
         if (options.isEmpty()) {
             throw new 
IllegalArgumentException(Errors.format(Errors.Keys.EmptyArgument_1, "options"));
         }
+        if (options.contains(OpenOption.UNKNOWN)) {
+            throw new IllegalArgumentException(Errors.format(
+                    Errors.Keys.IllegalArgumentValue_2, "options", 
OpenOption.UNKNOWN));
+        }
         StorageConnector connector;
         if (storage instanceof StorageConnector) {
             connector = (StorageConnector) storage;
@@ -102,7 +104,7 @@ final class DataStoreRegistry {
             synchronized (loader) {
                 for (final DataStoreProvider candidate : loader) {
                     final Set<OpenOption> capabilities = 
candidate.getOpenCapabilities(connector);
-                    if (capabilities == null) {
+                    if (capabilities.contains(OpenOption.UNKNOWN)) {
                         // TODO: not enough information.
                     } else if (capabilities.containsAll(options)) {
                         provider = candidate;

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=1509040&r1=1509039&r2=1509040&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] Wed Jul 31 21:27:16 2013
@@ -18,8 +18,6 @@ package org.apache.sis.storage;
 
 import java.util.Set;
 import java.util.Collections;
-import java.nio.file.OpenOption;
-import java.nio.file.StandardOpenOption;
 import org.apache.sis.util.Static;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.SystemListener;
@@ -62,7 +60,7 @@ public final class DataStores extends St
     /**
      * The options for opening storage in read mode.
      */
-    private static final Set<OpenOption> READ = 
Collections.<OpenOption>singleton(StandardOpenOption.READ);
+    private static final Set<OpenOption> READ = 
Collections.singleton(OpenOption.READ);
 
     /**
      * Do not allow instantiation of this class.
@@ -72,7 +70,7 @@ public final class DataStores extends St
 
     /**
      * Creates a {@link DataStore} for the given storage using its default set 
of {@code OpenOption}s.
-     * The data store is guaranteed to be opened at least with {@link 
StandardOpenOption#READ}.
+     * The data store is guaranteed to be opened at least with {@link 
OpenOption#READ}.
      * Whether the data store has also write or append capabilities is 
implementation dependent.
      *
      * <p>The {@code storage} argument can be any of the following types:</p>
@@ -111,7 +109,7 @@ public final class DataStores extends St
      * </ul>
      *
      * @param  storage The input/output object as a URL, file, image input 
stream, <i>etc.</i>.
-     * @param  options The open options. Shall contain at least one element, 
typically {@link StandardOpenOption#READ}.
+     * @param  options The open options. Shall contain at least one element, 
typically {@link OpenOption#READ}.
      * @return The object to use for reading geospatial data from the given 
storage.
      * @throws UnsupportedStorageException if no {@link DataStoreProvider} is 
found for a given storage object.
      * @throws DataStoreException If an error occurred while opening the 
storage.

Added: 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/OpenOption.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/OpenOption.java?rev=1509040&view=auto
==============================================================================
--- 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/OpenOption.java
 (added)
+++ 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/OpenOption.java
 [UTF-8] Wed Jul 31 21:27:16 2013
@@ -0,0 +1,141 @@
+/*
+ * 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.util.List;
+import java.util.ArrayList;
+import org.opengis.util.CodeList;
+
+
+/**
+ * Specifies how to open a {@link DataStore}.
+ * This code list serves a similar purpose than the {@code java.nio.file} 
{@link java.nio.file.OpenOption},
+ * except that it applies to data stores instead than files. The options 
provided by this code list are very
+ * similar to the {@code java.nio.file} ones except for {@link #UNKNOWN}.
+ *
+ * <p>This code list is extensible: some {@code DataStore} subclasses may 
provide their own implementation
+ * specific open options.</p>
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ *
+ * @see java.nio.file.OpenOption
+ */
+public final class OpenOption extends CodeList<OpenOption> {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = -892034640198004572L;
+
+    /**
+     * List of all enumerations of this type.
+     * Shall be declared before any enum declaration.
+     */
+    private static final List<OpenOption> VALUES = new ArrayList<>(5);
+
+    /**
+     * Open for reading data from a {@link DataStore}.
+     *
+     * @see java.nio.file.StandardOpenOption#READ
+     */
+    public static final OpenOption READ = new OpenOption("READ");
+
+    /**
+     * Open for overwriting existing data in a {@link DataStore}.
+     *
+     * @see java.nio.file.StandardOpenOption#WRITE
+     */
+    public static final OpenOption WRITE = new OpenOption("WRITE");
+
+    /**
+     * Open for appending new data in a {@link DataStore}.
+     *
+     * @see java.nio.file.StandardOpenOption#APPEND
+     */
+    public static final OpenOption APPEND = new OpenOption("APPEND");
+
+    /**
+     * Creates a new file or database if it does not exist.
+     *
+     * @see java.nio.file.StandardOpenOption#CREATE
+     */
+    public static final OpenOption CREATE = new OpenOption("CREATE");
+
+    /**
+     * Indicates that the open capabilities can not be determined.
+     * This value may be returned by {@linkplain 
DataStoreProvider#getOpenCapabilities(StorageConnector)}
+     * in two kind of situations:
+     *
+     * <ul>
+     *   <li>The method can not look ahead far enough in the file header,
+     *       for example because the buffer has not fetched enough bytes.</li>
+     *   <li>The {@code DataStore} could potentially open anything.
+     *       This is the case for example of the RAW image format.</li>
+     * </ul>
+     *
+     * This option is exclusive with all other options.
+     */
+    public static final OpenOption UNKNOWN = new OpenOption("UNKNOWN");
+
+    /**
+     * Creates a new code list element of the given name.
+     * The new element is automatically added to the {@link #VALUES} list.
+     *
+     * @param name The name of the new element. Must be unique in this code 
list.
+     */
+    private OpenOption(final String name) {
+        super(name, VALUES);
+    }
+
+    /**
+     * Returns the list of {@code OpenOption}s.
+     *
+     * @return The list of open options declared in the current JVM.
+     */
+    public static OpenOption[] values() {
+        synchronized (VALUES) {
+            return VALUES.toArray(new OpenOption[VALUES.size()]);
+        }
+    }
+
+    /**
+     * Returns the list of codes of the same kind than this code list element.
+     * Invoking this method is equivalent to invoking {@link #values()}, 
except that
+     * this method can be invoked on an instance of the parent {@code 
CodeList} class.
+     *
+     * @return The list of open options declared in the current JVM.
+     */
+    @Override
+    public OpenOption[] family() {
+        return values();
+    }
+
+    /**
+     * Returns the open option that matches the given string, or returns a new 
one if none match it.
+     * More specifically, this methods returns the first instance for which
+     * <code>{@linkplain #name() name()}.{@linkplain String#equals 
equals}(code)</code> returns {@code true}.
+     * If no existing instance is found, then a new one is created for the 
given name.
+     *
+     * @param code The name of the code to fetch or to create.
+     * @return A code matching the given name.
+     */
+    public static OpenOption valueOf(String code) {
+        return valueOf(OpenOption.class, code);
+    }
+}

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

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


Reply via email to