1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.scripting;
12
13 import org.astrogrid.config.SimpleConfig;
14
15 import org.apache.commons.digester.Digester;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.xml.sax.SAXException;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /*** Maintains lists of known astrogrid services<p />
28 *
29 * The list of services is loaded from an xml file. This can either loaded from the default location on the classpath,
30 * or by specifying a URL.
31 * @author Noel Winstanley nw@jb.man.ac.uk 27-Jan-2004
32 *@deprecated. Will replace these features with registry lookups at some point. Use Toolbox for now.
33 */
34 public class Services extends Toolbox {
35 /***
36 * Commons Logger for this class
37 */
38 private static final Log logger = LogFactory.getLog(Services.class);
39
40
41
42 /*** default location of service list document - at the root of the classpath */
43 public static final String DEFAULT_SERVICE_LIST= "/services.xml";
44
45 /*** key to look in config for a service list */
46 public static final String SERVICE_LIST_URL_KEY = "scripting.service.list.url";
47
48 /*** constructor, that populates list using the default service list document
49 *
50 * @return a {@link java.util.List} of {@link Service} objects
51 * @throws IOException
52 * @throws SAXException
53 */
54 public Services() throws IOException, SAXException {
55 this((URL)null);
56 }
57
58 /*** constructor, populates a list using a document at specified URL */
59 public Services(String url) throws MalformedURLException, IOException, SAXException {
60 this( new URL(url) );
61 }
62
63 /*** constructor, that populates service list using document at specified url
64 *
65 * @param serviceListDocument URL of the service list document to read in
66 * @return a {@link java.util.List} of {@link Service} objects
67 * @throws IOException
68 * @throws SAXException
69 */
70 public Services(URL serviceListDocument) throws IOException, SAXException {
71 Digester dig = new Digester();
72 dig.push(this);
73 dig.addObjectCreate("services/service",Service.class);
74 dig.addSetProperties("services/service");
75 dig.addBeanPropertySetter("services/service/endpoint");
76 dig.addBeanPropertySetter("services/service/description");
77 dig.addBeanPropertySetter("services/service/type");
78 dig.addSetNext("services/service","addService");
79 if (serviceListDocument == null){
80 serviceListDocument = SimpleConfig.getSingleton().getUrl(SERVICE_LIST_URL_KEY,this.getClass().getResource(DEFAULT_SERVICE_LIST));
81 if (serviceListDocument == null) {
82 logger.warn("No service document specified - service list will be empty");
83 return;
84 }
85 }
86 logger.info("Reading service list from " + serviceListDocument);
87 InputStream is = serviceListDocument.openStream();
88 if (is == null) {
89 logger.warn("No Service document present at " + serviceListDocument + " - service list will be empty");
90 return;
91 }
92 dig.parse(is);
93 is.close();
94
95 }
96
97
98 protected List datacenters = new ArrayList();
99 protected List cea = new ArrayList();
100 protected List registries = new ArrayList();
101 protected List registryAdmins = new ArrayList();
102 protected List jobcontrollers = new ArrayList();
103 protected List jobmonitors = new ArrayList();
104 protected List unknownServices = new ArrayList();
105
106 /*** helper method to create a service
107 *
108 * @param type one of the constants in {@link Service}
109 * @param endpoint url of the service endpoint
110 * @return a freshly-created service object.
111 */
112 public Service createService(String type,String endpoint) {
113 Service s = new Service();
114 s.setType(type);
115 s.setEndpoint(endpoint);
116 return s;
117 }
118
119 /*** add a service to the list */
120 public void addService(Service s) {
121 if (Service.CEA_SERVICE.equals(s.getType())) {
122 cea.add(s);
123 } else if (Service.DATACENTER_SERVICE.equals(s.getType())) {
124 datacenters.add(s);
125 } else if (Service.JOBCONTROL_SERVICE.equals(s.getType())){
126 jobcontrollers.add(s);
127 } else if (Service.JOBMONITOR_SERVICE.equals(s.getType())){
128 jobmonitors.add(s);
129 } else if (Service.REGISTRY_SERVICE.equals(s.getType())) {
130 registries.add(s);
131 } else if (Service.REGISTRYADMIN_SERVICE.equals(s.getType())) {
132 registryAdmins.add(s);
133 } else {
134 unknownServices.add(s);
135 }
136 }
137
138 /*** Access list of all declared services
139 * @return
140 */
141 public List getAllServices() {
142 List allServices = new ArrayList();
143 allServices.addAll(datacenters);
144 allServices.addAll(cea);
145 allServices.addAll(registries);
146 allServices.addAll(registryAdmins);
147 allServices.addAll(jobcontrollers);
148 allServices.addAll(jobmonitors);
149 allServices.addAll(unknownServices);
150 return allServices;
151 }
152
153 /*** access list of all application services
154 * @return
155 */
156 public List getApplications() {
157 return cea;
158 }
159
160 /***access list of all datacenter services
161 * @return
162 */
163 public List getDatacenters() {
164 return datacenters;
165 }
166
167 /*** access list of all jes services - job controllers, job monitors,
168 * @return
169 * @deprecated use {@link #getJobControllers()} or {@link #getJobMonitors()} instead
170 */
171 public List getJes() {
172 List a = new ArrayList(jobcontrollers);
173 a.addAll(jobmonitors);
174 return a;
175 }
176
177 public List getJobControllers() {
178 return jobcontrollers;
179 }
180
181 public List getJobMonitors() {
182 return jobmonitors;
183 }
184
185
186 /***access list of all registry services
187 * @return list of {@link Service} objects
188 */
189 public List getRegistries() {
190 return registries;
191 }
192
193 /***access list of all registry admin services
194 * @return
195 */
196 public List getRegistryAdmins() {
197 return registryAdmins;
198 }
199
200 /*** access list of all unknoqn servics
201 * @return
202 */
203 public List getUnknownServices() {
204 return unknownServices;
205 }
206
207
208
209 public String toString() {
210 StringBuffer buffer = new StringBuffer();
211 buffer.append("[Services:");
212 if (datacenters.size() > 0) {
213 buffer.append(" datacenters: ");
214 buffer.append(datacenters);
215 }
216 if (cea.size() > 0) {
217 buffer.append(" cea: ");
218 buffer.append(cea);
219 }
220 if (registries.size() > 0) {
221 buffer.append(" registries: ");
222 buffer.append(registries);
223 }
224 if (registryAdmins.size() > 0) {
225 buffer.append(" registryAdmins: ");
226 buffer.append(registryAdmins);
227 }
228 if (jobcontrollers.size() > 0) {
229 buffer.append(" jobcontrollers: ");
230 buffer.append(jobcontrollers);
231 }
232 if (jobmonitors.size() > 0) {
233 buffer.append(" jobmonitors: ");
234 buffer.append(jobmonitors);
235 }
236 if (unknownServices.size() > 0) {
237 buffer.append(" unknownServices: ");
238 buffer.append(unknownServices);
239 }
240 buffer.append("]");
241 return buffer.toString();
242 }
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301