1048fb8ba92b3a859d4b3caaaa09e53e794015f8
[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.Jronn;\r
37 import compbio.runner.msa.ClustalW;\r
38 import compbio.runner.msa.Mafft;\r
39 import compbio.runner.msa.Muscle;\r
40 import compbio.runner.msa.Probcons;\r
41 import compbio.runner.msa.Tcoffee;\r
42 \r
43 /**\r
44  * List of web services currently supported by JABAWS version 2\r
45  * \r
46  */\r
47 public enum Services {\r
48         MafftWS, MuscleWS, ClustalWS, TcoffeeWS, ProbconsWS, AAConWS, JronnWS, DisemblWS, GlobPlotWS, IUPred;\r
49 \r
50         public static Services getService(String servName) {\r
51                 servName = servName.trim().toLowerCase();\r
52                 for (Services service : Services.values()) {\r
53                         if (service.toString().equalsIgnoreCase(servName)) {\r
54                                 return service;\r
55                         }\r
56                 }\r
57                 return null;\r
58         }\r
59 \r
60         public static Services getServiceByRunner(\r
61                         Class<Executable<?>> runnerClassName) {\r
62                 assert runnerClassName != null;\r
63                 String sname = runnerClassName.getSimpleName().toLowerCase();\r
64                 for (Services service : Services.values()) {\r
65                         if (service.toString().toLowerCase().contains(sname)) {\r
66                                 return service;\r
67                         }\r
68                 }\r
69                 return null;\r
70         }\r
71 \r
72         public Class<? extends Executable<?>> getServiceImpl() {\r
73                 switch (this) {\r
74                         case AAConWS :\r
75                                 return AACon.class;\r
76                         case ClustalWS :\r
77                                 return ClustalW.class;\r
78                         case MafftWS :\r
79                                 return Mafft.class;\r
80                         case MuscleWS :\r
81                                 return Muscle.class;\r
82                         case TcoffeeWS :\r
83                                 return Tcoffee.class;\r
84                         case ProbconsWS :\r
85                                 return Probcons.class;\r
86                         case DisemblWS :\r
87                                 return Disembl.class;\r
88                         case GlobPlotWS :\r
89                                 return GlobPlot.class;\r
90                         case JronnWS :\r
91                                 return Jronn.class;\r
92                         default :\r
93                                 throw new RuntimeException(\r
94                                                 "Unknown web service implementation class for service: "\r
95                                                                 + this);\r
96                 }\r
97         }\r
98 \r
99         public static Class<? extends Executable<?>> getRunnerByJobDirectory(\r
100                         File jobdir) {\r
101                 Services service = getServiceByRunnerName(getRunnerNameByJobDirectory(jobdir));\r
102                 return service.getServiceImpl();\r
103         }\r
104 \r
105         private static String getRunnerNameByJobDirectory(File jobdir) {\r
106                 String name = jobdir.getName().split("#")[0];\r
107 \r
108                 if (name.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX)) {\r
109                         assert ConfExecutable.CLUSTER_TASK_ID_PREFIX.length() == 1;\r
110                         name = name.substring(1);\r
111                 }\r
112                 return name;\r
113         }\r
114 \r
115         public static Services getServiceByJobDirectory(File jobdir) {\r
116                 return getServiceByRunnerName(getRunnerNameByJobDirectory(jobdir));\r
117         }\r
118 \r
119         private static Services getServiceByRunnerName(String name) {\r
120                 for (Services service : Services.values()) {\r
121                         String runnerName = service.getServiceImpl().getSimpleName()\r
122                                         .toLowerCase();\r
123                         name = name.trim().toLowerCase();\r
124                         if (name.startsWith(runnerName)) {\r
125                                 return service;\r
126                         }\r
127                 }\r
128                 return null;\r
129         }\r
130 \r
131         Service getService(URL url, String sqname) {\r
132                 QName qname = new QName(sqname, this.toString());\r
133                 return Service.create(url, qname);\r
134         }\r
135 \r
136         public static String toString(Set<Services> services) {\r
137                 if (services == null || services.isEmpty()) {\r
138                         return "";\r
139                 }\r
140                 String value = "";\r
141                 String delim = ", ";\r
142                 for (Services serv : services) {\r
143                         value += serv.toString() + delim;\r
144                 }\r
145                 value = value.substring(0, value.length() - delim.length());\r
146                 return value;\r
147         }\r
148 \r
149         Class<? extends JABAService> getServiceType() {\r
150                 switch (this) {\r
151                         // deliberate leaking\r
152                         case AAConWS :\r
153                         case JronnWS :\r
154                         case DisemblWS :\r
155                         case GlobPlotWS :\r
156 \r
157                                 return SequenceAnnotation.class;\r
158 \r
159                                 // deliberate leaking\r
160                         case ClustalWS :\r
161                         case MafftWS :\r
162                         case MuscleWS :\r
163                         case ProbconsWS :\r
164                         case TcoffeeWS :\r
165 \r
166                                 return MsaWS.class;\r
167                         default :\r
168                                 throw new RuntimeException("Unrecognised Web Service Type "\r
169                                                 + this + " - Should never happened!");\r
170                 }\r
171         }\r
172 \r
173         JABAService getInterface(Service service) {\r
174                 assert service != null;\r
175 \r
176                 QName portName = new QName(service.getServiceName().getNamespaceURI(),\r
177                                 this.toString() + "Port");\r
178                 return service.getPort(portName, this.getServiceType());\r
179         }\r
180 }