View Javadoc

1   package org.opensync.engine.server.config;
2   
3   import java.io.IOException;
4   import java.io.StringReader;
5   
6   import javax.xml.parsers.ParserConfigurationException;
7   import javax.xml.parsers.SAXParser;
8   import javax.xml.parsers.SAXParserFactory;
9   
10  import org.opensync.engine.server.Adapter;
11  import org.opensync.engine.server.Connector;
12  import org.opensync.engine.server.DefaultAdapter;
13  import org.opensync.engine.server.DefaultConnector;
14  import org.opensync.engine.server.DefaultProtocol;
15  import org.opensync.engine.server.Protocol;
16  import org.opensync.engine.server.Return;
17  import org.opensync.engine.server.OpenSyncException;
18  import org.opensync.engine.server.Source;
19  import org.opensync.engine.server.Sources;
20  import org.opensync.engine.server.View;
21  import org.opensync.engine.server.connector.BDConnector;
22  import org.opensync.engine.server.connector.FileConnector;
23  import org.opensync.engine.server.protocol.FTPProtocol;
24  import org.opensync.engine.server.protocol.WinFileProtocol;
25  import org.xml.sax.Attributes;
26  import org.xml.sax.InputSource;
27  import org.xml.sax.SAXException;
28  import org.xml.sax.helpers.DefaultHandler;
29  
30  
31  
32  /***
33   * This class represents the parser use to parse the sources from the config file
34   */
35  
36  public class SourcesSxPsr extends DefaultHandler {
37    //private XMLReader parser;
38    private SAXParser parser;
39    private Sources sources = new Sources();
40    private Source source;
41    private View view;
42    private Connector connector;
43  
44    /***
45     * Construct the SourcesSxPsr
46     *
47     * @exception ParserConfigurationException
48     */
49    public SourcesSxPsr()throws ParserConfigurationException{
50  
51  
52  
53      try {
54        /*
55        parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
56        parser.setContentHandler(this);
57        */
58        SAXParserFactory factory = SAXParserFactory.newInstance();
59        factory.setValidating(false);
60        parser = factory.newSAXParser();
61      }
62      catch (SAXException ex) {
63        throw new RuntimeException(ex.getMessage());
64      }
65    }
66    /***
67     * @param xml
68     * @exception IOException
69     * @exception SAXException
70     */
71    public Sources parseSources(String xml)throws SAXException,IOException{
72      parser.parse( new InputSource(new StringReader(xml)), this);
73      //parser.parse(new InputSource(new StringReader(xml)));
74      return sources;
75    }
76  
77    /***
78     * @param uri
79     * @param localName ?qName?
80     * @param qName
81     * @param attributes
82     * @exception SAXException
83     */
84    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
85      if(qName.equals("source")){
86        source = new Source();
87        source.setName(attributes.getValue("nameId"));
88        sources.add(source);
89      }
90      else if(qName.equals("connector")){
91        String type = attributes.getValue("type");
92        if(type.equals(Connector.BD)){
93          connector = new BDConnector(source.getName());
94          ((BDConnector)connector).setDriver(attributes.getValue("driver"));
95          String tmp = attributes.getValue("initconns");
96          if(tmp != null){
97            ((BDConnector)connector).setInit(Integer.parseInt(tmp));
98          }
99          tmp = attributes.getValue("maxconns");
100         if(tmp != null){
101           ((BDConnector)connector).setMax(Integer.parseInt(tmp));
102         }
103         //if (Utils.debug) System.out.println("SourcesSxPsr::startElement - operatorConcat="+attributes.getValue("operator_concat"));
104         ((BDConnector)connector).setOperatorConcat(attributes.getValue("operator_concat"));
105         ((BDConnector)connector).setConversionFunction(attributes.getValue("conversion_function"));
106         ((BDConnector)connector).setPassword(attributes.getValue("password"));
107         tmp = attributes.getValue("logintimeout");
108         if(tmp != null){
109           ((BDConnector)connector).setTimeout(Integer.parseInt(tmp));
110         }
111         tmp = attributes.getValue("windowSize");
112         if (tmp != null) {
113           ( (BDConnector) connector).setWindowSize(Integer.parseInt(tmp));
114         } else {
115           ( (BDConnector) connector).setWindowSize(500);
116         }
117 
118         ((BDConnector)connector).setUrl(attributes.getValue("url"));
119         ((BDConnector)connector).setUser(attributes.getValue("user"));
120       }
121       else if(type.equals(Connector.FILE)){
122         connector = new FileConnector();
123         ((FileConnector)connector).setUrl(attributes.getValue("url"));
124         ((FileConnector)connector).setUser(attributes.getValue("user"));
125         ((FileConnector)connector).setPassword(attributes.getValue("password"));
126       }
127       String tmp = attributes.getValue("incremental");
128       connector.setIncremental(tmp != null && tmp.equalsIgnoreCase("yes"));
129       String adapterClass = attributes.getValue("adapter");
130       Adapter adapter = null;
131       if(adapterClass != null && !adapterClass.equals("")){
132         try {
133           adapter = (Adapter)Class.forName(adapterClass).newInstance();
134         }
135         catch (Exception ex) {
136           throw new SAXException(new OpenSyncException(
137               "error.config.adapter-not-instancied",new Object[]{adapterClass}
138             )
139           );
140         }
141         if(adapter instanceof DefaultAdapter){
142           ((DefaultAdapter)adapter).setConnector(connector);
143         }
144         //if(adapter instanceof BDAdapter){
145 	adapter.setTablesDescriptor(attributes.getValue("urlTablesDescriptor"));
146 	adapter.setThemesDescriptor(attributes.getValue("urlThemesDescriptor"));
147         /*try {
148 	((BDAdapter)adapter).setConn(((BDConnector)connector).getConnectionPool().getConnection());
149 	} catch (SQLException sqlex) {
150 	throw new SAXException(new OpenSyncException(sqlex.getMessage()+"\n"+connector.toString()));
151 	}*/
152 	//}
153         if(connector instanceof DefaultConnector){
154           ((DefaultConnector)connector).setAdapter(adapter);
155         }
156         source.setConnector(connector);
157       }
158     }
159     else if(qName.equals("protocol")){
160       String name = attributes.getValue("name");
161       Protocol protocol = null;
162       if(name.equals(Protocol.WIN_FILE)){
163         protocol = new WinFileProtocol();
164       }
165       else if(name.equals(Protocol.FTP)){
166         protocol = new FTPProtocol();
167       }
168       if(protocol instanceof DefaultProtocol){
169         ((DefaultProtocol)protocol).setFolder(attributes.getValue("folder"));
170         ((DefaultProtocol)protocol).setConnector(connector);
171         String tmp = attributes.getValue("remove-file");
172         ((DefaultProtocol)protocol).setRemoveFile(
173           tmp != null && tmp.equalsIgnoreCase("yes")
174         );
175         tmp = attributes.getValue("rename-file");
176         ((DefaultProtocol)protocol).setRenameFile(tmp != null && tmp.equalsIgnoreCase("yes"));
177 	if (protocol instanceof WinFileProtocol) {
178 	  tmp = attributes.getValue("readblockSize");
179 	  long readblockSize = 0;
180 	  try {
181 	    readblockSize = Long.parseLong(tmp);
182 	  } catch (Exception ex) {
183 	    readblockSize = 1000;
184 	  }
185 	  ((WinFileProtocol)protocol).setReadblockSize(readblockSize);
186 	}
187       }
188       if(connector instanceof DefaultConnector){
189         ((DefaultConnector)connector).setProtocol(protocol);
190       }
191     }
192     else if(qName.equals("return")){
193       Return ret = new Return();
194       ret.setTimeOut(attributes.getValue("timeout"));
195       ret.setType(attributes.getValue("type"));
196       ret.setUnit(attributes.getValue("unit"));
197       source.setReturn(ret);
198     }
199     else if(qName.equals("view")){
200       view = new View();
201       view.setName(attributes.getValue("nameId"));
202       view.setTheme(attributes.getValue("theme"));
203       view.setDescriptor(attributes.getValue("descriptor"));
204       view.setDescriptorIn(attributes.getValue("descriptor-in"));
205       view.setDescriptorOut(attributes.getValue("descriptor-out"));
206       view.setFile(attributes.getValue("file"));
207       source.addView(view);
208     }
209   }
210 }