/** @param folder */ public static void saveTriggers(File folder) { if (list.isEmpty()) { LOG.warning( "There are no triggers to persist, " + folder.getAbsolutePath() + " will not be altered."); return; } if (!folder.isDirectory()) { LOG.warning(folder.getAbsoluteFile() + " is not a valid trigger folder. Skipped"); return; } XStream xstream = FreedomXStream.getXstream(); deleteTriggerFiles(folder); try { LOG.config("Saving triggers to file in " + folder.getAbsolutePath()); for (Trigger trigger : list) { if (trigger.isToPersist()) { String uuid = trigger.getUUID(); if ((uuid == null) || uuid.isEmpty()) { trigger.setUUID(UUID.randomUUID().toString()); } String fileName = trigger.getUUID() + ".xtrg"; FileWriter fstream = new FileWriter(folder + "/" + fileName); BufferedWriter out = new BufferedWriter(fstream); out.write(xstream.toXML(trigger)); // persist only the data not the logic // Close the output stream out.close(); fstream.close(); } } } catch (Exception e) { LOG.info(e.getLocalizedMessage()); LOG.severe(Freedomotic.getStackTraceInfo(e)); } }
/** @param folder */ public static synchronized void loadTriggers(File folder) { XStream xstream = FreedomXStream.getXstream(); // This filter only returns object files FileFilter objectFileFileter = new FileFilter() { public boolean accept(File file) { if (file.isFile() && file.getName().endsWith(".xtrg")) { return true; } else { return false; } } }; File[] files = folder.listFiles(objectFileFileter); try { StringBuilder summary = new StringBuilder(); // print an header for the index.txt file summary.append("#Filename \t\t #TriggerName \t\t\t #ListenedChannel").append("\n"); if (files != null) { for (File file : files) { Trigger trigger = null; try { // validate the object against a predefined DTD String xml = DOMValidateDTD.validate( file, Info.getApplicationPath() + "/config/validator/trigger.dtd"); trigger = (Trigger) xstream.fromXML(xml); } catch (Exception e) { LOG.log( Level.SEVERE, "Trigger file {0} is not well formatted: {1}", new Object[] {file.getPath(), e.getLocalizedMessage()}); continue; } // addAndRegister trigger to the list if it is not a duplicate if (!list.contains(trigger)) { if (trigger.isHardwareLevel()) { trigger.setPersistence(false); // it has not to me stored in root/data folder addAndRegister(trigger); // in the list and start listening } else { if (folder.getAbsolutePath().startsWith(Info.getPluginsPath())) { trigger.setPersistence(false); } else { trigger.setPersistence(true); // not hardware trigger and not plugin related } list.add( trigger); // only in the list not registred. I will be registred only if used in // mapping } } else { LOG.warning("Trigger '" + trigger.getName() + "' is already in the list"); } summary .append(trigger.getUUID()) .append("\t\t") .append(trigger.getName()) .append("\t\t\t") .append(trigger.getChannel()) .append("\n"); } // writing a summary .txt file with the list of commands in this folder FileWriter fstream = new FileWriter(folder + "/index.txt"); BufferedWriter indexfile = new BufferedWriter(fstream); indexfile.write(summary.toString()); // Close the output stream indexfile.close(); } else { LOG.config("No triggers to load from this folder " + folder.toString()); } } catch (Exception e) { LOG.severe("Exception while loading this trigger.\n" + Freedomotic.getStackTraceInfo(e)); } }