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