1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.extensions.config.ConfigService;
24 import org.springframework.extensions.config.RemoteConfigElement;
25 import org.springframework.extensions.config.RemoteConfigElement.EndpointDescriptor;
26 import org.springframework.extensions.surf.exception.ConnectorProviderException;
27 import org.springframework.extensions.webscripts.connector.Connector;
28 import org.springframework.extensions.webscripts.connector.ConnectorProvider;
29 import org.springframework.extensions.webscripts.connector.ConnectorProviderImpl;
30 import org.springframework.extensions.webscripts.connector.Response;
31
32
33
34
35
36
37
38
39
40
41
42 public class ScriptRemote
43 {
44 private static final Log logger = LogFactory.getLog(ScriptRemote.class);
45
46 private ConfigService configService;
47 private ConnectorProvider connectorProvider;
48
49
50
51
52
53
54 public void setConfigService(ConfigService configService)
55 {
56 this.configService = configService;
57 }
58
59
60
61
62
63
64 public void setConnectorProvider(ConnectorProvider connectorProvider)
65 {
66 this.connectorProvider = connectorProvider;
67 }
68
69
70
71
72
73
74
75 public ScriptRemoteConnector connect()
76 {
77 ScriptRemoteConnector remoteConnector = null;
78
79
80 RemoteConfigElement remoteConfig = getRemoteConfig();
81 if (remoteConfig != null)
82 {
83
84 String defaultEndpointId = remoteConfig.getDefaultEndpointId();
85 if (defaultEndpointId != null)
86 {
87
88 remoteConnector = connect(defaultEndpointId);
89 }
90 }
91
92 return remoteConnector;
93 }
94
95
96
97
98
99
100
101
102
103 public ScriptRemoteConnector connect(String endpointId)
104 {
105 ScriptRemoteConnector remoteConnector = null;
106
107 RemoteConfigElement remoteConfig = getRemoteConfig();
108 if (remoteConfig != null)
109 {
110
111 EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
112 if (descriptor == null)
113 {
114 logger.error("No EndPoint descriptor configuration found for ID: " + endpointId);
115 }
116 else
117 {
118
119
120 if (connectorProvider == null)
121 {
122 connectorProvider = new ConnectorProviderImpl();
123 }
124
125 try
126 {
127
128 Connector connector = connectorProvider.provide(endpointId);
129 remoteConnector = new ScriptRemoteConnector(connector, descriptor);
130 }
131 catch (ConnectorProviderException cpe)
132 {
133 logger.error("Unable to provision connector for endpoint: " + endpointId);
134 }
135 }
136 }
137
138 return remoteConnector;
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public Response call(String uri)
155 {
156 return this.connect().call(uri);
157 }
158
159
160
161
162
163
164 public String[] getEndpointIds()
165 {
166 String[] endpointIds = null;
167
168 RemoteConfigElement remoteConfig = getRemoteConfig();
169 if(remoteConfig != null)
170 {
171 endpointIds = remoteConfig.getEndpointIds();
172 }
173
174 return endpointIds;
175 }
176
177
178
179
180
181
182
183 public String getEndpointName(String endpointId)
184 {
185 String name = null;
186
187 RemoteConfigElement remoteConfig = getRemoteConfig();
188 if(remoteConfig != null)
189 {
190 EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
191 if(descriptor != null)
192 {
193 name = descriptor.getName();
194 }
195 }
196
197 return name;
198 }
199
200
201
202
203
204
205
206 public String getEndpointDescription(String endpointId)
207 {
208 String description = null;
209
210 RemoteConfigElement remoteConfig = getRemoteConfig();
211 if(remoteConfig != null)
212 {
213 EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
214 if(descriptor != null)
215 {
216 description = descriptor.getDescription();
217 }
218 }
219
220 return description;
221 }
222
223 public boolean isEndpointPersistent(String id)
224 {
225 boolean persistent = false;
226
227 RemoteConfigElement remoteConfig = getRemoteConfig();
228 if(remoteConfig != null)
229 {
230 EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(id);
231 if(descriptor != null)
232 {
233 persistent = descriptor.getPersistent();
234 }
235 }
236
237 return persistent;
238 }
239
240
241
242
243
244
245
246
247 public String getEndpointURL(String endpointId)
248 {
249 String url = null;
250
251 RemoteConfigElement remoteConfig = getRemoteConfig();
252 if (remoteConfig != null)
253 {
254 EndpointDescriptor descriptor = remoteConfig.getEndpointDescriptor(endpointId);
255 if (descriptor != null)
256 {
257 url = descriptor.getEndpointUrl();
258 }
259 }
260
261 return url;
262 }
263
264
265
266
267 private RemoteConfigElement getRemoteConfig()
268 {
269 RemoteConfigElement remoteConfig = (RemoteConfigElement)configService.getConfig(
270 "Remote").getConfigElement("remote");
271 return remoteConfig;
272 }
273 }