1 /*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution, if
20 * any, must include the following acknowlegement:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowlegement may appear in the software itself,
24 * if and wherever such third-party acknowlegements normally appear.
25 *
26 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
27 * Foundation" must not be used to endorse or promote products derived
28 * from this software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache"
32 * nor may "Apache" appear in their names without prior written
33 * permission of the Apache Group.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 */
54 package org.xrn.ant;
55
56
57 //import org.apache.log4j.Logger;
58 import java.io.File;
59 import java.io.FileInputStream;
60 import java.io.FileNotFoundException;
61 import java.io.FileOutputStream;
62 import java.io.IOException;
63 import java.io.InputStream;
64
65 /***
66 * A helper static class, that enables, for instance to copy resources from the
67 * classpath.
68 *
69 * @author Edouard Mercier
70 * @version 1.0 : 2002.11.27
71 */
72 public abstract class JarHelper
73 {
74 //private final static Logger log = Logger.getLogger(JarHelper.class);
75
76 private static final int BUFFER_SIZE = 1024;
77
78 /***
79 * Copies the content of the file that should be present in the jar, into
80 * the provided local file. If the file could not be found, it is searched
81 * for in the provided alternative directory.<br/>
82 * Note that the original file is not supposed to be a text file.<br/>
83 * If the directory path to the new file does not exist, it is created.
84 *
85 * @param fileNameInsideJar the resource file name of the file inside the
86 * jar
87 * @param alternativeDirectoryPath the directory path used if the file
88 * cannot be found within the jar; if set to null, this argument is ignored
89 * @param copyFile the file that will be the copy of the original one
90 * @return <code>true</code> if the copy was OK
91 * @see #copyInputStreamIntoFile
92 */
93 public static boolean copyFileFromJar(final String fileNameInsideJar,
94 final String alternativeDirectoryPath, final File copyFile)
95 {
96 ClassLoader classLoader = JarHelper.class.getClassLoader();
97 InputStream inputStream =
98 classLoader.getResourceAsStream(fileNameInsideJar);
99
100 if ((inputStream == null) && (alternativeDirectoryPath != null))
101 {
102 //log.error("The file '" + fileNameInsideJar + "' is missing");
103 File alternativeDirectory = new File(alternativeDirectoryPath);
104
105 try
106 {
107 inputStream = new FileInputStream(new File(
108 alternativeDirectory, fileNameInsideJar));
109 }
110 catch (FileNotFoundException fileNotFoundException)
111 {
112 //log.error("The file '" + fileNameInsideJar
113 // + "' cannot be found in directory '"
114 // + alternativeDirectoryPath.getAbsolutePath() + "'");
115 return false;
116 }
117 }
118
119 return copyInputStreamIntoFile(inputStream, copyFile);
120 }
121
122 /***
123 * Copies the content of the input stream into the given file. If the
124 * directory path to the new file does not exist, it is created.
125 *
126 * @param inputStream the input stream, from which the data are copied;
127 * <b>note that the input stream will be closed if this method returns
128 * <code>true</code></b>. The special cas when it is null is also handled
129 * @param copyFile the file in which the content of the input stream should
130 * be copied
131 * @return <code>true</code> if no problem during the copy occured
132 */
133 public static boolean copyInputStreamIntoFile(InputStream inputStream,
134 File copyFile)
135 {
136 if (inputStream == null)
137 {
138 //log.error("Inexistent input stream");
139 return false;
140 }
141
142 try
143 {
144 int bufferSize = BUFFER_SIZE;
145 byte[] byteArray = new byte[bufferSize];
146 int readByteNumber = -1;
147 File parentDirectory = copyFile.getParentFile();
148
149 // Tests that the necessary directory path exists, and creates it if not
150 if (parentDirectory.isDirectory() == false)
151 {
152 if (parentDirectory.mkdirs() == true)
153 {
154 //log.debug("Created directory path '" + parentDirectory.getAbsolutePath() + "'");
155 }
156 }
157
158 FileOutputStream fileOutputStream = new FileOutputStream(copyFile);
159
160 while ((readByteNumber = inputStream.read(byteArray)) != -1)
161 {
162 fileOutputStream.write(byteArray, 0, readByteNumber);
163 }
164
165 fileOutputStream.flush();
166 fileOutputStream.close();
167 inputStream.close();
168 }
169 catch (IOException ioException)
170 {
171 //log.error("Could not copy a file from the jar");
172 return false;
173 }
174
175 return true;
176 }
177 }