source: main/trunk/gli/src/org/greenstone/gatherer/util/JarTools.java@ 32271

Last change on this file since 32271 was 32153, checked in by ak19, 6 years ago

2 Bugfixes and tidying up errorhandling in JarTools. 1. Dr Bainbridge fixed issue surrounding env.Path rather than env.PATH being used on Windows, while the empty env.PATH was being concatenated as a variable name by build.xml. This resulted in a cyclical reference, noticed when running the GLI Web Start Application (converted from GLI Applet). Fix is in build.xml. 2. My bugfix to WebGatherer.java, the new GLI Web Start Application. The collectionDir wasn't set correctly until the Gatherer object was instantiated, but the collect dir needed to be known by other code beforehand. So shifted the Gatherer object instantiation up a bit. 3. Can't yet reproduce the Exception seen twice before when running GLI Web Start application. The exception was thrown in ZipTools.java. Tested by rerunning the applet repeatedly, but it hasn't struck again yet. Perhaps it's been fixed now because of the other changes?

  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/**
2 *############################################################################
3 * A component of the Greenstone Librarian Interface, part of the Greenstone
4 * digital library suite from the New Zealand Digital Library Project at the
5 * University of Waikato, New Zealand.
6 *
7 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
8 *
9 * Copyright (C) 2005 New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *############################################################################
25 */
26
27package org.greenstone.gatherer.util;
28
29
30import java.awt.*;
31import java.io.*;
32import java.net.*;
33import javax.swing.*;
34import org.greenstone.gatherer.DebugStream;
35
36
37public class JarTools
38{
39 static private Class root_class = null;
40 static private ImageIcon ERROR_ICON = null;
41
42
43 static public void initialise(Object root_object)
44 {
45 initialise(root_object.getClass());
46 }
47
48 static public void initialise(Class rootClass)
49 {
50 root_class = rootClass;
51 ERROR_ICON = getImage("error.gif");
52 }
53
54
55 static public void extractFromJar(String filename, String dst_dir, boolean must_be_present)
56 {
57 try {
58 // setup input stream for slurping out file
59 InputStream fis = root_class.getResourceAsStream("/"+filename);
60 BufferedInputStream fbis = new BufferedInputStream(fis);
61 DataInputStream fdbis = new DataInputStream(fbis);
62
63 // set up output stream for writing to disk
64 String ofname = dst_dir + filename;
65 FileOutputStream fos = new FileOutputStream(ofname);
66 BufferedOutputStream bfos = new BufferedOutputStream(fos);
67
68 byte[] buf = new byte[1024];
69 int len;
70 int total_bytes = 0;
71 while ((len = fdbis.read(buf)) >= 0) {
72 bfos.write(buf,0,len);
73 total_bytes += len;
74 }
75
76 fdbis.close();
77 bfos.close();
78 }
79 catch (Exception exception) {
80 if (must_be_present) {
81 exception.printStackTrace();
82 }
83 }
84 }
85
86
87 /** Method to retrieve an image icon with the given filename found in classpath or the resouces directory.
88 * @return The specified <strong>ImageIcon</strong>, or an error image replacement if no such images exists.
89 */
90 static public ImageIcon getImage(String filename) {
91 return getImage(filename, false);
92 }
93
94 static public ImageIcon getImage(String filename, boolean wait_until_complete)
95 {
96 ImageIcon image = null;
97 try {
98 image = new ImageIcon(root_class.getResource("/images/" + filename));
99 }
100 catch (NullPointerException exception) {
101 System.err.println("Error: Could not load image " + filename);
102 DebugStream.println("Error: Could not load image " + filename);
103 }
104
105 if (image == null) {
106 image = ERROR_ICON;
107 }
108
109 if (wait_until_complete) {
110 int load_status;
111 do {
112 load_status = image.getImageLoadStatus();
113 }
114 while (load_status != MediaTracker.ABORTED && load_status != MediaTracker.ERRORED && load_status != MediaTracker.COMPLETE);
115 }
116
117 return image;
118 }
119
120
121 static public URL getResource(String resource_name)
122 {
123 return root_class.getResource(resource_name);
124 }
125
126
127 static public InputStream getResourceAsStream(String resource_name)
128 {
129 return root_class.getResourceAsStream(resource_name);
130 }
131
132
133 static public boolean isInJar(String filename)
134 {
135 InputStream fis = null;
136 try {
137 fis = root_class.getResourceAsStream("/" + filename);
138 if(fis == null) {
139 System.err.println("@@@ JarTools.isInJar(): file inputstream for file /"+filename+" is still null. Not in jar file.");
140 return false;
141 }
142 }
143 catch (Exception exception) {
144 exception.printStackTrace();
145 return false;
146 }
147 finally {
148 if(!SafeProcess.closeResource(fis)) {
149 System.err.println("@@@ JarTools.isInJar(): Couldn't close file inputstream");
150 return false;
151 }
152 }
153
154 return true;
155 }
156}
Note: See TracBrowser for help on using the repository browser.