added simconfig attribute EXPORT that determines how files should be handled when a simulation is exported as an executable JAR

This commit is contained in:
fros4943 2010-03-19 15:02:26 +00:00
parent e99c906caf
commit e83635832a

View file

@ -24,26 +24,28 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: ExecuteJAR.java,v 1.9 2010/03/19 12:34:37 fros4943 Exp $
* $Id: ExecuteJAR.java,v 1.10 2010/03/19 15:02:26 fros4943 Exp $
*/
package se.sics.cooja.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.security.AccessControlException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
@ -63,7 +65,6 @@ public class ExecuteJAR {
public final static String SIMCONFIG_FILENAME = "simulation.csc";
public final static String EXTERNALTOOLS_FILENAME = "exttools.config";
public final static String PROJECT_DEFAULT_CONFIG_FILENAME = "cooja_default.config";
public final static String FIRMWARE_SUFFIX = ".sky";
public static void main(String[] args) {
try {
@ -113,7 +114,7 @@ public class ExecuteJAR {
try {
buildExecutableJAR(s.getGUI(), jar);
} catch (RuntimeException e) {
logger.fatal(e.getMessage());
logger.fatal(e.getMessage(), e);
System.exit(1);
}
System.exit(0);
@ -176,33 +177,10 @@ public class ExecuteJAR {
}
GUI.externalToolsUserSettingsFile = diskFile;
int firmwareCounter = 0;
while (true) {
File firmwareFile = new File(firmwareCounter + FIRMWARE_SUFFIX);
diskFile = new File(executeDir, firmwareCounter + FIRMWARE_SUFFIX);
firmwareCounter++;
if (OVERWRITE || !diskFile.exists()) {
logger.info("Unpacking firmware file: " + firmwareFile.getName() + " -> " + diskFile.getName());
inputStream = GUI.class.getResourceAsStream("/" + firmwareFile.getName());
if (inputStream == null) {
/* Not available in simulation */
break;
}
byte[] fileData = ArrayUtils.readFromStream(inputStream);
if (fileData == null) {
logger.info("Failed extracting file (read fail)");
System.exit(1);
}
boolean ok = ArrayUtils.writeToFile(diskFile, fileData);
if (!ok) {
logger.info("Failed extracting file (write fail)");
System.exit(1);
}
} else {
logger.info("Skip: firmware files already exist: " + diskFile);
break;
}
}
/* Unpack files from JAR (with attribute EXPORT=copy) */
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(executeDir, SIMCONFIG_FILENAME));
handleExportAttributesFromJAR(doc.getRootElement(), new File(executeDir, SIMCONFIG_FILENAME), new File(executeDir));
} catch (Exception e) {
logger.fatal("Error when unpacking executable JAR: " + e.getMessage());
return;
@ -334,49 +312,7 @@ public class ExecuteJAR {
/* Prepare simulation config */
Element rootElement = gui.extractSimulationConfig();
logger.info("Extracting simulation configuration");
logger.info("Simconfig: Stripping project references");
rootElement.removeChildren("project"); /* Remove project references */
for (Object simElement : rootElement.getChildren()) {
if (((Element) simElement).getName().equals("simulation")) {
int firmwareCounter = 0;
for (Object moteTypeElement : ((Element)simElement).getChildren()) {
if (((Element) moteTypeElement).getName().equals("motetype")) {
logger.info("Simconfig: Stripping source references");
logger.info("Simconfig: Stripping build commands");
((Element)moteTypeElement).removeChildren("source"); /* Remove source references */
((Element)moteTypeElement).removeChildren("commands"); /* Remove build commands */
for (Object pathElement : ((Element)moteTypeElement).getChildren()) {
if (((Element) pathElement).getName().equals("firmware")) {
String newName = (firmwareCounter++) + FIRMWARE_SUFFIX;
/* Copy firmwares, and update firmware references */
String firmwarePath = ((Element)pathElement).getText();
File firmwareFile = gui.restorePortablePath(new File(firmwarePath));
if (!firmwareFile.exists()) {
throw new RuntimeException(
"Firmware file does not exist: " + firmwareFile
);
}
logger.info("Simconfig: Copying firmware file: " + firmwareFile.getAbsolutePath());
byte[] data = ArrayUtils.readFromFile(firmwareFile);
if (data == null) {
throw new RuntimeException(
"Error when reading firmware file: " + firmwareFile
);
}
boolean ok = ArrayUtils.writeToFile(new File(workingDir, newName), data);
if (!ok) {
throw new RuntimeException(
"Error when writing firmware file: " + firmwareFile
);
}
logger.info("Simconfig: Update firmware path reference: " + firmwareFile.getAbsolutePath() + " -> " + ("[CONFIG_DIR]/" + newName));
((Element)pathElement).setText("[CONFIG_DIR]/" + newName);
}
}
}
}
}
}
handleExportAttributesToJAR(rootElement, gui, workingDir);
/* Save simulation config */
File configFile = new File(workingDir, SIMCONFIG_FILENAME);
@ -506,7 +442,79 @@ public class ExecuteJAR {
logger.info("Done! To run simulation: > java -jar " + outputFile.getName());
return true;
}
private static void handleExportAttributesToJAR(Element e, GUI gui, File toDir) {
/* Checks configuration for EXPORT attributes:
* copy: file copy file to exported JAR, update file path.
* discard: remove config element */
for (Element c : ((List<Element>) e.getChildren()).toArray(new Element[0])) {
Attribute a = c.getAttribute("EXPORT");
if (a != null && a.getValue().equals("copy")) {
/* Copy file to JAR */
File file = gui.restorePortablePath(new File(c.getText()));
if (!file.exists()) {
throw new RuntimeException("File not found: " + file);
}
byte[] data = ArrayUtils.readFromFile(file);
if (data == null) {
throw new RuntimeException("Could not copy file: " + file);
}
String newFilename = file.getName();
while (new File(toDir, newFilename).exists()) {
newFilename += "-1";
}
boolean ok = ArrayUtils.writeToFile(new File(toDir, newFilename), data);
if (!ok) {
throw new RuntimeException("Error when copying file: " + file);
}
logger.info("Simconfig: Copied file: " + file.getAbsolutePath() + " -> " + ("[CONFIG_DIR]/" + newFilename));
((Element)c).setText("[CONFIG_DIR]/" + newFilename);
} else if (a != null && a.getValue().equals("discard")) {
/* Remove config element */
e.removeChild(c.getName());
logger.info("Simconfig: Discarded element '" + c.getName() + "': " + c.getText());
continue;
} else if (a != null) {
throw new RuntimeException("Unknown EXPORT attribute value: " + a.getValue());
}
/* Recursive search */
handleExportAttributesToJAR(c, gui, toDir);
}
}
private static void handleExportAttributesFromJAR(Element e, File config, File toDir) {
for (Element c : ((List<Element>) e.getChildren()).toArray(new Element[0])) {
Attribute a = c.getAttribute("EXPORT");
if (a != null && a.getValue().equals("copy")) {
/* Copy file from JAR */
File file = GUI.restoreConfigRelativePath(config, new File(c.getText()));
InputStream inputStream = GUI.class.getResourceAsStream("/" + file.getName());
if (inputStream == null) {
throw new RuntimeException("Could not unpack file: " + file);
}
byte[] fileData = ArrayUtils.readFromStream(inputStream);
if (fileData == null) {
logger.info("Failed unpacking file");
throw new RuntimeException("Could not unpack file: " + file);
}
if (OVERWRITE || !file.exists()) {
boolean ok = ArrayUtils.writeToFile(file, fileData);
if (!ok) {
throw new RuntimeException("Failed unpacking file: " + file);
}
logger.info("Unpacked file from JAR: " + file.getName());
} else if (OVERWRITE) {
logger.info("Skip: unpack file from JAR: " + file.getName());
}
}
/* Recursive search */
handleExportAttributesFromJAR(c, config, toDir);
}
}
private static boolean deleteDirectory(File path) {
if(path.exists()) {
File[] files = path.listFiles();