01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.tools.util;
19:
20: import java.net.MalformedURLException;
21: import java.net.URL;
22: import java.net.URLStreamHandler;
23: import java.util.StringTokenizer;
24:
25: public final class URLFactory {
26: public static final String PROTOCOL_HANDLER_PKGS = "java.protocol.handler.pkgs";
27: public static final String UNKNOWN_PROPTOCL_EX_MSG = "unknown protocol: ";
28:
29: private URLFactory() {
30:
31: }
32:
33: public static URL createURL(String spec)
34: throws MalformedURLException {
35: return createURL(null, spec);
36: }
37:
38: public static URL createURL(URL context, String spec)
39: throws MalformedURLException {
40: URL url = null;
41: try {
42: url = new URL(context, spec);
43: } catch (MalformedURLException mue) {
44:
45: String msg = mue.getMessage();
46: if (msg.indexOf(UNKNOWN_PROPTOCL_EX_MSG) != -1) {
47: URLStreamHandler handler = findHandler(msg
48: .substring(UNKNOWN_PROPTOCL_EX_MSG.length()));
49: if (handler != null) {
50: url = new URL(context, spec, handler);
51: }
52: }
53: if (url == null) {
54: throw mue;
55: }
56: }
57: return url;
58: }
59:
60: public static URLStreamHandler findHandler(String protocol) {
61:
62: URLStreamHandler handler = null;
63: String packagePrefixList = System.getProperty(
64: PROTOCOL_HANDLER_PKGS, "");
65: StringTokenizer packagePrefixIter = new StringTokenizer(
66: packagePrefixList, "|");
67: while (handler == null && packagePrefixIter.hasMoreTokens()) {
68: String packagePrefix = packagePrefixIter.nextToken().trim();
69: try {
70: String clsName = packagePrefix + "." + protocol
71: + ".Handler";
72: Class cls = null;
73: try {
74: cls = Class.forName(clsName);
75: } catch (ClassNotFoundException e) {
76: ClassLoader cl = Thread.currentThread()
77: .getContextClassLoader();
78: if (cl != null) {
79: cls = cl.loadClass(clsName);
80: }
81: }
82: if (cls != null) {
83: handler = (URLStreamHandler) cls.newInstance();
84: }
85: } catch (Exception ignored) {
86: ignored.getMessage();
87: }
88: }
89: return handler;
90: }
91: }
|