001: package net.xoetrope.optional.data;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.InputStream;
006: import java.io.Reader;
007: import java.io.StringReader;
008: import java.io.StringWriter;
009: import java.util.Date;
010: import java.util.Enumeration;
011: import java.util.Hashtable;
012: import java.util.Properties;
013: import java.util.StringTokenizer;
014: import java.util.Vector;
015:
016: import net.xoetrope.data.XDataSource;
017: import net.xoetrope.debug.DebugLogger;
018: import net.xoetrope.optional.data.sql.DatabaseTableModel;
019: import net.xoetrope.optional.data.sql.NamedConnectionManager;
020: import net.xoetrope.os.LibraryLoader;
021: import net.xoetrope.optional.service.XRouteManager;
022: import net.xoetrope.optional.service.XServiceModelNode;
023: import net.xoetrope.optional.service.XServiceProxyNotFoundException; //import net.xoetrope.xlib.plugin.dialog.XLibDataChooser;
024: import net.xoetrope.xml.XmlElement;
025: import net.xoetrope.xml.XmlSource;
026: import net.xoetrope.xml.nanoxml.NanoXmlElement;
027: import net.xoetrope.xml.nanoxml.NanoXmlWriter;
028: import net.xoetrope.xui.XProjectManager;
029: import net.xoetrope.xui.XResourceManager;
030: import net.xoetrope.xui.build.BuildProperties;
031: import net.xoetrope.xui.data.XModel;
032: import net.xoetrope.optional.data.sql.DatabaseTable;
033:
034: /**
035: * <p>An extended datasource for handling databses, services and routing</p>
036: * <p>Copyright: Copyright Xoetrope Ltd. (c) 2003</p>
037: * $Revision: 1.6 $
038: */
039: public class XOptionalDataSource extends XDataSource {
040: public XOptionalDataSource() {
041: XProjectManager.getCurrentProject().registerBindingFactory(
042: new XOptionalBindingFactory());
043:
044: String caseSensisitive = XResourceManager
045: .getStartupParam("CaseSensitiveDatabase");
046: if ((caseSensisitive != null)
047: && caseSensisitive.toLowerCase().equals("false"))
048: DatabaseTable.setCaseSensitive(false);
049: }
050:
051: protected void showVisualisation() {
052: String sp = XResourceManager.getStartupParam("XLibVisualise");
053: if ((sp != null) && (sp.compareTo("true") == 0)) {
054: String className = XResourceManager
055: .getStartupParam("VisualiseClass");
056: try {
057: XModelVisualiser visualiser = (XModelVisualiser) Class
058: .forName(className).newInstance();
059: visualiser.showDlg("");
060: } catch (Exception ex) {
061: ex.printStackTrace();
062: }
063: }
064: }
065:
066: /**
067: * Read a model from the Reader
068: * @param r the Reader
069: */
070: public void read(Reader r) {
071: XmlElement ele = XmlSource.read(r);
072: String saveConfigFilename = ele.getAttribute("SaveConfigFile");
073: XmlElement dbSrc = null, routeSrc = null, serviceSrc = null;
074: Vector v = ele.getChildren();
075: if (v != null) {
076: int numFiles = v.size();
077: XModel model = XModel.getInstance();
078: for (int i = 0; i < numFiles; i++) {
079: try {
080: XmlElement source = (XmlElement) v.elementAt(i);
081: Reader sr = XResourceManager.getBufferedReader(
082: source.getAttribute("filename"), "UTF8");
083:
084: if (BuildProperties.DEBUG)
085: DebugLogger.trace("Datasource: "
086: + source.getAttribute("filename"));
087:
088: XmlElement src = XmlSource.read(sr);
089: String type = source.getAttribute("type");
090: if ((type == null) || (type.length() == 0))
091: loadTable(src, model);
092: else if (type.equals("database"))
093: dbSrc = src;
094: else if (type.equals("routing"))
095: routeSrc = src;
096: else if (type.equals("service"))
097: serviceSrc = src;
098: registerDataSource(source);
099: } catch (Exception ex) {
100: if (BuildProperties.DEBUG)
101: DebugLogger
102: .logError("Failed to read configuration file");
103: ex.printStackTrace();
104: }
105: }
106:
107: try {
108: if (routeSrc != null)
109: loadRoutes(routeSrc, model);
110: } catch (Exception ex1) {
111: if (BuildProperties.DEBUG)
112: DebugLogger
113: .logError("Failed to read routes config file");
114: }
115:
116: try {
117: if (serviceSrc != null)
118: loadService(serviceSrc, model);
119: } catch (Exception ex1) {
120: if (BuildProperties.DEBUG)
121: DebugLogger
122: .logError("Failed to read services config file");
123: }
124:
125: try {
126: if (dbSrc != null) {
127: loadDatabase(dbSrc, model);
128: }
129: } catch (Exception ex1) {
130: if (BuildProperties.DEBUG)
131: DebugLogger
132: .logError("Failed to read database config file");
133: }
134: }
135: showVisualisation();
136: }
137:
138: // Design time methods.
139: public void registerDataSource(XmlElement source) {
140: }
141:
142: public void registerDatabaseNode(XmlElement source) {
143: }
144:
145: /**
146: * Recursively load the model data
147: * @param source the source element
148: * @param model the model for the source element
149: */
150: public void loadDatabase(XmlElement source, XModel model) {
151: XmlElement connEle = source.elementAt(0);
152: String dbDriver = connEle.getAttribute("driver");
153: String dbUrl = connEle.getAttribute("url");
154: if (dbDriver.equals("org.hsqldb.jdbcDriver")) {
155: if (dbUrl.indexOf('.') < 0) {
156: String dbName = dbUrl
157: .substring(dbUrl.lastIndexOf(':') + 1);
158: try {
159: String fileName = getClass().getClassLoader()
160: .getResource(dbName + ".script").getFile();
161: if (fileName.indexOf(".jar") > 0) {
162: fileName = LibraryLoader.extractFile(getClass()
163: .getClassLoader(), dbName + ".script");
164: LibraryLoader.extractFile(getClass()
165: .getClassLoader(), dbName
166: + ".properties");
167: LibraryLoader.extractFile(getClass()
168: .getClassLoader(), dbName + ".data");
169: }
170: dbUrl = dbUrl.substring(0,
171: dbUrl.lastIndexOf(':') + 1)
172: + fileName.substring(0, fileName
173: .lastIndexOf('.'));
174: } catch (Exception ex) {
175: if (BuildProperties.DEBUG)
176: DebugLogger
177: .logWarning("Unable to open the resource file: "
178: + dbName + ".script");
179: }
180: }
181: }
182:
183: NamedConnectionManager connMgr = (NamedConnectionManager) NamedConnectionManager
184: .getInstance();
185: connMgr = (NamedConnectionManager) connMgr.reset(connEle
186: .getAttribute("id"), dbDriver, dbUrl, connEle
187: .getAttribute("user"), connEle.getAttribute("pwd"));
188:
189: registerDatabaseNode(connEle);
190: Vector vRS = source.getChildren();
191: int rsCount = vRS.size();
192: int numChildren = model.getNumChildren();
193: numChildren += rsCount;
194: model.setNumChildren(numChildren);
195: for (int i = 1; i < rsCount; i++) {
196: XmlElement rsEle = (XmlElement) vRS.elementAt(i);
197: if (rsEle.getName().compareTo("Connection") == 0) {
198: connMgr.addConnection(rsEle.getAttribute("id"), rsEle
199: .getAttribute("driver"), rsEle
200: .getAttribute("url"), rsEle
201: .getAttribute("user"), rsEle
202: .getAttribute("pwd"));
203: } else {
204: DatabaseTableModel tableModel = new DatabaseTableModel();
205: tableModel.setName(rsEle.getAttribute("id"));
206: boolean updateDb = false;
207: String updateStr = rsEle.getAttribute("update");
208: if (updateStr != null)
209: updateDb = updateStr.compareTo("true") == 0;
210:
211: String sqlStr = rsEle.getAttribute("sql");
212: if ((sqlStr != null) && (sqlStr.length() > 0))
213: tableModel.setSqlStatement(sqlStr, rsEle
214: .getAttribute("conn"), updateDb);
215: else {
216: // We should probably change the name table to from as it equates to the FROM clause
217: tableModel.setupTable(rsEle.getAttribute("from"),
218: rsEle.getAttribute("fields"), rsEle
219: .getAttribute("where"), rsEle
220: .getAttribute("conn"), updateDb);
221: String distinctStr = rsEle.getAttribute("distinct");
222: if (distinctStr != null)
223: tableModel.setDistinct(distinctStr
224: .compareTo("true") == 0);
225:
226: tableModel.setOrderField(rsEle
227: .getAttribute("order"));
228: }
229: model.append(tableModel);
230: }
231: registerDatabaseNode(rsEle);
232: }
233: }
234:
235: /**
236: * Recursively load the model data
237: * @param source the source element
238: * @param model the model for the source element
239: */
240: public void loadService(XmlElement source, XModel model) {
241: XRouteManager routeMgr = XRouteManager.getInstance();
242:
243: // Get the 'Services' node
244: XmlElement parentEle = source.elementAt(0);
245: Vector serviceNodes = source.getChildren();
246:
247: // iterate all of the 'service' children
248: int serviceCount = serviceNodes.size();
249: for (int i = 0; i < serviceCount; i++) {
250: XmlElement eleService = (XmlElement) serviceNodes
251: .elementAt(i);
252: String serviceName = eleService.getAttribute("id");
253: String serviceRoute = eleService.getAttribute("route");
254: String serviceImplementationClass = eleService
255: .getAttribute("server");
256: int argSize = eleService.getChildren().size();
257:
258: // iterate the 'arg' and 'return' elements of the service
259: String[] args = new String[argSize];
260: for (int argNode = 0; argNode < argSize; argNode++) {
261: // Get the type of all args and only the type of the 'return' element.
262: String argName, argType;
263: XmlElement argEle = eleService.elementAt(argNode);
264: argType = argEle.getAttribute("type");
265: if (argEle.getName().compareTo("arg") == 0) {
266: argName = argEle.getAttribute("id");
267: args[argNode] = argName;
268: }
269: }
270:
271: XServiceModelNode node = new XServiceModelNode();
272: try {
273: node
274: .setupService(serviceName, routeMgr.getRoute(
275: serviceRoute,
276: serviceImplementationClass), args);
277: node.getServiceProxy().setServiceName(serviceName);
278: model.set(serviceName, node);
279: } catch (XServiceProxyNotFoundException ex) {
280: ex.printStackTrace();
281: }
282: }
283: }
284:
285: /**
286: * Load the routing information
287: * @param source the source element
288: * @param model the model for the source element
289: */
290: public void loadRoutes(XmlElement source, XModel model) {
291: // initialise the XRouteManager
292: XRouteManager.getInstance();
293:
294: Vector routeNodes = source.getChildren();
295:
296: // iterate all of the 'route' children
297: int routeCount = routeNodes.size();
298: for (int i = 0; i < routeCount; i++) {
299: XmlElement routeElement = (XmlElement) routeNodes
300: .elementAt(i);
301: String routeName = routeElement.getAttribute("id");
302:
303: // For each route add the layers
304: int layerCount = routeElement.getChildren().size();
305: Hashtable[] layers = new Hashtable[layerCount];
306: for (int layer = 0; layer < layerCount; layer++) {
307: XmlElement layerElement = routeElement.elementAt(layer);
308: Hashtable layerTable = new Hashtable();
309:
310: // Put any extra attributes into a hashtable
311: Enumeration attribs = layerElement
312: .enumerateAttributeNames();
313: while (attribs.hasMoreElements()) {
314: String attribName = attribs.nextElement()
315: .toString();
316: layerTable.put(attribName, layerElement
317: .getAttribute(attribName));
318:
319: // Add the children of the layer node
320: int pathCount = layerElement.getChildren().size();
321: for (int pathIdx = 0; pathIdx < pathCount; pathIdx++) {
322: XmlElement pathElement = layerElement
323: .elementAt(pathIdx);
324: Vector pathTable = new Vector();
325:
326: // Put any extra path attributes into a hashtable
327: Enumeration pathAttribs = pathElement
328: .enumerateAttributeNames();
329: while (pathAttribs.hasMoreElements()) {
330: String pathAttribName = (String) pathAttribs
331: .nextElement();
332: if (pathAttribName.compareTo("id") == 0)
333: pathTable.add(pathElement
334: .getAttribute("id"));
335: else
336: ; // do nothing with the extra data for now.
337: }
338:
339: layerTable.put(new Integer(pathIdx).toString(),
340: pathTable);
341: }
342: }
343: layers[layer] = layerTable;
344:
345: }
346: // add the route to the XRouteManager
347: XRouteManager.addRoute(routeName, layers);
348: }
349: }
350: }
|