View Javadoc

1   /*$Id: Services.java,v 1.15 2004/11/22 18:26:54 clq2 Exp $
2    * Created on 27-Jan-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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) {// not even found the default??
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 $Log: Services.java,v $
248 Revision 1.15  2004/11/22 18:26:54  clq2
249 scripting-nww-715
250 
251 Revision 1.14.68.1  2004/11/22 15:54:51  nw
252 deprecated existing scripting interface (which includes service lists).
253 produced new scripting interface, with more helpler objects.
254 
255 Revision 1.14  2004/08/09 11:34:20  nw
256 improvied behaviour when no service list is found.
257 
258 Revision 1.13  2004/08/09 11:28:17  nw
259 improvied behaviour when no service list is found.
260 tidied imports.
261 
262 Revision 1.12  2004/08/09 09:55:17  nw
263 altered default location on classpath to look for services.xml
264 
265 Revision 1.11  2004/08/09 09:11:24  nw
266 added logging.
267 improved toString()
268 altered so that it degrades gracefully if service list document cannot be found
269 
270 Revision 1.10  2004/07/01 11:36:25  nw
271 fixed for removal of myspace delegate
272 
273 Revision 1.9  2004/05/07 15:33:50  pah
274 added egistryAdmin as a service type
275 
276 Revision 1.8  2004/03/12 13:50:23  nw
277 improved scripting object
278 
279 Revision 1.7  2004/03/05 16:27:28  nw
280 updated to new jes delegates
281 
282 Revision 1.6  2004/02/27 00:51:32  nw
283 improved loading
284 
285 Revision 1.5  2004/02/02 18:43:14  nw
286 improved constructors
287 
288 Revision 1.4  2004/02/02 10:30:48  nw
289 added helper method to create services.
290 
291 Revision 1.3  2004/02/01 00:38:24  nw
292 added two other kinds of job delegate
293 
294 Revision 1.2  2004/01/29 10:41:40  nw
295 extended scripting model with helper functions,
296 imporove javadoc
297 
298 Revision 1.1  2004/01/28 17:19:58  nw
299 first check in of scripting project
300  
301 */