|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  */ | 
|  |  | 
|  | /*****************************************************************************/ | 
|  | /*                    Copyright (c) IBM Corporation 1998                     */ | 
|  | /*                                                                           */ | 
|  | /* (C) Copyright IBM Corp. 1998                                              */ | 
|  | /*                                                                           */ | 
|  | /*****************************************************************************/ | 
|  |  | 
|  | package sun.rmi.rmic; | 
|  |  | 
|  | import java.io.File; | 
|  | import java.io.IOException; | 
|  | import java.io.OutputStream; | 
|  | import java.util.Collection; | 
|  | import java.util.Enumeration; | 
|  | import java.util.Iterator; | 
|  | import java.util.LinkedHashSet; | 
|  | import java.util.StringTokenizer; | 
|  | import java.util.Vector; | 
|  | import java.util.jar.JarFile; | 
|  | import java.util.jar.Manifest; | 
|  | import java.util.jar.Attributes; | 
|  | import sun.tools.java.ClassPath; | 
|  |  | 
|  | /** | 
|  |  * BatchEnvironment for rmic extends javac's version in four ways: | 
|  |  * 1. It overrides errorString() to handle looking for rmic-specific | 
|  |  * error messages in rmic's resource bundle | 
|  |  * 2. It provides a mechanism for recording intermediate generated | 
|  |  * files so that they can be deleted later. | 
|  |  * 3. It holds a reference to the Main instance so that generators | 
|  |  * can refer to it. | 
|  |  * 4. It provides access to the ClassPath passed to the constructor. | 
|  |  * | 
|  |  * WARNING: The contents of this source file are not part of any | 
|  |  * supported API.  Code that depends on them does so at its own risk: | 
|  |  * they are subject to change or removal without notice. | 
|  |  */ | 
|  |  | 
|  | public class BatchEnvironment extends sun.tools.javac.BatchEnvironment { | 
|  |  | 
|  |      | 
|  |     private Main main; | 
|  |  | 
|  |      | 
|  |  | 
|  |      */ | 
|  |     public static ClassPath createClassPath(String classPathString) { | 
|  |         ClassPath[] paths = classPaths(null, classPathString, null, null); | 
|  |         return paths[1]; | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |  | 
|  |      */ | 
|  |     public static ClassPath createClassPath(String classPathString, | 
|  |                                             String sysClassPathString, | 
|  |                                             String extDirsString) | 
|  |     { | 
|  |          | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |          */ | 
|  |         Path path = new Path(); | 
|  |  | 
|  |         if (sysClassPathString == null) { | 
|  |             sysClassPathString = System.getProperty("sun.boot.class.path"); | 
|  |         } | 
|  |         if (sysClassPathString != null) { | 
|  |             path.addFiles(sysClassPathString); | 
|  |         } | 
|  |  | 
|  |          | 
|  |  | 
|  |  | 
|  |          */ | 
|  |         path.expandJarClassPaths(true); | 
|  |  | 
|  |         if (extDirsString == null) { | 
|  |             extDirsString = System.getProperty("java.ext.dirs"); | 
|  |         } | 
|  |         if (extDirsString != null) { | 
|  |             path.addDirectories(extDirsString); | 
|  |         } | 
|  |  | 
|  |          | 
|  |  | 
|  |  | 
|  |          */ | 
|  |         path.emptyPathDefault("."); | 
|  |  | 
|  |         if (classPathString == null) { | 
|  |             // The env.class.path property is the user's CLASSPATH | 
|  |             // environment variable, and it set by the wrapper (ie, | 
|  |              | 
|  |             classPathString = System.getProperty("env.class.path"); | 
|  |             if (classPathString == null) { | 
|  |                 classPathString = "."; | 
|  |             } | 
|  |         } | 
|  |         path.addFiles(classPathString); | 
|  |  | 
|  |         return new ClassPath(path.toArray(new String[path.size()])); | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |  | 
|  |      */ | 
|  |     public BatchEnvironment(OutputStream out, ClassPath path, Main main) { | 
|  |         super(out, new ClassPath(""), path); | 
|  |                                  | 
|  |         this.main = main; | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |      */ | 
|  |     public Main getMain() { | 
|  |         return main; | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |      */ | 
|  |     public ClassPath getClassPath() { | 
|  |         return binaryPath; | 
|  |     } | 
|  |  | 
|  |      | 
|  |     private Vector<File> generatedFiles = new Vector<>(); | 
|  |  | 
|  |      | 
|  |  | 
|  |  | 
|  |      */ | 
|  |     public void addGeneratedFile(File file) { | 
|  |         generatedFiles.addElement(file); | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |  | 
|  |  | 
|  |      */ | 
|  |     public void deleteGeneratedFiles() { | 
|  |         synchronized(generatedFiles) { | 
|  |             Enumeration<File> enumeration = generatedFiles.elements(); | 
|  |             while (enumeration.hasMoreElements()) { | 
|  |                 File file = enumeration.nextElement(); | 
|  |                 file.delete(); | 
|  |             } | 
|  |             generatedFiles.removeAllElements(); | 
|  |         } | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |      */ | 
|  |     public void shutdown() { | 
|  |         main = null; | 
|  |         generatedFiles = null; | 
|  |         super.shutdown(); | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |      */ | 
|  |     public String errorString(String err, | 
|  |                               Object arg0, Object arg1, Object arg2) | 
|  |     { | 
|  |         if (err.startsWith("rmic.") || err.startsWith("warn.rmic.")) { | 
|  |             String result =  Main.getText(err, | 
|  |                                           (arg0 != null ? arg0.toString() : null), | 
|  |                                           (arg1 != null ? arg1.toString() : null), | 
|  |                                           (arg2 != null ? arg2.toString() : null)); | 
|  |  | 
|  |             if (err.startsWith("warn.")) { | 
|  |                 result = "warning: " + result; | 
|  |             } | 
|  |             return result; | 
|  |         } else { | 
|  |             return super.errorString(err, arg0, arg1, arg2); | 
|  |         } | 
|  |     } | 
|  |     public void reset() { | 
|  |     } | 
|  |  | 
|  |      | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |      */ | 
|  |     private static class Path extends LinkedHashSet<String> { | 
|  |         private static final long serialVersionUID = 0; | 
|  |         private static final boolean warn = false; | 
|  |  | 
|  |         private static class PathIterator implements Collection<String> { | 
|  |             private int pos = 0; | 
|  |             private final String path; | 
|  |             private final String emptyPathDefault; | 
|  |  | 
|  |             public PathIterator(String path, String emptyPathDefault) { | 
|  |                 this.path = path; | 
|  |                 this.emptyPathDefault = emptyPathDefault; | 
|  |             } | 
|  |             public PathIterator(String path) { this(path, null); } | 
|  |             public Iterator<String> iterator() { | 
|  |                 return new Iterator<String>() { | 
|  |                     public boolean hasNext() { | 
|  |                         return pos <= path.length(); | 
|  |                     } | 
|  |                     public String next() { | 
|  |                         int beg = pos; | 
|  |                         int end = path.indexOf(File.pathSeparator, beg); | 
|  |                         if (end == -1) | 
|  |                             end = path.length(); | 
|  |                         pos = end + 1; | 
|  |  | 
|  |                         if (beg == end && emptyPathDefault != null) | 
|  |                             return emptyPathDefault; | 
|  |                         else | 
|  |                             return path.substring(beg, end); | 
|  |                     } | 
|  |                     public void remove() { | 
|  |                         throw new UnsupportedOperationException(); | 
|  |                     } | 
|  |                 }; | 
|  |             } | 
|  |  | 
|  |              | 
|  |             public int size() { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean isEmpty() { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean contains(Object o) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public Object[] toArray() { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public <T> T[] toArray(T[] a) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean add(String o) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean remove(Object o) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean containsAll(Collection<?> c) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean addAll(Collection<? extends String> c) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean removeAll(Collection<?> c) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean retainAll(Collection<?> c) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public void clear() { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public boolean equals(Object o) { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |             public int hashCode() { | 
|  |                 throw new UnsupportedOperationException(); | 
|  |             } | 
|  |         } | 
|  |  | 
|  |          | 
|  |         private static boolean isZip(String name) { | 
|  |             return new File(name).isFile(); | 
|  |         } | 
|  |  | 
|  |         private boolean expandJarClassPaths = false; | 
|  |  | 
|  |         public Path expandJarClassPaths(boolean x) { | 
|  |             expandJarClassPaths = x; | 
|  |             return this; | 
|  |         } | 
|  |  | 
|  |          | 
|  |         private String emptyPathDefault = null; | 
|  |  | 
|  |         public Path emptyPathDefault(String x) { | 
|  |             emptyPathDefault = x; | 
|  |             return this; | 
|  |         } | 
|  |  | 
|  |         public Path() { super(); } | 
|  |  | 
|  |         public Path addDirectories(String dirs, boolean warn) { | 
|  |             if (dirs != null) | 
|  |                 for (String dir : new PathIterator(dirs)) | 
|  |                     addDirectory(dir, warn); | 
|  |             return this; | 
|  |         } | 
|  |  | 
|  |         public Path addDirectories(String dirs) { | 
|  |             return addDirectories(dirs, warn); | 
|  |         } | 
|  |  | 
|  |         private void addDirectory(String dir, boolean warn) { | 
|  |             if (! new File(dir).isDirectory()) { | 
|  | //              if (warn) | 
|  | //                  log.warning(Position.NOPOS, | 
|  |  | 
|  |                 return; | 
|  |             } | 
|  |  | 
|  |             for (String direntry : new File(dir).list()) { | 
|  |                 String canonicalized = direntry.toLowerCase(); | 
|  |                 if (canonicalized.endsWith(".jar") || | 
|  |                     canonicalized.endsWith(".zip")) | 
|  |                     addFile(dir + File.separator + direntry, warn); | 
|  |             } | 
|  |         } | 
|  |  | 
|  |         public Path addFiles(String files, boolean warn) { | 
|  |             if (files != null) | 
|  |                 for (String file : new PathIterator(files, emptyPathDefault)) | 
|  |                     addFile(file, warn); | 
|  |             return this; | 
|  |         } | 
|  |  | 
|  |         public Path addFiles(String files) { | 
|  |             return addFiles(files, warn); | 
|  |         } | 
|  |  | 
|  |         private void addFile(String file, boolean warn) { | 
|  |             if (contains(file)) { | 
|  |                  | 
|  |                 return; | 
|  |             } | 
|  |  | 
|  |             File ele = new File(file); | 
|  |             if (! ele.exists()) { | 
|  |                  | 
|  |                 if (warn) | 
|  | //                      log.warning(Position.NOPOS, | 
|  |  | 
|  |                     return; | 
|  |             } | 
|  |  | 
|  |             if (ele.isFile()) { | 
|  |                  | 
|  |                 String arcname = file.toLowerCase(); | 
|  |                 if (! (arcname.endsWith(".zip") || | 
|  |                        arcname.endsWith(".jar"))) { | 
|  |                     /* File name don't have right extension */ | 
|  | //                      if (warn) | 
|  | //                          log.warning(Position.NOPOS, | 
|  |  | 
|  |                     return; | 
|  |                 } | 
|  |             } | 
|  |  | 
|  |             /* Now what we have left is either a directory or a file name | 
|  |                confirming to archive naming convention */ | 
|  |  | 
|  |             super.add(file); | 
|  |             if (expandJarClassPaths && isZip(file)) | 
|  |                 addJarClassPath(file, warn); | 
|  |         } | 
|  |  | 
|  |         // Adds referenced classpath elements from a jar's Class-Path | 
|  |         // Manifest entry.  In some future release, we may want to | 
|  |         // update this code to recognize URLs rather than simple | 
|  |          | 
|  |         private void addJarClassPath(String jarFileName, boolean warn) { | 
|  |             try { | 
|  |                 String jarParent = new File(jarFileName).getParent(); | 
|  |                 JarFile jar = new JarFile(jarFileName); | 
|  |  | 
|  |                 try { | 
|  |                     Manifest man = jar.getManifest(); | 
|  |                     if (man == null) return; | 
|  |  | 
|  |                     Attributes attr = man.getMainAttributes(); | 
|  |                     if (attr == null) return; | 
|  |  | 
|  |                     String path = attr.getValue(Attributes.Name.CLASS_PATH); | 
|  |                     if (path == null) return; | 
|  |  | 
|  |                     for (StringTokenizer st = new StringTokenizer(path); | 
|  |                         st.hasMoreTokens();) { | 
|  |                         String elt = st.nextToken(); | 
|  |                         if (jarParent != null) | 
|  |                             elt = new File(jarParent, elt).getCanonicalPath(); | 
|  |                         addFile(elt, warn); | 
|  |                     } | 
|  |                 } finally { | 
|  |                     jar.close(); | 
|  |                 } | 
|  |             } catch (IOException e) { | 
|  | //              log.error(Position.NOPOS, | 
|  | //                        "error.reading.file", jarFileName, | 
|  | //                        e.getLocalizedMessage()); | 
|  |             } | 
|  |         } | 
|  |     } | 
|  | } |