4e069214c70ab47d5a8f2e5d122679942394284c
[portal.git] / ecomp-portal-BE-common / src / main / java / jarutil / ExtractJar.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package jarutil;
39
40 import java.io.File;
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;
48 import java.net.URL;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarInputStream;
51
52 public class ExtractJar {
53
54         public static int bufferSize = 8192;
55         public static String JARFILE = "raptor_upgrade.jar";
56
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]);
60                 else {
61                         System.out.println("Current Directory is taken as webapp path");
62                         String currentDir = new File(".").getAbsolutePath();
63                         extractFilesFromJar(currentDir);
64                 }
65         }
66
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();
73
74                 JarInputStream entryStream = new JarInputStream(jarUrl.openStream());
75                 JarEntry entry;
76                 while (true) {
77                         entry = entryStream.getNextJarEntry();
78                         if (entry == null)
79                                 break;
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()) {
84                                         if (!file.exists())
85                                                 file.mkdirs();
86                                 } else {
87                                         // make directory (some jars don't list dirs)
88                                         File dir = new File(file.getParent());
89                                         if (!dir.exists())
90                                                 dir.mkdirs();
91                                         if (file.exists())
92                                                 file.delete();
93                                         // Make file
94                                         FileOutputStream fout = new FileOutputStream(file);
95                                         copy(entryStream, fout);
96                                         fout.close();
97
98                                         // touch the file.
99                                         if (entry.getTime() >= 0)
100                                                 file.setLastModified(entry.getTime());
101                                 }
102
103                         }
104                         entryStream.closeEntry();
105                 }
106                 entryStream.close();
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("************************************************");
115         }
116
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);
124                                 else
125                                         len = in.read(buffer, 0, bufferSize);
126                                 if (len == -1)
127                                         break;
128
129                                 byteCount -= len;
130                                 out.write(buffer, 0, len);
131                         }
132                 } else {
133                         while (true) {
134                                 len = in.read(buffer, 0, bufferSize);
135                                 if (len < 0)
136                                         break;
137                                 out.write(buffer, 0, len);
138                         }
139                 }
140         }
141
142         /* ------------------------------------------------------------------- */
143         /**
144          * Copy Reader to Writer for byteCount bytes or until EOF or exception.
145          */
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);
153                                 else
154                                         len = in.read(buffer, 0, bufferSize);
155
156                                 if (len == -1)
157                                         break;
158                                 byteCount -= len;
159                                 out.write(buffer, 0, len);
160                         }
161                 } else {
162                         while (true) {
163                                 len = in.read(buffer, 0, bufferSize);
164                                 if (len == -1)
165                                         break;
166                                 out.write(buffer, 0, len);
167                         }
168                 }
169         }
170
171         /* ------------------------------------------------------------------- */
172         /**
173          * Copy Stream in to Stream out until EOF or exception.
174          */
175         public static void copy(InputStream in, OutputStream out) throws IOException {
176                 copy(in, out, -1);
177         }
178
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]));
187                                 if (!success) {
188                                         return false;
189                                 }
190                         }
191                 }
192
193                 // The directory is now empty so delete it
194                 return dir.delete();
195         }
196
197 }