1 package net.sourceforge.argval.packageinfo.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.jar.JarFile; 12 13 import net.sourceforge.argval.manifestinfo.impl.ManifestInfoAsPackageInfo; 14 import net.sourceforge.argval.packageinfo.PackageInfo; 15 import net.sourceforge.argval.packageinfo.PackageInfoManager; 16 import net.sourceforge.argval.packageinfo.PackageInfoVisitor; 17 import net.sourceforge.argval.packageinfo.PackageInfoVisitorAcceptor; 18 19 import org.slf4j.Logger; 20 import org.slf4j.LoggerFactory; 21 22 23 /** 24 * Utility for visiting the added <code>Package</code> instances. 25 * First add the <code>Package</code> instances. Then use a 26 * <code>PackageVisitor</code> instance to visit all the 27 * <code>PackageInfo</code> instances. 28 * 29 * <p> 30 * Usage: 31 * <pre> 32 * PackageVisitor visitor = new SimplePackageVisitor(); 33 * PackageInfoManager manager = new PackageInfoManager(); 34 * manager.addPackage(Package.getPackages()); 35 * manager.accept(visitor); 36 * System.out.println(visitor);</pre> 37 * </p> 38 * 39 * @author <a href="http://sourceforge.net/users/verhagent/">T. Verhagen</a> 40 */ 41 final public class PackageInfoManagerImpl implements PackageInfoVisitorAcceptor, PackageInfoManager { 42 /** The logging instance. */ 43 protected static Logger logger = LoggerFactory.getLogger(PackageInfoManagerImpl.class); 44 /** */ 45 private Map<String, PackageInfo> packageInfoMap; 46 // /** */ 47 // private Map<String, ManifestInfo> manifestInfoMap; 48 49 50 /** 51 * Default constructor. 52 */ 53 public PackageInfoManagerImpl() { 54 super(); 55 packageInfoMap = new HashMap<String, PackageInfo>(); 56 // manifestInfoMap = new HashMap<String, ManifestInfo>(); 57 } 58 59 60 /* (non-Javadoc) 61 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#addPackage(java.lang.Package[]) 62 */ 63 public void addPackage(Package[] packageArray) { 64 for (int index = 0; index < packageArray.length; index++) { 65 addPackage(packageArray[index]); 66 } 67 } 68 69 70 /* (non-Javadoc) 71 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#addPackage(java.lang.Package) 72 */ 73 public void addPackage(Package pckg) { 74 PackageInfoImpl packageInfo = new PackageInfoImpl(pckg); 75 addPackageInfo(packageInfo, pckg.getName()); 76 } 77 78 /* (non-Javadoc) 79 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#addPackage(java.util.jar.JarFile) 80 */ 81 public void addPackage(JarFile jarFile) { 82 try { 83 addPackageInfo(new ManifestInfoAsPackageInfo(new File(jarFile.getName()), jarFile.getManifest()), 84 null); 85 } 86 catch (IOException e) { 87 // TODO Auto-generated catch block 88 e.printStackTrace(); 89 } 90 } 91 92 // public void addJar(String classPath) { 93 // String[] pathArray = classPath.split(System.getProperty(SystemConstants.PATH_SEPARATOR)); 94 // for (String pathStr : pathArray) { 95 // if (pathStr.toLowerCase().endsWith(".jar")) { 96 // File path = new File(pathStr); 97 // addJar(path); 98 // } 99 // } 100 // if (logger.isTraceEnabled()) { 101 // // FIXME Make use of CollectionUtils.toString(StringbUilder, Object[]) as this class is using StringBuilder. 102 // StringBuilder builder = new StringBuilder(); 103 // for (String pathStr : pathArray) { 104 // builder.append(pathStr).append(StringUtil.LINE_SEPARATOR); 105 // } 106 // logger.trace("Added Jar-Manifest from class path entries:" 107 // + StringUtil.LINE_SEPARATOR + builder.toString()); 108 // } 109 // 110 // } 111 // 112 // public void addJar(File path) { 113 // if (! path.isFile()) { 114 // logger.warn("Given jar path is not a file"); 115 // return; 116 // } 117 // try { 118 // JarFile jarFile = new JarFile(path); 119 // Manifest manifest = jarFile.getManifest(); 120 // ManifestInfo manifestInfo = new ManifestInfoAsPackageInfo(path, manifest); 121 // addManifestInfo(manifestInfo); 122 // } 123 // catch (IOException e) { 124 // // TODO Auto-generated catch block 125 // e.printStackTrace(); 126 // } 127 // } 128 129 130 // private void addManifestInfo(ManifestInfo manifestInfo) { 131 // this.manifestInfoMap.put(manifestInfo.getJarPath().toString(), manifestInfo); 132 // } 133 // 134 // public ManifestInfo getManifestInfo(PackageInfo packageInfo) { 135 // for (Entry<String, ManifestInfo> entry : manifestInfoMap.entrySet()) { 136 // entry.getKey(); 137 // ManifestInfo manifestInfo = entry.getValue(); 138 // if (manifestInfo != null 139 // && equals(packageInfo.getSpecificationTitle(), manifestInfo.getSpecificationTitle()) 140 // && equals(packageInfo.getSpecificationVersion(), manifestInfo.getSpecificationVersion()) 141 // && equals(packageInfo.getSpecificationVendor(), manifestInfo.getSpecificationVendor())) { 142 // return manifestInfo; 143 // } 144 // } 145 // return null; 146 // } 147 // 148 // public ManifestInfo getManifestInfo(String regularExpression) { 149 // for (Entry<String, ManifestInfo> entry : this.manifestInfoMap.entrySet()) { 150 // if (Pattern.matches(regularExpression, entry.getKey())) { 151 // return entry.getValue(); 152 // } 153 // } 154 // logger.info("No Manifest Information found for file based expression '" + regularExpression + "'."); 155 // return null; 156 // } 157 158 159 // private boolean equals(String textA, String textB) { 160 // if (textA == null && textB == null) 161 // { 162 // return true; 163 // } 164 // return (textA != null) ? textA.equals(textB) : false; 165 // } 166 167 168 /* (non-Javadoc) 169 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#addPackageInfo(net.sourceforge.argval.packageinfo.PackageInfo) 170 */ 171 public void addPackageInfo(PackageInfo packageInfo) { 172 // Check if the given PackageInfo instance, is already known. 173 // If not add it. 174 if (! packageInfoMap.containsKey(packageInfo.getTitle())) { 175 packageInfoMap.put(packageInfo.getTitle(), packageInfo); 176 } 177 } 178 /* (non-Javadoc) 179 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#addPackageInfo(net.sourceforge.argval.packageinfo.PackageInfo, java.lang.String) 180 */ 181 public void addPackageInfo(PackageInfo packageInfo, String packageName) { 182 addPackageInfo(packageInfo); 183 // Add the 'package name' to the PackageInfo instance. 184 if (packageName != null) { 185 packageInfoMap.get(packageInfo.getTitle()).addPackageName(packageName); 186 } 187 } 188 189 /* (non-Javadoc) 190 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#getTitleSet() 191 */ 192 public Set<String> getTitleSet() { 193 return new TreeSet<String>(packageInfoMap.keySet()); 194 } 195 196 197 /* (non-Javadoc) 198 * @see net.sourceforge.argval.packageinfo.PackageInfoManager#getPackage(java.lang.String) 199 */ 200 public PackageInfo getPackage(String title) { 201 return (PackageInfo)packageInfoMap.get(title); 202 } 203 204 205 /** 206 * Entry point for the visitor of this class. All added PackageInfo instance 207 * are visited through their {@link PackageInfo#accept(PackageInfoVisitor)} method. 208 * 209 * @param visitor The visitor. 210 */ 211 public void acceptRuntime(PackageInfoVisitor visitor) { 212 for (Iterator<String> iter = new TreeSet<String>(packageInfoMap.keySet()).iterator(); iter.hasNext();) { 213 String title = (String) iter.next(); 214 PackageInfo packageInfo = (PackageInfo)packageInfoMap.get(title); 215 packageInfo.accept(visitor); 216 } 217 } 218 /** 219 * @see #acceptRuntime(PackageInfoVisitor) 220 */ 221 public void accept(PackageInfoVisitor visitor) { 222 acceptRuntime(visitor); 223 } 224 225 // /** 226 // * Entry point for a {@link PackageVisitor} which will visit all registered 227 // * {@link ManifestInfo} instances as if they are {@link PackageInfo} instances. 228 // * 229 // * @param visitor The visitor. 230 // * @see ManifestInfo#getPackageInfo() 231 // */ 232 // public void acceptClassPath(PackageVisitor visitor) { 233 // for (Iterator<String> iter = new TreeSet<String>(manifestInfoMap.keySet()).iterator(); iter.hasNext();) { 234 // String title = (String) iter.next(); 235 // ManifestInfo manifestInfo = (ManifestInfo)manifestInfoMap.get(title); 236 // manifestInfo.getPackageInfo().accept(visitor); 237 // } 238 // } 239 240 241 // /** 242 // * Entry point for a {@link ManifestInfoVisitor} which will visit all registered 243 // * {@link ManifestInfo} instances. 244 // * 245 // * @param visitor The visitor. 246 // */ 247 // public void accept(ManifestInfoVisitor visitor) { 248 // for (String jarPathStr : new TreeSet<String>(manifestInfoMap.keySet())) { 249 // manifestInfoMap.get(jarPathStr).accept(visitor); 250 // } 251 // } 252 253 /** 254 * Returns a plain text showing all the added <code>Package</code> instances. 255 * 256 * @return A plain text showing all <code>Package</code> instances added to 257 * this PackageInfoManager. 258 */ 259 public String toString() { 260 return new SimplePackageInfoVisitor().toString(); 261 } 262 263 }