2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.Reader;
29 import java.io.Writer;
31 import java.util.ArrayList;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarInputStream;
35 public class ExtractJar {
41 public static int bufferSize = 8192;
42 public static String JARFILE = "raptor_upgrade.jar";
44 public static void main(String[] args) throws Exception {
45 if(args.length > 0 && args[0]!=null && args[0].length()>0)
46 extractFilesFromJar(args[0]);
48 System.out.println("Current Directory is taken as webapp path");
49 String currentDir = new File(".").getAbsolutePath();
50 extractFilesFromJar(currentDir);
54 public static void readJar(File jarFile) throws Exception{
55 JarInputStream in = new JarInputStream(new FileInputStream(jarFile));
56 ArrayList entries = new ArrayList( );
58 while((je=in.getNextJarEntry( ))!=null){
59 if( je.isDirectory() == false ) {
60 if(je.getName().startsWith("att/raptor/config/")) {
61 //while((je=in.getNextJarEntry( ))!=null) {
62 System.out.println(je.getName() + " " + je.getTime());
71 public static void extractFilesFromJar(String directory) throws IOException{
72 //JarFile jar = new JarFile(jarFile);
73 Class clazz = ExtractJar.class;
74 String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
75 //System.out.println("classContainer ---------> " + classContainer);
76 URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();
78 JarInputStream entryStream = new JarInputStream(jarUrl.openStream());
81 entry = entryStream.getNextJarEntry();
84 if(entry.getName().indexOf("jarutil") < 0) {
85 System.out.println(entry.getName());
86 File file=new File(directory,entry.getName());
87 if(entry.isDirectory()) {
91 // make directory (some jars don't list dirs)
92 File dir = new File(file.getParent());
95 if(file.exists()) file.delete();
97 FileOutputStream fout = new FileOutputStream(file);
98 copy(entryStream,fout);
102 if (entry.getTime()>=0)
103 file.setLastModified(entry.getTime());
107 entryStream.closeEntry();
110 System.out.println("************************************************");
111 System.out.println("* *");
112 System.out.println("* *");
113 System.out.println("* RAPTOR SETUP COMPLETE. *");
114 System.out.println("* *");
115 System.out.println("* Thank you for upgrading. *");
116 System.out.println("* *");
117 System.out.println("************************************************");
121 public static void copy(InputStream in, OutputStream out, long byteCount)
123 byte buffer[] = new byte[bufferSize];
126 while (byteCount>0) {
127 if (byteCount<bufferSize)
128 len=in.read(buffer,0,(int)byteCount);
130 len=in.read(buffer,0,bufferSize);
135 out.write(buffer,0,len);
139 len=in.read(buffer,0,bufferSize);
142 out.write(buffer,0,len);
147 /* ------------------------------------------------------------------- */
148 /** Copy Reader to Writer for byteCount bytes or until EOF or exception.
150 public static void copy(Reader in, Writer out, long byteCount)
152 char buffer[] = new char[bufferSize];
155 while (byteCount>0) {
156 if (byteCount<bufferSize)
157 len=in.read(buffer,0,(int)byteCount);
159 len=in.read(buffer,0,bufferSize);
164 out.write(buffer,0,len);
168 len=in.read(buffer,0,bufferSize);
171 out.write(buffer,0,len);
176 /* ------------------------------------------------------------------- */
177 /** Copy Stream in to Stream out until EOF or exception.
179 public static void copy(InputStream in, OutputStream out)
185 // Deletes all files and subdirectories under dir.
186 // Returns true if all deletions were successful.
187 // If a deletion fails, the method stops attempting to delete and returns false.
188 public static boolean deleteDir(File dir) {
189 if (dir.isDirectory()) {
190 String[] children = dir.list();
191 for (int i=0; i<children.length; i++) {
192 boolean success = deleteDir(new File(dir, children[i]));
199 // The directory is now empty so delete it