| 1 package net.sf.invicta.dumper; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileNotFoundException ; 6 import java.io.FileOutputStream ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.io.PrintWriter ; 10 11 import net.sf.invicta.InvictaException; 12 import net.sf.invicta.Logger; 13 import net.sf.invicta.api.InvictaProject; 14 import net.sf.invicta.process.ConfigurationManager; 15 16 21 public abstract class InvictaBasicDumper implements InvictaDumper { 22 public final static String DUMPER_PROPERTY_PREFIX = "invicta.dumpers"; 23 public final static String PROPERTY_SEPARATOR = "."; 24 public final static String DUMP_ENABLED_PROPERTY = "enabled"; 25 public final static String DUMP_FILE_PROPERTY = "file"; 26 27 protected InvictaProject project; 28 protected ConfigurationManager configurationManager; 29 30 31 36 public void init(ConfigurationManager configurationManager) { 37 this.configurationManager = configurationManager; 38 } 39 40 43 public InvictaBasicDumper() { 44 super(); 45 } 46 47 51 public void setProject(InvictaProject project) { 52 this.project = project; 53 } 54 55 61 public void dump() throws InvictaException { 62 63 String fileName = getFileName(); 64 String dumpContent = getDumpContent(); 65 66 if (areFilesIdentical(fileName, dumpContent)) { 67 log("No need to modify file ('" 68 + fileName + "')"); 69 return; 70 } 71 72 writeContentToFile(fileName, dumpContent); 73 74 log("File ('" + fileName + "') was created successfully !"); 75 } 76 77 83 public boolean isEnabled() throws InvictaException { 84 try { 85 String valueStr = getRequiredProperty(DUMP_ENABLED_PROPERTY); 86 return Boolean.valueOf(valueStr).booleanValue(); 87 } catch (InvictaException e) { 88 return false; 89 } 90 } 91 92 97 protected boolean areFilesIdentical(String fileName, String dumpContent) 98 throws InvictaDumperException { 99 File file = new File (fileName); 100 if (!file.exists()) 101 return false; 102 103 byte[] buffer = new byte[1024]; 104 InputStream inputStream; 105 StringBuffer fileContent = new StringBuffer (); 106 try { 107 inputStream = new FileInputStream (file); 108 int res = 0; 109 while ((res = inputStream.read(buffer)) != -1) 110 fileContent.append(new String (buffer, 0, res)); 111 inputStream.close(); 112 } catch (FileNotFoundException e) { 113 return false; 114 } catch (IOException e) { 115 return false; 116 } 117 118 return fileContent.toString().equals(dumpContent); 119 } 120 121 127 protected void writeContentToFile(String fileName, String content) 128 throws InvictaDumperException { 129 File outputFile = new File (fileName); 130 FileOutputStream outputStream; 131 try { 132 outputStream = new FileOutputStream (outputFile); 133 } catch (FileNotFoundException e) { 134 throw new InvictaDumperException(this, 135 "Error in writing to output file: " + outputFile); 136 } 137 PrintWriter writer = new PrintWriter (outputStream); 138 writer.print(content); 139 writer.close(); 140 } 141 142 149 protected String getRequiredProperty(String propertyName) throws InvictaException { 150 return this.configurationManager.getRequiredProperty( 151 getFullPropertyName(propertyName)); 152 } 153 154 161 protected String getProperty(String propertyName) throws InvictaException { 162 return this.configurationManager.getProperty( 163 getFullPropertyName(propertyName)); 164 } 165 166 173 protected String getFullPropertyName(String propertyName) { 174 return DUMPER_PROPERTY_PREFIX 175 + PROPERTY_SEPARATOR 176 + getName() 177 + PROPERTY_SEPARATOR 178 + propertyName; 179 } 180 181 188 public boolean shouldForceRunning() throws InvictaException { 189 String fileName = getFileName(); 190 File outputFile = new File (fileName); 191 if (!outputFile.exists()) { 192 log("Output file ('" 193 + fileName 194 + "') not found. Forcing Invicta execution."); 195 return true; 196 } 197 return false; 198 } 199 200 205 protected String getFileName() throws InvictaException { 206 return getRequiredProperty(DUMP_FILE_PROPERTY); 207 } 208 209 213 protected InvictaProject getProject() { 214 return this.project; 215 } 216 217 221 protected ConfigurationManager getConfigurationManager() { 222 return this.configurationManager; 223 } 224 225 229 protected void log(String message) { 230 Logger.info(getName() + ": " + message); 231 } 232 233 237 protected void debug(String message) { 238 Logger.info(getName() + ": " + message); 239 } 240 241 247 protected abstract String getDumpContent() throws InvictaException; 248 249 } 250 | Popular Tags |