-
Notifications
You must be signed in to change notification settings - Fork 10
/
SasServer.cs
193 lines (171 loc) · 6.48 KB
/
SasServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace SasHarness
{
/// <summary>
/// A simple class to "wrap" the features of the SAS Workspace
/// for use within the main calling program
/// </summary>
public class SasServer
{
/// <summary>
/// Name of the server ("SASApp")
/// </summary>
public string Name { get; set; }
/// <summary>
/// Node name of the server
/// </summary>
public string Host { get; set; }
/// <summary>
/// Port (number), such as 8591
/// </summary>
public string Port { get; set; }
/// <summary>
/// User ID that can connect to a Workspace
/// </summary>
public string UserId { get; set; }
/// <summary>
/// Password to connect to the Workspace
/// </summary>
public string Password { get; set; }
/// <summary>
/// Whether to use Local (COM) connection instead of IOM Bridge
/// </summary>
public bool UseLocal { get; set; }
// Use the ObjectKeeper, which keeps track of SAS Workspaces
// We need this so that the OLE DB provider can find the workspace to
// connect to if/when the user opens a data set to view
internal static SASObjectManager.ObjectKeeper objectKeeper =
new SASObjectManager.ObjectKeeper();
/// <summary>
/// Property for the SAS Workspace connection.
/// Will connect if needed.
/// </summary>
public SAS.Workspace Workspace
{
get
{
if (_workspace == null)
Connect();
if (_workspace!=null)
return _workspace;
else
throw new Exception("Could not connect to SAS Workspace");
}
}
/// <summary>
/// Is a Workspace connected?
/// </summary>
public bool IsConnected
{
get
{
return _workspace != null;
}
}
/// <summary>
/// Close the Workspace if connected
/// </summary>
public void Close()
{
if (IsConnected) _workspace.Close();
// clear out the ObjectKeeper
objectKeeper.RemoveAllObjects();
_workspace = null;
}
#region Save and restore settings for convenience
public string ToXml()
{
// note: we're not saving the password
XElement settings = new XElement("SasServer");
settings.SetAttributeValue("name", Name);
settings.SetAttributeValue("host", Host);
settings.SetAttributeValue("port", Port);
settings.SetAttributeValue("userid", UserId);
settings.SetAttributeValue("useLocal", XmlConvert.ToString(UseLocal));
return settings.ToString();
}
public static SasServer FromXml(string xml)
{
SasServer s = new SasServer();
try
{
XElement settings = XElement.Parse(xml);
if (settings.Attribute("name") != null)
s.Name = settings.Attribute("name").Value;
if (settings.Attribute("host") != null)
s.Host = settings.Attribute("host").Value;
if (settings.Attribute("port") != null)
s.Port = settings.Attribute("port").Value;
if (settings.Attribute("userid") != null)
s.UserId = settings.Attribute("userid").Value;
if (settings.Attribute("useLocal") != null)
s.UseLocal = XmlConvert.ToBoolean(settings.Attribute("useLocal").Value);
}
catch (System.Exception)
{
// no need to bomb if this goes poorly because of bad XML
}
return s;
}
#endregion
/// <summary>
/// Connect to a SAS Workspace
/// </summary>
public void Connect()
{
if (_workspace != null)
try
{
Close();
}
catch { }
finally
{
_workspace = null;
}
if (!UseLocal)
{
// Connect using the IOM Bridge (TCP) for remote server
SASObjectManager.IObjectFactory2 obObjectFactory =
new SASObjectManager.ObjectFactoryMulti2();
SASObjectManager.ServerDef obServer =
new SASObjectManager.ServerDef();
obServer.MachineDNSName = Host;
obServer.Protocol = SASObjectManager.Protocols.ProtocolBridge;
obServer.Port = Convert.ToInt32(Port);
obServer.ClassIdentifier = "440196d4-90f0-11d0-9f41-00a024bb830c";
// handle the case where there is no UserID or PW, and try IWA
// Doc on integrated Windows auth in SAS: http://bit.ly/14jAF7X
if (string.IsNullOrEmpty(UserId))
{
obServer.BridgeSecurityPackage = "Negotiate";
}
_workspace = (SAS.Workspace)obObjectFactory.CreateObjectByServer(
Name, true,
obServer,
// if trying IWA, pass null in
// otherwise try supplied credentials
string.IsNullOrEmpty(UserId) ? null : UserId,
string.IsNullOrEmpty(Password) ? null : Password);
objectKeeper.AddObject(1, Name, _workspace);
}
else
{
// Connect using COM protocol, locally installed SAS only
SASObjectManager.IObjectFactory2 obObjectFactory = new SASObjectManager.ObjectFactoryMulti2();
SASObjectManager.ServerDef obServer = new SASObjectManager.ServerDef();
obServer.MachineDNSName = "localhost";
obServer.Protocol = SASObjectManager.Protocols.ProtocolCom;
obServer.Port = 0;
_workspace = (SAS.Workspace)obObjectFactory.CreateObjectByServer(Name, true, obServer, null, null);
objectKeeper.AddObject(1, Name, _workspace);
}
}
private SAS.Workspace _workspace = null;
}
}