|
| 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.catalina.webresources; |
| 18 | + |
| 19 | +import java.net.URL; |
| 20 | +import java.net.URLStreamHandler; |
| 21 | +import java.net.URLStreamHandlerFactory; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 24 | + |
| 25 | +import org.apache.catalina.webresources.war.Handler; |
| 26 | + |
| 27 | +public class TomcatURLStreamHandlerFactory implements URLStreamHandlerFactory { |
| 28 | + |
| 29 | + private static final String WAR_PROTOCOL = "war"; |
| 30 | + private static final String CLASSPATH_PROTOCOL = "classpath"; |
| 31 | + |
| 32 | + // Singleton instance |
| 33 | + private static volatile TomcatURLStreamHandlerFactory instance = null; |
| 34 | + |
| 35 | + /** |
| 36 | + * Obtain a reference to the singleton instance. It is recommended that |
| 37 | + * callers check the value of {@link #isRegistered()} before using the |
| 38 | + * returned instance. |
| 39 | + * |
| 40 | + * @return A reference to the singleton instance |
| 41 | + */ |
| 42 | + public static TomcatURLStreamHandlerFactory getInstance() { |
| 43 | + getInstanceInternal(true); |
| 44 | + return instance; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + private static TomcatURLStreamHandlerFactory getInstanceInternal(boolean register) { |
| 49 | + // Double checked locking. OK because instance is volatile. |
| 50 | + if (instance == null) { |
| 51 | + synchronized (TomcatURLStreamHandlerFactory.class) { |
| 52 | + if (instance == null) { |
| 53 | + instance = new TomcatURLStreamHandlerFactory(register); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return instance; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + private final boolean registered; |
| 62 | + |
| 63 | + // List of factories for application defined stream handler factories. |
| 64 | + private final List<URLStreamHandlerFactory> userFactories = |
| 65 | + new CopyOnWriteArrayList<>(); |
| 66 | + |
| 67 | + /** |
| 68 | + * Register this factory with the JVM. May be called more than once. The |
| 69 | + * implementation ensures that registration only occurs once. |
| 70 | + * |
| 71 | + * @return <code>true</code> if the factory is already registered with the |
| 72 | + * JVM or was successfully registered as a result of this call. |
| 73 | + * <code>false</code> if the factory was disabled prior to this |
| 74 | + * call. |
| 75 | + */ |
| 76 | + public static boolean register() { |
| 77 | + return getInstanceInternal(true).isRegistered(); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * Prevent this this factory from registering with the JVM. May be called |
| 83 | + * more than once. |
| 84 | + * |
| 85 | + * @return <code>true</code> if the factory is already disabled or was |
| 86 | + * successfully disabled as a result of this call. |
| 87 | + * <code>false</code> if the factory was already registered prior |
| 88 | + * to this call. |
| 89 | + */ |
| 90 | + public static boolean disable() { |
| 91 | + return !getInstanceInternal(false).isRegistered(); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Release references to any user provided factories that have been loaded |
| 97 | + * using the provided class loader. Called during web application stop to |
| 98 | + * prevent memory leaks. |
| 99 | + * |
| 100 | + * @param classLoader The class loader to release |
| 101 | + */ |
| 102 | + public static void release(ClassLoader classLoader) { |
| 103 | + if (instance == null) { |
| 104 | + return; |
| 105 | + } |
| 106 | + List<URLStreamHandlerFactory> factories = instance.userFactories; |
| 107 | + for (URLStreamHandlerFactory factory : factories) { |
| 108 | + ClassLoader factoryLoader = factory.getClass().getClassLoader(); |
| 109 | + while (factoryLoader != null) { |
| 110 | + if (classLoader.equals(factoryLoader)) { |
| 111 | + // Implementation note: userFactories is a |
| 112 | + // CopyOnWriteArrayList, so items are removed with |
| 113 | + // List.remove() instead of usual Iterator.remove() |
| 114 | + factories.remove(factory); |
| 115 | + break; |
| 116 | + } |
| 117 | + factoryLoader = factoryLoader.getParent(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + private TomcatURLStreamHandlerFactory(boolean register) { |
| 124 | + // Hide default constructor |
| 125 | + // Singleton pattern to ensure there is only one instance of this |
| 126 | + // factory |
| 127 | + this.registered = register; |
| 128 | + if (register) { |
| 129 | + URL.setURLStreamHandlerFactory(this); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + public boolean isRegistered() { |
| 135 | + return registered; |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + /** |
| 140 | + * Since the JVM only allows a single call to |
| 141 | + * {@link URL#setURLStreamHandlerFactory(URLStreamHandlerFactory)} and |
| 142 | + * Tomcat needs to register a handler, provide a mechanism to allow |
| 143 | + * applications to register their own handlers. |
| 144 | + * |
| 145 | + * @param factory The user provided factory to add to the factories Tomcat |
| 146 | + * has already registered |
| 147 | + */ |
| 148 | + public void addUserFactory(URLStreamHandlerFactory factory) { |
| 149 | + userFactories.add(factory); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + @Override |
| 154 | + public URLStreamHandler createURLStreamHandler(String protocol) { |
| 155 | + |
| 156 | + // Tomcat's handler always takes priority so applications can't override |
| 157 | + // it. |
| 158 | + if (WAR_PROTOCOL.equals(protocol)) { |
| 159 | + return new Handler(); |
| 160 | + } else if (CLASSPATH_PROTOCOL.equals(protocol)) { |
| 161 | + return new ClasspathURLStreamHandler(); |
| 162 | + } |
| 163 | + |
| 164 | + // Application handlers |
| 165 | + for (URLStreamHandlerFactory factory : userFactories) { |
| 166 | + URLStreamHandler handler = |
| 167 | + factory.createURLStreamHandler(protocol); |
| 168 | + if (handler != null) { |
| 169 | + return handler; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Unknown protocol |
| 174 | + return null; |
| 175 | + } |
| 176 | +} |
0 commit comments