|
楼主

楼主 |
发表于 2005-11-4 21:31:21
|
只看该作者
SAS/Connect连接Server的TelnetConnectClient的参数怎么写
我的spawner是用 spawner -autherserver username启动的
现在我写Java Client连Server的时候
发现TelentConnectClient的构造参数是一个Properties对象
这个对象应该怎么设参数
servername port user password应该怎么设?
有没有人用过SAS/Connect的知道这个
SAS的Connect包里有个example,我实在是不明白他关于参数设置是怎么写的,特别是getParameter()函数怎么来的啊?
谁能帮我看看
[code:762d6]
/**
* Copyright (c)1996 SAS Institute Inc, Cary NC USA 27513
* Unpublished- All Rights Reserved.
*/
package com.sas.net.connect.samples;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Choice;
import java.awt.Event;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Vector;
import com.sas.net.connect.ConnectClient;
import com.sas.net.connect.ConnectException;
import com.sas.net.connect.TelnetConnectClient;
import com.sas.net.connect.TunneledConnectClient;
/**
*
*History Records:
*
* Date Name Remarks
*
* 07/28/97 SASDJH/SASBBW - Used as a model to build a simple demo that
* populates list boxes with data sets and
* allows users to either submit a proc print
* or a proc contents.
* 03/29/99 SASBFB - Added encryption options.
* 09/29/00 SASBFB - Removed deprecated Thread.stop() call.
*/
public class ConnectApplet extends java.applet.Applet implements Runnable
{
/**
* These are all the variables that are needed for the connection to the SAS server.
* In general, most applets will require all of these.
*/
static final int doConnect = 1;
static final int doSubmit = 2;
boolean connect = false;
Thread connectThread = null;
boolean cThreadDone = false;
boolean stopCalled = false;
int cThreadCommand = 0;
String host = null;
int port = 23;
String routerUrl = null;
String userNameResponse = null;
Properties info = new Properties();
java.sql.Statement statement = null;
java.sql.ResultSet resultset = null;
java.sql.Connection jdbcConnection = null;
ConnectClient connection = null;
String lines = new String();
/**
* These are all the variables that are needed for the GUI.
*/
Button print;
Button contents;
Button clear;
List dset;
List dlib;
TextField status;
TextArea list;
/**
* This method initializes the applet and calls readParameters to read the parameters from
* the html file. It also initializes the visual components. This method should do the
* absolute minimum required to initialize the applet. This method is called once, when the
* applet is created.
*
*/
public void init()
{
readParameters();
setLayout(new BorderLayout());
/* Layout body in grid */
GridBagPanel panel = new GridBagPanel();
add("Center", panel);
Label label = null;
/* add components */
label = new Label("Data Library: ");
panel.addComponent(label, 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
dlib = new List(2,false);
panel.addComponent(dlib, 0, 1, 1, 2, 0.0, 0.5,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
label = new Label("Data Set: ");
panel.addComponent(label, 0, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
dset = new List(2,false);
panel.addComponent(dset, 0, 4, 1, 2, 0.0, 0.5,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
print = new Button("Print");
panel.addComponent(print, 0, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
contents = new Button("Contents");
panel.addComponent(contents, 0, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
label = new Label("Procedure Output", Label.CENTER);
panel.addComponent(label, 1, 0, 4, 1, 1.0, 0.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
list = new TextArea("", 3, 40);
Font f = new Font("Courier",Font.PLAIN,12);
list.setFont(f);
panel.addComponent(list, 1, 1, 5, 7, 1.0, 1.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
print.disable();
contents.disable();
status = new TextField("", 40);
panel.addComponent(status, 0, 8, 6, 1, 1.0, 0.0,
GridBagConstraints.NORTHWEST,
GridBagConstraints.BOTH);
}
/**
* The submitLines method submits a SAS statements to the SAS server and displays the results.
* Applet writers may want to change the way results are displayed, and therefore modify
* this method.
*/
synchronized void submitLines() {
try {
if ((lines != null) && (lines.length() != 0)) {
status.setText("Submitting SAS statements ...");
connection.clearListLines();
connection.rsubmit(lines);
connection.clearEditLines();
lines = connection.getListLines();
if (lines.length() != 0) {
list.setText(lines);
repaint();
}
}
status.setText("Execution complete.");
} catch (ConnectException se) {
status.setText(se.getMessage());
}
lines = null;
}
/**
* The getLibnames method obtains the list of libraries defined for the SAS session and
* adds them to a list box.
* Applet writers may want to change the way results are displayed, and therefore modify
* this method.
*/
synchronized void getLibnames()
{ /* populate the libnames list box at startup */
status.setText("Getting library names ...");
try
{
statement = jdbcConnection.createStatement();
String query = new String("select distinct libname from dictionary.tables order libname");
resultset = statement.executeQuery(query);
while (resultset.next())
{
dlib.addItem(resultset.getString(1));
}
resultset.close();
statement.close();
status.setText("Execution complete.");
} catch (java.sql.SQLException e) {
status.setText(e.getMessage());
}
}
/**
* The getDataSetNames method obtains the list of data sets defined in a library and
* adds them to a list box.
* Applet writers may want to change the way results are displayed, and therefore modify
* this method.
*/
synchronized void getDataSetNames() {
status.setText("Getting data set names ...");
try {
/* construct and submit the SQL query, add result to data set list box */
statement = jdbcConnection.createStatement();
String query = new String("select memname from dictionary.tables " +
"where libname ='" + dlib.getSelectedItem() +
"' order memname" );
resultset = statement.executeQuery(query);
while (resultset.next())
{
dset.addItem(resultset.getString(1));
}
resultset.close();
statement.close();
status.setText("Execution complete.");
}
/* following is standard exception handling */
catch (java.sql.SQLException e) {
status.setText(e.getMessage());
}
}
/**
* This method receives all the events from the applet's GUI. SAS applet
* writers will modify this method to accept events from their GUI.
*/
public boolean handleEvent(Event evt)
{
if (stopCalled == true) {
return false;
}
if ( (evt.target instanceof List) && (evt.target == dlib) &&
( (evt.id == Event.LIST_SELECT) | (evt.id == Event.LIST_DESELECT) ) ) {
/* user clicked on the libnames list box */
if (dset.countItems() > 0)
dset.clear();
getDataSetNames();
}
/* user clicked on one of the buttons */
if ((evt.target instanceof Button) && (evt.id == Event.ACTION_EVENT)) {
if (evt.target == print) lines = "proc print data=" +
dlib.getSelectedItem() + '.' + dset.getSelectedItem() +
";run;";
else lines = "proc contents data=" +
dlib.getSelectedItem() + '.' + dset.getSelectedItem() +
";run;";
cThreadDone = false;
cThreadCommand = doSubmit;
connectThread = new Thread(this, "Connect thread");
connectThread.start();
return true;
}
/* user selected an item from the data set list box */
/* disable/enable the buttons based on whether a data set is selected */
if ( (evt.target instanceof List) && (evt.target == dset) &&
( (evt.id == Event.LIST_SELECT) | (evt.id == Event.LIST_DESELECT) ) ) {
if (dset.getSelectedItem() == null)
{
print.disable();
contents.disable();
}
else
{
print.enable();
contents.enable();
}
}
return false;
}
/** This reads all of the parameters neccessary for the applet.
*
*/
public void readParameters() {
String temp = null;
host = getParameter("host");
temp = getParameter("port");
if (temp != null)
port = Integer.parseInt(temp);
temp = getParameter("sasPortTag");
if (temp != null)
info.put("sasPortTag", temp);
temp = getParameter("sasPortTagTimeout");
if (temp != null)
info.put("sasPortTagTimeout", temp);
userNameResponse = getParameter("userNameResponse");
if (userNameResponse != null)
info.put("userNameResponse", userNameResponse);
temp = getParameter("passwordResponse");
if (temp != null)
info.put("passwordResponse", temp);
int i = 1;
for (;;) {
String prompt = new String("prompt" + i);
temp = getParameter(prompt);
if (temp == null)
break;
info.put(prompt, temp);
prompt = new String("promptTimeout" + i);
temp = getParameter(prompt);
if (temp != null)
info.put(prompt, temp);
String response = new String("response" + i);
temp = getParameter(response).trim();
info.put(response, temp);
response = new String("responseTimeout" + i);
temp = getParameter(response);
if (temp != null)
info.put(response, temp);
i++;
}
routerUrl = getParameter("routerUrl");
if (routerUrl != null)
info.put("routerUrl", routerUrl);
temp = getParameter("encryptionPolicy");
if (temp != null)
info.put("encryptionPolicy",temp);
temp = getParameter("encryptionAlgorithms");
if (temp != null)
info.put("encryptionAlgorithms",temp);
temp = getParameter("encryptionTarget");
if (temp != null)
info.put("encryptionTarget",temp);
}
/**
* The following methods manage the connection with the SAS server. We do not expect
* these methods to be modified.
*
*/
/**
* Start is a very light-weight method that starts a separate thread to establish
* a connection to the data source. After the start method has completed, the
* applet's GUI will be displayed in its window.
*/
public void start()
{
if (userNameResponse != null) {
String temp = (String) info.get(userNameResponse);
if ((temp != null) && (temp.length() != 0)) {
cThreadDone = false;
cThreadCommand = doConnect;
connectThread = new Thread(this, "Connect thread");
connectThread.start();
return;
} else {
logonPrompt();
return;
}
} else {
cThreadDone = false;
cThreadCommand = doConnect;
connectThread = new Thread(this, "Connect thread");
connectThread.start();
return;
}
}
/**
* The logonPrompt method is called when a userid/password is required by the data source.
* I would not expect SAS applet writers to modify this method. I had problems with the
* modality of dialog boxes, so I avoided their use and overlaid the middle panel with the
* logon prompt.
*/
public void logonPrompt() {
java.awt.Component parent = (java.awt.Component)this;
while ((parent != null) && !(parent instanceof java.awt.Frame)) {
parent = parent.getParent();
}
LoginDialog dialog = new LoginDialog((java.awt.Frame) parent, "Login", true, this);
dialog.show();
}
/**
* The run method is invoked by the thread created by start. It establishes the connection
* to the data source and submits the first query if one is available.
* SAS applet writers may want to add additional code to this method, but I would not
* expect them to remove any of the existing code.
*/
public void run()
{
cThreadDone = false;
switch (cThreadCommand) {
case doConnect:
/* Here is where the initial query gets run */
connectThread.yield();
if (stopCalled == true) {
cThreadDone = true;
cThreadCommand = 0;
connectThread = null;
return;
}
if (connect == false) {
connect();
}
connectThread.yield();
if (stopCalled == true) {
cThreadDone = true;
cThreadCommand = 0;
connectThread = null;
return;
}
if (connect == true) {
getLibnames();
}
break;
case doSubmit:
if (connect == true) {
submitLines();
}
break;
default:
break;
}
cThreadCommand = 0;
cThreadDone = true;
connectThread = null;
}
public void loginDone() {
String userName = (String) info.get("userName");
String temp = (String) info.get("userNameResponse");
if (temp != null) {
info.put(temp, userName);
}
String password = (String) info.get("password");
temp = (String) info.get("passwordResponse");
if (temp != null) {
info.put(temp, password);
}
cThreadDone = false;
cThreadCommand = doConnect;
connectThread = new Thread(this, "Connect thread");
connectThread.start();
}
/**
* The connect method establishes a socket connection to the data source.
* I would not expect SAS applet writers to modify this method. This
* method is likely to hang (and cause the applet to hang) if an invalid
* user name, password, or sas command have been specified and appropriate
* time out values have not been set.
*/
synchronized void connect() {
try {
status.setText("Logging onto " + host);
if (routerUrl == null)
connection = (ConnectClient) new TelnetConnectClient(info);
else
connection = (ConnectClient) new TunneledConnectClient(info);
connection.connect( host, port);
jdbcConnection = connection.getSharenet();
connect = true;
status.setText("SAS initialized, connection complete");
} catch (ConnectException e) {
status.setText(e.getMessage());
}
return;
}
/**
* The stop method destroys the connection thread, any on-going queries and
* closes the connection to the data source.
* I would not expect SAS applet writers to delete any code in this method.
* There may be additional code added if necessary.
*/
public void stop()
{
print.disable();
contents.disable();
if (dset.countItems() > 0)
dset.clear();
if (dlib.countItems() > 0)
dlib.clear();
list.setText("");
stopCalled = true;
if (connectThread != null) {
while (cThreadDone != true)
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
connectThread = null;
}
if (connect == true) {
disconnect();
}
stopCalled = false;
}
/**
* The disconnect method closes the socket connection to the data source.
* I would not expect SAS applet writers to modify this method.
*/
void disconnect()
{
if (connect == true) {
try {
connection.disconnect();
connect = false;
} catch (ConnectException e) {
connect = false;
}
connection = null;
}
}
}
[/code:762d6] |
|