Initial import
[jpf-core.git] / src / classes / java / net / URLClassLoader.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package java.net;
19
20 import java.io.IOException;
21 import java.security.CodeSource;
22 import java.security.PermissionCollection;
23 import java.security.SecureClassLoader;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.Vector;
27 import java.util.jar.Manifest;
28
29 /**
30  * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
31  * 
32  *  Model class for java.net.URLClassLoader
33  */
34 public class URLClassLoader extends SecureClassLoader {
35
36   private ArrayList<URL> urls = new ArrayList<URL>(0);
37
38   public URLClassLoader(URL[] urls) {
39     super();
40     addURLs(urls);
41   }
42
43   public URLClassLoader(URL[] urls, ClassLoader parent) {
44     super(parent);
45     addURLs(urls);
46   }
47
48   private void addURLs(URL[] urls) {
49     for(URL url: urls) {
50       addURL(url);
51     }
52   }
53
54   private native void addURL0(String url);
55
56   protected void addURL(URL url) {
57     addURL0(url.toString());
58     urls.add(url);
59   }
60
61   @Override
62   protected native Class<?> findClass(final String name) throws ClassNotFoundException;
63
64   private native String findResource0 (String rname);
65
66   @Override
67   public URL findResource(final String rname) {
68     URL url = null;
69     String path = findResource0(rname);
70
71     try {
72       url = new URL(path);
73     } catch (MalformedURLException x){
74       url = null;
75     }
76
77     return url;
78   }
79
80   private native String[] findResources0 (String rname);
81
82   @Override
83   public Enumeration<URL> findResources(String name) throws IOException {
84     String[] urls = findResources0(name);
85     Vector<URL> list = new Vector<URL>(0);
86     for(String url: urls) {
87       try {
88         list.add(new URL(url));
89       } catch (MalformedURLException x){
90         // process the rest
91       }
92     }
93
94     return list.elements();
95   }
96
97   public URL[] getURLs() {
98     return urls.toArray(new URL[urls.size()]);
99   }
100
101   public static URLClassLoader newInstance(URL[] urls) {
102     return (new URLClassLoader(urls));
103   }
104
105   public static URLClassLoader newInstance(URL[] urls, ClassLoader parent) {
106     return (new URLClassLoader(urls, parent));
107   }
108
109   //--- unsupported methods
110
111   public URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
112     throw new UnsupportedOperationException();
113   }
114
115   public void close() throws IOException {
116     throw new UnsupportedOperationException();
117   }
118
119   protected Package definePackage(String name, Manifest man, URL url) 
120       throws IllegalArgumentException {
121     throw new UnsupportedOperationException();
122   }
123
124   protected PermissionCollection getPermissions(CodeSource codesource) {
125     throw new UnsupportedOperationException();
126   }
127 }