[Arakhnę-Dev] [112] Add URLStreamHandlerFactory which is able to support many URL protocols.

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


Revision: 112
Author:   galland
Date:     2010-01-13 16:48:40 +0100 (Wed, 13 Jan 2010)
Log Message:
-----------
Add URLStreamHandlerFactory which is able to support many URL protocols.
By default file and resource protocols are supported.
Other protocols may be dynamicaly added.

Added Paths:
-----------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/DynamicURLStreamHandlerFactory.java

Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/DynamicURLStreamHandlerFactory.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/DynamicURLStreamHandlerFactory.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/DynamicURLStreamHandlerFactory.java	2010-01-13 15:48:40 UTC (rev 112)
@@ -0,0 +1,130 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2010 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.net.URL;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+/**
+ * This class defines a factory for <code>URL</code> stream protocol handlers
+ * which is generic and extendable.
+ * <p>
+ * It is used by the <code>URL</code> class to create a
+ * <code>URLStreamHandler</code> for protocols.
+ * <p>
+ * To use this factory, invoke the following code only ONCE time:
+ * <code>URL.setURLStreamHandlerFactory(new GenericURLStreamHandlerFactory());</code>.
+ * <p>
+ * By default this factory known the following protocols:
+ * <ul>
+ * <li><code>file</code>: {@link FileURLStreamHandler}</li>
+ * <li><code>resource</code>: {@link ResourceURLStreamHandler}</li>
+ * </ul>
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see URLStreamHandlerFactory
+ * @see URL#setURLStreamHandlerFactory(URLStreamHandlerFactory)
+ */
+public class DynamicURLStreamHandlerFactory
+implements URLStreamHandlerFactory {
+
+	private final Map<String, Class<? extends URLStreamHandler>> handlers
+		= new TreeMap<String, Class<? extends URLStreamHandler>>();
+	
+	/**
+	 * Create an URLStreamHandler factory with default protocols.
+	 */
+	public DynamicURLStreamHandlerFactory() {
+		addHandler("file", FileURLStreamHandler.class); //$NON-NLS-1$
+		addHandler("resource", ResourceURLStreamHandler.class); //$NON-NLS-1$
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public URLStreamHandler createURLStreamHandler(String protocol) {
+		Class<? extends URLStreamHandler> handler = this.handlers.get(protocol.toLowerCase());
+		if (handler!=null) {
+			try {
+				return handler.newInstance();
+			}
+			catch (Throwable _) {
+				// Ignore errors.
+			}
+		}
+		// Force the default factory to retreive stream handler.
+		return null;
+	}
+	
+	/** Add an handler for the given protocol.
+	 * 
+	 * @param protocol is the protocol to register.
+	 * @param handler is the type of handler to instance when treating an URL with given protocol.
+	 * @return the handler previously binded to the given protocol, or <code>null</code>.
+	 */
+	public Class<? extends URLStreamHandler> addHandler(String protocol, Class<? extends URLStreamHandler> handler) {
+		return this.handlers.put(protocol.toLowerCase(), handler);
+	}
+
+	/** Remove handler for the given protocol.
+	 * 
+	 * @param protocol is the protocol to remove.
+	 * @return the handler previously binded to the given protocol, or <code>null</code>.
+	 */
+	public Class<? extends URLStreamHandler> removeHandler(String protocol) {
+		return this.handlers.remove(protocol.toLowerCase());
+	}
+
+	/** Replies handler for the given protocol.
+	 * 
+	 * @param protocol is the protocol to search for.
+	 * @return the handler binded to the given protocol, or <code>null</code>.
+	 */
+	public Class<? extends URLStreamHandler> getHandler(String protocol) {
+		return this.handlers.get(protocol.toLowerCase());
+	}
+
+	/** Replies if the given protocol is supported.
+	 * 
+	 * @param protocol is the protocol.
+	 * @return <code>true</code> if the given protocol is known, otherwise
+	 * <code>false</code>
+	 */
+	public boolean contains(String protocol) {
+		return this.handlers.containsKey(protocol.toLowerCase());
+	}
+	
+	/** Replies an unmodifiable set of the known protocols.
+	 * 
+	 * @return the known protocols
+	 */
+	public Set<String> getProtocols() {
+		return Collections.unmodifiableSet(this.handlers.keySet());
+	}
+
+}


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