2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
41 import java.io.FileInputStream;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
46 import java.io.Reader;
47 import java.io.Writer;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarInputStream;
52 public class ExtractJar {
54 public static int bufferSize = 8192;
55 public static String JARFILE = "raptor_upgrade.jar";
57 public static void main(String[] args) throws Exception {
58 if (args.length > 0 && args[0] != null && args[0].length() > 0)
59 extractFilesFromJar(args[0]);
61 System.out.println("Current Directory is taken as webapp path");
62 String currentDir = new File(".").getAbsolutePath();
63 extractFilesFromJar(currentDir);
67 public static void extractFilesFromJar(String directory) throws IOException {
68 // JarFile jar = new JarFile(jarFile);
69 Class clazz = ExtractJar.class;
70 String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
71 // System.out.println("classContainer ---------> " + classContainer);
72 URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();
74 JarInputStream entryStream = new JarInputStream(jarUrl.openStream());
77 entry = entryStream.getNextJarEntry();
80 if (entry.getName().indexOf("jarutil") < 0) {
81 System.out.println(entry.getName());
82 File file = new File(directory, entry.getName());
83 if (entry.isDirectory()) {
87 // make directory (some jars don't list dirs)
88 File dir = new File(file.getParent());
94 FileOutputStream fout = new FileOutputStream(file);
95 copy(entryStream, fout);
99 if (entry.getTime() >= 0)
100 file.setLastModified(entry.getTime());
104 entryStream.closeEntry();
107 System.out.println("************************************************");
108 System.out.println("* *");
109 System.out.println("* *");
110 System.out.println("* RAPTOR SETUP COMPLETE. *");
111 System.out.println("* *");
112 System.out.println("* Thank you for upgrading. *");
113 System.out.println("* *");
114 System.out.println("************************************************");
117 public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException {
118 byte buffer[] = new byte[bufferSize];
119 int len = bufferSize;
120 if (byteCount >= 0) {
121 while (byteCount > 0) {
122 if (byteCount < bufferSize)
123 len = in.read(buffer, 0, (int) byteCount);
125 len = in.read(buffer, 0, bufferSize);
130 out.write(buffer, 0, len);
134 len = in.read(buffer, 0, bufferSize);
137 out.write(buffer, 0, len);
142 /* ------------------------------------------------------------------- */
144 * Copy Reader to Writer for byteCount bytes or until EOF or exception.
146 public static void copy(Reader in, Writer out, long byteCount) throws IOException {
147 char buffer[] = new char[bufferSize];
148 int len = bufferSize;
149 if (byteCount >= 0) {
150 while (byteCount > 0) {
151 if (byteCount < bufferSize)
152 len = in.read(buffer, 0, (int) byteCount);
154 len = in.read(buffer, 0, bufferSize);
159 out.write(buffer, 0, len);
163 len = in.read(buffer, 0, bufferSize);
166 out.write(buffer, 0, len);
171 /* ------------------------------------------------------------------- */
173 * Copy Stream in to Stream out until EOF or exception.
175 public static void copy(InputStream in, OutputStream out) throws IOException {
179 // Deletes all files and subdirectories under dir.
180 // Returns true if all deletions were successful.
181 // If a deletion fails, the method stops attempting to delete and returns false.
182 public static boolean deleteDir(File dir) {
183 if (dir.isDirectory()) {
184 String[] children = dir.list();
185 for (int i = 0; i < children.length; i++) {
186 boolean success = deleteDir(new File(dir, children[i]));
193 // The directory is now empty so delete it