Revision: 7755
Author: victormote
Date: 2006-07-07 18:01:27 -0700 (Fri, 07 Jul 2006)
ViewCVS: http://svn.sourceforge.net/foray/?rev=7755&view=rev
Log Message:
-----------
Add new classes contributed by Vincent Hennebert.
Added Paths:
-----------
trunk/foray/foray-common/src/java/org/foray/common/url/AbstractProtocolRegistration.java
trunk/foray/foray-common/src/java/org/foray/common/url/PropertyProtocolRegistration.java
trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationBroker.java
trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationStrategy.java
Added: trunk/foray/foray-common/src/java/org/foray/common/url/AbstractProtocolRegistration.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/url/AbstractProtocolRegistration.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/url/AbstractProtocolRegistration.java 2006-07-08 01:01:27 UTC (rev 7755)
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2006 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed 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.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/*
+$Id$
+ */
+
+package org.foray.common.url;
+
+import java.net.URLStreamHandler;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Abstract implementation of ProtocolHandlerRegistry which factorizes out
+ * things common two both registration methods: by using the
+ * URL.setURLStreamHandlerFactory of the "java.protocol.handler.pkgs" property.
+ */
+public abstract class AbstractProtocolRegistration
+ implements ProtocolRegistrationStrategy {
+
+ /**
+ * Map of already registered protocols.
+ * The key is a String containing the protocol name.
+ * The value is the {@link URLStreamHandler} which should be used for that
+ * protocol.
+ */
+ protected Map protocols = new HashMap();
+
+ /**
+ * Package-private constructor. To obtain an instance of this class, use
+ * the {@link ProtocolRegistrationBroker#getRegistry()} method.
+ */
+ AbstractProtocolRegistration() {
+ }
+
+ /**
+ * Actually does the registration. This is a hook for subclasses so that
+ * they perform what is needed for their particular implementations.
+ */
+ abstract void doRegister(String protocol, URLStreamHandler handler);
+
+ public synchronized void registerProtocolHandler(String protocol,
+ URLStreamHandler handler) {
+ if (protocol == null) {
+ throw new IllegalArgumentException("Cannot register a null "
+ + "protocol.");
+ }
+ if (handler == null) {
+ throw new IllegalArgumentException("Cannot register a null "
+ + "URLStreamHandler.");
+ }
+ if (!this.protocols.containsKey(protocol)) {
+ this.protocols.put(protocol, handler);
+ doRegister(protocol, handler);
+ }
+ }
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/url/AbstractProtocolRegistration.java
___________________________________________________________________
Name: svn:keywords
+ "Author Id Rev Date URL"
Name: svn:eol-style
+ native
Added: trunk/foray/foray-common/src/java/org/foray/common/url/PropertyProtocolRegistration.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/url/PropertyProtocolRegistration.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/url/PropertyProtocolRegistration.java 2006-07-08 01:01:27 UTC (rev 7755)
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2006 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed 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.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/* $Id$ */
+
+package org.foray.common.url;
+
+import java.net.URLStreamHandler;
+import java.util.StringTokenizer;
+
+
+/**
+ * An implementation of {#link ProtocolHandlerRegistry} which modifies the
+ * "java.protocol.handler.pkgs" property to register new URLStreamHandlers.
+ */
+public class PropertyProtocolRegistration
+ extends AbstractProtocolRegistration {
+
+ /**
+ * Package-private constructor.
+ * @see ProtocolRegistrationBroker#getRegistry
+ */
+ PropertyProtocolRegistration() {
+ }
+
+ void doRegister(String protocol, URLStreamHandler handler) {
+ /* Parse the path. */
+ Class theClass = handler.getClass();
+ String classPath = theClass.getName();
+ int lastTokenDelimiter = classPath.lastIndexOf('.');
+ String className = classPath.substring(lastTokenDelimiter + 1,
+ classPath.length());
+
+ /* Validate the class name. */
+ if (! className.equals("Handler")) {
+ throw new IllegalArgumentException(
+ "The URLStreamHandler for the protocol "
+ + protocol
+ + " should be named Handler");
+ }
+
+ String packageName = classPath.substring(0, lastTokenDelimiter);
+ lastTokenDelimiter = packageName.lastIndexOf('.');
+ String lastSubPackage = packageName.substring(lastTokenDelimiter + 1,
+ packageName.length());
+ /* The last sub-package must be equal to the name of the protocol, or
+ * Java won't find it. */
+ if (! lastSubPackage.equals(protocol)) {
+ throw new IllegalArgumentException(
+ "The URLStreamHandler for the protocol "
+ + protocol
+ + " should be in a package named "
+ + protocol);
+ }
+
+ /* Add the packagePrefix to the Java search path. */
+ String packagePrefix = packageName.substring(0, lastTokenDelimiter);
+ String javaProperty = "java.protocol.handler.pkgs";
+ String searchPath = System.getProperty(javaProperty);
+
+ /* The search path doesn't exist. Create it. */
+ if (searchPath == null) {
+ System.setProperty(javaProperty, packagePrefix);
+ return;
+ }
+
+ /* The search path does exist. See if the package prefix is already
+ * registered. */
+ StringTokenizer packagePrefixIter = new StringTokenizer(
+ searchPath, "|");
+ while (packagePrefixIter.hasMoreTokens()) {
+ String token = packagePrefixIter.nextToken();
+ if (token.equals(packagePrefix)) {
+ /* It is already registered. No need to do it again. */
+ return;
+ }
+ }
+
+ /* It is not registered, so we need to add it. */
+ searchPath += "|" + packagePrefix;
+ System.setProperty(javaProperty, searchPath);
+ }
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/url/PropertyProtocolRegistration.java
___________________________________________________________________
Name: svn:keywords
+ "Author Id Rev Date URL"
Name: svn:eol-style
+ native
Added: trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationBroker.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationBroker.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationBroker.java 2006-07-08 01:01:27 UTC (rev 7755)
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2006 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed 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.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/*
+$Id$
+ */
+
+package org.foray.common.url;
+
+/**
+ * Class that returns an instance of ProtocolHandlerRegistry which is
+ * unique within the whole application.
+ */
+public class ProtocolRegistrationBroker {
+
+ /** The singleton instance of ProtocolHandlerRegistry. */
+ private static ProtocolRegistrationStrategy registry = null;
+
+ /**
+ * Sets the registry which will be used within the application for
+ * registering custom URL protocol handlers. This method may be called only
+ * once at the beginning, before any call to the {@link #getRegistry}
+ * method.
+ * @param registry the registry used to record custom URL protocol handlers
+ */
+ public static synchronized void setRegistry(
+ ProtocolRegistrationStrategy registry) {
+ if (registry != null) {
+ throw new RuntimeException("ProtocolHandlerRegistryBroker: "
+ + "registry already set");
+ }
+ ProtocolRegistrationBroker.registry = registry;
+ }
+
+ /**
+ * Provides access to a ProtocolHandlerRegistry instance, creating it and
+ * registering it with Java if needed.
+ * @return The custom ProtocolHandlerRegistry instance for this session.
+ */
+ public static synchronized ProtocolRegistrationStrategy getRegistry() {
+ /* Here a mechanism will have to be added to choose between the two
+ * methods of registering a protocol handler: URLStreamHandlerFactory
+ * or Java property
+ */
+ if (registry == null) {
+ registry = new PropertyProtocolRegistration();
+ }
+ return registry;
+ }
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationBroker.java
___________________________________________________________________
Name: svn:keywords
+ "Author Id Rev Date URL"
Name: svn:eol-style
+ native
Added: trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationStrategy.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationStrategy.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationStrategy.java 2006-07-08 01:01:27 UTC (rev 7755)
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2006 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed 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.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/* $Id$ */
+
+package org.foray.common.url;
+
+import java.net.URLStreamHandler;
+
+/**
+ * A standardized way to register a URLStreamHandler for a particular protocol.
+ */
+public interface ProtocolRegistrationStrategy {
+
+ /**
+ * Registers a URLStreamHandler for the given protocol. When implemented,
+ * this method should be synchronized. If a handler already exists for the
+ * given protocol, this method does nothing.
+ * @param protocol The protocol to be registered
+ * @param handler The {@link URLStreamHandler} instance which should be
+ * used for URLs whose protocol is <code>protocol</code>.
+ */
+ void registerProtocolHandler(String protocol, URLStreamHandler handler);
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/url/ProtocolRegistrationStrategy.java
___________________________________________________________________
Name: svn:keywords
+ "Author Id Rev Date URL"
Name: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|