| 1 | .. _services-create: |
|---|
| 2 | |
|---|
| 3 | Create your own ZOO-Services |
|---|
| 4 | ========================= |
|---|
| 5 | |
|---|
| 6 | :ref:`services_index` are quite easy to create once you have installed the ZOO Kernel and have |
|---|
| 7 | chosen code (in the language of your choice) to turn into a ZOO service. Here are some |
|---|
| 8 | HelloWorlds in Python, PHP, Java and JavaScript with links to their corresponding |
|---|
| 9 | ``.zcfg`` files. |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | General information |
|---|
| 13 | ---------------------- |
|---|
| 14 | |
|---|
| 15 | The function of the process for each programming language take three arguments: the main |
|---|
| 16 | configuration, inputs and outputs. |
|---|
| 17 | |
|---|
| 18 | .. note:: The service must return **3** if the process run successfully |
|---|
| 19 | |
|---|
| 20 | .. note:: The service must return **4** if the process ended with an error |
|---|
| 21 | |
|---|
| 22 | Python |
|---|
| 23 | ------ |
|---|
| 24 | |
|---|
| 25 | You'll find here information needed to deploy your own Python Services Provider. |
|---|
| 26 | |
|---|
| 27 | Python ZCFG requirements |
|---|
| 28 | ************************ |
|---|
| 29 | |
|---|
| 30 | .. Note:: For each Service provided by your ZOO Python Services Provider, the ZCFG File |
|---|
| 31 | must be named the same as the Python module function name (also the case of |
|---|
| 32 | characters is important). |
|---|
| 33 | |
|---|
| 34 | The ZCFG file should contain the following : |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | serviceType |
|---|
| 38 | Python |
|---|
| 39 | serviceProvider |
|---|
| 40 | The name of the Python module to use as a ZOO Service Provider. For instance, if your |
|---|
| 41 | script, located in the same directory as your ZOO Kernel, was named ``my_module.py`` then |
|---|
| 42 | you should use ``my_module`` (the Python module name) for the serviceProvider value in ZCFG file. |
|---|
| 43 | |
|---|
| 44 | Python Data Structure used |
|---|
| 45 | ************************** |
|---|
| 46 | The three parameters of the function are passed to the Python module as dictionaries. |
|---|
| 47 | |
|---|
| 48 | Following you'll find an example for each parameters |
|---|
| 49 | |
|---|
| 50 | Main configuration |
|---|
| 51 | ^^^^^^^^^^^^^^^^^^^^^ |
|---|
| 52 | Main configuration contains several informations, some of them are really useful to develop your service. |
|---|
| 53 | Following an example :: |
|---|
| 54 | |
|---|
| 55 | { |
|---|
| 56 | 'main': {'lang': 'en-UK', |
|---|
| 57 | 'language': 'en-US', |
|---|
| 58 | 'encoding': 'utf-8', |
|---|
| 59 | 'dataPath': '/var/www/tmp', |
|---|
| 60 | 'tmpPath': '/var/www/tmp', |
|---|
| 61 | 'version': '1.0.0', |
|---|
| 62 | 'mapserverAddress': 'http://localhost/cgi-bin/mapserv', |
|---|
| 63 | 'isSoap': 'false', |
|---|
| 64 | 'tmpUrl': 'http://localhost/tmp/', |
|---|
| 65 | 'serverAddress': 'http://localhost/zoo' |
|---|
| 66 | }, |
|---|
| 67 | 'identification': {'keywords': 'WPS,GIS', |
|---|
| 68 | 'abstract': 'WPS services for testing ZOO', |
|---|
| 69 | 'fees': 'None', |
|---|
| 70 | 'accessConstraints': 'none', |
|---|
| 71 | 'title': 'testing services' |
|---|
| 72 | }, |
|---|
| 73 | 'lenv': {'status': '0', |
|---|
| 74 | 'soap': 'false', |
|---|
| 75 | 'cwd': '/usr/lib/cgi-bin', |
|---|
| 76 | 'sid': '24709' |
|---|
| 77 | }, |
|---|
| 78 | 'env': {'DISPLAY': 'localhost:0'}, |
|---|
| 79 | 'provider': {'addressCountry': 'it', |
|---|
| 80 | 'positionName': 'Developer', |
|---|
| 81 | 'providerName': 'Name of provider', |
|---|
| 82 | 'addressAdministrativeArea': 'False', |
|---|
| 83 | 'phoneVoice': 'False', |
|---|
| 84 | 'addressCity': 'City', |
|---|
| 85 | 'providerSite': 'http://www.your.site', |
|---|
| 86 | 'addressPostalCode': '38122', |
|---|
| 87 | 'role': 'Developer', |
|---|
| 88 | 'addressDeliveryPoint': 'False', |
|---|
| 89 | 'phoneFacsimile': 'False', |
|---|
| 90 | 'addressElectronicMailAddress': 'your@email.com', |
|---|
| 91 | 'individualName': 'Your Name' |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | Inputs |
|---|
| 96 | ^^^^^^^^^^^^ |
|---|
| 97 | The inputs are somethings like this :: |
|---|
| 98 | |
|---|
| 99 | { |
|---|
| 100 | 'variable_name': {'minOccurs': '1', |
|---|
| 101 | 'DataType': 'string', |
|---|
| 102 | 'value': 'this_is_the_value', |
|---|
| 103 | 'maxOccurs': '1', |
|---|
| 104 | 'inRequest': 'true' |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | The access to the value you have to require for the ``value`` parameter, something like this :: |
|---|
| 109 | |
|---|
| 110 | yourVariable = inputs['variable_name']['value'] |
|---|
| 111 | |
|---|
| 112 | Outputs |
|---|
| 113 | ^^^^^^^^^^^^^ |
|---|
| 114 | The outputs data as a structure really similar to the inputs one :: |
|---|
| 115 | |
|---|
| 116 | { |
|---|
| 117 | 'result': {'DataType': 'string', |
|---|
| 118 | 'inRequest': 'true', |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | There is no ``'value'`` parameter before you assign it :: |
|---|
| 123 | |
|---|
| 124 | inputs['result']['value'] = yourOutputDataVariable |
|---|
| 125 | |
|---|
| 126 | The return statement has to be an integer: corresponding to the service status code. |
|---|
| 127 | |
|---|
| 128 | To add a message for the wrong result you can add the massage to ``conf["lenv"]["message"]``, |
|---|
| 129 | for example: |
|---|
| 130 | |
|---|
| 131 | .. code-block:: python |
|---|
| 132 | |
|---|
| 133 | conf["lenv"]["message"] = 'Your module return an error' |
|---|
| 134 | |
|---|
| 135 | Sample ZOO Python Services Provider |
|---|
| 136 | *********************************** |
|---|
| 137 | |
|---|
| 138 | The following code represents a simple ZOO Python Services Provider which provides only one |
|---|
| 139 | Service, the HelloPy one. |
|---|
| 140 | |
|---|
| 141 | .. code-block:: python |
|---|
| 142 | |
|---|
| 143 | import zoo |
|---|
| 144 | import sys |
|---|
| 145 | def HelloPy(conf,inputs,outputs): |
|---|
| 146 | outputs["Result"]["value"]="Hello "+inputs["a"]["value"]+" from Python World !" |
|---|
| 147 | return zoo.SERVICE_SUCCEEDED |
|---|
| 148 | |
|---|
| 149 | PHP |
|---|
| 150 | --- |
|---|
| 151 | |
|---|
| 152 | ZOO-API |
|---|
| 153 | ******* |
|---|
| 154 | |
|---|
| 155 | The ZOO-API for the PHP language is automatically available from your |
|---|
| 156 | service code. Tthe following functions are defined in the ZOO-API: |
|---|
| 157 | |
|---|
| 158 | int zoo_SERVICE_SUCCEEDED() |
|---|
| 159 | return the value of SERVICE_SUCCEEDED |
|---|
| 160 | int zoo_SERVICE_FAILED() |
|---|
| 161 | return the value of SERVICE_FAILED |
|---|
| 162 | string zoo_Translate(string a) |
|---|
| 163 | return the translated string (using the "zoo-service" `textdomain |
|---|
| 164 | <http://www.gnu.org/software/libc/manual/html_node/Locating-gettext-catalog.html#index-textdomain>`__) |
|---|
| 165 | |
|---|
| 166 | void zoo_UpdateStatus(Array conf,string message,int pourcent) |
|---|
| 167 | update the status of the running service |
|---|
| 168 | |
|---|
| 169 | PHP ZCFG requirements |
|---|
| 170 | ********************************** |
|---|
| 171 | |
|---|
| 172 | The ZCFG file should contain the following : |
|---|
| 173 | |
|---|
| 174 | serviceType |
|---|
| 175 | PHP |
|---|
| 176 | serviceProvider |
|---|
| 177 | The name of the php script (ie. service.php) to use as a ZOO Service Provider. |
|---|
| 178 | |
|---|
| 179 | PHP Data Structure used |
|---|
| 180 | ******************************** |
|---|
| 181 | |
|---|
| 182 | The three parameters are passed to the PHP function as |
|---|
| 183 | `Arrays <php.net/manual/language.types.array.php>`__. |
|---|
| 184 | |
|---|
| 185 | Sample ZOO PHP Services Provider |
|---|
| 186 | ****************************************** |
|---|
| 187 | |
|---|
| 188 | .. code-block:: php |
|---|
| 189 | |
|---|
| 190 | <? |
|---|
| 191 | function HelloPHP(&$main_conf,&$inputs,&$outputs){ |
|---|
| 192 | $tmp="Hello ".$inputs[S][value]." from PHP world !"; |
|---|
| 193 | $outputs["Result"]["value"]=zoo_Translate($tmp); |
|---|
| 194 | return zoo_SERVICE_SUCCEEDED(); |
|---|
| 195 | } |
|---|
| 196 | ?> |
|---|
| 197 | |
|---|
| 198 | Java |
|---|
| 199 | ---- |
|---|
| 200 | |
|---|
| 201 | Specifically for the Java support, you may add the following two |
|---|
| 202 | sections to your ``main.cfg`` file: |
|---|
| 203 | |
|---|
| 204 | :[javaxx]: |
|---|
| 205 | This section is used to pass -XX:* parameters to the JVM created by the |
|---|
| 206 | ZOO-Kernel to handle your ZOO-Service (see `ref. 1 |
|---|
| 207 | <http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#BehavioralOptions>`__ |
|---|
| 208 | or `ref. 2 |
|---|
| 209 | <http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#PerformanceTuning>`__ |
|---|
| 210 | for sample available). |
|---|
| 211 | For each map ``a = b`` available in the ``[javaxx]`` section, the |
|---|
| 212 | option ``-XX:a=b`` will be passed to the JVM. In case of a map ``a = |
|---|
| 213 | minus`` (respectively ``a=plus``) then the option ``-XX:-a`` |
|---|
| 214 | (respectivelly ``-XX:+a``) will be passed. |
|---|
| 215 | :[javax]: |
|---|
| 216 | The section is used to pass -X* options to the JVM (see |
|---|
| 217 | `ref. <http://docs.oracle.com/cd/E22289_01/html/821-1274/configuring-the-default-jvm-and-java-arguments.html>`__). For |
|---|
| 218 | each map ``a = b`` available in the ``[javax]`` section, the option |
|---|
| 219 | ``-Xab`` will be passed to the JVM (ie. set ``mx=2G`` to pass |
|---|
| 220 | ``-Xmx2G``). |
|---|
| 221 | |
|---|
| 222 | ZOO-API |
|---|
| 223 | ******* |
|---|
| 224 | |
|---|
| 225 | Before you build your first ZOO-Service implemented in Java, it is |
|---|
| 226 | recommended that you first build the ZOO class of the Java ZOO-API. |
|---|
| 227 | |
|---|
| 228 | .. Note:: You should build ZOO-Kernel prior to follow this instructions. |
|---|
| 229 | |
|---|
| 230 | To build the ZOO.class of the ZOO-API for Java, use the following |
|---|
| 231 | command: |
|---|
| 232 | |
|---|
| 233 | .. code-block:: guess |
|---|
| 234 | |
|---|
| 235 | cd zoo-api/java |
|---|
| 236 | make |
|---|
| 237 | cp ZOO.class libZOO.so /usr/lib/cgi-bin |
|---|
| 238 | |
|---|
| 239 | .. Note:: running the previous commands will require that both |
|---|
| 240 | ``javac`` and ``javah`` are in your PATH. |
|---|
| 241 | |
|---|
| 242 | Java ZCFG requirements |
|---|
| 243 | ********************************** |
|---|
| 244 | |
|---|
| 245 | .. Note:: For each Service provided by your ZOO Java Services Provider |
|---|
| 246 | (your corresponding Java class), the ZCFG File should have |
|---|
| 247 | the name of the Java public method corresponding to the |
|---|
| 248 | service (case-sensitive). |
|---|
| 249 | |
|---|
| 250 | The ZCFG file should contain the following : |
|---|
| 251 | |
|---|
| 252 | serviceType |
|---|
| 253 | Java |
|---|
| 254 | serviceProvider |
|---|
| 255 | The name of the Java class to use as a ZOO Service Provider. For instance, if your |
|---|
| 256 | java class, located in the same directory as your ZOO-Kernel, was |
|---|
| 257 | named ``HelloJava.class`` then you should use ``HelloJava``. |
|---|
| 258 | |
|---|
| 259 | Java Data Structure used |
|---|
| 260 | ******************************** |
|---|
| 261 | |
|---|
| 262 | The three parameters are passed to the Java function as |
|---|
| 263 | `java.util.HashMap <http://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html>`__. |
|---|
| 264 | |
|---|
| 265 | Sample ZOO Java Services Provider |
|---|
| 266 | ****************************************** |
|---|
| 267 | |
|---|
| 268 | .. code-block:: java |
|---|
| 269 | |
|---|
| 270 | import java.util.*; |
|---|
| 271 | public class HelloJava { |
|---|
| 272 | public static int HelloWorldJava(HashMap conf,HashMap inputs, HashMap outputs) { |
|---|
| 273 | HashMap hm1 = new HashMap(); |
|---|
| 274 | hm1.put("dataType","string"); |
|---|
| 275 | HashMap tmp=(HashMap)(inputs.get("S")); |
|---|
| 276 | java.lang.String v=tmp.get("value").toString(); |
|---|
| 277 | hm1.put("value","Hello "+v+" from JAVA WOrld !"); |
|---|
| 278 | outputs.put("Result",hm1); |
|---|
| 279 | System.err.println("Hello from JAVA WOrld !"); |
|---|
| 280 | return ZOO.SERVICE_SUCCEEDED; |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | Javascript |
|---|
| 285 | ---------- |
|---|
| 286 | |
|---|
| 287 | ZOO API |
|---|
| 288 | ********* |
|---|
| 289 | |
|---|
| 290 | If you need to use :ref:`ZOO API <api>` in your service, you have first to copy ``zoo-api.js`` |
|---|
| 291 | and ``zoo-proj4js.js`` where your services are located (for example in Unix system probably in |
|---|
| 292 | ``/usr/lib/cgi-bin/`` |
|---|
| 293 | |
|---|
| 294 | Javascript ZCFG requirements |
|---|
| 295 | ********************************** |
|---|
| 296 | |
|---|
| 297 | .. Note:: For each Service provided by your ZOO Javascript Services Provider, the ZCFG File |
|---|
| 298 | must be named the same as the Javascript function name (also the case of |
|---|
| 299 | characters is important). |
|---|
| 300 | |
|---|
| 301 | The ZCFG file should contain the following : |
|---|
| 302 | |
|---|
| 303 | serviceType |
|---|
| 304 | JS |
|---|
| 305 | serviceProvider |
|---|
| 306 | The name of the JavaScript file to use as a ZOO Service Provider. For instance, if your |
|---|
| 307 | script, located in the same directory as your ZOO Kernel, was named ``my_module.js`` then |
|---|
| 308 | you should use ``my_module.js``. |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | Javascript Data Structure used |
|---|
| 312 | ******************************** |
|---|
| 313 | |
|---|
| 314 | The three parameters of the function are passed to the JavaScript function as Object. |
|---|
| 315 | |
|---|
| 316 | Sample ZOO Javascript Services Provider |
|---|
| 317 | ****************************************** |
|---|
| 318 | |
|---|
| 319 | .. code-block:: javascript |
|---|
| 320 | |
|---|
| 321 | function hellojs(conf,inputs,outputs){ |
|---|
| 322 | outputs=new Array(); |
|---|
| 323 | outputs={}; |
|---|
| 324 | outputs["result"]["value"]="Hello "+inputs["S"]["value"]+" from JS World !"; |
|---|
| 325 | return Array(3,outputs); |
|---|
| 326 | } |
|---|
| 327 | |
|---|