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

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

Rewrote WebGatherer as a Java Web Start application, rather than an Applet which it still was during the previous commit.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 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 try {
136 InputStream fis = root_class.getResourceAsStream("/" + filename);
137 fis.close();
138 }
139 catch (Exception exception) {
140 exception.printStackTrace();
141 return false;
142 }
143
144 return true;
145 }
146}
Note: See TracBrowser for help on using the repository browser.