1 package com.highstreet.technologies.helpserver.data;
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;
10 import org.osgi.framework.Bundle;
13 * Extract subtree with resources from Opendaylight/Karaf/OSGi bundle into Karaf directory<br>
15 * Reference: Eclipsezone @see <a href="https://www.eclipsezone.com/eclipse/forums/t101557.html">https://www.eclipszone.com</a>
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
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.
27 public class ExtactBundleResource {
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.
36 public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot) throws IOException {
37 copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot));
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.
45 public static void deleteRecursively(File file) throws IOException {
47 if (file.isDirectory()) {
48 for (File childFile : file.listFiles()) {
49 if (childFile.isDirectory()) {
50 deleteRecursively(childFile);
52 if (!childFile.delete()) {
53 throw new IOException();
60 throw new IOException();
64 // ------------- Private functions
67 * Recurse function to steps through the resource element tree
68 * @param b Bundle index for bundle with resourcs
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);
78 copyResourceTreeRecurse(b, filePrefix, list);
81 InputStream in = b.getEntry(name).openStream();
82 File targetFile = new File(filePrefix+name);
83 targetFile.getParentFile().mkdirs();
84 OutputStream outStream = new FileOutputStream(targetFile);
86 while ((theInt = in.read()) >= 0) {
87 outStream.write(theInt);