1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package tests.support;
17
18import java.io.File;
19import java.net.URL;
20import java.net.URLClassLoader;
21
22import dalvik.system.DexClassLoader;
23
24/**
25 * Support class for creating a file-based ClassLoader. Delegates to either
26 * Dalvik's PathClassLoader or the RI's URLClassLoader, but does so by-name.
27 * This allows us to run corresponding tests in both environments.
28 */
29public abstract class Support_ClassLoader {
30
31    public abstract ClassLoader getClassLoader(URL url, ClassLoader parent);
32
33    public static ClassLoader getInstance(URL url, ClassLoader parent) {
34        try {
35            Support_ClassLoader factory;
36
37            if ("Dalvik".equals(System.getProperty("java.vm.name"))) {
38                factory = (Support_ClassLoader)Class.forName(
39                    "tests.support.Support_ClassLoader$Dalvik").newInstance();
40            } else {
41                factory = (Support_ClassLoader)Class.forName(
42                    "tests.support.Support_ClassLoader$RefImpl").newInstance();
43            }
44
45            return factory.getClassLoader(url, parent);
46        } catch (Exception ex) {
47            throw new RuntimeException("Unable to create ClassLoader", ex);
48        }
49    }
50
51    /**
52     * Implementation for Dalvik. Uses the DexClassLoader, so we can write
53     * temporary DEX files to a special directory. We don't want to spoil the
54     * system's DEX cache with our files. Also, we might not have write access
55     * to the system's DEX cache at all (which is the case when we're running
56     * CTS).
57     */
58    static class Dalvik extends Support_ClassLoader {
59
60        private static File tmp;
61
62        static {
63            tmp = new File(System.getProperty("java.io.tmpdir"), "dex-cache");
64            tmp.mkdirs();
65        }
66
67        @Override
68        public ClassLoader getClassLoader(URL url, ClassLoader parent) {
69            return new DexClassLoader(url.getPath(), tmp.getAbsolutePath(),
70                    null, parent);
71        }
72    }
73
74    /**
75     * Implementation for the reference implementation. Nothing interesting to
76     * see here. Please get along.
77     */
78    static class RefImpl extends Support_ClassLoader {
79        @Override
80        public ClassLoader getClassLoader(URL url, ClassLoader parent) {
81            return new URLClassLoader(new URL[] { url }, parent);
82        }
83    }
84}
85