First patch for * JAL-493
[jalview.git] / src / jalview / ws / jws2 / Jws2Discoverer.java
1 package jalview.ws.jws2;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.HashSet;
6 import java.util.Vector;
7
8 import javax.swing.JMenu;
9 import javax.swing.JMenuItem;
10
11 import org.apache.log4j.Level;
12
13 import jalview.bin.Cache;
14 import jalview.datamodel.AlignmentView;
15 import jalview.gui.AlignFrame;
16 import jalview.ws.WSMenuEntryProviderI;
17 import compbio.data.msa.MsaWS;
18 import compbio.ws.client.Jws2Base;
19 import compbio.ws.client.Jws2Base.Services;
20
21 /**
22  * discoverer for jws2 services. Follows the lightweight service discoverer
23  * pattern (archetyped by EnfinEnvision2OneWay)
24  * 
25  * @author JimP
26  * 
27  */
28 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
29 {
30   compbio.data.msa.MsaWS service;
31   boolean running=false;
32   @Override
33   public void run()
34   {
35     if (running)
36     {
37       return;
38     }
39     running=true;
40 //    Cache.initLogger();
41 //    Cache.log.setLevel(Level.DEBUG);
42     // TODO: Document and PACK JWS2
43     String jwsservers = Cache.getDefault("JWS2HOSTURLS",
44             "http://webservices.compbio.dundee.ac.uk:8084/jws2");
45     try
46     {
47       if (Jws2Base.validURL(jwsservers))
48       {
49         // look for services
50         for (Services srv : Jws2Base.Services.values())
51         {
52           MsaWS service = null;
53           try
54           {
55             service = Jws2Base.connect(jwsservers, srv);
56           } catch (Exception e)
57           {
58           }
59           ;
60           if (service != null)
61           {
62             addService(jwsservers, srv, service);
63           }
64         }
65
66       }
67       else
68       {
69         Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
70       }
71     } catch (Exception e)
72     {
73       Cache.log.warn("Exception when discovering Jws2 services.", e);
74     } catch (Error e)
75     {
76       Cache.log.error("Exception when discovering Jws2 services.", e);
77     }
78     running=false;
79
80   }
81
82   /**
83    * record this service endpoint so we can use it
84    * 
85    * @param jwsservers
86    * @param srv
87    * @param service2
88    */
89   private void addService(String jwsservers, Services srv, MsaWS service2)
90   {
91     Cache.log.debug("Discovered service: " + jwsservers + " "
92             + srv.toString());
93     if (services==null) {
94       services = new Vector<Jws2Instance>();
95     }
96     services.add(new Jws2Instance(jwsservers, "Align with "
97             + srv.toString(), service2));
98   }
99
100   public class Jws2Instance
101   {
102     String hosturl;
103
104     String serviceType;
105
106     MsaWS service;
107
108     public Jws2Instance(String hosturl, String serviceType, MsaWS service)
109     {
110       super();
111       this.hosturl = hosturl;
112       this.serviceType = serviceType;
113       this.service = service;
114     }
115
116   };
117
118   /**
119    * holds list of services.
120    */
121   Vector<Jws2Instance> services;
122
123   @Override
124   public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
125   {
126     if (running || services==null || services.size() == 0)
127     {
128       return;
129     }
130     /**
131      * eventually, JWS2 services will appear under the same align/etc submenus.
132      * for moment we keep them separate.
133      */
134     JMenu jws2 = new JMenu("JWS2 Alignment");
135     MsaWSClient msacl = new MsaWSClient();
136     for (final Jws2Instance service : services)
137     {
138       msacl.attachWSMenuEntry(jws2, service, alignFrame);
139       /*JMenuItem sitem = new JMenuItem(service.serviceType);
140       sitem.setToolTipText("Hosted at " + service.hosturl);
141       sitem.addActionListener(new ActionListener()
142       {
143
144         @Override
145         public void actionPerformed(ActionEvent e)
146         {
147           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
148           MsaWSClient client = new MsaWSClient(service,
149                   "JWS2 Alignment of " + alignFrame.getTitle(), msa, false,
150                   true, alignFrame.getViewport().getAlignment().getDataset(),
151                   alignFrame);
152         }
153       });*/
154     }
155     if (services.size()>0)
156     {
157       wsmenu.add(jws2);
158     }
159     
160   }
161   public static void main(String[] args)
162   {
163     Thread runner = new Thread(getDiscoverer());
164     runner.start();
165     while (runner.isAlive())
166     {
167       try
168       {
169         Thread.sleep(50);
170       } catch (InterruptedException e)
171       {
172       }
173       ;
174     }
175   }
176   private static Jws2Discoverer discoverer;
177   public static Jws2Discoverer getDiscoverer()
178   {
179     if (discoverer==null)
180     {
181       discoverer = new Jws2Discoverer();
182     }
183     return discoverer;
184   }
185
186   public boolean hasServices()
187   {
188     // TODO Auto-generated method stub
189     return services!=null && services.size()>0;
190   }
191   
192 }