View Javadoc

1   package org.lsst.ccs.plugin.jas3.rest;
2   
3   import com.sun.jersey.api.client.Client;
4   import com.sun.jersey.api.client.WebResource;
5   import com.sun.jersey.api.client.config.ClientConfig;
6   import com.sun.jersey.api.client.config.DefaultClientConfig;
7   import java.beans.PropertyChangeEvent;
8   import java.beans.PropertyChangeListener;
9   import java.util.logging.Level;
10  import java.util.logging.Logger;
11  import javax.ws.rs.core.MediaType;
12  import org.freehep.application.studio.Studio;
13  import org.freehep.util.FreeHEPLookup;
14  import org.lsst.ccs.localdb.statusdb.server.DataChannel;
15  
16  /**
17   * This class is used to connect to a restful service. It can be run on a
18   * background thread and does not directly interface to the GUI.
19   *
20   * @author tonyj
21   */
22  class RestConnector implements Runnable, PropertyChangeListener {
23  
24      private static final Logger logger = Logger.getLogger(RestConnector.class.getName());
25      private final RestPreferences prefs;
26      private final Client client;
27      private DataChannel.DataChannelList channelList;
28      private final Studio studio;
29      private WebResource service;
30  
31      RestConnector(Studio studio, RestPreferences restPrefs) {
32          this.studio = studio;
33          this.prefs = restPrefs;
34          ClientConfig config = new DefaultClientConfig();
35          client = Client.create(config);
36          initialize();
37      }
38  
39      private void initialize() {
40          prefs.addPropertyChangeListener(this);
41      }
42  
43      @Override
44      public void propertyChange(PropertyChangeEvent evt) {
45          // Fired if the RestPreferences URL has been changed.
46          Thread t = new Thread(this);
47          t.start();
48      }
49      
50      /**
51       * The run method is called to establish a new connection to the rest
52       * server, and register the channels obtained so they can be displayed to
53       * the user. It can also be invoked if the rest URL is changed, in which
54       * case it discards the old channel list if any, and obtains a new one.
55       * 
56       * Once obtained both the WebResource (service) and the channel list are 
57       * registered with the lookup service, so that any plugin which wants to 
58       * react to the change can do so.
59       */
60      @Override
61      public synchronized void run() {
62          final FreeHEPLookup lookup = studio.getLookup();
63          
64          if (service != null) {
65              logger.log(Level.INFO, "Discarding old channel list");
66              lookup.remove(channelList);
67              lookup.remove(service,"dataserver");
68              service = null;
69          }
70          
71          String restFullUrl = prefs.getRestURL();
72          logger.log(Level.INFO, "Connecting to rest server {0}", restFullUrl);
73          WebResource service = client.resource(restFullUrl).path("dataserver");
74          try {
75              channelList = service.path("listchannels").accept(MediaType.TEXT_XML).get(DataChannel.DataChannelList.class);
76              lookup.add(channelList);
77              lookup.add(service, "dataserver");
78              this.service = service;
79              logger.log(Level.INFO, "Connection successful, {0} channels read", channelList.list.size());
80          } catch (Exception e) {
81              logger.log(Level.WARNING, "Connection unsuccessful", e);
82          }
83      }
84  }