Merge "EpNotificationItem class DB constraints"
[portal.git] / ecomp-portal-BE-common / src / main / java / jarutil / ExtractJar.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38 package jarutil;
39
40 import java.io.File;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45 import java.io.Reader;
46 import java.io.Writer;
47 import java.net.URL;
48 import java.util.Objects;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarInputStream;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52
53
54 public class ExtractJar {
55         private static final int BUFFER_SIZE = 8192;
56         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExtractJar.class);
57
58         public static void main(String[] args) {
59                 if (args.length > 0 && args[0] != null && args[0].length() > 0)
60                         extractFilesFromJar(args[0]);
61                 else {
62                         logger.info("Current Directory is taken as webapp path");
63                         String currentDir = new File(".").getAbsolutePath();
64                         extractFilesFromJar(currentDir);
65                 }
66         }
67
68         @SuppressWarnings("ResultOfMethodCallIgnored")
69         public static void extractFilesFromJar(String directory) {
70         
71                 Class clazz = ExtractJar.class;
72                 URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();
73
74                 try(JarInputStream entryStream = new JarInputStream(jarUrl.openStream())){
75                         JarEntry entry;
76                                 entry = entryStream.getNextJarEntry();
77                                 if (entry == null)
78                                         logger.info("Raptor setup complete");
79                                         if (!Objects.requireNonNull(entry).getName().contains("jarutil")) {
80                                                 logger.info(entry.getName());
81                                                 File file = new File(directory, entry.getName());
82                                                 if (entry.isDirectory() && !file.exists()) {
83                                                         file.mkdirs();
84                                                 } else {
85                                                         // make directory (some jars don't list dirs)
86                                                         File dir = new File(file.getParent());
87                                                         if (!dir.exists())
88                                                                 dir.mkdirs();
89                                                         if (file.exists())
90                                                                 file.delete();
91                                                         // Make file
92                                                         FileOutputStream fos = new FileOutputStream(file);
93                                                         copy(entryStream, fos);
94                                                         fos.close();
95
96                                                         // touch the file.
97                                                         if (entry.getTime() >= 0)
98                                                                 file.setLastModified(entry.getTime());
99                                                 }
100
101                                         }
102                                 entryStream.closeEntry();
103                         logger.info("Raptor setup complete");
104                 }catch(Exception e) {
105                         logger.error("Exception in extractFilesFromJar",e);
106                 }
107                 
108         }
109
110         public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException {
111                 byte[] buffer = new byte[BUFFER_SIZE];
112                 int len;
113                 if (byteCount >= 0) {
114                         while (byteCount > 0) {
115                                 if (byteCount < BUFFER_SIZE)
116                                         len = in.read(buffer, 0, (int) byteCount);
117                                 else
118                                         len = in.read(buffer, 0, BUFFER_SIZE);
119                                 if (len == -1)
120                                         break;
121
122                                 byteCount -= len;
123                                 out.write(buffer, 0, len);
124                         }
125                 } else {
126                         while (true) {
127                                 len = in.read(buffer, 0, BUFFER_SIZE);
128                                 if (len < 0)
129                                         break;
130                                 out.write(buffer, 0, len);
131                         }
132                 }
133         }
134
135         /* ------------------------------------------------------------------- */
136         /**
137          * Copy Reader to Writer for byteCount bytes or until EOF or exception.
138          */
139         public static void copy(Reader in, Writer out, long byteCount) throws IOException {
140                 char[] buffer = new char[BUFFER_SIZE];
141                 int len;
142                 if (byteCount >= 0) {
143                         while (byteCount > 0) {
144                                 if (byteCount < BUFFER_SIZE)
145                                         len = in.read(buffer, 0, (int) byteCount);
146                                 else
147                                         len = in.read(buffer, 0, BUFFER_SIZE);
148
149                                 if (len == -1)
150                                         break;
151                                 byteCount -= len;
152                                 out.write(buffer, 0, len);
153                         }
154                 } else {
155                         while (true) {
156                                 len = in.read(buffer, 0, BUFFER_SIZE);
157                                 if (len == -1)
158                                         break;
159                                 out.write(buffer, 0, len);
160                         }
161                 }
162         }
163
164         /* ------------------------------------------------------------------- */
165         /**
166          * Copy Stream in to Stream out until EOF or exception.
167          */
168         public static void copy(InputStream in, OutputStream out) throws IOException {
169                 copy(in, out, -1);
170         }
171
172         // Deletes all files and subdirectories under dir.
173         // Returns true if all deletions were successful.
174         // If a deletion fails, the method stops attempting to delete and returns false.
175         public static boolean deleteDir(File dir) {
176                 if (dir.isDirectory()) {
177                         String[] children = dir.list();
178                         assert children != null;
179                         for (String child : children) {
180                                 boolean success = deleteDir(new File(dir, child));
181                                 if (!success) {
182                                         return false;
183                                 }
184                         }
185                 }
186
187                 // The directory is now empty so delete it
188                 return dir.delete();
189         }
190
191 }