~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/classpath/ikvm/internal/AssemblyClassLoader.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2006, 2007, 2010 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
// HACK because of historical reasons this class' source lives in ikvm/internal instead of ikvm/runtime
 
25
package ikvm.runtime;
 
26
 
 
27
import cli.System.Reflection.Assembly;
 
28
import gnu.java.util.EmptyEnumeration;
 
29
import ikvm.lang.Internal;
 
30
import java.io.IOException;
 
31
import java.net.MalformedURLException;
 
32
import java.net.URL;
 
33
import java.util.Enumeration;
 
34
import java.util.Vector;
 
35
import java.util.jar.Attributes;
 
36
import java.util.jar.Manifest;
 
37
 
 
38
public final class AssemblyClassLoader extends ClassLoader
 
39
{
 
40
    // NOTE assembly is null for "generics" class loader instances
 
41
    private final Assembly assembly;
 
42
    private boolean packagesDefined;
 
43
 
 
44
    public AssemblyClassLoader(Assembly assembly)
 
45
    {
 
46
        this(assembly, System.getSecurityManager());
 
47
    }
 
48
 
 
49
    // this constructor is used by the runtime to avoid the security check (by passing in null as the security manager)    
 
50
    AssemblyClassLoader(Assembly assembly, SecurityManager security)
 
51
    {
 
52
        super(null, security);
 
53
        this.assembly = assembly;
 
54
    }
 
55
 
 
56
    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
 
57
    {
 
58
        return LoadClass(this, assembly, name);
 
59
    }
 
60
 
 
61
    private static native Class LoadClass(ClassLoader classLoader, Assembly assembly, String name) throws ClassNotFoundException;
 
62
 
 
63
    public URL getResource(String name)
 
64
    {
 
65
        return getResource(this, assembly, name);
 
66
    }
 
67
 
 
68
    public Enumeration getResources(String name) throws IOException
 
69
    {
 
70
        return getResources(this, assembly, name);
 
71
    }
 
72
 
 
73
    protected URL findResource(String name)
 
74
    {
 
75
        return getResource(this, assembly, name);
 
76
    }
 
77
 
 
78
    protected Enumeration findResources(String name) throws IOException
 
79
    {
 
80
        return getResources(this, assembly, name);
 
81
    }
 
82
 
 
83
    @Internal
 
84
    public static native URL getResource(ClassLoader classLoader, Assembly assembly, String name);
 
85
    
 
86
    @Internal
 
87
    public static native Enumeration getResources(ClassLoader classLoader, Assembly assembly, String name) throws IOException;
 
88
 
 
89
    private static native String GetGenericClassLoaderName(Object classLoader);
 
90
    // also used by java.lang.LangHelper
 
91
    @Internal
 
92
    public static native String[] GetPackages(Assembly assembly);
 
93
 
 
94
    private static native URL GetManifest(Assembly assembly);
 
95
 
 
96
    private synchronized void lazyDefinePackagesCheck()
 
97
    {
 
98
        if(!packagesDefined)
 
99
        {
 
100
            packagesDefined = true;
 
101
            lazyDefinePackages();
 
102
        }
 
103
    }
 
104
 
 
105
    private static String getAttributeValue(Attributes.Name name, Attributes first, Attributes second)
 
106
    {
 
107
        String result = null;
 
108
        if(first != null)
 
109
        {
 
110
            result = first.getValue(name);
 
111
        }
 
112
        if(second != null && result == null)
 
113
        {
 
114
            result = second.getValue(name);
 
115
        }
 
116
        return result;
 
117
    }
 
118
 
 
119
    private Manifest getManifest()
 
120
    {
 
121
        try
 
122
        {
 
123
            if(assembly != null)
 
124
            {
 
125
                URL url = GetManifest(assembly);
 
126
                if (url != null)
 
127
                {
 
128
                    return new Manifest(url.openStream());
 
129
                }
 
130
            }
 
131
        }
 
132
        catch (MalformedURLException _)
 
133
        {
 
134
        }
 
135
        catch (IOException _)
 
136
        {
 
137
        }
 
138
        return null;
 
139
    }
 
140
 
 
141
    private void lazyDefinePackages()
 
142
    {
 
143
        if(assembly == null)
 
144
        {
 
145
            // generic class loader (doesn't support packages)
 
146
            return;
 
147
        }
 
148
        URL sealBase = getCodeBase();
 
149
        Manifest manifest = getManifest();
 
150
        Attributes attr = null;
 
151
        if(manifest != null)
 
152
        {
 
153
            attr = manifest.getMainAttributes();
 
154
        }
 
155
        String[] packages = GetPackages(assembly);
 
156
        for(int i = 0; i < packages.length; i++)
 
157
        {
 
158
            String name = packages[i];
 
159
            if(super.getPackage(name) == null)
 
160
            {
 
161
                Attributes entryAttr = null;
 
162
                if(manifest != null)
 
163
                {
 
164
                    entryAttr = manifest.getAttributes(name.replace('.', '/') + '/');
 
165
                }
 
166
                definePackage(name,
 
167
                    getAttributeValue(Attributes.Name.SPECIFICATION_TITLE, entryAttr, attr),
 
168
                    getAttributeValue(Attributes.Name.SPECIFICATION_VERSION, entryAttr, attr),
 
169
                    getAttributeValue(Attributes.Name.SPECIFICATION_VENDOR, entryAttr, attr),
 
170
                    getAttributeValue(Attributes.Name.IMPLEMENTATION_TITLE, entryAttr, attr),
 
171
                    getAttributeValue(Attributes.Name.IMPLEMENTATION_VERSION, entryAttr, attr),
 
172
                    getAttributeValue(Attributes.Name.IMPLEMENTATION_VENDOR, entryAttr, attr),
 
173
                    "true".equalsIgnoreCase(getAttributeValue(Attributes.Name.SEALED, entryAttr, attr)) ? sealBase : null);
 
174
            }
 
175
        }
 
176
    }
 
177
 
 
178
    protected Package getPackage(String name)
 
179
    {
 
180
        lazyDefinePackagesCheck();
 
181
        return super.getPackage(name);
 
182
    }
 
183
 
 
184
    protected Package[] getPackages()
 
185
    {
 
186
        lazyDefinePackagesCheck();
 
187
        return super.getPackages();
 
188
    }
 
189
 
 
190
    public String toString()
 
191
    {
 
192
        if(assembly != null)
 
193
        {
 
194
            return assembly.get_FullName();
 
195
        }
 
196
        return GetGenericClassLoaderName(this);
 
197
    }
 
198
 
 
199
    private URL getCodeBase()
 
200
    {
 
201
        try
 
202
        {
 
203
            if(assembly != null)
 
204
            {
 
205
                if(false) throw new cli.System.NotSupportedException();
 
206
                return new URL(assembly.get_CodeBase());
 
207
            }
 
208
        }
 
209
        catch(cli.System.NotSupportedException _)
 
210
        {
 
211
        }
 
212
        catch(MalformedURLException _)
 
213
        {
 
214
        }
 
215
        return null;
 
216
    }
 
217
    
 
218
    // return the ClassLoader for the assembly. Note that this doesn't have to be an AssemblyClassLoader.
 
219
    public static native ClassLoader getAssemblyClassLoader(Assembly asm);
 
220
}