Advertisement
Guest User

Untitled

a guest
May 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.34 KB | None | 0 0
  1. package com.side;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.jar.Attributes;
  10. import java.util.jar.JarEntry;
  11. import java.util.jar.JarOutputStream;
  12. import java.util.jar.Manifest;
  13. import javax.tools.JavaCompiler;
  14. import javax.tools.JavaFileObject;
  15. import javax.tools.StandardJavaFileManager;
  16. import javax.tools.ToolProvider;
  17.  
  18. /**
  19.  * Class to replace the functionality of the Sun JDK.
  20.  * Currently implements a replacement for javac, javadoc, and jar.
  21.  *
  22.  */
  23. public class JDKClass {
  24.  
  25.     private String homeDir;
  26.  
  27.     /**
  28.      * Default Constructor
  29.      */
  30.     public JDKClass() {
  31.     }
  32.  
  33.     /**
  34.      * Constructs a new JDKClass with the given absolute path to the home directory.
  35.      *
  36.      * @param homeDir Absolute path to home directory.
  37.      */
  38.     public JDKClass(String homeDir) {
  39.         this.homeDir = homeDir;
  40.     }
  41.  
  42.     /**
  43.      * Gets the current home directory.
  44.      *
  45.      * @return Home Directory
  46.      */
  47.     public String getHomeDir() {
  48.         return homeDir;
  49.     }
  50.  
  51.     /**
  52.      * Sets the home directory.
  53.      *
  54.      * @param homeDir Home Directory
  55.      */
  56.     public void setHomeDir(String homeDir) {
  57.         this.homeDir = homeDir;
  58.     }
  59.  
  60.     /**
  61.      * Searches a directory recursively for java source files. Uses wildcards in paths
  62.      *
  63.      * @param buffer The buffer to append the path of the .java files in a directory, deliminated by ;
  64.      * @param srcDir Directory File.
  65.      */
  66.     private static void findJavaRecursive(List<String> buffer, File srcDir) {
  67.         File[] javaFiles = srcDir.listFiles();
  68.         for (File file : javaFiles) {
  69.             if (file.getName().endsWith(".java")) {
  70.                 buffer.add(file.getAbsolutePath());
  71.                 continue;
  72.             }
  73.             if (file.isDirectory()) {
  74.                 findJavaRecursive(buffer, file);
  75.                 continue;
  76.             }
  77.         }
  78.     }
  79.  
  80.     private static void findRecursive(List<File> buffer, File dir) {
  81.         File[] javaFiles = dir.listFiles();
  82.         for (File file : javaFiles) {
  83.             if (file.getName().endsWith(".class") || file.getName().endsWith(".java")) {
  84.                 buffer.add(file);
  85.                 continue;
  86.             }
  87.             if (file.isDirectory()) {
  88.                 findRecursive(buffer, file);
  89.                 continue;
  90.             }
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * This compiles the java source files using the newer javax.tools.JavaCompiler
  96.      * as the other is deprecated.
  97.      *
  98.      * @param srcDir The source directory.
  99.      * @param binDir The build directory.
  100.      * @param classPath The class path.
  101.      * @return
  102.      */
  103.     public boolean compile(String srcDir, String binDir, String classPath) {
  104.         boolean success = false;
  105.         try {
  106.             //Arrays.asList("-classpath", System.getProperty("java.class.path"));
  107.             List<String> files = new LinkedList<String>();
  108.             List<String> args = new LinkedList<String>();
  109.             args.add("-cp");
  110.             args.add(classPath.replaceAll("[:;]", System.getProperty("path.separator")));
  111.             args.add("-d");
  112.             args.add(homeDir + binDir);
  113.             findJavaRecursive(files, new File(homeDir + srcDir));
  114.             JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  115.             StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
  116.             Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files);
  117.             success = compiler.getTask(null, fileManager, null, args, null, compilationUnits).call();
  118.             fileManager.close();
  119.         } catch (IOException ex) {
  120.             return false;
  121.         }
  122.         return success;
  123.     }
  124.  
  125.     /**
  126.      * This produces javadocs in the javadoc subdirectory of the home directory.
  127.      *
  128.      * @param srcDir The subdirectory of the home directory where the source files reside.
  129.      * @return True if javadoc'n was succesful, false otherwise.
  130.      */
  131.     public boolean javaDoc(String srcDir) {
  132.         if (homeDir == null) {
  133.             return false;
  134.         }
  135.  
  136.         List<String> args = new LinkedList<String>();
  137.         args.add("-d");
  138.         args.add(homeDir + "javadoc/");
  139.         findJavaRecursive(args, new File(homeDir + srcDir));
  140.  
  141.         StringBuilder param = new StringBuilder().append("javadoc");
  142.         for (String a : args) {
  143.             param.append(' ').append(a);
  144.         }
  145.         System.out.println(param);
  146.  
  147.         int exitCode = com.sun.tools.javadoc.Main.execute(args.toArray(new String[0]));
  148.         return exitCode == 0;
  149.     }
  150.  
  151.     /**
  152.      * This produces a jar file in the home directory, complete with a manifest attribute of Main-Class:
  153.      *
  154.      * @param name      Name of the jar file to create.
  155.      * @param binDir   The first subdirectory of home directory of whose content you want in the jar file.
  156.      * @param srcDir   The second subdirectory of home directory of whose content you want in the jar file.
  157.      * @param mainClass Main Class to put in the manifest.
  158.      * @return True if the jar was created succesfully, false otherwise.
  159.      */
  160.     public boolean jar(String name, String binDir, String mainClass, String srcDir) {
  161.         if (homeDir == null) {
  162.             return false;
  163.         }
  164.         if (!name.endsWith(".jar")) {
  165.             name += ".jar";
  166.         }
  167.         File archiveFile = new File(homeDir + name);
  168.         List<File> toBeJaredList = new LinkedList<File>();
  169.         if (binDir != null) {
  170.             findRecursive(toBeJaredList, new File(homeDir + binDir));
  171.         }
  172.         if (srcDir != null) {
  173.             findRecursive(toBeJaredList, new File(homeDir + srcDir));
  174.         }
  175.         try {
  176.             byte[] buffer = new byte[10240];
  177.             FileOutputStream stream = new FileOutputStream(archiveFile);
  178.             Manifest manifest = new Manifest();
  179.             if (mainClass != null) {
  180.                 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
  181.                 manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
  182.  
  183.             }
  184.             JarOutputStream out = new JarOutputStream(stream, manifest);
  185.             for (File tobeJared : toBeJaredList) {
  186.                 if (tobeJared == null || !tobeJared.exists()
  187.                         || tobeJared.isDirectory()) {
  188.                     continue;
  189.                 }
  190.                 System.out.println("Compressing and adding " + tobeJared.getName());
  191.  
  192.                 JarEntry jarAdd = new JarEntry(tobeJared.getName());
  193.                 jarAdd.setTime(tobeJared.lastModified());
  194.                 out.putNextEntry(jarAdd);
  195.                 FileInputStream in = new FileInputStream(tobeJared);
  196.                 while (true) {
  197.                     int nRead = in.read(buffer, 0, buffer.length);
  198.                     if (nRead <= 0) {
  199.                         break;
  200.                     }
  201.                     out.write(buffer, 0, nRead);
  202.                 }
  203.                 in.close();
  204.             }
  205.  
  206.             out.close();
  207.             stream.close();
  208.             return true;
  209.         } catch (Exception ex) {
  210.             ex.printStackTrace();
  211.             return false;
  212.         }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement