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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2005 Israfil Consulting Services Corporation
Copyright (C) 2005 Christian Gruber
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.foundation;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import net.wotonomy.foundation.internal.NetworkClassLoader;
/**
* An implementation of NSBundle. Unlike the standard WebObjects NSBundle, this
* implementation loads bundles dynamically. This means that all bundles do not
* need to exist in the classpath at startup time. Practically, this means that
* NSBundle has a custom classloader.
*
* This behaviour is not supported in Apple's WebObjects, and should be used
* only if compatibility is not desired. It is largely intended for internal use
* within this framework, but is exposed through bundleForURL()
*
* Another difference between Wotonomy's NSBundle and Apple's implementation is
* the ability to initialize the application with a custom resource lookup
* "path".
*
* @author cgruber@israfil.net
* @author $Author: cgruber $
* @version $Revision: 892 $
*
*/
public class NSBundle {
/* Class variables */
public static final String BundleDidLoadNotification = "NSBundleDidLoadNotification";
public static final String LoadedClassesNotification = "NSLoadedClassesNotification";
private static final NSMutableArray _allBundles = new NSMutableArray();
private static final NSMutableArray _allFrameworks = new NSMutableArray();
private static NSMutableDictionary _languageCodes = new NSMutableDictionary();
private static NSBundle _mainBundle = null;
protected static NetworkClassLoader _classLoader = new NetworkClassLoader(ClassLoader.getSystemClassLoader());
/* Instance variables */
protected String name;
protected NSMutableDictionary info = null;
protected String path;
protected NSMutableArray classNames = new NSMutableArray();
protected NSMutableArray packages = new NSMutableArray();
protected Properties properties;
protected boolean isFramework = false;
protected boolean loaded = false;
protected Class principalClass;
/* Constructors */
/**
* The default constructor, which is only public to support other framework
* functionality, and to be API compatible with Apple's WebObjects.
* Generally, framework users should use bundleForXXXX() methods.
*/
public NSBundle() {
}
/* Methods */
/**
* @deprecated use mainBundle() to access the application bundle and
* frameworkBundles() to access any frameworks.
*/
public static synchronized NSArray allBundles() {
return _allBundles.immutableClone();
}
/**
* @deprecated use frameworkBundles() to access any frameworks.
*/
public static NSArray allFrameworks() {
return frameworkBundles();
}
/**
* Returns the bundle that contains the provided class, if any. Otherwise,
* it returns null. Because NSBundles have a specialized class-loader, if
* any two bundles contain duiplicates of the same class, the second will
* fail to load. TODO: Determine if class-load scoping of duplicate classes
* is appropriate.
*
* @param class1
* @return NSBundle
*/
public static synchronized NSBundle bundleForClass(Class class1) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
/**
* @deprecated Apple's WebObjects says you should not load from arbitrary
* path.
* @param path
* @return
*/
public static synchronized NSBundle bundleWithPath(String path) {
try {
return bundleWithURL(new File(path).toURI().toURL());
} catch (MalformedURLException e) {
NSLog.err.appendln("Bundle path is invalid: " + path);
return null;
}
}
/**
* <strong>Note:</strong>This method is only in Wotonomy.
*
* This method returns a bundle at a given URL, registering that bundle as
* well. If the bundle has already been loaded/registered, it is simply
* returned from the cache.
*
* @param url
* @return
*/
public static synchronized NSBundle bundleWithURL(URL url) {
NSBundle result = null;
String sep = System.getProperty("file.separator");
String protocol = url.getProtocol();
if (protocol.equals("file")) {
File f = new File(url.getPath());
if (!f.exists()) {
NSLog.err.appendln("Bundle not found: " + url);
return null;
}
StringBuffer filename = new StringBuffer(f.getName());
int extensionIndex = filename.lastIndexOf(".");
if (extensionIndex == -1) {
NSLog.err
.appendln("Named URL does not point to a bundle with an extension: "
+ url);
return null;
}
String basename = filename.substring(0, extensionIndex);
String extension = filename.substring(extensionIndex + 1, filename
.length());
System.out.println("basename: " + basename);
System.out.println("extension: " + extension);
result = new NSBundle();
result.name = basename;
result.isFramework = extension.equals("framework");
if (f.isDirectory()) {
try {
File javadir = new File(f.getCanonicalPath() + sep + "Contents"
+ sep + "Resources" + sep + "Java");
System.out.println(javadir);
System.out.println(javadir.exists());
File[] jars = javadir.listFiles();
} catch (IOException e) { }
} else {
throw new RuntimeException(
"Compressed bundle files not currently supported.");
}
throw new RuntimeException("Method not finished.");
} else {
try {
JarInputStream j = new JarInputStream(url.openStream());
JarEntry entry1 = j.getNextJarEntry();
JarFile f;
throw new RuntimeException("Method not finished.");
} catch (IOException e) {
NSLog.err
.appendln("IOException loading framework jar from URL "
+ url + " - message: "
+ e.getLocalizedMessage());
StringWriter stacktrace = new StringWriter();
e.printStackTrace(new PrintWriter(stacktrace));
NSLog.err.appendln(stacktrace);
return null;
}
}
}
/**
* This method returns a bundle, either from cache, or if it doesn't exist
* yet, it attempts to look it up - first from the classpath, then from the
* resource path. TODO: Determine if the lookup order is the desired
* semantic.
*
* @param name
* @return
*/
public static synchronized NSBundle bundleForName(String name) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public static synchronized NSArray frameworkBundles() {
return _allFrameworks.immutableClone();
}
/**
* Used to set the "Main" application bundle, in which primary resources are
* loaded for GUI applications. This is mostly only relevant for
* XXApplication objects. This should therefore not be generally used by
* consumers of the framework.
*
* @param aBundle
*/
public static void setMainBundle(NSBundle aBundle) {
_mainBundle = aBundle;
}
public static NSBundle mainBundle() {
return _mainBundle;
}
/**
* Get the default prefix for locale. TODO: This really needs to be made
* dynamic somehow.
*
* @return
*/
protected static String defaultLocalePrefix() {
String language = (String) _languageCodes.objectForKey(Locale
.getDefault().getLanguage());
return language + ".lproj";
}
protected static synchronized NSBundle findOrCreateBundleWithPath(String s) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
/**
* TODO: figure out what this does.
*
* @return
*/
public NSArray bundleClassPackageNames() {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public String bundlePath() {
return this.path;
}
/**
* Returns a byte array for the given resource path. TODO: Lookup semantics
* in WebObjects javadocs.
*
* @param path
* @return
*/
public byte[] bytesForResourcePath(String path) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public NSArray bundleClassNames() {
return classNames.immutableClone();
}
public NSDictionary infoDictionary() {
return info;
}
/**
* Returns an input stream for a given resource path. TODO: Lookup semantics
* in WebObjects javadocs.
*
* @param path
* @return
*/
public InputStream inputStreamForResourcePath(String path) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public boolean isFramework() {
return isFramework;
}
public boolean load() {
return loaded;
}
public String name() {
return name;
}
/**
* @deprecated Don't use this method, use
* resourcePathForLocalizedResourceNamed() instead.
*/
public String pathForResource(String aName, String anExtension) {
return this.resourcePathForLocalizedResourceNamed(aName, null);
}
/**
* @deprecated Don't use this method, use
* resourcePathForLocalizedResourceNamed() instead.
*/
public String pathForResource(String aName, String anExtension,
String subDir) {
return this.resourcePathForLocalizedResourceNamed(aName, subDir);
}
/**
* @deprecated Don't use this method, use resourcePathsForResources()
* instead.
*/
public NSArray pathsForResources(String aName, String anExtension) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public Class principalClass() {
return principalClass;
}
public Properties properties() {
return properties;
}
/**
* @deprecated Resources are now accessed using the bytesForResourcePath()
* and inputStreamForResourcePath() methods.
*/
public String resourcePath() {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public String resourcePathForLocalizedResourceNamed(String aName,
String subDir) {
throw new UnsupportedOperationException("Method not yet implemented.");
// TODO: Implement.
}
public NSArray resourcePathsForDirectories(String extension,
String subdirPath) {
throw new UnsupportedOperationException("Method not yet implemented.");
}
public NSArray resourcePathsForLocalizedResources(String extension,
String subdirPath) {
throw new UnsupportedOperationException("Method not yet implemented.");
}
public NSArray resourcePathsForResources(String extension, String subdirPath) {
throw new UnsupportedOperationException("Method not yet implemented.");
}
public String toString() {
int i = 0;
if (classNames != null)
i = classNames.count();
return "<" + getClass().getName() + " name:'" + name + "' bundlePath:'"
+ path + "' packages:'" + packages + "' " + i + " classes >";
}
/* Static initialization code */
private static void _initLanguages() {
_languageCodes.setObjectForKey("de", "German");
_languageCodes.setObjectForKey("en", "English");
_languageCodes.setObjectForKey("eo", "Esperanto");
_languageCodes.setObjectForKey("es", "Spanish");
_languageCodes.setObjectForKey("fr", "French");
_languageCodes.setObjectForKey("ja", "Japanese");
}
static {
NSBundle._initLanguages();
}
}
|