Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / helpServer / impl / src / main / java / com / highstreet / technologies / helpserver / data / ExtactBundleResource.java
1 package com.highstreet.technologies.helpserver.data;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.util.Enumeration;
9
10 import org.osgi.framework.Bundle;
11
12 /**
13  * Extract subtree with resources from Opendaylight/Karaf/OSGi bundle into Karaf directory<br>
14  *
15  * Reference: Eclipsezone @see <a href="https://www.eclipsezone.com/eclipse/forums/t101557.html">https://www.eclipszone.com</a>
16  * <br><br>
17  * Example for resource and directory path from karaf log.
18  *       write resource: help/FAQ/0.4.0/README.md
19  *   Create directories for: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md
20  *   Open the file: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md
21  *   Problem: Binary, JPG files => do not use buffer related functions
22  *
23  * Hint: Werify with file manager the content of the bundle.jar file to see the location of the resources.
24  * There is no need to mark them via the classpath.
25  */
26
27 public class ExtactBundleResource {
28
29         /**
30          * Extract resources from Karaf/OSGi bundle into karaf directory structure.
31          * @param bundle Karaf/OSGi bundle with resources
32          * @param filePrefix prefix in karaf file system for destination e.g. "data/cache/com.highstreet.technologies."
33          * @param ressoureRoot root name of ressources, with leading "/". e.g. "/help"
34          * @throws IOException In case of problems.
35          */
36         public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot) throws IOException {
37                  copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot));
38         }
39
40         /**
41          * Delete a file or a directory and its children.
42          * @param file The directory to delete.
43          * @throws IOException Exception when problem occurs during deleting the directory.
44          */
45         public static void deleteRecursively(File file) throws IOException {
46
47                 if (file.isDirectory()) {
48                         for (File childFile : file.listFiles()) {
49                                 if (childFile.isDirectory()) {
50                                         deleteRecursively(childFile);
51                                 } else {
52                                         if (!childFile.delete()) {
53                                                 throw new IOException();
54                                         }
55                                 }
56                         }
57                 }
58
59                 if (!file.delete()) {
60                         throw new IOException();
61                 }
62         }
63
64         // ------------- Private functions
65
66         /**
67          * Recurse function to steps through the resource element tree
68          * @param b Bundle index for bundle with resourcs
69          * @param filePrefix
70          * @param resource
71          * @throws IOException
72          */
73         private static void copyResourceTreeRecurse(Bundle b, String filePrefix, Enumeration<String> resource) throws IOException {
74                 while (resource.hasMoreElements()) {
75                         String name = resource.nextElement();
76                         Enumeration<String> list = b.getEntryPaths(name);
77                         if (list != null) {
78                                 copyResourceTreeRecurse(b, filePrefix, list);
79                         } else {
80                                 //Read
81                                 InputStream in = b.getEntry(name).openStream();
82                                 File targetFile = new File(filePrefix+name);
83                                 targetFile.getParentFile().mkdirs();
84                                 OutputStream outStream = new FileOutputStream(targetFile);
85                                 int theInt;
86                                 while ((theInt = in.read()) >= 0) {
87                                         outStream.write(theInt);
88                                 }
89                                 in.close();
90                                 outStream.flush();
91                                 outStream.close();
92                         }
93                 }
94         }
95 }