33import javax .validation .constraints .NotNull ;
44import java .io .File ;
55import java .net .MalformedURLException ;
6+ import java .net .URI ;
7+ import java .net .URISyntaxException ;
68import java .net .URL ;
79import java .text .MessageFormat ;
810import java .util .Objects ;
1416 * <p>
1517 * The scripting plugin loads CommonJS modules either from a directory in
1618 * the file system or from a jar file in the local file system. It doesn't
17- * load modules from remote locations, i.e. from a HTTP server.
19+ * load modules from remote locations, i.e. from an HTTP server.
1820 */
1921public class CommonJSModuleRepository {
2022 @ SuppressWarnings ("unused" )
2123 static private final Logger logger = Logger .getLogger (
2224 CommonJSModuleRepository .class .getName ());
2325
2426 final private URL url ;
27+ final private URI uri ;
2528
2629 /**
2730 * Creates a repository.
@@ -32,14 +35,14 @@ public class CommonJSModuleRepository {
3235 * @param dir a directory. Must not be null.
3336 * @throws IllegalArgumentException thrown if dir is null
3437 */
35- public CommonJSModuleRepository (@ NotNull File dir )
36- throws IllegalArgumentException {
38+ public CommonJSModuleRepository (@ NotNull File dir ) throws IllegalArgumentException {
3739 Objects .requireNonNull (dir );
40+ uri = dir .toURI ();
3841 try {
39- url = dir . toURI () .toURL ();
42+ url = uri .toURL ();
4043 } catch (MalformedURLException e ) {
41- throw new IllegalArgumentException (MessageFormat . format (
42- "Failed to convert file {0} to URL. Exception is: {1} " , dir , e ) );
44+ throw new IllegalArgumentException (
45+ MessageFormat . format ( "failed to convert file '' {0}'' to URL" , dir ) , e );
4346 }
4447 }
4548
@@ -74,14 +77,19 @@ protected void ensureValidUrl(URL url) {
7477 * @param url an acceptable URL for a module repository as string.
7578 * Must not be null.
7679 * @throws NullPointerException thrown if url is null
77- * @throws MalformedURLException thrown if url isn't a valid URL
80+ * @throws IllegalArgumentException thrown if url isn't a valid URL
7881 */
79- public CommonJSModuleRepository (@ NotNull String url )
80- throws MalformedURLException {
82+ public CommonJSModuleRepository (@ NotNull String url ) {
8183 Objects .requireNonNull (url );
82- URL repo = new URL (url );
83- ensureValidUrl (repo );
84- this .url = repo ;
84+ try {
85+ URL repo = new URL (url );
86+ ensureValidUrl (repo );
87+ this .uri = repo .toURI ();
88+ this .url = uri .toURL ();
89+ } catch (MalformedURLException | URISyntaxException e ) {
90+ throw new IllegalArgumentException (
91+ MessageFormat .format ("failed to convert string ''{0}'' to URL" , url ), e );
92+ }
8593 }
8694
8795 /**
@@ -90,11 +98,18 @@ public CommonJSModuleRepository(@NotNull String url)
9098 * <code>url</code> must be a valid file or jar URL.
9199 *
92100 * @param url an acceptable URL for a module repository. Must not be null.
101+ * @throws IllegalArgumentException thrown if url isn't a valid URL
93102 */
94- public CommonJSModuleRepository (@ NotNull URL url ) {
103+ public CommonJSModuleRepository (@ NotNull URL url ) throws IllegalArgumentException {
95104 Objects .requireNonNull (url );
96105 ensureValidUrl (url );
97106 this .url = url ;
107+ try {
108+ this .uri = url .toURI ();
109+ } catch (URISyntaxException e ) {
110+ throw new IllegalArgumentException (
111+ MessageFormat .format ("failed to convert URL ''{0}'' to URI" , url ), e );
112+ }
98113 }
99114
100115 /**
@@ -121,21 +136,20 @@ public CommonJSModuleRepository(JarFile jar)
121136 * @throws IllegalArgumentException thrown if jar is null or if the jar URL
122137 * can't be created
123138 */
124- public CommonJSModuleRepository (@ NotNull JarFile jar , String jarPath )
125- throws IllegalArgumentException {
139+ public CommonJSModuleRepository (@ NotNull JarFile jar , String jarPath ) throws IllegalArgumentException {
126140 Objects .requireNonNull (jar );
127141 if (jarPath == null ) jarPath = "/" ;
128142 jarPath = "/" + jarPath .trim ().replace ("\\ " , "/" )
129143 .replaceAll ("/+" , "/" )
130144 .replaceAll ("^/" ,"" );
131145
132146 try {
133- this .url = new URL ("jar:" + new File (jar .getName ()).toURI ()
134- . toURL () + "!" + jarPath );
135- } catch (MalformedURLException e ) {
147+ this .url = new URL ("jar:" + new File (jar .getName ()).toURI (). toURL () + "!" + jarPath );
148+ this . uri = this . url . toURI ( );
149+ } catch (MalformedURLException | URISyntaxException e ) {
136150 throw new IllegalArgumentException (MessageFormat .format (
137- "Failed to create jar URL for jar file < {0}> and jar path "
138- + "< {1}> . Exception is: {2}" , jar , jarPath , e ));
151+ "Failed to create jar URL for jar file '' {0}'' and jar path "
152+ + "'' {1}'' . Exception is: {2}" , jar , jarPath , e ));
139153 }
140154 }
141155
@@ -152,9 +166,7 @@ public File getFile() {
152166 } else {
153167 try {
154168 return new File (new URL (url .getFile ().split ("!" )[0 ]).getFile ());
155- } catch (MalformedURLException e ) {
156- // should not happen
157- e .printStackTrace ();
169+ } catch (MalformedURLException e ) {
158170 return null ;
159171 }
160172 }
@@ -194,7 +206,7 @@ public URL getURL() {
194206 public int hashCode () {
195207 final int prime = 31 ;
196208 int result = 1 ;
197- result = prime * result + ((url == null ) ? 0 : url .hashCode ());
209+ result = prime * result + ((uri == null ) ? 0 : uri .hashCode ());
198210 return result ;
199211 }
200212
@@ -207,10 +219,10 @@ public boolean equals(Object obj) {
207219 if (getClass () != obj .getClass ())
208220 return false ;
209221 CommonJSModuleRepository other = (CommonJSModuleRepository ) obj ;
210- if (url == null ) {
211- return other .url == null ;
222+ if (uri == null ) {
223+ return other .uri == null ;
212224 } else {
213- return url .equals (other .url );
225+ return uri .equals (other .uri );
214226 }
215227 }
216228}
0 commit comments