Code for cleaning up old job directories
[jabaws.git] / webservices / compbio / ws / client / CmdHelper.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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 package compbio.ws.client;\r
19 import static compbio.ws.client.Constraints.hostkey;\r
20 import static compbio.ws.client.Constraints.limitList;\r
21 import static compbio.ws.client.Constraints.listServices;\r
22 import static compbio.ws.client.Constraints.paramList;\r
23 import static compbio.ws.client.Constraints.presetList;\r
24 import static compbio.ws.client.Constraints.presetkey;\r
25 import static compbio.ws.client.Constraints.pseparator;\r
26 import static compbio.ws.client.Constraints.servicekey;\r
27 import static compbio.ws.client.Constraints.testKey;\r
28 \r
29 class CmdHelper {\r
30         /**\r
31          * Check whether presetList is set in the command line\r
32          * \r
33          * @param cmd\r
34          *            command line options\r
35          * @return true if presetList is found, false otherwise\r
36          */\r
37         static boolean listPresets(String[] cmd) {\r
38                 return keyFound(cmd, presetList);\r
39         }\r
40 \r
41         /**\r
42          * Checks whether limitList parameter is in the command line\r
43          * \r
44          * @param cmd\r
45          *            - command line options\r
46          * @return true if it is, false otherwise\r
47          */\r
48         static boolean listLimits(String[] cmd) {\r
49                 return keyFound(cmd, limitList);\r
50         }\r
51 \r
52         /**\r
53          * list available services\r
54          * \r
55          * @param cmd\r
56          * @return\r
57          */\r
58         static boolean listServices(String[] cmd) {\r
59                 return keyFound(cmd, listServices);\r
60         }\r
61 \r
62         /**\r
63          * tests service\r
64          * \r
65          * @param cmd\r
66          * @return\r
67          */\r
68         static boolean testService(String[] cmd) {\r
69                 return keyFound(cmd, testKey);\r
70         }\r
71 \r
72         /**\r
73          * Checks whether the key is in the command line\r
74          * \r
75          * @param cmd\r
76          * @param key\r
77          * @return true if it is, false otherwise\r
78          */\r
79         static boolean keyFound(String[] cmd, String key) {\r
80                 assert cmd != null && cmd.length > 0;\r
81                 assert key != null;\r
82 \r
83                 for (String c : cmd) {\r
84                         if (c.trim().equalsIgnoreCase(key)) {\r
85                                 return true;\r
86                         }\r
87                 }\r
88                 return false;\r
89         }\r
90 \r
91         /**\r
92          * Extracts preset name from the command line is any\r
93          * \r
94          * @param cmd\r
95          *            command line options\r
96          * @return presetName or null if no presets is defined\r
97          */\r
98         static String getPresetName(String[] cmd) {\r
99                 String preset = null;\r
100                 for (String c : cmd) {\r
101                         if (c.trim().toLowerCase().startsWith(presetkey + pseparator)) {\r
102                                 preset = c.substring(c.indexOf(pseparator) + 1);\r
103                                 break;\r
104                         }\r
105                 }\r
106                 return preset;\r
107         }\r
108 \r
109         /**\r
110          * Extracts service name from the command line\r
111          * \r
112          * @param cmd\r
113          *            command line options\r
114          * @return service name or null if it is not defined\r
115          */\r
116         public static String getServiceName(String[] cmd) {\r
117                 for (String c : cmd) {\r
118                         if (c.trim().toLowerCase().startsWith(servicekey + pseparator)) {\r
119                                 return c.substring(c.indexOf(pseparator) + 1);\r
120                         }\r
121                 }\r
122                 return null;\r
123         }\r
124 \r
125         /**\r
126          * Extracts host name from the command line\r
127          * \r
128          * @param cmd\r
129          *            command line options\r
130          * @return host name or null if it is not defined\r
131          */\r
132         public static String getHost(String[] cmd) {\r
133                 for (String c : cmd) {\r
134                         if (c.trim().toLowerCase().startsWith(hostkey + pseparator)) {\r
135                                 return c.substring(c.indexOf(pseparator) + 1);\r
136                         }\r
137                 }\r
138                 return null;\r
139         }\r
140 \r
141         /**\r
142          * Searches the command line keys in the array of parameters\r
143          * \r
144          * @param cmd\r
145          *            command line options\r
146          * @return true is the list of Parameters is requested, false otherwise\r
147          */\r
148         static boolean listParameters(String[] cmd) {\r
149                 return keyFound(cmd, paramList);\r
150         }\r
151 }\r