ac99849dc2aa166a1a13dc4e58bab26e00105aec
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.helpserver.data;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.Enumeration;
26
27 import org.osgi.framework.Bundle;
28
29 /**
30  * Extract subtree with resources from Opendaylight/Karaf/OSGi bundle into Karaf directory<br>
31  *
32  * Reference: Eclipsezone @see <a href="https://www.eclipsezone.com/eclipse/forums/t101557.html">https://www.eclipszone.com</a>
33  * <br><br>
34  * Example for resource and directory path from karaf log.
35  *     write resource: help/FAQ/0.4.0/README.md
36  *   Create directories for: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md
37  *   Open the file: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md
38  *   Problem: Binary, JPG files => do not use buffer related functions
39  *
40  * Hint: Werify with file manager the content of the bundle.jar file to see the location of the resources.
41  * There is no need to mark them via the classpath.
42  */
43
44 public class ExtactBundleResource {
45
46     /**
47      * Extract resources from Karaf/OSGi bundle into karaf directory structure.
48      * @param bundle Karaf/OSGi bundle with resources
49      * @param filePrefix prefix in karaf file system for destination e.g. "data/cache/com.highstreet.technologies."
50      * @param ressoureRoot root name of ressources, with leading "/". e.g. "/help"
51      * @throws IOException In case of problems.
52      */
53     public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot) throws IOException {
54          copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot));
55     }
56
57     /**
58      * Delete a file or a directory and its children.
59      * @param file The directory to delete.
60      * @throws IOException Exception when problem occurs during deleting the directory.
61      */
62     public static void deleteRecursively(File file) throws IOException {
63
64         if (file.isDirectory()) {
65             for (File childFile : file.listFiles()) {
66                 if (childFile.isDirectory()) {
67                     deleteRecursively(childFile);
68                 } else {
69                     if (!childFile.delete()) {
70                         throw new IOException();
71                     }
72                 }
73             }
74         }
75
76         if (!file.delete()) {
77             throw new IOException();
78         }
79     }
80
81     // ------------- Private functions
82
83     /**
84      * Recurse function to steps through the resource element tree
85      * @param b Bundle index for bundle with resourcs
86      * @param filePrefix
87      * @param resource
88      * @throws IOException
89      */
90     private static void copyResourceTreeRecurse(Bundle b, String filePrefix, Enumeration<String> resource) throws IOException {
91         while (resource.hasMoreElements()) {
92             String name = resource.nextElement();
93             Enumeration<String> list = b.getEntryPaths(name);
94             if (list != null) {
95                 copyResourceTreeRecurse(b, filePrefix, list);
96             } else {
97                 //Read
98                 File targetFile = new File(filePrefix+name);
99                 targetFile.getParentFile().mkdirs();
100
101                 try(InputStream in = b.getEntry(name).openStream();
102                     OutputStream outStream = new FileOutputStream(targetFile);) {
103
104                     int theInt;
105                     while ((theInt = in.read()) >= 0) {
106                         outStream.write(theInt);
107                     }
108                     in.close();
109                     outStream.flush();
110                     outStream.close();
111                 }
112             }
113         }
114     }
115 }