1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* Copyright 2012 Red Hat, Inc.
* This file is part of IcedTea, http://icedtea.classpath.org
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sourceforge.jnlp;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import net.sourceforge.jnlp.runtime.JNLPRuntime;
import sun.misc.BASE64Decoder;
/**
* Allows reuse of code that expects a JNLPFile object,
* while overriding behaviour specific to applets.
*/
public class PluginBridge extends JNLPFile {
private PluginParameters params;
private Set<String> jars = new HashSet<String>();
private List<ExtensionDesc> extensionJars = new ArrayList<ExtensionDesc>();
//Folders can be added to the code-base through the archive tag
private List<String> codeBaseFolders = new ArrayList<String>();
private String[] cacheJars = new String[0];
private String[] cacheExJars = new String[0];
private boolean usePack;
private boolean useVersion;
private boolean useJNLPHref;
/**
* Creates a new PluginBridge using a default JNLPCreator.
*/
public PluginBridge(URL codebase, URL documentBase, String jar, String main,
int width, int height, PluginParameters params)
throws Exception {
this(codebase, documentBase, jar, main, width, height, params, new JNLPCreator());
}
/**
* Handles archive tag entries, which may be folders or jar files
* @param archives the components of the archive tag
*/
private void addArchiveEntries(String[] archives) {
for (String archiveEntry : archives){
// trim white spaces
archiveEntry = archiveEntry.trim();
/*Only '/' on linux, '/' or '\\' on windows*/
if (archiveEntry.endsWith("/") || archiveEntry.endsWith(File.pathSeparator)) {
this.codeBaseFolders.add(archiveEntry);
} else {
this.jars.add(archiveEntry);
}
}
}
public PluginBridge(URL codebase, URL documentBase, String archive, String main,
int width, int height, PluginParameters params, JNLPCreator jnlpCreator)
throws Exception {
specVersion = new Version("1.0");
fileVersion = new Version("1.1");
this.codeBase = codebase;
this.sourceLocation = documentBase;
this.params = params;
this.parserSettings = ParserSettings.getGlobalParserSettings();
if (params.getJNLPHref() != null) {
useJNLPHref = true;
try {
// Use codeBase as the context for the URL. If jnlp_href's
// value is a complete URL, it will replace codeBase's context.
ParserSettings defaultSettings = new ParserSettings();
URL jnlp = new URL(codeBase, params.getJNLPHref());
JNLPFile jnlpFile = null;
if (params.getJNLPEmbedded() != null) {
InputStream jnlpInputStream = new ByteArrayInputStream(decodeBase64String(params.getJNLPEmbedded()));
jnlpFile = new JNLPFile(jnlpInputStream, codeBase, defaultSettings);
} else {
jnlpFile = jnlpCreator.create(jnlp, null, defaultSettings, JNLPRuntime.getDefaultUpdatePolicy(), codeBase);
}
if (jnlpFile.isApplet())
main = jnlpFile.getApplet().getMainClass();
Map<String, String> jnlpParams = jnlpFile.getApplet().getParameters();
info = jnlpFile.info;
// Change the parameter name to lowercase to follow conventions.
for (Map.Entry<String, String> entry : jnlpParams.entrySet()) {
this.params.put(entry.getKey().toLowerCase(), entry.getValue());
}
JARDesc[] jarDescs = jnlpFile.getResources().getJARs();
for (JARDesc jarDesc : jarDescs) {
String fileName = jarDesc.getLocation().toExternalForm();
this.jars.add(fileName);
}
// Store any extensions listed in the JNLP file to be returned later on, namely in getResources()
extensionJars = Arrays.asList(jnlpFile.getResources().getExtensions());
} catch (MalformedURLException e) {
// Don't fail because we cannot get the jnlp file. Parameters are optional not required.
// it is the site developer who should ensure that file exist.
System.err.println("Unable to get JNLP file at: " + params.getJNLPHref()
+ " with context of URL as: " + codeBase.toExternalForm());
}
} else {
// Should we populate this list with applet attribute tags?
info = new ArrayList<InformationDesc>();
useJNLPHref = false;
}
// also, see if cache_archive is specified
String cacheArchive = params.getCacheArchive();
if (!cacheArchive.isEmpty()) {
String[] versions = new String[0];
// are there accompanying versions?
String cacheVersion = params.getCacheVersion();
if (!cacheVersion.isEmpty()) {
versions = cacheVersion.split(",");
}
String[] jars = cacheArchive.split(",");
cacheJars = new String[jars.length];
for (int i = 0; i < jars.length; i++) {
cacheJars[i] = jars[i].trim();
if (versions.length > 0) {
cacheJars[i] += ";" + versions[i].trim();
}
}
}
String cacheArchiveEx = params.getCacheArchiveEx();
if (!cacheArchiveEx.isEmpty()) {
cacheExJars = cacheArchiveEx.split(",");
}
if (archive != null && archive.length() > 0) {
String[] archives = archive.split(",");
addArchiveEntries(archives);
if (JNLPRuntime.isDebug()) {
System.err.println("Jar string: " + archive);
System.err.println("jars length: " + archives.length);
}
}
if (main.endsWith(".class"))
main = main.substring(0, main.length() - 6);
// the class name should be of the form foo.bar.Baz not foo/bar/Baz
String mainClass = main.replace('/', '.');
launchType = new AppletDesc(params.getAppletTitle(), mainClass, documentBase, width,
height, params.getUnmodifiableMap());
if (main.endsWith(".class")) //single class file only
security = new SecurityDesc(this, SecurityDesc.SANDBOX_PERMISSIONS,
codebase.getHost());
else
security = null;
this.uniqueKey = params.getUniqueKey(codebase);
usePack = false;
useVersion = false;
String jargs = params.getJavaArguments();
if (!jargs.isEmpty()) {
for (String s : jargs.split(" ")) {
String[] parts = s.trim().split("=");
if (parts.length == 2 && Boolean.valueOf(parts[1])) {
if ("-Djnlp.packEnabled".equals(parts[0])) {
usePack = true;
} else if ("-Djnlp.versionEnabled".equals(parts[0])) {
useVersion = true;
}
}
}
}
}
public List<String> getArchiveJars() {
return new ArrayList<String>(jars);
}
public boolean codeBaseLookup() {
return params.useCodebaseLookup();
}
public boolean useJNLPHref() {
return useJNLPHref;
}
/**
* {@inheritDoc }
*/
@Override
public DownloadOptions getDownloadOptionsForJar(JARDesc jar) {
return new DownloadOptions(usePack, useVersion);
}
public String getTitle() {
return params.getAppletTitle();
}
public ResourcesDesc getResources(final Locale locale, final String os,
final String arch) {
return new ResourcesDesc(this, new Locale[] { locale }, new String[] { os },
new String[] { arch }) {
@Override
public <T> List<T> getResources(Class<T> launchType) {
// Need to add the JAR manually...
//should this be done to sharedResources on init?
if (launchType.equals(JARDesc.class)) {
try {
List<JARDesc> jarDescs = new ArrayList<JARDesc>();
jarDescs.addAll(sharedResources.getResources(JARDesc.class));
for (String name : jars) {
if (name.length() > 0)
jarDescs.add(new JARDesc(new URL(codeBase, name),
null, null, false, true, false, true));
}
boolean cacheable = true;
if (params.getCacheOption().equalsIgnoreCase("no"))
cacheable = false;
for (String cacheJar : cacheJars) {
String[] jarAndVer = cacheJar.split(";");
String jar = jarAndVer[0];
Version version = null;
if (jar.length() == 0)
continue;
if (jarAndVer.length > 1) {
version = new Version(jarAndVer[1]);
}
jarDescs.add(new JARDesc(new URL(codeBase, jar),
version, null, false, true, false, cacheable));
}
for (String cacheExJar : cacheExJars) {
if (cacheExJar.length() == 0)
continue;
String[] jarInfo = cacheExJar.split(";");
String jar = jarInfo[0].trim();
Version version = null;
boolean lazy = true;
if (jarInfo.length > 1) {
// format is name[[;preload];version]
if (jarInfo[1].equals("preload")) {
lazy = false;
} else {
version = new Version(jarInfo[1].trim());
}
if (jarInfo.length > 2) {
lazy = false;
version = new Version(jarInfo[2].trim());
}
}
jarDescs.add(new JARDesc(new URL(codeBase, jar),
version, null, lazy, true, false, false));
}
// We know this is a safe list of JarDesc objects
@SuppressWarnings("unchecked")
List<T> result = (List<T>) jarDescs;
return result;
} catch (MalformedURLException ex) { /* Ignored */
}
} else if (launchType.equals(ExtensionDesc.class)) {
return (List<T>) extensionJars; // this list is populated when the PluginBridge is first constructed
}
return sharedResources.getResources(launchType);
}
@Override
public JARDesc[] getJARs() {
List<JARDesc> jarDescs = getResources(JARDesc.class);
return jarDescs.toArray(new JARDesc[jarDescs.size()]);
}
public void addResource(Object resource) {
// todo: honor the current locale, os, arch values
sharedResources.addResource(resource);
}
};
}
/**
* Returns the list of folders to be added to the codebase
*/
public List<String> getCodeBaseFolders() {
return new ArrayList<String>(codeBaseFolders);
}
/**
* Returns the resources section of the JNLP file for the
* specified locale, os, and arch.
*/
public ResourcesDesc[] getResourcesDescs(final Locale locale, final String os, final String arch) {
return new ResourcesDesc[] { getResources(locale, os, arch) };
}
public boolean isApplet() {
return true;
}
public boolean isApplication() {
return false;
}
public boolean isComponent() {
return false;
}
public boolean isInstaller() {
return false;
}
/**
* Returns the decoded BASE64 string
*/
static byte[] decodeBase64String(String encodedString) throws IOException {
BASE64Decoder base64 = new BASE64Decoder();
return base64.decodeBuffer(encodedString);
}
}
|