001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ref.org.apache.commons.jnet;
018
019import java.net.URLStreamHandler;
020import java.net.URLStreamHandlerFactory;
021
022/**
023 * A parent aware url stream handler factory delegates to a parent
024 * url stream handler factory,
025 */
026public abstract class ParentAwareURLStreamHandlerFactory implements URLStreamHandlerFactory {
027
028    protected URLStreamHandlerFactory parentFactory;
029
030    /**
031     * Set the parent factory.
032     * @param factory
033     */
034    public void setParentFactory(URLStreamHandlerFactory factory) {
035        this.parentFactory = factory;
036    }
037
038    /**
039     * Return the parent factory.
040     * @return The parent factory.
041     */
042    public URLStreamHandlerFactory getParent() {
043        return this.parentFactory;
044    }
045
046    /**
047     * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(java.lang.String)
048     */
049    public URLStreamHandler createURLStreamHandler(String protocol) {
050        URLStreamHandler handler = this.create(protocol);
051        if ( handler == null && this.parentFactory != null ) {
052            handler = this.parentFactory.createURLStreamHandler(protocol);
053        }
054        return handler;
055    }
056
057    /**
058     * This method can be overwritten by subclasses to instantiate url stream
059     * handlers for the given protocol.
060     * @param protocol The protocol.
061     * @return A url stream handler for the protocol or null.
062     */
063    protected abstract URLStreamHandler create(String protocol);
064}