Update license; improve coverage; add docs dir
[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 readJar(File jarFile) throws Exception {
68                 JarInputStream in = new JarInputStream(new FileInputStream(jarFile));
69                 JarEntry je;
70                 while ((je = in.getNextJarEntry()) != null) {
71                         if (je.isDirectory() == false) {
72                                 if (je.getName().startsWith("att/raptor/config/")) {
73                                         // while((je=in.getNextJarEntry( ))!=null) {
74                                         System.out.println(je.getName() + " " + je.getTime());
75                                         // }
76
77                                 }
78                         }
79                 }
80                 in.close();
81         }
82
83         public static void extractFilesFromJar(String directory) throws IOException {
84                 // JarFile jar = new JarFile(jarFile);
85                 Class clazz = ExtractJar.class;
86                 String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
87                 // System.out.println("classContainer ---------> " + classContainer);
88                 URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();
89
90                 JarInputStream entryStream = new JarInputStream(jarUrl.openStream());
91                 JarEntry entry;
92                 while (true) {
93                         entry = entryStream.getNextJarEntry();
94                         if (entry == null)
95                                 break;
96                         if (entry.getName().indexOf("jarutil") < 0) {
97                                 System.out.println(entry.getName());
98                                 File file = new File(directory, entry.getName());
99                                 if (entry.isDirectory()) {
100                                         if (!file.exists())
101                                                 file.mkdirs();
102                                 } else {
103                                         // make directory (some jars don't list dirs)
104                                         File dir = new File(file.getParent());
105                                         if (!dir.exists())
106                                                 dir.mkdirs();
107                                         if (file.exists())
108                                                 file.delete();
109                                         // Make file
110                                         FileOutputStream fout = new FileOutputStream(file);
111                                         copy(entryStream, fout);
112                                         fout.close();
113
114                                         // touch the file.
115                                         if (entry.getTime() >= 0)
116                                                 file.setLastModified(entry.getTime());
117                                 }
118
119                         }
120                         entryStream.closeEntry();
121                 }
122                 entryStream.close();
123                 System.out.println("************************************************");
124                 System.out.println("*                                              *");
125                 System.out.println("*                                              *");
126                 System.out.println("*          RAPTOR SETUP COMPLETE.              *");
127                 System.out.println("*                                              *");
128                 System.out.println("*         Thank you for upgrading.             *");
129                 System.out.println("*                                              *");
130                 System.out.println("************************************************");
131         }
132
133         public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException {
134                 byte buffer[] = new byte[bufferSize];
135                 int len = bufferSize;
136                 if (byteCount >= 0) {
137                         while (byteCount > 0) {
138                                 if (byteCount < bufferSize)
139                                         len = in.read(buffer, 0, (int) byteCount);
140                                 else
141                                         len = in.read(buffer, 0, bufferSize);
142                                 if (len == -1)
143                                         break;
144
145                                 byteCount -= len;
146                                 out.write(buffer, 0, len);
147                         }
148                 } else {
149                         while (true) {
150                                 len = in.read(buffer, 0, bufferSize);
151                                 if (len < 0)
152                                         break;
153                                 out.write(buffer, 0, len);
154                         }
155                 }
156         }
157
158         /* ------------------------------------------------------------------- */
159         /**
160          * Copy Reader to Writer for byteCount bytes or until EOF or exception.
161          */
162         public static void copy(Reader in, Writer out, long byteCount) throws IOException {
163                 char buffer[] = new char[bufferSize];
164                 int len = bufferSize;
165                 if (byteCount >= 0) {
166                         while (byteCount > 0) {
167                                 if (byteCount < bufferSize)
168                                         len = in.read(buffer, 0, (int) byteCount);
169                                 else
170                                         len = in.read(buffer, 0, bufferSize);
171
172                                 if (len == -1)
173                                         break;
174                                 byteCount -= len;
175                                 out.write(buffer, 0, len);
176                         }
177                 } else {
178                         while (true) {
179                                 len = in.read(buffer, 0, bufferSize);
180                                 if (len == -1)
181                                         break;
182                                 out.write(buffer, 0, len);
183                         }
184                 }
185         }
186
187         /* ------------------------------------------------------------------- */
188         /**
189          * Copy Stream in to Stream out until EOF or exception.
190          */
191         public static void copy(InputStream in, OutputStream out) throws IOException {
192                 copy(in, out, -1);
193         }
194
195         // Deletes all files and subdirectories under dir.
196         // Returns true if all deletions were successful.
197         // If a deletion fails, the method stops attempting to delete and returns false.
198         public static boolean deleteDir(File dir) {
199                 if (dir.isDirectory()) {
200                         String[] children = dir.list();
201                         for (int i = 0; i < children.length; i++) {
202                                 boolean success = deleteDir(new File(dir, children[i]));
203                                 if (!success) {
204                                         return false;
205                                 }
206                         }
207                 }
208
209                 // The directory is now empty so delete it
210                 return dir.delete();
211         }
212
213 }