a7b996911ba0b5304efe043f409a57d69ecfa087
[jabaws.git] / webservices / compbio / ws / client / Services.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.ws.client;\r
20 \r
21 import java.io.File;\r
22 import java.net.URL;\r
23 import java.util.Set;\r
24 \r
25 import javax.xml.namespace.QName;\r
26 import javax.xml.ws.Service;\r
27 \r
28 import compbio.data.msa.JABAService;\r
29 import compbio.data.msa.MsaWS;\r
30 import compbio.data.msa.SequenceAnnotation;\r
31 import compbio.engine.client.ConfExecutable;\r
32 import compbio.engine.client.Executable;\r
33 import compbio.runner.conservation.AACon;\r
34 import compbio.runner.disorder.Disembl;\r
35 import compbio.runner.disorder.GlobPlot;\r
36 import compbio.runner.disorder.IUPred;\r
37 import compbio.runner.disorder.Jronn;\r
38 import compbio.runner.msa.ClustalW;\r
39 import compbio.runner.msa.Mafft;\r
40 import compbio.runner.msa.Muscle;\r
41 import compbio.runner.msa.Probcons;\r
42 import compbio.runner.msa.Tcoffee;\r
43 \r
44 /**\r
45  * List of web services currently supported by JABAWS version 2\r
46  * \r
47  */\r
48 public enum Services {\r
49         MafftWS, MuscleWS, ClustalWS, TcoffeeWS, ProbconsWS, AAConWS, JronnWS, DisemblWS, GlobPlotWS, IUPredWS;\r
50 \r
51         public static Services getService(String servName) {\r
52                 servName = servName.trim().toLowerCase();\r
53                 for (Services service : Services.values()) {\r
54                         if (service.toString().equalsIgnoreCase(servName)) {\r
55                                 return service;\r
56                         }\r
57                 }\r
58                 return null;\r
59         }\r
60 \r
61         public static Services getServiceByRunner(\r
62                         Class<Executable<?>> runnerClassName) {\r
63                 assert runnerClassName != null;\r
64                 String sname = runnerClassName.getSimpleName().toLowerCase();\r
65                 for (Services service : Services.values()) {\r
66                         if (service.toString().toLowerCase().contains(sname)) {\r
67                                 return service;\r
68                         }\r
69                 }\r
70                 return null;\r
71         }\r
72 \r
73         public Class<? extends Executable<?>> getServiceImpl() {\r
74                 switch (this) {\r
75                         case AAConWS :\r
76                                 return AACon.class;\r
77                         case ClustalWS :\r
78                                 return ClustalW.class;\r
79                         case MafftWS :\r
80                                 return Mafft.class;\r
81                         case MuscleWS :\r
82                                 return Muscle.class;\r
83                         case TcoffeeWS :\r
84                                 return Tcoffee.class;\r
85                         case ProbconsWS :\r
86                                 return Probcons.class;\r
87                         case DisemblWS :\r
88                                 return Disembl.class;\r
89                         case GlobPlotWS :\r
90                                 return GlobPlot.class;\r
91                         case JronnWS :\r
92                                 return Jronn.class;\r
93                         case IUPredWS :\r
94                                 return IUPred.class;\r
95                         default :\r
96                                 throw new RuntimeException(\r
97                                                 "Unknown web service implementation class for service: "\r
98                                                                 + this);\r
99                 }\r
100         }\r
101 \r
102         public static Class<? extends Executable<?>> getRunnerByJobDirectory(\r
103                         File jobdir) {\r
104                 Services service = getServiceByRunnerName(getRunnerNameByJobDirectory(jobdir));\r
105                 return service.getServiceImpl();\r
106         }\r
107 \r
108         private static String getRunnerNameByJobDirectory(File jobdir) {\r
109                 String name = jobdir.getName().split("#")[0];\r
110 \r
111                 if (name.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX)) {\r
112                         assert ConfExecutable.CLUSTER_TASK_ID_PREFIX.length() == 1;\r
113                         name = name.substring(1);\r
114                 }\r
115                 return name;\r
116         }\r
117 \r
118         public static Services getServiceByJobDirectory(File jobdir) {\r
119                 return getServiceByRunnerName(getRunnerNameByJobDirectory(jobdir));\r
120         }\r
121 \r
122         private static Services getServiceByRunnerName(String name) {\r
123                 for (Services service : Services.values()) {\r
124                         String runnerName = service.getServiceImpl().getSimpleName()\r
125                                         .toLowerCase();\r
126                         name = name.trim().toLowerCase();\r
127                         if (name.startsWith(runnerName)) {\r
128                                 return service;\r
129                         }\r
130                 }\r
131                 return null;\r
132         }\r
133 \r
134         Service getService(URL url, String sqname) {\r
135                 QName qname = new QName(sqname, this.toString());\r
136                 return Service.create(url, qname);\r
137         }\r
138 \r
139         public static String toString(Set<Services> services) {\r
140                 if (services == null || services.isEmpty()) {\r
141                         return "";\r
142                 }\r
143                 String value = "";\r
144                 String delim = ", ";\r
145                 for (Services serv : services) {\r
146                         value += serv.toString() + delim;\r
147                 }\r
148                 value = value.substring(0, value.length() - delim.length());\r
149                 return value;\r
150         }\r
151 \r
152         Class<? extends JABAService> getServiceType() {\r
153                 switch (this) {\r
154                         // deliberate leaking\r
155                         case AAConWS :\r
156                         case JronnWS :\r
157                         case DisemblWS :\r
158                         case GlobPlotWS :\r
159                         case IUPredWS :\r
160                                 return SequenceAnnotation.class;\r
161 \r
162                                 // deliberate leaking\r
163                         case ClustalWS :\r
164                         case MafftWS :\r
165                         case MuscleWS :\r
166                         case ProbconsWS :\r
167                         case TcoffeeWS :\r
168 \r
169                                 return MsaWS.class;\r
170                         default :\r
171                                 throw new RuntimeException("Unrecognised Web Service Type "\r
172                                                 + this + " - Should never happened!");\r
173                 }\r
174         }\r
175 \r
176         JABAService getInterface(Service service) {\r
177                 assert service != null;\r
178 \r
179                 QName portName = new QName(service.getServiceName().getNamespaceURI(),\r
180                                 this.toString() + "Port");\r
181                 return service.getPort(portName, this.getServiceType());\r
182         }\r
183 }