4f2c36f08b025b288d94c132a68e2f436f23184b
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / info / impl / src / main / java / com / highstreet / technologies / info / KarafBundleList.java
1 package com.highstreet.technologies.info;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.nio.file.Path;
8 import java.util.ArrayList;
9 import java.util.jar.Attributes;
10 import java.util.jar.JarInputStream;
11 import java.util.jar.Manifest;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class KarafBundleList extends ArrayList<KarafBundle>{
17
18
19         private static Logger LOG = LoggerFactory.getLogger(KarafBundleList.class);
20         /**
21          *
22          */
23         private static final long serialVersionUID = 2856302408338858883L;
24         private final Path _root;
25         public KarafBundleList(String root)
26         {
27                 this._root=new File(root).toPath();
28         }
29         public void scan()
30         {
31                 LOG.debug("start scanning "+this._root.toString()+" for bundles");
32                 for(File f : this._root.toFile().listFiles())
33                 {
34                         if(!f.isDirectory())
35                                 continue;
36                         int id=Integer.parseInt(f.getName());
37                         File bundleFile=new File(f,"1/bundlefile");
38                         if(bundleFile.exists() && bundleFile.isFile())
39                         {
40                                 LOG.trace("try to load bundleinfos for "+bundleFile.getName()+"("+id+")");
41                                 try {
42                                         KarafBundle infos=this._readManifestFromZip(id,bundleFile);
43                                         this.add(infos);
44                                 } catch (IOException e) {
45                                         LOG.warn("problem reading bundle with id="+id+": "+e.getMessage());
46                                 }
47                         }
48                 }
49         }
50         private KarafBundle _readManifestFromZip(int id,File bundleFile) throws FileNotFoundException, IOException  {
51
52                 JarInputStream jarStream = new JarInputStream(new FileInputStream(bundleFile));
53                 Manifest mf = jarStream.getManifest();
54                 Attributes a=mf.getMainAttributes();
55                 jarStream.close();
56                 return new KarafBundle(id, a.getValue("Bundle-Name"), a.getValue("Bundle-SymbolicName"), a.getValue("Bundle-Vendor"), a.getValue("Bundle-Version"));
57         }
58
59         public String toJSON()
60         {
61                 return "["+String.join(",", this)+"]";
62         }
63 }