BrightSide Workbench Full Report + Source Code
ScheduledEntitySet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.scheduler.entities;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.jdom.Document;
29 import org.jdom.Element;
30 import org.jdom.JDOMException;
31 import org.jdom.input.SAXBuilder;
32 import org.jdom.output.Format;
33 import org.jdom.output.XMLOutputter;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.impl.util.FileUtil;
36 
41 public class ScheduledEntitySet extends HashSet<ScheduledEntity> {
42 
43  public static final String
44  SCHEDULER_FILE = "/WEB-INF/elephant/conf/scheduled-entities.xml";
45 
46  public static void saveSet(Set set) {
47  Document doc = new Document(new Element("elephant-entities"));
48  saveXML(doc.getRootElement(), set);
49  try {
50  OutputStreamWriter fw = FileUtil.getFileWriter(
52  Format fm = Format.getPrettyFormat();
53  fm.setEncoding(ElephantContext.getEncoding());
54  XMLOutputter xo = new XMLOutputter(fm);
55  xo.output(doc, fw);
56  fw.close();
57  } catch (IOException ex) {
58  Logger.getLogger(ScheduledEntitySet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
59  }
60  }
61 
62  public static void loadSet(Set set) {
63  SAXBuilder builder = new SAXBuilder();
64  Document doc;
65  try {
66  File confFile = new File(ElephantContext.getRealPath(SCHEDULER_FILE));
67  if(confFile.exists()) {
68  doc = builder.build(confFile);
69  readXML(doc.getRootElement(), set);
70  }
71  } catch (IOException ex) {
72  Logger.getLogger(ScheduledEntitySet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
73  } catch (JDOMException ex) {
74  Logger.getLogger(ScheduledEntitySet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
75  }
76  }
77 
78  private static void readXML(Element rootElement, Set set) {
79  for(Element el : (List<Element>) rootElement.getChildren("scheduled-entity")) {
80  ScheduledEntity st = new DefaultEntity(el.getAttributeValue("path"));
81  st.readXML(el);
82  set.add(st);
83  }
84  }
85 
86  private static void saveXML(Element rootElement, Set set) {
87  for(Object st : set) {
88  ((ScheduledEntity) st).writeXML(rootElement);
89  }
90  }
91 
92  public static void addEntity(ScheduledEntity scheduledEntity) {
95  ses.doAddEntity(scheduledEntity);
97  }
98 
99  public static void removeEntity(ScheduledEntity scheduledEntity) {
102  ses.remove(scheduledEntity);
104  }
105 
106  public static ScheduledEntity getScheduledEntity(String path) {
109  for(ScheduledEntity se : ses) {
110  if(se.getEntityPath().equals(path)) {
111  return se;
112  }
113  }
114  return new DefaultEntity(path);
115  }
116 
117  private void doAddEntity(ScheduledEntity scheduledEntity) {
118  remove(scheduledEntity);
119  add(scheduledEntity);
120  }
121 
122 }
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static void addEntity(ScheduledEntity scheduledEntity)
static ScheduledEntity getScheduledEntity(String path)
static void removeEntity(ScheduledEntity scheduledEntity)