Math2mat
|
00001 package m2m.backend.buildingblocks; 00002 00003 import java.util.*; 00004 import java.io.*; 00005 00006 import m2m.backend.utils.FileUtils; 00007 00008 00009 public class BuildingBlocksManager { 00010 00011 private static BuildingBlocksManager instance = null; 00012 protected String m_dirName; 00013 protected String m_packageName; 00014 00015 private BuildingBlocksManager() { 00016 this.m_dirName = "m2m/backend/buildingblocks/blocks"; 00017 this.m_packageName = "m2m.backend.buildingblocks.blocks"; 00018 this.m_list = new ArrayList<BuildingBlock>(); 00019 this.m_loader = BuildingBlockLoader.getInstance(); 00020 } 00021 00022 public static BuildingBlocksManager getInstance() { 00023 if (instance == null) { 00024 instance = new BuildingBlocksManager(); 00025 } 00026 return instance; 00027 } 00028 00029 public void setDirName(String dirName) { 00030 this.m_dirName = dirName; 00031 this.m_loader.setDir(dirName); 00032 } 00033 00034 public void setPackageName(String packageName) { 00035 this.m_packageName = packageName; 00036 this.m_loader.setPackageName(packageName); 00037 } 00038 00039 public String getDirName() { 00040 return this.m_dirName; 00041 } 00042 00043 public String getPackageName() { 00044 return this.m_packageName; 00045 } 00046 00047 class ClassFilter implements FileFilter { 00048 00049 public boolean accept(File pathname) { 00050 try { 00051 return pathname.getCanonicalPath().endsWith(".class"); 00052 } catch (IOException e) { 00053 return false; 00054 } 00055 } 00056 } 00057 00058 class JavaFilter implements FileFilter { 00059 00060 public boolean accept(File pathname) { 00061 try { 00062 return pathname.getCanonicalPath().endsWith(".java"); 00063 } catch (IOException e) { 00064 return false; 00065 } 00066 } 00067 } 00068 protected ArrayList<BuildingBlock> m_list; 00069 protected BuildingBlockLoader m_loader; 00070 00071 public boolean importFile(String fileName) { 00072 File f = new File(fileName); 00073 String destFileName = this.m_dirName + "/" + f.getName(); 00074 FileUtils.copyFile(fileName, destFileName); 00075 File destFile = new File(destFileName); 00076 String className; 00077 if (f.getName().endsWith(".java")) { 00078 compileFile(destFileName); 00079 className = destFile.getName().replace(".java", ""); 00080 } else { 00081 className = destFile.getName().replace(".class", ""); 00082 } 00083 return load(className); 00084 } 00085 00086 public boolean compileFile(String fileName) { 00087 try { 00088 System.out.println("File to compile: " + fileName); 00089 ProcessBuilder pb = new ProcessBuilder("javac", fileName); 00090 pb.redirectErrorStream(true); 00091 Process p = pb.start(); 00092 int exitCode = p.waitFor(); 00093 00094 final BufferedReader br = new BufferedReader(new InputStreamReader( 00095 p.getInputStream()), 50 /* keep small for testing */); 00096 String line; 00097 while ((line = br.readLine()) != null) { 00098 System.out.println(line); 00099 } 00100 br.close(); 00101 00102 if (exitCode != 0) { 00103 System.out.println("The file " + fileName + " could not be compiled"); 00104 return false; 00105 } 00106 } catch (Exception e) { 00107 System.out.println("Error with the file compilation. " + 00108 "Please check that javac is accessible in the current Path"); 00109 return false; 00110 } 00111 return true; 00112 } 00113 00114 public boolean load(BuildingBlockLoader loader, String fileName) { 00115 BuildingBlock block; 00116 String className = fileName; 00117 className = className.replaceFirst(".class", ""); 00118 try { 00119 block = loader.newInstance(className); 00120 System.out.println("--------------------------------------"); 00121 System.out.println("Adding a new building block:"); 00122 System.out.println("Function name :" + block.functionName()); 00123 System.out.println("Entity name :" + block.entityName()); 00124 System.out.println("Author :" + block.author()); 00125 System.out.println("Description :" + block.description()); 00126 System.out.println("--------------------------------------"); 00127 addBuildingBlock(block); 00128 return true; 00129 } catch (Exception e) { 00130 System.out.println("Caught exception for class " + className + ": " + e); 00131 return false; 00132 } 00133 } 00134 00135 public boolean load(String fileName) { 00136 if (this.m_loader != null) { 00137 return load(this.m_loader, fileName); 00138 } else { 00139 return load(this.m_loader = BuildingBlockLoader.getInstance(), fileName); 00140 } 00141 } 00142 00143 public boolean compileAll() { 00144 File dir = new File(this.m_dirName); 00145 File[] files = dir.listFiles(new JavaFilter()); 00146 boolean ok = true; 00147 for (int i = 0; i < files.length; i++) { 00148 try { 00149 ok &= compileFile(files[i].getCanonicalPath()); 00150 } catch (IOException e) { 00151 ok = false; 00152 } 00153 } 00154 return ok; 00155 } 00156 00157 public boolean loadAll() { 00158 File dir = new File(this.m_dirName); 00159 System.out.println("Load blocks from folder: "+dir.getAbsolutePath()); 00160 File[] files = dir.listFiles(new ClassFilter()); 00161 if (files==null) 00162 return false; 00163 for (int i = 0; i < files.length; i++) { 00164 load(files[i].getName()); 00165 } 00166 return true; 00167 } 00168 00169 public void addBuildingBlock(BuildingBlock block) { 00170 this.m_list.add(block); 00171 } 00172 00173 public List<BuildingBlock> blockNamed(String functionName) { 00174 List<BuildingBlock> res = new ArrayList<BuildingBlock>(); 00175 for (BuildingBlock block : m_list) { 00176 if (block.functionName().equals(functionName)) { 00177 res.add(block); 00178 } 00179 } 00180 return res; 00181 } 00182 }