Debian Codegraph @

tests/reproducers/signed/LoadResources/srcs/LoadResources.java

raw
1 /*
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version.
36 */
37
38 import java.applet.Applet;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43 import java.nio.charset.StandardCharsets;
44 //import javax.swing.JApplet;
45
46 /**
47 * Intentionally copypasted so LoadResource and LoadResource packed do not call
48 * each other or climb outside of its pacakage
49 */
50 //public class LoadResources extends JApplet{
51 public class LoadResources extends Applet {
52
53 public static final String PASS = "Pass";
54 public static final String FAIL = "Fail";
55
56 public static final String CONTEXT = "context";
57 public static final String CLASS = "class";
58 public static final String SYSTEM = "system";
59 public static final String[] STATIC_LOADERS = new String[]{CONTEXT, CLASS, SYSTEM};
60
61 public static final String THIS = "this";
62 public static final String[] INSTANCE_LOADERS = new String[]{THIS};
63
64 public static String[] FILES = new String[]{"some.file", "LoadResources.class", "some/pkg/LoadResourcesPackaged.class"};
65 private static String phase;
66
67 static {
68 System.out.println("LoadResources started");
69 }
70
71 public static void main(String[] args) {
72 phase = "MAIN";
73 checkAllStatic();
74 }
75
76 public LoadResources() {
77 phase = "CONSTRUCTOR";
78 checkAllStatic();
79 checkAllInstance();
80 }
81
82 @Override
83 public void init() {
84 phase = "INIT";
85 checkAllStatic();
86 checkAllInstance();
87 }
88
89 @Override
90 public void start() {
91 phase = "START";
92 checkAllStatic();
93 checkAllInstance();
94 System.out.println("*** APPLET FINISHED ***");
95 }
96
97 private static void checkAllStatic() {
98 for (String file : FILES) {
99 for (String loader : STATIC_LOADERS) {
100 checkStaticOnly(loader, file);
101 }
102 }
103 }
104
105 private void checkAllInstance() {
106 for (String file : FILES) {
107 for (String loader : INSTANCE_LOADERS) {
108 checkInstance(loader, file);
109 }
110 }
111 }
112
113 private static void checkStaticOnly(String clType, String resource) {
114 title(clType, resource);
115 try {
116 //we need catch exception both in classlaoder check and resource check
117 ClassLoader cl = getStaticClassLoader(clType);
118 String res = read(getResource(cl, resource));
119 pass(res);
120 } catch (Exception ex) {
121 fail(ex);
122 ex.printStackTrace();
123 }
124
125 }
126
127 private static void fail(Exception ex) {
128 System.out.println(FAIL + " - " + ex.getMessage());
129 }
130
131 private static void pass(String res) {
132 System.out.println(PASS + " - " + res);
133 }
134
135 private static void title(String clType, String resource) {
136 System.out.print("[" + phase + "]" + clType + "(" + resource + "): ");
137 System.err.print("[" + phase + "]" + clType + "(" + resource + "): ");
138 }
139
140 private void checkInstance(String clType, String resource) {
141 title(clType, resource);
142 try {
143 //we need catch exception both in classlaoder check and resource check
144 ClassLoader cl = getInstanceClassLoader(clType);
145 String res = read(getResource(cl, resource));
146 pass(res);
147 } catch (Exception ex) {
148 fail(ex);
149 ex.printStackTrace();
150 }
151
152 }
153
154 private static ClassLoader getStaticClassLoader(String type) {
155 switch (type) {
156 case CONTEXT:
157 return Thread.currentThread().getContextClassLoader();
158
159 case CLASS:
160 return LoadResources.class
161 .getClassLoader();
162 case SYSTEM:
163 return ClassLoader.getSystemClassLoader();
164 }
165 return null;
166
167 }
168
169 private ClassLoader getInstanceClassLoader(String type) {
170 switch (type) {
171 case THIS:
172 return this.getClass().getClassLoader();
173 }
174 return null;
175 }
176
177 private static InputStream getResource(ClassLoader classLoader, String arg) {
178 return classLoader.getResourceAsStream(arg);
179 }
180
181 private static String read(java.io.InputStream is) throws IOException {
182 try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
183 return br.readLine();
184 }
185 }
186 }