1 /*
2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
95 */
96 protected Charset charset;
97
98 protected Options options;
99
100 protected String classLoaderClass;
101
102 protected Locations locations;
103
104 protected Source getSource() {
105 String sourceName = options.get(Option.SOURCE);
106 Source source = null;
107 if (sourceName != null)
108 source = Source.lookup(sourceName);
109 return (source != null ? source : Source.DEFAULT);
110 }
111
112 protected ClassLoader getClassLoader(URL[] urls) {
113 ClassLoader thisClassLoader = getClass().getClassLoader();
114
115 // Bug: 6558476
116 // Ideally, ClassLoader should be Closeable, but before JDK7 it is not.
117 // On older versions, try the following, to get a closeable classloader.
118
119 // 1: Allow client to specify the class to use via hidden option
120 if (classLoaderClass != null) {
121 try {
122 Class<? extends ClassLoader> loader =
123 Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
124 Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
125 Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
126 return constr.newInstance(new Object[] { urls, thisClassLoader });
127 } catch (Throwable t) {
128 // ignore errors loading user-provided class loader, fall through
129 }
130 }
131
132 // 2: If URLClassLoader implements Closeable, use that.
133 if (Closeable.class.isAssignableFrom(URLClassLoader.class))
134 return new URLClassLoader(urls, thisClassLoader);
135
136 // 3: Try using private reflection-based CloseableURLClassLoader
137 try {
138 return new CloseableURLClassLoader(urls, thisClassLoader);
139 } catch (Throwable t) {
140 // ignore errors loading workaround class loader, fall through
141 }
142
143 // 4: If all else fails, use plain old standard URLClassLoader
144 return new URLClassLoader(urls, thisClassLoader);
145 }
146
147 // <editor-fold defaultstate="collapsed" desc="Option handling">
148 public boolean handleOption(String current, Iterator<String> remaining) {
149 OptionHelper helper = new GrumpyHelper(log) {
150 @Override
151 public String get(Option option) {
152 return options.get(option.getText());
153 }
154
155 @Override
156 public void put(String name, String value) {
157 options.put(name, value);
158 }
159
160 @Override
161 public void remove(String name) {
162 options.remove(name);
163 }
|
1 /*
2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
95 */
96 protected Charset charset;
97
98 protected Options options;
99
100 protected String classLoaderClass;
101
102 protected Locations locations;
103
104 protected Source getSource() {
105 String sourceName = options.get(Option.SOURCE);
106 Source source = null;
107 if (sourceName != null)
108 source = Source.lookup(sourceName);
109 return (source != null ? source : Source.DEFAULT);
110 }
111
112 protected ClassLoader getClassLoader(URL[] urls) {
113 ClassLoader thisClassLoader = getClass().getClassLoader();
114
115 // Allow the following to specify a closeable classloader
116 // other than URLClassLoader.
117
118 // 1: Allow client to specify the class to use via hidden option
119 if (classLoaderClass != null) {
120 try {
121 Class<? extends ClassLoader> loader =
122 Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
123 Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
124 Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
125 return constr.newInstance(new Object[] { urls, thisClassLoader });
126 } catch (Throwable t) {
127 // ignore errors loading user-provided class loader, fall through
128 }
129 }
130 return new URLClassLoader(urls, thisClassLoader);
131 }
132
133 // <editor-fold defaultstate="collapsed" desc="Option handling">
134 public boolean handleOption(String current, Iterator<String> remaining) {
135 OptionHelper helper = new GrumpyHelper(log) {
136 @Override
137 public String get(Option option) {
138 return options.get(option.getText());
139 }
140
141 @Override
142 public void put(String name, String value) {
143 options.put(name, value);
144 }
145
146 @Override
147 public void remove(String name) {
148 options.remove(name);
149 }
|