001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.sql;
021:
022: import java.net.*;
023: import java.io.*;
024: import java.util.*;
025: import java.util.zip.InflaterInputStream;
026:
027: /**
028: * This type was created in VisualAge.
029: */
030: class DSDataSourceProxy implements Serializable {
031: String _url, _sessionId;
032: ObjectInputStream _in;
033: ObjectOutputStream _out;
034: String _userID;
035: String _pw;
036: String _proxyUserID;
037: String _proxyPw;
038: URLConnection _conn;
039: int _count;
040: DataStoreProxy _ds;
041:
042: /**
043: * DSDataSourceRemote constructor comment.
044: */
045: public DSDataSourceProxy(String url, String sessionId,
046: String userID, String pw, String proxyUserID,
047: String proxyPassword, String criteria, DataStoreProxy ds)
048: throws DataStoreException {
049: super ();
050: _url = url;
051: _sessionId = sessionId;
052: _userID = userID;
053: _proxyUserID = proxyUserID;
054: _proxyPw = proxyPassword;
055: _pw = pw;
056: _ds = ds;
057:
058: try {
059: URLConnection conn = openConnection(ds, url, sessionId,
060: DataStoreProxy.OPERATION_CREATE, criteria, userID,
061: pw);
062: Properties p = new Properties();
063: ObjectInputStream in = getInputStream(conn);
064: int result = getResponse(conn);
065: if (result == DataStoreProxy.REMOTE_STATUS_OK) {
066: byte[] b = (byte[]) in.readObject();
067: p.load(new ByteArrayInputStream(b));
068: ds.setProperties(p);
069:
070: Integer col = (Integer) in.readObject();
071: while (col.intValue() != -1) {
072: int colNo = col.intValue();
073: ValidationRule val[] = (ValidationRule[]) in
074: .readObject();
075: for (int j = 0; j < val.length; j++)
076: ds.addValidationRule(colNo, val[j]);
077: col = (Integer) in.readObject();
078: }
079: in.close();
080: _out.close();
081:
082: } else {
083: if (result == DataStoreProxy.REMOTE_STATUS_BAD_REQUEST) {
084: ObjectInputStream oIn = new ObjectInputStream(in);
085: Object o = oIn.readObject();
086: if (o != null && o instanceof DataStoreException)
087: throw ((DataStoreException) o);
088: }
089: handleError(result, null);
090: }
091: } catch (Exception ex) {
092: throw new DataStoreException(ex.toString(), ex);
093: }
094: }
095:
096: /**
097: * preRetrieve method comment.
098: */
099: public void cancelRetrieve(DataStoreProxy ds) throws Exception {
100: try {
101: _count = 0;
102: URLConnection conn = null;
103: conn = openConnection(ds, _url, _sessionId,
104: DataStoreProxy.OPERATION_CANCEL, null, _userID, _pw);
105: InputStream is = conn.getInputStream();
106: is.close();
107: _out.close();
108: } catch (Exception ex) {
109: throw new DataStoreException(ex.toString());
110: }
111: }
112:
113: /**
114: * insertRow method comment.
115: */
116: public boolean commit(DataStoreProxy ds) throws Exception {
117: Character c = new Character('Z');
118: _out.writeObject(c);
119:
120: _in = getInputStream(_conn);
121: _out.flush();
122:
123: int res = getResponse(_conn);
124: if (res == DataStoreProxy.REMOTE_STATUS_OK)
125: return true;
126: else if (res == DataStoreProxy.REMOTE_STATUS_SQL_ERROR
127: || res == DataStoreProxy.REMOTE_STATUS_NOT_FOUND
128: || res == DataStoreProxy.REMOTE_STATUS_ACCESS_DENIED) {
129: Object o = _in.readObject();
130: Exception e = (Exception) o;
131: throw (e);
132: } else {
133: return false;
134: }
135:
136: }
137:
138: /**
139: * deleteRow method comment.
140: */
141: public boolean deleteRow(DataStoreProxy ds, DataStoreRow row)
142: throws Exception {
143: Character c = new Character('D');
144: _out.writeObject(c);
145: _out.writeObject(new Integer(row.getDSDataRow().getProxyRow()));
146: int count = ds.getColumnCount();
147:
148: //write out the old values for the where clause
149: for (int i = 0; i < count; i++) {
150: if (ds.isPrimaryKey(i)
151: || (ds.getConcurrencyCheckColumn(i) && ds
152: .getCheckConcurrency()))
153: _out.writeObject(row.getOriginalData(i));
154: }
155:
156: _out.writeObject(new Character('X'));
157:
158: return true;
159: }
160:
161: /**
162: * preRetrieve method comment.
163: */
164: public boolean destroy(DataStoreProxy ds) throws Exception {
165: try {
166: _count = 0;
167: URLConnection conn = null;
168: conn = openConnection(ds, _url, _sessionId,
169: DataStoreProxy.OPERATION_DESTROY, null, _userID,
170: _pw);
171: int result = getResponse(conn);
172: if (result == DataStoreProxy.REMOTE_STATUS_OK) {
173: _out.close();
174: return true;
175: } else {
176: handleError(result, null);
177: return false;
178: }
179: } catch (Exception ex) {
180: throw new DataStoreException(ex.toString());
181: }
182: }
183:
184: /**
185: * This method was created in VisualAge.
186: * @return int
187: */
188: public int getCount() {
189: return _count;
190: }
191:
192: private int getResponse(URLConnection conn) {
193: return conn.getHeaderFieldInt("DataServerResponse", 0);
194: }
195:
196: private void handleError(int ret, Throwable t)
197: throws DataStoreException, java.sql.SQLException {
198: if (ret == DataStoreProxy.REMOTE_STATUS_NOT_FOUND) {
199: throw new DataStoreException("Remote DataStore not found",
200: t);
201: } else if (ret == DataStoreProxy.REMOTE_STATUS_BAD_REQUEST) {
202: throw new DataStoreException("Bad request to data server",
203: t);
204: } else if (ret == DataStoreProxy.REMOTE_STATUS_SQL_ERROR) {
205: if (t != null && t instanceof java.sql.SQLException)
206: throw ((java.sql.SQLException) t);
207: else
208: throw new DataStoreException(
209: "SQL Error running statement.", t);
210: } else if (ret == DataStoreProxy.REMOTE_STATUS_ACCESS_DENIED) {
211: throw new DataStoreException("Access Denied", t);
212: }
213:
214: }
215:
216: private URLConnection openConnection(DataStoreProxy ds, String url,
217: String sessionId, String operation,
218: String selectionCriteria, String userID, String pw)
219: throws Exception {
220: URL u = new URL(url);
221: URLConnection conn = u.openConnection();
222: conn.setDoInput(true);
223: conn.setDoOutput(true);
224: conn.setUseCaches(false);
225:
226: String session = sessionId;
227: String remoteID = ds.getRemoteID();
228: if (session == null && remoteID != null) {
229: int pos = remoteID.lastIndexOf("-");
230: if (pos > -1)
231: session = remoteID.substring(pos + 1);
232: }
233:
234: conn.setRequestProperty("Cookie", "sesessionid=" + session
235: + ";session=" + session + ";sessionid=" + session
236: + ";JSESSIONID=" + session + ";jsessionid=" + session);
237: if (_proxyUserID != null) {
238: String authString = _proxyUserID + ":" + _proxyPw;
239: String auth = "Basic "
240: + new Base64Encoder().encode(authString);
241: conn.setRequestProperty("Proxy-Authorization", auth);
242: }
243:
244: if (ds.getUseCompression())
245: conn.setRequestProperty(DataStoreProxy.USE_COMPRESSION,
246: "true");
247:
248: if (ds.getKeepDataOnServer())
249: conn.setRequestProperty(DataStoreProxy.KEEP_DATA_ON_SERVER,
250: "true");
251:
252: _out = new ObjectOutputStream(conn.getOutputStream());
253: _out.writeObject(operation);
254: _out.writeObject(remoteID);
255: _out.writeObject(selectionCriteria);
256: _out.writeObject(userID);
257: _out.writeObject(pw);
258: return conn;
259: }
260:
261: /**
262: * ping method comment.
263: */
264: public boolean ping(DataStoreProxy ds) throws Exception {
265: try {
266: _count = 0;
267: URLConnection conn = null;
268: conn = openConnection(ds, _url, _sessionId,
269: DataStoreProxy.OPERATION_PING, null, _userID, _pw);
270: int result = getResponse(conn);
271: if (result == DataStoreProxy.REMOTE_STATUS_OK) {
272: _out.close();
273: return true;
274: } else {
275: handleError(result, null);
276: return false;
277: }
278: } catch (Exception ex) {
279: throw new DataStoreException(ex.toString());
280: }
281: }
282:
283: /**
284: * ping method comment.
285: */
286: public ColumnDefinition[] getColumnsForTable(DataStoreProxy ds,
287: String table) throws Exception {
288: try {
289: _count = 0;
290: URLConnection conn = null;
291: conn = openConnection(ds, _url, _sessionId,
292: DataStoreProxy.OPERATION_GET_TABLE_COLUMNS, table,
293: _userID, _pw);
294: int result = getResponse(conn);
295: if (result == DataStoreProxy.REMOTE_STATUS_OK) {
296: _in = getInputStream(conn);
297: ColumnDefinition[] ret = (ColumnDefinition[]) _in
298: .readObject();
299: _out.close();
300: _in.close();
301: return ret;
302:
303: } else {
304: handleError(result, null);
305: return null;
306: }
307: } catch (Exception ex) {
308: throw new DataStoreException(ex.toString());
309: }
310: }
311:
312: /**
313: * postRetrieve method comment.
314: */
315: public void postRetrieve(DataStoreProxy ds) throws Exception {
316: if (_out != null) {
317: _out.close();
318: _out = null;
319: }
320: if (_in != null) {
321: _in.close();
322: _in = null;
323: }
324: }
325:
326: /**
327: * postUpdate method comment.
328: */
329: public String postUpdate(DataStoreProxy ds[],
330: boolean updateSucceeded) throws Exception {
331: String ret = null;
332: if (_out != null) {
333: _out.close();
334: _out = null;
335: }
336: if (_in != null && updateSucceeded) {
337: Object o = _in.readObject();
338: int ndx = -1;
339: boolean eof = false;
340: while (!eof) {
341: if (o instanceof Character) {
342: char c = ((Character) o).charValue();
343: if (c == 'S') {
344: ndx++;
345: populateOneDsAfterUpdate(_in, ds[ndx]);
346: o = _in.readObject();
347: } else if (c == 'X')
348: eof = true;
349: }
350: }
351: _in.close();
352: _in = null;
353: }
354: return ret;
355:
356: }
357:
358: private void populateOneDsAfterUpdate(ObjectInputStream in,
359: DataStoreProxy ds) throws Exception {
360: int currentRow = ds.getRow();
361: ds.setRemoteUpdateReturnValue((String) in.readObject());
362: Object o = in.readObject();
363: boolean end = false;
364: while (!end) {
365: char c = ' ';
366: if (o instanceof Character) {
367: c = ((Character) o).charValue();
368: if (c == 'E')
369: end = true;
370: else if (c == 'R') {
371: int rowNo = ((Integer) in.readObject()).intValue();
372: if (rowNo == -1)
373: rowNo = ds.insertRow();
374: DataStoreRow row = ds.getDataStoreRow(rowNo,
375: DataStoreBuffer.BUFFER_STANDARD);
376: o = in.readObject();
377: while (!(o instanceof Character)) {
378: int col = ((Integer) o).intValue();
379: o = in.readObject();
380: row.setData(col, o);
381: o = in.readObject();
382: }
383: row.resetStatus();
384: } else if (c == 'T')
385: o = in.readObject();
386: }
387: }
388: if (currentRow > -1)
389: ds.gotoRow(currentRow);
390: }
391:
392: /**
393: * preRetrieve method comment.
394: */
395: public boolean preRetrieve(DataStoreProxy ds,
396: String selectionCriteria, boolean countOnly)
397: throws Exception {
398:
399: _count = 0;
400: URLConnection conn = null;
401: String op = DataStoreProxy.OPERATION_RETRIEVE;
402:
403: if (countOnly)
404: op = DataStoreProxy.OPERATION_COUNT;
405:
406: conn = openConnection(ds, _url, _sessionId, op,
407: selectionCriteria, _userID, _pw);
408: int result = getResponse(conn);
409: if (result == DataStoreProxy.REMOTE_STATUS_OK) {
410: _in = getInputStream(conn);
411: if (countOnly) {
412: Integer i = (Integer) _in.readObject();
413: _count = i.intValue();
414: }
415: return true;
416: } else {
417: _in = getInputStream(conn);
418: Throwable t = (Throwable) _in.readObject();
419: _in.close();
420: handleError(result, t);
421: return false;
422: }
423:
424: }
425:
426: /**
427: * preUpdate method comment.
428: */
429: public boolean preUpdate(DataStoreProxy ds) throws Exception {
430: URLConnection conn = openConnection(ds, _url, _sessionId,
431: DataStoreProxy.OPERATION_UPDATE, null, _userID, _pw);
432: _conn = conn;
433: return true;
434: }
435:
436: /**
437: * retrieveRow method comment.
438: */
439: public boolean retrieveRow(DataStoreProxy ds, DataStoreRow row)
440: throws Exception {
441:
442: try {
443: for (int i = 0; i < ds.getColumnCount(); i++) {
444: row.setData(i, _in.readObject());
445: }
446: row.resetStatus();
447: _count++;
448: } catch (Exception e) {
449: return false;
450: }
451:
452: return true;
453: }
454:
455: /**
456: * updateRow method comment.
457: */
458: public boolean insertRow(DataStoreProxy ds, DataStoreRow row)
459: throws Exception {
460: Character c = new Character('I');
461: _out.writeObject(c);
462: _out.writeObject(new Integer(row.getDSDataRow().getProxyRow()));
463: int count = ds.getColumnCount();
464:
465: for (int i = 0; i < count; i++) {
466: _out.writeObject(new Integer(row.getColumnStatus(i)));
467: _out.writeObject(row.getData(i));
468: _out.writeObject(row.getTempValue(i));
469: }
470:
471: _out.writeObject(new Character('X'));
472:
473: return true;
474: }
475:
476: /**
477: * updateRow method comment.
478: */
479: public boolean updateRow(DataStoreProxy ds, DataStoreRow row)
480: throws Exception {
481: Character c = new Character('U');
482: _out.writeObject(c);
483: _out.writeObject(new Integer(row.getDSDataRow().getProxyRow()));
484: int count = ds.getColumnCount();
485:
486: //write out the old values for the where clause
487: for (int i = 0; i < count; i++) {
488: if (ds.isPrimaryKey(i)
489: || (ds.getConcurrencyCheckColumn(i) && ds
490: .getCheckConcurrency())) {
491: Object orig = row.getOriginalData(i);
492: _out.writeObject(orig);
493: }
494: }
495:
496: //write out the new values
497: for (int i = 0; i < count; i++) {
498: _out.writeObject(new Integer(row.getColumnStatus(i)));
499: _out.writeObject(row.getData(i));
500: _out.writeObject(row.getTempValue(i));
501: }
502:
503: _out.writeObject(new Character('X'));
504:
505: return true;
506: }
507:
508: public void writeDSHeader(DataStoreProxy ds) throws Exception {
509: _out.writeObject(new Character('N'));
510: _out.writeObject(ds.getRemoteID());
511: }
512:
513: public DataStoreException[] validateRemoteRules(DataStoreProxy ds,
514: DataStoreRow row, int rowNo, int column) {
515: Vector exceptions = new Vector();
516: try {
517: _count = 0;
518: URLConnection conn = null;
519: conn = openConnection(ds, _url, _sessionId,
520: DataStoreProxy.OPERATION_VALIDATE, null, _userID,
521: _pw);
522: _out.writeObject(new Integer(column));
523: _out.writeObject(new Integer(row.getDSDataRow()
524: .getProxyRow()));
525: for (int i = 0; i < ds.getColumnCount(); i++)
526: _out.writeObject(row.getData(i));
527: int result = getResponse(conn);
528: if (result == DataStoreProxy.REMOTE_STATUS_OK) {
529: _in = getInputStream(conn);
530: Object o = _in.readObject();
531: while (o instanceof DataStoreException) {
532: exceptions.add(o);
533: o = _in.readObject();
534: }
535: while (!(o instanceof Character)) {
536: int col = ((Integer) o).intValue();
537: Object data = _in.readObject();
538: ds.setAny(rowNo, col, data);
539: o = _in.readObject();
540: }
541: _in.close();
542:
543: _out.close();
544: } else {
545: handleError(result, null);
546: }
547: } catch (Exception ex) {
548: ex.printStackTrace();
549: }
550: DataStoreException retVal[] = new DataStoreException[exceptions
551: .size()];
552: exceptions.copyInto(retVal);
553: return retVal;
554: }
555:
556: private ObjectInputStream getInputStream(URLConnection conn)
557: throws IOException {
558: if (!_ds.getUseCompression())
559: return new ObjectInputStream(conn.getInputStream());
560: else
561: return new ObjectInputStream(new InflaterInputStream(conn
562: .getInputStream()));
563:
564: }
565:
566: }
|