001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General public final synchronized License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG 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 final synchronized License for more details.
013:
014: You should have received a copy of the GNU General public final synchronized License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020:
021: package net.sf.borg.model.db.remote;
022:
023: import java.util.Collection;
024:
025: import net.sf.borg.common.XTree;
026: import net.sf.borg.model.BorgOption;
027: import net.sf.borg.model.beans.KeyedBean;
028: import net.sf.borg.model.db.BeanDB;
029:
030: /**
031: * @author Mohan Embar
032: */
033: class RemoteBeanDB implements BeanDB {
034: // BeanDB overrides
035: public final synchronized Collection readAll() throws Exception {
036: Collection col = (Collection) call("readAll", null);
037: return col;
038: }
039:
040: public final synchronized KeyedBean readObj(int key)
041: throws Exception {
042: return (KeyedBean) call("readObj", new Integer(key));
043: }
044:
045: public final synchronized KeyedBean newObj() {
046: try {
047: return (KeyedBean) cls.newInstance();
048: } catch (Exception e) {
049: throw new RuntimeException(e.getClass().getName() + ": "
050: + e.getMessage());
051: }
052: }
053:
054: public final synchronized void addObj(KeyedBean bean, boolean crypt)
055: throws Exception {
056: call("addObj", new IRemoteProxy.ComposedObject(bean,
057: new Boolean(crypt) /* Boolean.valueOf(crypt) */));
058: }
059:
060: public final synchronized void updateObj(KeyedBean bean,
061: boolean crypt) throws Exception {
062: call("updateObj", new IRemoteProxy.ComposedObject(bean,
063: new Boolean(crypt) /* Boolean.valueOf(crypt) */));
064: }
065:
066: public final synchronized void delete(int key) throws Exception {
067: call("delete", new Integer(key));
068: }
069:
070: public final synchronized String getOption(String oname)
071: throws Exception {
072: return (String) call("getOption", oname);
073: }
074:
075: public final synchronized void setOption(BorgOption option)
076: throws Exception {
077: call("setOption", option);
078: }
079:
080: public final synchronized Collection getOptions() throws Exception {
081: return (Collection) call("getOptions", null);
082: }
083:
084: public final synchronized void close() throws Exception {
085: // ignore this - we're sharing
086: }
087:
088: public final synchronized int nextkey() throws Exception {
089: return ((Integer) call("nextkey", null)).intValue();
090: }
091:
092: public final synchronized boolean isDirty() throws Exception {
093: boolean result = ((Boolean) call("isDirty", null))
094: .booleanValue();
095: // System.out.println("isDirty = "+result);
096: return result;
097: }
098:
099: public final synchronized void sync() {
100: }
101:
102: // protected //
103: protected Object call(String command, Object args) throws Exception {
104: IRemoteProxy.Parms parms = new IRemoteProxy.Parms(clsstr,
105: command, args, user);
106: XTree xmlParms = XmlObjectHelper.toXml(parms);
107: String xmlstr = xmlParms.toString();
108: // System.out.println(xmlstr);
109: RemoteProxyHome home = RemoteProxyHome.getInstance();
110: String result = home.getProxy(impl).execute(xmlstr,
111: home.getProxyProvider());
112: //System.out.println("OutTrace - " + result);
113: //System.err.println("ErrTrace - " + result);
114: XTree xmlResult = XTree.readFromBuffer(result);
115: Object retval = XmlObjectHelper.fromXml(xmlResult);
116: if (retval instanceof Exception)
117: throw (Exception) retval;
118:
119: return retval;
120: }
121:
122: // package //
123: RemoteBeanDB(Class cls, String clsstr, String impl, String user) {
124: this .cls = cls;
125: this .clsstr = clsstr;
126: this .impl = impl;
127: this .user = user;
128: }
129:
130: // private //
131: private Class cls;
132:
133: private String clsstr;
134:
135: private String impl;
136:
137: private String user;
138: }
|