001: package net.sourceforge.squirrel_sql.plugins.sqlval;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell and Olof Edlund
005: * colbell@users.sourceforge.net
006: *
007: * This code is based on the example web service client code originally written
008: * by Olof Edlund.
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or (at your option) any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: */
024: import java.rmi.RemoteException;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.rpc.ParameterMode;
028: import javax.xml.rpc.ServiceException;
029:
030: import org.apache.axis.client.Call;
031: import org.apache.axis.client.Service;
032: import org.apache.axis.encoding.XMLType;
033:
034: import com.mimer.ws.validateSQL.SessionData;
035:
036: public class WebServiceSession {
037: /** Preferences. */
038: private final WebServicePreferences _prefs;
039:
040: /** Session properties. */
041: private final WebServiceSessionProperties _sessionProps;
042:
043: private SessionData _sessionData;
044:
045: public WebServiceSession(WebServicePreferences prefs,
046: WebServiceSessionProperties sessionProps) {
047: super ();
048: if (prefs == null) {
049: throw new IllegalArgumentException(
050: "WebServicePreferences == null");
051: }
052: if (sessionProps == null) {
053: throw new IllegalArgumentException(
054: "WebServiceSessionProperties == null");
055: }
056: _prefs = prefs;
057: _sessionProps = sessionProps;
058: }
059:
060: public boolean isOpen() {
061: return _sessionData != null;
062: }
063:
064: /**
065: * Open a sesion to the web service.
066: */
067: public void open() throws RemoteException, ServiceException {
068: Service l_service = new Service();
069: Call l_call = (Call) l_service.createCall();
070:
071: //Set the target server and name space
072: l_call.setTargetEndpointAddress(IWebServiceURL.WEB_SERVICE_URL);
073: l_call.setOperationName(new QName("SQL99Validator",
074: "openSession"));
075:
076: // Supply the user name. If you use anonymous you will be logged in and
077: // the pw will be ignored
078: l_call.addParameter("a_userName", XMLType.XSD_STRING,
079: ParameterMode.IN);
080:
081: // The pw. If user name is anonymous this can be anything. But it has to
082: // be supplied anyway.
083: l_call.addParameter("a_password", XMLType.XSD_STRING,
084: ParameterMode.IN);
085:
086: //The name of the calling client program.
087: //This is optional. If you don't want to give out this info, please enter "N/A"
088: l_call.addParameter("a_callingProgram", XMLType.XSD_STRING,
089: ParameterMode.IN);
090:
091: //And the version of the calling program.
092: //This is optional. If you don't want to give out this info, please enter "N/A"
093: l_call.addParameter("a_callingProgramVersion",
094: XMLType.XSD_STRING, ParameterMode.IN);
095:
096: //The target DBMS, could be Mimer SQL Engine, Oracle, ...
097: //This is optional. If you don't want to give out this info, please enter "N/A"
098: l_call.addParameter("a_targetDbms", XMLType.XSD_STRING,
099: ParameterMode.IN);
100:
101: //The version of the target DBMS
102: //This is optional. If you don't want to give out this info, please enter "N/A"
103: l_call.addParameter("a_targetDbmsVersion", XMLType.XSD_STRING,
104: ParameterMode.IN);
105:
106: //The connection Technology used, could be ODBC, JDBC, ADO
107: //This is optional. If you don't want to give out this info, please enter "N/A"
108: l_call.addParameter("a_connectionTechnology",
109: XMLType.XSD_STRING, ParameterMode.IN);
110:
111: //Version
112: //This is optional. If you don't want to give out this info, please enter "N/A"
113: l_call.addParameter("a_connectionTechnologyVersion",
114: XMLType.XSD_STRING, ParameterMode.IN);
115:
116: //Set this to 1 if your application is interactive where the user enters queries and then runs them
117: //Set it to 2 if it is non interactive, such as for instance a JDBC Bridge driver that intercepts SQL
118: l_call.addParameter("a_interactive", XMLType.XSD_INT,
119: ParameterMode.IN);
120:
121: QName l_qn = new QName(IWebServiceURL.REQUEST_URL,
122: "SessionData");
123:
124: l_call
125: .registerTypeMapping(
126: SessionData.class,
127: l_qn,
128: new org.apache.axis.encoding.ser.BeanSerializerFactory(
129: SessionData.class, l_qn),
130: new org.apache.axis.encoding.ser.BeanDeserializerFactory(
131: SessionData.class, l_qn));
132:
133: //Set the return type
134: l_call.setReturnType(l_qn);
135:
136: // Open the session.
137: final boolean anonLogon = _prefs.getUseAnonymousLogon();
138: final boolean anonClient = _prefs.getUseAnonymousClient();
139: final boolean anonDBMS = _sessionProps.getUseAnonymousDBMS();
140:
141: final Object[] parms = new Object[] {
142: anonLogon ? "anonymous" : _prefs.getUserName(),
143: anonLogon ? "N/A" : _prefs.retrievePassword(),
144: anonClient ? "N/A" : _prefs.getClientName(),
145: anonClient ? "N/A" : _prefs.getClientVersion(),
146: anonDBMS ? "N/A" : _sessionProps.getTargetDBMSName(),
147: anonDBMS ? "N/A" : _sessionProps.getTargetDBMSVersion(),
148: anonDBMS ? "N/A" : _sessionProps
149: .getConnectionTechnology(),
150: anonDBMS ? "N/A" : _sessionProps
151: .getConnectionTechnologyVersion(),
152: Integer.valueOf(1) // 1 = interactive, 0 = batch
153: };
154: _sessionData = (SessionData) l_call.invoke(parms);
155: }
156:
157: /**
158: * Close the session.
159: */
160: public void close() {
161: _sessionData = null;
162: }
163:
164: /**
165: * Return the target URL to the open session.
166: *
167: * @return Target URL.
168: *
169: * @throws IllegalStateException
170: * Thrown if connection has not yet been opened.
171: */
172: String getTargetURL() {
173: validateState();
174: return _sessionData.getTarget();
175: }
176:
177: /**
178: * Return the ID of the web service session.
179: *
180: * @return session ID
181: *
182: * @throws IllegalStateException
183: * Thrown if connection has not yet been opened.
184: */
185: int getSessionID() {
186: validateState();
187: return _sessionData.getSessionId();
188: }
189:
190: /**
191: * Return the key of the web service session.
192: *
193: * @return session key
194: *
195: * @throws IllegalStateException
196: * Thrown if connection has not yet been opened.
197: */
198: int getSessionKey() {
199: validateState();
200: return _sessionData.getSessionKey();
201: }
202:
203: private void validateState() {
204: if (_sessionData == null) {
205: throw new IllegalStateException(
206: "Connection to web service has not been opened");
207: }
208: }
209: }
|