You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/01/04 02:53:03 UTC

incubator-tamaya git commit: TAMAYA-42, 43, 44: Readded test code and service configs.

Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 7272198b6 -> 64a6af71a


TAMAYA-42,43,44: Readded test code and service configs.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/64a6af71
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/64a6af71
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/64a6af71

Branch: refs/heads/master
Commit: 64a6af71ad230e4ee61aed425f8b98df10142173
Parents: 7272198
Author: anatole <an...@apache.org>
Authored: Sun Jan 4 02:52:55 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Sun Jan 4 02:52:55 2015 +0100

----------------------------------------------------------------------
 .../resource/DefaultResourceLoader.java         |   4 +-
 .../core/internal/resource/UrlResource.java     | 135 +++++++++++++++++++
 2 files changed, 137 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/64a6af71/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java
index eb56c1d..5198717 100644
--- a/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/DefaultResourceLoader.java
@@ -56,7 +56,7 @@ public class DefaultResourceLoader implements ResourceLoader {
             Enumeration<URL> urls = classLoader.getResources(expression);
             while (urls.hasMoreElements()) {
                 URL url = urls.nextElement();
-                resources.add(new URLResource(url));
+                resources.add(new UrlResource(url));
             }
             return !resources.isEmpty();
         } catch (Exception e) {
@@ -81,7 +81,7 @@ public class DefaultResourceLoader implements ResourceLoader {
     private boolean tryURL(String expression, List<Resource> resources) {
         try {
             URL url = new URL(expression);
-            resources.add(new URLResource(url));
+            resources.add(new UrlResource(url));
             return true;
         } catch (Exception e) {
             LOG.finest(() -> "Failed to load resource from file: " + expression);

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/64a6af71/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java b/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java
new file mode 100644
index 0000000..3a74608
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/resource/UrlResource.java
@@ -0,0 +1,135 @@
+/*
+ * 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.tamaya.core.internal.resource;
+
+import org.apache.tamaya.core.resources.Resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Objects;
+
+/**
+ * Implementation of a resource based on a {@code java.net.URL}.
+ */
+public class UrlResource implements Resource {
+
+    /**
+     * Original URL, used for actual access.
+     */
+    private final URL url;
+
+    /**
+     * Create a new instance based on the given URL.
+     *
+     * @param url a URL
+     */
+    public UrlResource(URL url) {
+        this.url = Objects.requireNonNull(url, "URL null");
+    }
+
+    /**
+     * Create a new URLResource based on a URL path.
+     *
+     * @param path a URL path
+     * @throws MalformedURLException if the given URL path is not valid
+     * @see java.net.URL#URL(String)
+     */
+    public UrlResource(String path) throws MalformedURLException {
+        Objects.requireNonNull(path, "Path must not be null");
+        this.url = new URL(path);
+    }
+
+
+    /**
+     * This implementation opens an InputStream for the given URL.
+     *
+     * @see java.net.URL#openConnection()
+     * @see java.net.URLConnection#setUseCaches(boolean)
+     * @see java.net.URLConnection#getInputStream()
+     */
+    @Override
+    public InputStream getInputStream() throws IOException {
+        URLConnection con = null;
+        try {
+            con = this.url.openConnection();
+            useCachesIfNecessary(con);
+            return con.getInputStream();
+        } catch (IOException e) {
+            if (con instanceof HttpURLConnection) {
+                ((HttpURLConnection) con).disconnect();
+            }
+            throw e;
+        }
+    }
+
+    @Override
+    public URI toURI() throws IOException {
+        try {
+            return this.url.toURI();
+        } catch (URISyntaxException e) {
+            throw new IOException("Failed to create URI from " + url);
+        }
+    }
+
+    @Override
+    public String getName() {
+        return this.url.toString();
+    }
+
+    @Override
+    public String toString() {
+        return "URL [" + this.url + "]";
+    }
+
+    /**
+     * Compares the underlying URL references.
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return (obj == this ||
+                (obj instanceof UrlResource && this.url.equals(((UrlResource) obj).url)));
+    }
+
+    /**
+     * This implementation returns the hash code current the underlying URL reference.
+     */
+    @Override
+    public int hashCode() {
+        return this.url.hashCode();
+    }
+
+    /**
+     * Set the {@link URLConnection#setUseCaches "useCaches"} flag on the
+     * given connection, preferring {@code false} but leaving the
+     * flag at {@code true} for JNLP based resources.
+     *
+     * @param con the URLConnection to set the flag on
+     */
+    private void useCachesIfNecessary(URLConnection con) {
+        con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
+    }
+
+}
+