View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jnet;
18  
19  import java.net.URLStreamHandler;
20  import java.net.URLStreamHandlerFactory;
21  
22  /**
23   * A parent aware url stream handler factory delegates to a parent
24   * url stream handler factory,
25   */
26  public abstract class ParentAwareURLStreamHandlerFactory implements URLStreamHandlerFactory {
27  
28      protected URLStreamHandlerFactory parentFactory;
29  
30      /**
31       * Set the parent factory.
32       * @param factory
33       */
34      public void setParentFactory(URLStreamHandlerFactory factory) {
35          this.parentFactory = factory;
36      }
37  
38      /**
39       * Return the parent factory.
40       * @return The parent factory.
41       */
42      public URLStreamHandlerFactory getParent() {
43          return this.parentFactory;
44      }
45  
46      /**
47       * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(java.lang.String)
48       */
49      public URLStreamHandler createURLStreamHandler(String protocol) {
50          URLStreamHandler handler = this.create(protocol);
51          if ( handler == null && this.parentFactory != null ) {
52              handler = this.parentFactory.createURLStreamHandler(protocol);
53          }
54          return handler;
55      }
56  
57      /**
58       * This method can be overwritten by subclasses to instantiate url stream
59       * handlers for the given protocol.
60       * @param protocol The protocol.
61       * @return A url stream handler for the protocol or null.
62       */
63      protected abstract URLStreamHandler create(String protocol);
64  }