Debian Codegraph @

tests/reproducers/signed/LoadResources/srcs/LoadResourcesPackaged.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 package some.pkg;
38
39 import java.applet.Applet;
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.InputStreamReader;
44 import java.nio.charset.StandardCharsets;
45 //import javax.swing.JApplet;
46
47 /**
48 * Intentionally copypasted so LoadResource and LoadResource packed do not call
49 * each other or climb outside of its pacakage
50 */
51 //public class LoadResourcesPackaged extends JApplet {
52 public class LoadResourcesPackaged extends Applet {
53
54 public static final String PASS = "Pass";
55 public static final String FAIL = "Fail";
56
57 public static final String CONTEXT = "context";
58 public static final String CLASS = "class";
59 public static final String SYSTEM = "system";
60 public static final String[] STATIC_LOADERS = new String[]{CONTEXT, CLASS, SYSTEM};
61
62 public static final String THIS = "this";
63 public static final String[] INSTANCE_LOADERS = new String[]{THIS};
64
65 public static String[] FILES = new String[]{"some.file", "LoadResources.class", "some/pkg/LoadResourcesPackaged.class"};
66 private static String phase;
67
68 static {
69 System.out.println("LoadResourcesPackaged started");
70 }
71
72 public static void main(String[] args) {
73 phase = "MAIN";
74 checkAllStatic();
75 }
76
77 public LoadResourcesPackaged() {
78 phase = "CONSTRUCTOR";
79 checkAllStatic();
80 checkAllInstance();
81 }
82
83 @Override
84 public void init() {
85 phase = "INIT";
86 checkAllStatic();
87 checkAllInstance();
88 }
89
90 @Override
91 public void start() {
92 phase = "START";
93 checkAllStatic();
94 checkAllInstance();
95 System.out.println("*** APPLET FINISHED ***");
96 }
97
98 private static void checkAllStatic() {
99 for (String file : FILES) {
100 for (String loader : STATIC_LOADERS) {
101 checkStaticOnly(loader, file);
102 }
103 }
104 }
105
106 private void checkAllInstance() {
107 for (String file : FILES) {
108 for (String loader : INSTANCE_LOADERS) {
109 checkInstance(loader, file);
110 }
111 }
112 }
113
114 private static void checkStaticOnly(String clType, String resource) {
115 title(clType, resource);
116 try {
117 //we need catch exception both in classlaoder check and resource check
118 ClassLoader cl = getStaticClassLoader(clType);
119 String res = read(getResource(cl, resource));
120 pass(res);
121 } catch (Exception ex) {
122 fail(ex);
123 ex.printStackTrace();
124 }
125
126 }
127
128 private static void fail(Exception ex) {
129 System.out.println(FAIL + " - " + ex.getMessage());
130 }
131
132 private static void pass(String res) {
133 System.out.println(PASS + " - " + res);
134 }
135
136 private static void title(String clType, String resource) {
137 System.out.print("[" + phase + "]" + clType + "(" + resource + "): ");
138 System.err.print("[" + phase + "]" + clType + "(" + resource + "): ");
139 }
140
141 private void checkInstance(String clType, String resource) {
142 title(clType, resource);
143 try {
144 //we need catch exception both in classlaoder check and resource check
145 ClassLoader cl = getInstanceClassLoader(clType);
146 String res = read(getResource(cl, resource));
147 pass(res);
148 } catch (Exception ex) {
149 fail(ex);
150 ex.printStackTrace();
151 }
152
153 }
154
155 private static ClassLoader getStaticClassLoader(String type) {
156 switch (type) {
157 case CONTEXT:
158 return Thread.currentThread().getContextClassLoader();
159
160 case CLASS:
161 return LoadResourcesPackaged.class
162 .getClassLoader();
163 case SYSTEM:
164 return ClassLoader.getSystemClassLoader();
165 }
166 return null;
167
168 }
169
170 private ClassLoader getInstanceClassLoader(String type) {
171 switch (type) {
172 case THIS:
173 return this.getClass().getClassLoader();
174 }
175 return null;
176 }
177
178 private static InputStream getResource(ClassLoader classLoader, String arg) {
179 return classLoader.getResourceAsStream(arg);
180 }
181
182 private static String read(java.io.InputStream is) throws IOException {
183 try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
184 return br.readLine();
185 }
186 }
187 }