BrightSide Workbench Full Report + Source Code
ScheduledSet.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.motor;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
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.context.IConstructor;
36 import org.turro.elephant.impl.context.ContextFactory;
37 import org.turro.elephant.impl.util.FileUtil;
38 import org.turro.reflection.Reflections;
39 
44 @Deprecated
45 public class ScheduledSet extends TreeSet<ScheduledTask> {
46 
47  public static final String
48  SCHEDULER_FILE = "/WEB-INF/elephant/conf/scheduler.xml";
49 
50  public ScheduledSet() {
51  super(new ScheduledTaskComparator());
52  }
53 
54  public static void saveSet(IConstructor constructor, Set set) {
55  Document doc = new Document(new Element("elephant-scheduler"));
56  saveXML(doc.getRootElement(), set);
57  try {
58  OutputStreamWriter fw = FileUtil.getFileWriter(
60  Format fm = Format.getPrettyFormat();
61  fm.setEncoding(ElephantContext.getEncoding());
62  XMLOutputter xo = new XMLOutputter(fm);
63  xo.output(doc, fw);
64  fw.close();
65  } catch (IOException ex) {
66  Logger.getLogger(ScheduledSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
67  }
68  }
69 
70  public static void loadSet(IConstructor constructor, Set set) {
71  SAXBuilder builder = new SAXBuilder();
72  Document doc;
73  try {
74  File confFile = new File(ElephantContext.getRealPath(SCHEDULER_FILE));
75  if(confFile.exists()) {
76  doc = builder.build(confFile);
77  readXML(constructor, doc.getRootElement(), set);
78  }
79  } catch (IOException ex) {
80  Logger.getLogger(ScheduledSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
81  } catch (JDOMException ex) {
82  Logger.getLogger(ScheduledSet.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
83  }
84  }
85 
86  private static void readXML(IConstructor constructor, Element rootElement, Set set) {
87  for(Element el : (List<Element>) rootElement.getChildren("scheduled-task")) {
88  ScheduledTask st = (ScheduledTask) ContextFactory.getImplementation(constructor, el.getAttributeValue("implementation"));
89  if(st == null) {
90  st = (ScheduledTask) Reflections.of(el.getAttributeValue("implementation")).instance();
91  }
92  if(st != null) {
93  st.readXML(el);
94  set.add(st);
95  }
96  }
97  }
98 
99  private static void saveXML(Element rootElement, Set set) {
100  for(Object st : set) {
101  ((ScheduledTask) st).writeXML(rootElement);
102  }
103  }
104 
105 }
static Object getImplementation(IElement iel, String name)
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static void saveSet(IConstructor constructor, Set set)
static void loadSet(IConstructor constructor, Set set)