KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > scheduler > Scheduler


1 package com.openedit.modules.scheduler;
2
3 import java.io.StringWriter JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.dom4j.DocumentHelper;
11 import org.dom4j.Element;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.WebPageRequest;
15 import com.openedit.config.Configuration;
16 import com.openedit.config.XMLConfiguration;
17 import com.openedit.page.Page;
18 import com.openedit.page.manage.PageManager;
19 import com.openedit.users.User;
20 import com.openedit.util.XmlUtil;
21
22 public class Scheduler
23 {
24     protected List JavaDoc fieldActions;
25     protected PageManager fieldPageManager;
26     private static final Log log = LogFactory.getLog(Scheduler.class);
27     
28     public void loadFromFile()
29     {
30         try
31         {
32             Page config = getPageManager().getPage("/WEB-INF/scheduler.xml");
33         
34             if( config.exists() )
35             {
36                 XMLConfiguration configuration = new XMLConfiguration();
37                 configuration.populate(new XmlUtil().getXml(config.getReader(),"UTF-8"));
38
39                 //configuration.readXML(config.getReader());
40
List JavaDoc actions = configuration.getChildren("action");
41                 for ( Iterator JavaDoc iter = actions.iterator(); iter.hasNext(); )
42                 {
43                     Configuration actionConfig = (Configuration) iter.next();
44                     Action action = new Action();
45                     
46                     String JavaDoc username = actionConfig.getAttribute("username");
47                     String JavaDoc path = actionConfig.getAttribute("path");
48                     if( path == null)
49                     {
50                         path = "/openedit/index.html?oe-action=" + actionConfig.getValue();
51                         if( username == null)
52                         {
53                             username = "admin";
54                         }
55                     }
56                     action.setUserName(username);
57                     action.setPath(path);
58                     String JavaDoc delay = actionConfig.getAttribute("startdelay");
59                     action.setDelay(delay);
60                     String JavaDoc period = actionConfig.getAttribute("period");
61                     action.setPeriod(period);
62                     action.setConfig(actionConfig );
63                     addAction(action);
64                 }
65             }
66         } catch (OpenEditException e)
67         {
68             // TODO Auto-generated catch block
69
e.printStackTrace();
70         }
71     }
72     
73     private void writeToFile(Element inRoot, User inUser)
74     {
75         StringWriter JavaDoc out = new StringWriter JavaDoc();
76         try
77         {
78             new XmlUtil().saveXml(inRoot, out, getConfigPage().getCharacterEncoding());
79             getPageManager().saveContent(getConfigPage(), inUser, out.toString(), "scheduler run");
80         } catch (OpenEditException e)
81         {
82             // TODO Auto-generated catch block
83
e.printStackTrace();
84         }
85     }
86     
87
88 /*
89 <configuration>
90     <action period="1d" startdelay="0m">
91             ChangeLog.notifyUsers
92     </action>
93 </configuration>
94 */

95     public void saveConfig(WebPageRequest inReq)
96     {
97         Element root = DocumentHelper.createElement("configuration");
98         for (Iterator JavaDoc iter = getActions().iterator(); iter.hasNext();)
99         {
100             Action action = (Action) iter.next();
101             Element element = root.addElement("action");
102             element.addAttribute("period", action.getFormattedPeriod());
103             element.addAttribute("delay", action.getFormattedDelay());
104             element.addAttribute("username", action.getUserName());
105             element.addAttribute("path",action.getPath());
106         }
107         writeToFile(root, inReq.getUser());
108
109     }
110     
111     public Page getConfigPage() throws OpenEditException
112     {
113         return getPageManager().getPage("/WEB-INF/scheduler.xml");
114     }
115     
116     public Element getPageRoot() throws OpenEditException
117     {
118         Page xml = getConfigPage();
119         Element root = new XmlUtil().getXml(xml.getReader(), xml.getCharacterEncoding());
120         return root;
121     }
122     
123     public List JavaDoc getActions()
124     {
125         if (fieldActions == null)
126         {
127             fieldActions = new ArrayList JavaDoc();
128         }
129         return fieldActions;
130     }
131     public void setActions(List JavaDoc inActions)
132     {
133         fieldActions = inActions;
134     }
135     public void addAction(Action inAction)
136     {
137         if (getAction(inAction.getPath()) == null)
138         {
139             getActions().add(inAction);
140         }
141     }
142     
143     public void removeAction(Action inAction)
144     {
145         getActions().remove(inAction);
146     }
147
148     public PageManager getPageManager()
149     {
150         return fieldPageManager;
151     }
152     public void setPageManager(PageManager inPageManager)
153     {
154         fieldPageManager = inPageManager;
155     }
156     
157     
158     public Action getAction(String JavaDoc inName)
159     {
160         for (Iterator JavaDoc iter = getActions().iterator(); iter.hasNext();)
161         {
162             Action action = (Action) iter.next();
163             if (inName.equals(action.getPath() ) )
164             {
165                 return action;
166             }
167         }
168         return null;
169     }
170
171 }
172
Popular Tags