View Javadoc

1   package org.opensync.engine.server.config;
2   
3   import java.net.*;
4   import java.io.*;
5   import java.util.*;
6   import javax.xml.parsers.*;
7   import org.xml.sax.*;
8   import org.xml.sax.helpers.*;
9   
10  import org.opensync.tools.Utils;
11  import org.opensync.engine.server.Agenda;
12  import org.opensync.engine.server.Agendas;
13  import org.opensync.engine.server.Log;
14  import org.opensync.engine.server.OpenSync;
15  import org.opensync.engine.server.OpenSyncException;
16  import org.opensync.engine.server.Source;
17  import org.opensync.engine.server.Sources;
18  import org.opensync.engine.server.Synchronizations;
19  import org.opensync.engine.server.Synchronization;
20  import org.opensync.engine.server.Task;
21  
22  /***
23   * This class represents the document of the OpenSync server
24   */
25  
26  public class ConfigDoc extends DefaultHandler implements Serializable, ErrorHandler{
27    /***
28     * The file path
29     *
30     */
31    private final static String CONFIG_FILE = "config.xml";
32    /***
33     * The date of the last modification
34     *
35     */
36    protected long lastModified;
37    /***
38     * The sources
39     *
40     */
41    protected Sources sources;
42    /***
43     * The synchronizations
44     *
45     */
46    protected Synchronizations synchronizations;
47    /***
48     * The agendas
49     *
50     */
51    protected Agendas agendas;
52  
53    /***
54     * Construct the ConfigDoc object
55     *
56     */
57    public ConfigDoc() {
58    }
59    /***
60     * Get all synchronizations
61     *
62     */
63    public Synchronizations getSynchronizations(){
64      return synchronizations;
65    }
66    /***
67     * Get all sources
68     *
69     */
70    public Sources getSources(){
71      return sources;
72    }
73    /***
74     * Get a source
75     *
76     * @param source the source name to get
77     */
78    public Source getSource(String source){
79      return sources == null ? null : (Source)sources.get(source);
80    }
81    /***
82     * Get a synchronization
83     *
84     * @param synchronization the synchronization name to get
85     */
86    public Synchronization getSynchronization(String synchronization){
87      return synchronizations == null ? null : (Synchronization)synchronizations.get(synchronization);
88    }
89    /***
90     * Initialize the document with parsing the config file
91     *
92     * @exception OpenSyncException
93     * @exception IOException
94     * @exception SAXException
95     * @exception DocumentException
96     * @exception ParserConfigurationException
97     */
98    public void init() throws ParserConfigurationException, OpenSyncException,
99    SAXException,IOException {
100     OpenSync.getInstance().getLog().debug(Log.CONFIG,"info.config.reading",null);
101     lastModified = new File(OpenSync.getInstance().getFilePath(getConfigFileName(),true)).lastModified();
102     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
103     DocumentBuilder builder = factory.newDocumentBuilder();
104 
105     org.w3c.dom.Document document = builder.parse(
106       new File(OpenSync.getInstance().getFilePath(getConfigFileName(),true)).toURL().toString()
107     );
108     SourcesSxPsr sourcesSxPsr = new SourcesSxPsr();
109     String xml = document.getElementsByTagName("sources").item(0).toString();
110     OpenSync.getInstance().getLog().debug(Log.CONFIG,"info.config.reading-sources",null);
111     sources = sourcesSxPsr.parseSources(xml);
112 
113     SynchronizationsSxPsr synchronizationsSxPsr = new SynchronizationsSxPsr(sources);
114     xml = document.getElementsByTagName("synchronizations").item(0).toString();
115     OpenSync.getInstance().getLog().debug(Log.CONFIG,"info.config.reading-synchronizations",null);
116     synchronizations = synchronizationsSxPsr.parseSynchronizations(xml);
117 
118     AgendasSxPsr agendasSxPsr = new AgendasSxPsr(synchronizations);
119     xml = document.getElementsByTagName("agendas").item(0).toString();
120     OpenSync.getInstance().getLog().debug(Log.CONFIG,"info.config.reading-agendas",null);
121     agendas = agendasSxPsr.parseAgendas(xml);
122     //OpenSync.getInstance().getLog().debug(Log.CONFIG,toString());
123 
124     // LastModified must also be computed from all the files used
125     // by the config file !
126     lastModified = this.checkLastModified(lastModified);
127   }
128 
129   /***
130    * Return the lastModified from the files used in the Agendas
131    * Thus, we follow the Agendas, Tasks, Synchronisations, Sources, Views
132    * hierarchy to find XML configurations files which may have changed since
133    * the last update of the opensync.dat file in %OPENSYNC_HOME%\etc folder.
134    * @param lm
135    * @return
136    * @throws IOException
137    * @throws OpenSyncException
138    */
139   public long checkLastModified(long lastModified) throws IOException, OpenSyncException {
140 
141     for (Enumeration e = agendas.keys(); e.hasMoreElements();) {
142       Agenda anAgenda = (Agenda)(agendas.get(e.nextElement()));
143       Iterator iter = anAgenda.getTasks().iterator();
144       while (iter.hasNext()) {
145 	Task aTask = (Task)iter.next();
146 	Synchronization aSynchro = aTask.getSynchronization();
147 	lastModified = Math.max(lastModified, aSynchro.lastModified());
148       }
149     }
150     return lastModified;
151   }
152 
153   public void readFile() throws ParserConfigurationException, OpenSyncException,
154   SAXException,IOException  {
155 
156     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
157 
158     DocumentBuilder builder = factory.newDocumentBuilder();
159 
160     org.w3c.dom.Document document = builder.parse(
161       new File(OpenSync.getInstance().getFilePath(getConfigFileName(),true)).toURL().toExternalForm()
162     );
163     SourcesSxPsr sourcesSxPsr = new SourcesSxPsr();
164     String xml = document.getElementsByTagName("sources").item(0).toString();
165     sources = sourcesSxPsr.parseSources(xml);
166     SynchronizationsSxPsr synchronizationsSxPsr = new SynchronizationsSxPsr(sources);
167     xml = document.getElementsByTagName("synchronizations").item(0).toString();
168     synchronizations = synchronizationsSxPsr.parseSynchronizations(xml);
169     AgendasSxPsr agendasSxPsr = new AgendasSxPsr(synchronizations);
170     xml = document.getElementsByTagName("agendas").item(0).toString();
171     agendas = agendasSxPsr.parseAgendas(xml);
172 
173   }
174 
175   public void setLastModified(long lastModified) {
176     this.lastModified = lastModified;
177   }
178 
179   /***
180    *
181    * @throws SAXException
182    * @throws ParserConfigurationException
183    * @throws FileNotFoundException
184    * @throws MalformedURLException
185    * @throws IOException
186    */
187    public void checkXML() throws SAXException, ParserConfigurationException, FileNotFoundException, MalformedURLException, IOException {
188      SAXParserFactory factory = SAXParserFactory.newInstance();
189      factory.setValidating(true);
190      SAXParser parser = factory.newSAXParser();
191      parser.parse( new InputSource(new File(OpenSync.getInstance().getFilePath(getConfigFileName(),true)).toURL().toExternalForm()), this);
192    }
193 
194    /***
195     *
196     * @throws OpenSyncException
197     */
198    public void checkSourcesAvailability() throws OpenSyncException {
199      for (Enumeration e = agendas.keys(); e.hasMoreElements();) {
200       Agenda anAgenda = (Agenda)(agendas.get(e.nextElement()));
201       Iterator iter = anAgenda.getTasks().iterator();
202       Synchronization aSynchro = null;
203       while (iter.hasNext()) {
204 	Task aTask = (Task)iter.next();
205 	aSynchro = aTask.getSynchronization();
206 	aSynchro.getFromSource().getConnector().validate();
207 	aSynchro.getToSource().getConnector().validate();
208 	checkSourcesAvailability(aTask);
209       }
210     }
211    }
212 
213    private void checkSourcesAvailability(Task task) throws OpenSyncException {
214 
215      Iterator iterator = task.getSubTasks().iterator();
216      Synchronization aSynchro = null;
217      while (iterator.hasNext()){
218 	  Task subTask = (Task)iterator.next();
219 	  aSynchro = subTask.getSynchronization();
220 	  aSynchro.getFromSource().getConnector().validate();
221 	  aSynchro.getToSource().getConnector().validate();
222 	  checkSourcesAvailability(subTask);
223      }
224    }
225    // Receive notification of a recoverable error.
226   public void error(SAXParseException exception) throws SAXException {
227     OpenSyncException ex = new OpenSyncException("\r\nError at line "+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
228     throw new SAXException(ex);
229   }
230 
231   // Receive notification of a non-recoverable error.
232   public void fatalError(SAXParseException exception) throws SAXException {
233         throw new SAXException(new OpenSyncException("\r\nFatal error at line "+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage()));
234   }
235 
236   public void warning(SAXParseException exception) throws SAXException {
237         throw new SAXException(new OpenSyncException("\r\nWarning at line "+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage()));
238   }
239   /***
240    * Get all agendas
241    *
242    */
243   public Agendas getAgendas(){
244     return agendas;
245   }
246   /***
247    * Use for debug only
248    *
249    */
250   public String toString(){
251     StringBuffer buffer = new StringBuffer();
252     buffer.append("ConfigDoc{\n");
253     if(sources != null){
254       buffer.append(sources.toString());
255     }
256     if(synchronizations != null){
257       buffer.append(synchronizations.toString());
258     }
259     if(agendas != null){
260       buffer.append(agendas.toString());
261     }
262     buffer.append("}\n");
263     return buffer.toString();
264   }
265   /***
266    * Write the document in a stream
267    *
268    * @param oos the stream
269    * @exception IOException
270    */
271   protected void writeObject(ObjectOutputStream oos) throws IOException {
272     oos.writeObject(getSources());
273     oos.writeObject(getSynchronizations());
274     oos.writeObject(getAgendas());
275   }
276   /***
277    * Read the document from a stream
278    *
279    * @param ois the stream
280    * @exception IOException
281    * @exception ClassNotFoundException
282    */
283   protected void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
284     sources = (Sources)ois.readObject();
285     synchronizations = (Synchronizations)ois.readObject();
286     agendas = (Agendas)ois.readObject();
287   }
288   /***
289    * Get the date of the last modification
290    *
291    */
292   public long getLastModified() {
293     //System.out.println("ConfigDoc: "+lastModified);
294     return lastModified;
295   }
296 
297   public static String getConfigFileName(){
298     String fileSeparator = System.getProperty("file.separator");
299     String sConfigFilePrefix = "etc"+fileSeparator;
300 
301     String sConfigFolder = System.getProperty("opensync.configfolder");
302     sConfigFolder = (sConfigFolder == null ? "" : sConfigFolder);
303 
304     if (!sConfigFolder.equals("")){
305       sConfigFilePrefix += sConfigFolder+fileSeparator;
306     }
307 
308     String sConfigFile = System.getProperty("opensync.config");
309     if (sConfigFile == null || sConfigFile.equals("")){
310       sConfigFile = sConfigFilePrefix+CONFIG_FILE;
311       System.out.println("No opensync.config property, defaulting to "+sConfigFile);
312     }else{
313       sConfigFile = sConfigFilePrefix+sConfigFile;
314     }
315     if (Utils.debug) System.out.println("Using "+sConfigFile);
316     return sConfigFile;
317   }
318 }