KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > dumper > InvictaBasicDumper


1 package net.sf.invicta.dumper;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.PrintWriter JavaDoc;
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 /**
17  * An abstract class of a dumper. Each specific dumper implementation
18  * should extend this class that provides useful methods for creating
19  * a dumper.
20  */

21 public abstract class InvictaBasicDumper implements InvictaDumper {
22     public final static String JavaDoc DUMPER_PROPERTY_PREFIX = "invicta.dumpers";
23     public final static String JavaDoc PROPERTY_SEPARATOR = ".";
24     public final static String JavaDoc DUMP_ENABLED_PROPERTY = "enabled";
25     public final static String JavaDoc DUMP_FILE_PROPERTY = "file";
26
27     protected InvictaProject project;
28     protected ConfigurationManager configurationManager;
29
30
31     /**
32      * Initialization of the dumper with the configuration manager
33      * of Invicta process.
34      * @param configurationManager
35      */

36     public void init(ConfigurationManager configurationManager) {
37         this.configurationManager = configurationManager;
38     }
39
40     /**
41      * Constructor for InvictaBasicDumper.
42      */

43     public InvictaBasicDumper() {
44         super();
45     }
46
47     /**
48      * Sets the project that should be dumped.
49      * @param project
50      */

51     public void setProject(InvictaProject project) {
52         this.project = project;
53     }
54
55     /**
56      * Default implementation of the dumping: gets file name from
57      * a property, gets the dump content (using an abstract method),
58      * checks whether the content of the file was changed and writes
59      * the updated content to the file.
60      */

61     public void dump() throws InvictaException {
62         
63         String JavaDoc fileName = getFileName();
64         String JavaDoc 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     /**
78      * Returns whether this dumper is enabled. Reads the property
79      * 'enabled' of this dumper.
80      * @return boolean
81      * @throws InvictaException
82      */

83     public boolean isEnabled() throws InvictaException {
84         try {
85             String JavaDoc valueStr = getRequiredProperty(DUMP_ENABLED_PROPERTY);
86             return Boolean.valueOf(valueStr).booleanValue();
87         } catch (InvictaException e) {
88             return false;
89         }
90     }
91
92     /**
93      * Receives a file name and a dump content.
94      * If the file exists, it compares between the file and the dump
95      * content.
96      */

97     protected boolean areFilesIdentical(String JavaDoc fileName, String JavaDoc dumpContent)
98         throws InvictaDumperException {
99         File JavaDoc file = new File JavaDoc(fileName);
100         if (!file.exists())
101             return false;
102
103         byte[] buffer = new byte[1024];
104         InputStream JavaDoc inputStream;
105         StringBuffer JavaDoc fileContent = new StringBuffer JavaDoc();
106         try {
107             inputStream = new FileInputStream JavaDoc(file);
108             int res = 0;
109             while ((res = inputStream.read(buffer)) != -1)
110                 fileContent.append(new String JavaDoc(buffer, 0, res));
111             inputStream.close();
112         } catch (FileNotFoundException JavaDoc e) {
113             return false;
114         } catch (IOException JavaDoc e) {
115             return false;
116         }
117
118         return fileContent.toString().equals(dumpContent);
119     }
120
121     /**
122      * Writes the given content into the given file name.
123      * @param fileName
124      * @param content
125      * @throws InvictaDumperException
126      */

127     protected void writeContentToFile(String JavaDoc fileName, String JavaDoc content)
128         throws InvictaDumperException {
129         File JavaDoc outputFile = new File JavaDoc(fileName);
130         FileOutputStream JavaDoc outputStream;
131         try {
132             outputStream = new FileOutputStream JavaDoc(outputFile);
133         } catch (FileNotFoundException JavaDoc e) {
134             throw new InvictaDumperException(this,
135                 "Error in writing to output file: " + outputFile);
136         }
137         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(outputStream);
138         writer.print(content);
139         writer.close();
140     }
141
142     /**
143      * Returns the value of a property of this dumper. Throws an exception
144      * if the property is not defined.
145      * @param propertyName
146      * @return String
147      * @throws InvictaException
148      */

149     protected String JavaDoc getRequiredProperty(String JavaDoc propertyName) throws InvictaException {
150         return this.configurationManager.getRequiredProperty(
151             getFullPropertyName(propertyName));
152     }
153
154     /**
155      * Returns the value of a property of this dumper or null if the
156      * property is not defined.
157      * @param propertyName
158      * @return String
159      * @throws InvictaException
160      */

161     protected String JavaDoc getProperty(String JavaDoc propertyName) throws InvictaException {
162         return this.configurationManager.getProperty(
163             getFullPropertyName(propertyName));
164     }
165
166     /**
167      * Returns the full name of a property with the given short name.
168      * For example: 'invicta.dumpers.ant.file' will be returned for the
169      * 'file' short name of the 'ant' dumper.
170      * @param propertyName
171      * @return String
172      */

173     protected String JavaDoc getFullPropertyName(String JavaDoc propertyName) {
174         return DUMPER_PROPERTY_PREFIX
175                 + PROPERTY_SEPARATOR
176                 + getName()
177                 + PROPERTY_SEPARATOR
178                 + propertyName;
179     }
180
181     /**
182      * Returns whether this dumper should force Invicta processing.
183      * This default implementation checks whether the output file is
184      * missing.
185      * @return boolean
186      * @throws InvictaException
187      */

188     public boolean shouldForceRunning() throws InvictaException {
189         String JavaDoc fileName = getFileName();
190         File JavaDoc outputFile = new File JavaDoc(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     /**
201      * Returns the name of the file to dump.
202      * @return Strign
203      * @throws InvictaException
204      */

205     protected String JavaDoc getFileName() throws InvictaException {
206         return getRequiredProperty(DUMP_FILE_PROPERTY);
207     }
208
209     /**
210      * Returns the project that should be dumped.
211      * @return InvictaProject
212      */

213     protected InvictaProject getProject() {
214         return this.project;
215     }
216
217     /**
218      * Returns the configuration manager of Invicta process.
219      * @return ConfigurationManager
220      */

221     protected ConfigurationManager getConfigurationManager() {
222         return this.configurationManager;
223     }
224     
225     /**
226      * Writes an info message to the log.
227      * @param message
228      */

229     protected void log(String JavaDoc message) {
230         Logger.info(getName() + ": " + message);
231     }
232     
233     /**
234      * Writes a debug message to the log.
235      * @param message
236      */

237     protected void debug(String JavaDoc message) {
238         Logger.info(getName() + ": " + message);
239     }
240     
241     /**
242      * An abstract method. Returns the content of the output file that
243      * this dumper should write.
244      * @return String
245      * @throws InvictaException
246      */

247     protected abstract String JavaDoc getDumpContent() throws InvictaException;
248     
249 }
250
Popular Tags