[Arakhnę-Dev] [111] Add URLStreamHandler for "resource" protocol which is supporting output stream.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 111
Author:   galland
Date:     2010-01-13 16:47:43 +0100 (Wed, 13 Jan 2010)
Log Message:
-----------
Add URLStreamHandler for "resource" protocol which is supporting output stream.
Resource protocol is used to describes Java resource, ie a filename inside one of the class paths.

Added Paths:
-----------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceNotFoundException.java
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLConnection.java
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandler.java
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandlerFactory.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourceURLConnectionTest.java

Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceNotFoundException.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceNotFoundException.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceNotFoundException.java	2010-01-13 15:47:43 UTC (rev 111)
@@ -0,0 +1,64 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 Alexandre WILLAUME, Stéphane GALLAND
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.io.IOException;
+
+/**
+ * The exception <code>ResourceNotFoundException</code> is 
+ * thrown when a required Java resource was not found.
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ */
+public class ResourceNotFoundException extends IOException {
+
+	private static final long serialVersionUID = 4951959393164477007L;
+
+	/**
+	 */
+	public ResourceNotFoundException() {
+		super();
+	}
+		
+	/**
+	 * @param message
+	 */
+	public ResourceNotFoundException(String message) {
+		super(message);
+	}
+
+	/**
+	 * @param cause
+	 */
+	public ResourceNotFoundException(Throwable cause) {
+		super(cause);
+	}
+
+	/**
+	 * @param message
+	 * @param cause
+	 */
+	public ResourceNotFoundException(String message, Throwable cause) {
+		super(message, cause);
+	}
+
+}

Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLConnection.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLConnection.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLConnection.java	2010-01-13 15:47:43 UTC (rev 111)
@@ -0,0 +1,154 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 Alexandre WILLAUME, St&eacute;phane GALLAND
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The class <code>ResourceURLConnection</code> is implementing
+ * connection between an URL and a Java resource.
+ * Instances of this class can be used to
+ * read from the resource referenced by the resource URL. Write
+ * is allowed depending on where resource is located.
+ * <p>
+ * Supported header fields are the same as the real resource URL
+ * (basicaly, file or jar protocols).
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see URLConnection
+ */
+class ResourceURLConnection extends URLConnection {
+
+	private URL location = null;
+	private URLConnection connection = null;
+	
+	/**
+	 * @param url is the "file"-protocol url to use.
+	 */
+	protected ResourceURLConnection(URL url) {
+		super(url);
+	}
+	
+	/**
+     * {@inheritDoc}
+     */
+	@Override
+    public String getHeaderField(int n) {
+		try {
+			connect();
+		}
+		catch(IOException e) {
+			throw new IllegalStateException(e);
+		}
+    	return this.connection.getHeaderField(n);
+    }
+	
+    /**
+     * {@inheritDoc}
+     */
+	@Override
+    public String getHeaderField(String name) {
+		try {
+			connect();
+		}
+		catch(IOException e) {
+			throw new IllegalStateException(e);
+		}
+    	return this.connection.getHeaderField(name);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+	@Override
+    public String getHeaderFieldKey(int n) {
+		try {
+			connect();
+		}
+		catch(IOException e) {
+			throw new IllegalStateException(e);
+		}
+    	return this.connection.getHeaderFieldKey(n);
+    }
+		
+    /**
+     * {@inheritDoc}
+     */
+	@Override
+    public Map<String,List<String>> getHeaderFields() {
+		try {
+			connect();
+		}
+		catch(IOException e) {
+			throw new IllegalStateException(e);
+		}
+    	return this.connection.getHeaderFields();
+    }
+	
+    /**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void connect() throws IOException {
+		if (!this.connected) {
+			this.location = Resources.getResource(this.url.getFile());
+			if (this.location==null)
+				throw new ResourceNotFoundException(this.url.toExternalForm());
+			this.connection = this.location.openConnection();
+			if (this.connection==null)
+				throw new ResourceNotFoundException(this.url.toExternalForm());
+			this.connection.setDoInput(getDoInput());
+			this.connection.setDoOutput(getDoOutput());
+			this.connection.setAllowUserInteraction(getAllowUserInteraction());
+			this.connection.setConnectTimeout(getConnectTimeout());
+			this.connection.setDefaultUseCaches(getDefaultUseCaches());
+			this.connection.setReadTimeout(getReadTimeout());
+			this.connection.setIfModifiedSince(getIfModifiedSince());
+			this.connected = true;
+		}
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public OutputStream getOutputStream() throws IOException {
+        connect();
+        return this.connection.getOutputStream();
+    }
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public InputStream getInputStream() throws IOException {
+        connect();
+        return this.connection.getInputStream();
+    }
+	
+}

Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandler.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandler.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandler.java	2010-01-13 15:47:43 UTC (rev 111)
@@ -0,0 +1,60 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 Alexandre WILLAUME, St&eacute;phane GALLAND
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ * The class <code>ResourceURLStreamHandler</code> is supporting resource protocol
+ * for URL streams. This stream protocol
+ * handler knows how to make a connection for "resource" protocol.
+ * <p>
+ * In most cases, an instance of a <code>URLStreamHandler</code>
+ * subclass is not created directly by an application. Rather, the
+ * first time a protocol name is encountered when constructing a
+ * <code>URL</code>, the appropriate stream protocol handler is
+ * automatically loaded.
+ * <p>
+ * To use this factory, invoke the following code only ONCE time:
+ * <code>URL.setURLStreamHandlerFactory(new ResourceURLStreamHandlerFactory());</code>.
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see URLStreamHandler
+ * @see ResourceURLStreamHandlerFactory
+ */
+class ResourceURLStreamHandler extends URLStreamHandler {
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	protected URLConnection openConnection(URL url) throws IOException {
+		if ("resource".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
+			return new ResourceURLConnection(url);
+		}
+		throw new UnsupportedOperationException("Unsupported protocol: "+url); //$NON-NLS-1$
+	}
+
+}

Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandlerFactory.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandlerFactory.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ResourceURLStreamHandlerFactory.java	2010-01-13 15:47:43 UTC (rev 111)
@@ -0,0 +1,56 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 St&eacute;phane GALLAND
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.net.URL;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+
+/**
+ * This class defines a factory for <code>URL</code> stream
+ * "resource" protocol handlers.
+ * <p>
+ * It is used by the <code>URL</code> class to create a
+ * <code>URLStreamHandler</code> for a "resource" protocol.
+ * <p>
+ * To use this factory, invoke the following code only ONCE time:
+ * <code>URL.setURLStreamHandlerFactory(new ResourceURLStreamHandlerFactory());</code>.
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see URLStreamHandlerFactory
+ * @see URL#setURLStreamHandlerFactory(URLStreamHandlerFactory)
+ */
+public class ResourceURLStreamHandlerFactory
+implements URLStreamHandlerFactory {
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public URLStreamHandler createURLStreamHandler(String protocol) {
+		if ("resource".equalsIgnoreCase(protocol)) //$NON-NLS-1$
+            return new ResourceURLStreamHandler();
+		// Force the default factory to retreive stream handler.
+		return null;
+	}
+
+}

Added: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourceURLConnectionTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourceURLConnectionTest.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourceURLConnectionTest.java	2010-01-13 15:47:43 UTC (rev 111)
@@ -0,0 +1,74 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 St&eacute;phane GALLAND
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+/**
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ */
+public class ResourceURLConnectionTest extends TestCase {
+
+	private ResourceURLConnection connection;
+	
+	static {
+		URL.setURLStreamHandlerFactory(new ResourceURLStreamHandlerFactory());
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void setUp() throws Exception {
+		super.setUp();
+		URL resourceUrl = new URL("resource:org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+		assertNotNull(resourceUrl);
+		this.connection = new ResourceURLConnection(resourceUrl);
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void tearDown() throws Exception {
+		this.connection = null;
+		super.tearDown();
+	}
+	
+	/**
+	 * @throws IOException
+	 */
+	public void testGetInputStream() throws IOException {
+		InputStream is = this.connection.getInputStream();
+		BufferedReader br = new BufferedReader(new InputStreamReader(is));
+		String line = br.readLine();
+		br.close();
+		assertEquals("FOR UNIT TEST ONLY ", line); //$NON-NLS-1$
+    }
+
+}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/