JAL-1645 source formatting and organise imports
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraListener.java
1 package jalview.ext.rbvi.chimera;
2
3 import jalview.httpserver.AbstractRequestHandler;
4 import jalview.structure.SelectionSource;
5
6 import java.net.BindException;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 /**
12  * This is a simple Http handler that can listen for selections in Chimera.
13  * <p/>
14  * Lifecycle:
15  * <ul>
16  * <li>Start the Chimera process</li>
17  * <li>Start the REST service on Chimera, get the port number it is listening on
18  * </li>
19  * <li>Start the ChimeraListener, get the URL it is listening on</li>
20  * <li>The first listener started will start the singleton HttpServer</li>
21  * <li>Send a 'listen' command to Chimera with the URL of the listener</li>
22  * <li>When Jalview's Chimera window is closed, shut down the ChimeraListener</li>
23  * <li>Multiple linked Chimera instances will each have a separate listener (but
24  * share one Http server)</li>
25  * </ul>
26  * 
27  * @author gmcarstairs
28  *
29  */
30 public class ChimeraListener extends AbstractRequestHandler implements
31         SelectionSource
32 {
33   /*
34    * Chimera notification parameter name
35    */
36   private static final String CHIMERA_NOTIFICATION = "chimeraNotification";
37
38   /*
39    * Chimera model changed notifications start with this
40    */
41   private static final String MODEL_CHANGED = "ModelChanged: ";
42
43   /*
44    * Chimera selection changed notification message
45    */
46   private static final String SELECTION_CHANGED = "SelectionChanged: selection changed\n";
47
48   /*
49    * A static counter so each listener can be associated with a distinct context
50    * root (/chimera0,1,2,3...). This is needed so we can fetch selections from
51    * multiple Chimera instances without confusion.
52    */
53   private static int chimeraId = 0;
54
55   /*
56    * Prefix for path below context root (myChimeraId is appended)
57    */
58   private static final String PATH_PREFIX = "chimera";
59
60   /*
61    * Value of chimeraId (0, 1, 2...) for this instance
62    */
63   private int myChimeraId = 0;
64
65   /*
66    * A reference to the object by which we can talk to Chimera
67    */
68   private JalviewChimeraBinding chimeraBinding;
69
70   /**
71    * Constructor that registers this as an Http request handler on path
72    * /chimeraN, where N is incremented for each instance. Call getUri to get the
73    * resulting URI for this handler.
74    * 
75    * @param chimeraBinding
76    * @throws BindException
77    *           if no free port can be assigned
78    */
79   public ChimeraListener(JalviewChimeraBinding binding)
80           throws BindException
81   {
82     myChimeraId = chimeraId++;
83     this.chimeraBinding = binding;
84     setPath(PATH_PREFIX + myChimeraId);
85     registerHandler();
86   }
87
88   /**
89    * Process a message from Chimera
90    */
91   @Override
92   protected void processRequest(HttpServletRequest request,
93           HttpServletResponse response)
94   {
95     // dumpRequest(request);
96     String message = request.getParameter(CHIMERA_NOTIFICATION);
97     if (SELECTION_CHANGED.equals(message))
98     {
99       this.chimeraBinding.highlightChimeraSelection();
100     }
101     else if (message != null && message.startsWith(MODEL_CHANGED))
102     {
103       processModelChanged(message.substring(MODEL_CHANGED.length()));
104     }
105     else
106     {
107       System.err.println("Unexpected chimeraNotification: " + message);
108     }
109   }
110
111   /**
112    * Handler a ModelChanged notification from Chimera
113    * 
114    * @param substring
115    */
116   protected void processModelChanged(String message)
117   {
118     // System.out.println(message + " (not implemented in Jalview)");
119   }
120
121   /**
122    * Returns a display name for this service
123    */
124   @Override
125   public String getName()
126   {
127     return "ChimeraListener";
128   }
129 }