1 package com.highstreet.technologies.helpserver.data;
4 import java.io.IOException;
5 import java.net.URISyntaxException;
6 import java.nio.file.Path;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Comparator;
11 import org.json.JSONObject;
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.FrameworkUtil;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public class HelpInfrastructureObject extends JSONObject {
19 private static final Logger LOG = LoggerFactory.getLogger(HelpInfrastructureObject.class);
20 private static String HELPBASE = "help";
21 private static String KARAFBUNDLERESOURCEHELPROOT = "/"+HELPBASE;
22 private static String KARAFHELPDIRPREFIX = "data/cache/com.highstreet.technologies.";
23 public static File KARAFHELPDIRECTORY = new File(KARAFHELPDIRPREFIX+HELPBASE);
25 public static class VersionObject extends JSONObject
27 private static Comparator<VersionObject> comp;
28 private final String mVersion;
29 public String getVersion() {return this.mVersion;}
30 public VersionObject(String path,String date,String label,String version)
32 this.mVersion=version;
33 this.put("path", path);
34 this.put("date",date);
35 this.put("label", label);
37 public static Comparator<VersionObject> getComparer() {
39 comp=new Comparator<HelpInfrastructureObject.VersionObject>() {
42 public int compare(VersionObject o1, VersionObject o2) {
43 return o1.getVersion().compareTo(o2.getVersion());
48 public VersionObject cloneAsLatest() {
49 return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"), "latest");
51 public VersionObject cloneAsCurrent() {
52 return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"), "current");
55 public static class NodeObject extends JSONObject
57 public NodeObject(Path base,File dir,String label,ArrayList<VersionObject> versions)
59 this.put("label", label);
60 if(versions!=null && versions.size()>0)
62 JSONObject o=new JSONObject();
63 this.put("versions", o);
64 for(VersionObject version : versions)
65 o.put(version.getVersion(), version);
68 File[] list = dir.listFiles();
69 if (list == null) return;
74 ArrayList<VersionObject> versions2=findReadmeVersionFolders(base, f.toPath(),true);
75 if(versions2!=null && versions2.size()>0)
78 if(!this.has("nodes"))
79 this.put("nodes",new JSONObject());
80 nodes=this.getJSONObject("nodes");
82 NodeObject o=new NodeObject(base,f,f.getName(),versions2);
83 nodes.put(o.getString("label").toLowerCase(),o);
90 public HelpInfrastructureObject(Path proot) throws URISyntaxException
92 File root = proot.toFile();
93 File[] list = root.listFiles();
94 if (list == null) return;
99 ArrayList<VersionObject> versions=findReadmeVersionFolders(root.toPath(), f.toPath(),true);
100 if(versions!=null && versions.size()>0)
102 NodeObject o=new NodeObject(proot,f,f.getName(),versions);
103 this.put(o.getString("label").toLowerCase(), o);
110 public static void walk(ArrayList<File> results, String path ) {
112 File root = new File( path );
113 File[] list = root.listFiles();
115 if (list == null) return;
117 for ( File f : list ) {
118 if ( f.isDirectory() ) {
119 walk(results, f.getAbsolutePath() );
120 //System.out.println( "Dir:" + f.getAbsoluteFile() );
123 //System.out.println( "File:" + f.getAbsoluteFile() );
124 if(f.isFile() && f.getName().endsWith(".md") )
129 private static ArrayList<VersionObject> findReadmeVersionFolders(Path base,Path root,boolean appendCurrent)
131 ArrayList<VersionObject> list=new ArrayList<>();
132 File[] files=root.toFile().listFiles();
133 int baselen=base.toFile().getAbsolutePath().length();
138 if(f.isDirectory() && new File(f.getAbsolutePath()+"/README.md").exists())
139 list.add(new VersionObject(f.getAbsolutePath().substring(baselen+1)+"/README.md","","",f.getName()));
142 Collections.sort(list, VersionObject.getComparer());
143 Collections.reverse(list);
144 if(list.size()>0 && appendCurrent)
146 list.add(list.get(0).cloneAsCurrent());
152 public static void createFilesFromResources()
155 if (KARAFHELPDIRECTORY.exists()) {
156 LOG.info("Delete existing directory");
158 ExtactBundleResource.deleteRecursively(KARAFHELPDIRECTORY);
159 } catch (IOException e1) {
160 LOG.warn(e1.toString());
166 Bundle b=FrameworkUtil.getBundle(HelpInfrastructureObject.class);
168 LOG.info("No bundlereference: Use target in filesystem.");
169 //URL helpRessource = JarFileUtils.stringToJarURL("target/helpserver-impl-0.4.0-SNAPSHOT.jar",KARAFBUNDLERESOURCEHELPROOT);
172 LOG.info("Bundle location:{} State:{}",b.getLocation(), b.getState());
173 LOG.info("Write files from Resource");
174 ExtactBundleResource.copyBundleResoucesRecursively(b,"data/cache/com.highstreet.technologies." ,KARAFBUNDLERESOURCEHELPROOT);
176 } catch (IOException e) {
177 LOG.warn("No help files available. Exception: "+e.toString());
181 public static Path getHelpDirectoryBase() {
182 return(KARAFHELPDIRECTORY.toPath());