View Javadoc

1   package net.sourceforge.argval.manifestinfo.impl;
2   
3   
4   import java.io.File;
5   import java.io.IOException;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   import java.util.Set;
10  import java.util.TreeSet;
11  import java.util.Map.Entry;
12  import java.util.jar.Attributes;
13  import java.util.jar.JarFile;
14  import java.util.jar.Manifest;
15  import java.util.regex.Pattern;
16  
17  import net.sourceforge.argval.lang.SystemConstants;
18  import net.sourceforge.argval.manifestinfo.ManifestInfo;
19  import net.sourceforge.argval.manifestinfo.ManifestInfoManager;
20  import net.sourceforge.argval.manifestinfo.ManifestInfoVisitor;
21  import net.sourceforge.argval.packageinfo.PackageInfo;
22  import net.sourceforge.argval.packageinfo.PackageInfoVisitor;
23  import net.sourceforge.argval.packageinfo.PackageInfoVisitorAcceptor;
24  import net.sourceforge.argval.utils.StringUtil;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  final public class ManifestInfoManagerImpl implements ManifestInfoManager, PackageInfoVisitorAcceptor {
31      /** The logging instance. */
32      public static Logger logger = LoggerFactory.getLogger(ManifestInfoManagerImpl.class);
33      /** A Map containing the ManifestInfo instance, by their jar file name. */
34      public Map<String, ManifestInfo> manifestInfoMap;
35  
36  
37      public ManifestInfoManagerImpl() {
38          super();
39          this.manifestInfoMap = new HashMap<String, ManifestInfo>();    
40      }
41  
42      
43      public void addJar(String classPath) {
44          String[] pathArray = classPath.split(System.getProperty(SystemConstants.PATH_SEPARATOR));
45          for (String pathStr : pathArray) {
46              if (pathStr.toLowerCase().endsWith(".jar")) {
47                  File path = new File(pathStr);
48                  addJar(path);
49              }
50          }
51          if (logger.isTraceEnabled()) {
52              // FIXME Make use of CollectionUtils.toString(StringbUilder, Object[]) as this class is using StringBuilder.
53              StringBuilder builder = new StringBuilder();
54              for (String pathStr : pathArray) {
55                  builder.append(pathStr).append(StringUtil.LINE_SEPARATOR);
56              }
57              logger.trace("Added Jar-Manifest from class path entries:" 
58                      + StringUtil.LINE_SEPARATOR + builder.toString());
59          }
60              
61      }
62      
63      public void addJar(File path) {
64          if (! path.isFile()) {
65              logger.warn("Given jar path is not a file");
66              return;
67          }
68          try {
69              JarFile jarFile = new JarFile(path);
70              Manifest manifest = jarFile.getManifest();
71              ManifestInfo manifestInfo = new ManifestInfoAsPackageInfo(path, manifest);
72              addManifestInfo(manifestInfo);
73          }
74          catch (IOException e) {
75              // TODO Auto-generated catch block
76              e.printStackTrace();
77          }
78      }
79  
80      
81      public ManifestInfo getManifestInfo(String regularExpression) {
82          for (Entry<String, ManifestInfo> entry : this.manifestInfoMap.entrySet()) {
83              if (Pattern.matches(regularExpression, entry.getKey())) {
84                  return entry.getValue();
85              }
86          }
87          logger.info("No Manifest Information found for file based expression '" + regularExpression + "'.");
88          return null;
89      }
90  
91      public String getAttributeValue(String regularExpression , String name) {
92          ManifestInfo manifestInfo = getManifestInfo(regularExpression);
93          if (manifestInfo == null)
94          {
95              return null;
96          }
97          return getAttributeValue(manifestInfo, name);
98      }
99      
100     public String getAttributeValue(ManifestInfo manifestInfo, String name) {
101         Attributes attributes = manifestInfo.getManifest().getMainAttributes();
102         if (attributes == null) {
103             return null;
104         }
105         return attributes.getValue(name);
106     }
107 
108     public void addManifestInfo(ManifestInfo manifestInfo) {
109         this.manifestInfoMap.put(manifestInfo.getJarPath().toString(), manifestInfo);
110     }
111     
112     // TODO Check if is this implementation actually is correct.
113     public ManifestInfo getManifestInfo(PackageInfo packageInfo) {
114         for (Entry<String, ManifestInfo> entry : manifestInfoMap.entrySet()) {
115             entry.getKey();
116             ManifestInfo manifestInfo = entry.getValue();
117             if (manifestInfo != null 
118                     && equals(packageInfo.getSpecificationTitle(), manifestInfo.getSpecificationTitle())
119                     && equals(packageInfo.getSpecificationVersion(), manifestInfo.getSpecificationVersion())
120                     && equals(packageInfo.getSpecificationVendor(), manifestInfo.getSpecificationVendor())) {
121                 return manifestInfo;
122             }
123         }
124         return null;
125     }
126     
127     public boolean equals(String textA, String textB) {
128         if (textA == null && textB == null)
129         {
130             return true;
131         }
132         return (textA != null) ? textA.equals(textB) : false;
133     }
134 
135     public Set<String> getTitleSet() {
136         // TODO: Check if this is really good, or if we should use the title (now jar name)?
137         return new TreeSet<String>(manifestInfoMap.keySet());
138     }
139     
140     /**
141      * Entry point for a {@link PackageInfoVisitor} which will visit all registered 
142      * {@link ManifestInfo} instances as if they are {@link PackageInfo} instances. 
143      *
144      * @param  visitor  The visitor.
145      * @see ManifestInfo#getPackageInfo()
146      */
147     //@Override
148     public void accept(PackageInfoVisitor visitor) {
149         for (Iterator<String> iter = new TreeSet<String>(manifestInfoMap.keySet()).iterator(); iter.hasNext();) {
150             String title = iter.next();
151             ManifestInfo manifestInfo = manifestInfoMap.get(title);
152             manifestInfo.getPackageInfo().accept(visitor);
153         }
154     }
155     
156     /**
157      * Entry point for a {@link ManifestInfoVisitor} which will visit all registered 
158      * {@link ManifestInfo} instances. 
159      * 
160      * @param visitor  The visitor.
161      */
162     //@Override
163     public void accept(ManifestInfoVisitor visitor) {
164         for (String jarPathStr : new TreeSet<String>(manifestInfoMap.keySet())) {
165             manifestInfoMap.get(jarPathStr).accept(visitor);
166         }
167     }
168     
169 }