Strabon

view scripts/strabon @ 242:6174ff9d242a

print reading configuration file only in debug mode
author Babis Nikolaou <charnik@di.uoa.gr>
date Sun Jun 03 18:00:58 2012 +0300 (2012-06-03)
parents 6e556c0b0b77
children eaef3863cd22
line source
1 #!/bin/bash
2 #
3 # Script for running the main classes of Strabon. The main classes of Strabon comprises
4 # QueryOp, , UpdateOp, and StoreOp.
5 #
6 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
7 #
9 # command name
10 CMD="$(basename ${0})"
12 # absolute directory name of this command
13 LOC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15 RUNTIME="${LOC}/../runtime"
17 # runtime package
18 PKG="eu.earthobservatory.runtime"
20 # the underlying database to use (one of `postgis' or `monetdb')
21 DATABASE="postgis"
23 # the main class to run
24 CLASS=
26 # the hostname at which the database runs
27 HOST="localhost"
29 # the port at which the database listens
30 PORT=
32 # the database name to connect to
33 DB="strabon"
35 # the username for the database connection
36 DBUSER=
38 # the password for the database connection
39 DBPASS=
41 # the query to run
42 QUERY=
44 # the RDF format of the files to store (defaults to ntriples)
45 FORMAT="ntriples"
47 # predefined queries
48 QUERY_SIZE="SELECT (COUNT(*) as ?C) WHERE {?s ?p ?o}"
49 QUERY_DELETEALL="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
51 # debug option for log4j configuration:
52 #-Dlog4j.debug
53 #-Dlog4j.configuration=\"${RUNTIME}/log4j.properties\"
55 # just print what shall be executed
56 DEBUG=false
58 # if true, DEBUG has been set in command line,
59 # so it must not be overidden
60 DEBUG_SAVE=false
62 # configuration file for the Strabon connection
63 STRABON_CONF="${HOME}/.strabon"
65 function help() {
66 echo "Usage: ${CMD} [OPTIONS] COMMAND ARGS"
67 echo
68 echo "Interface to execute the main classes of Strabon, such as QueryOp, StoreOp, UpdateOp, etc."
69 echo
70 echo " COMMAND : one of \`query', \`update', \`store', or \`help'"
71 echo " ARGS : arguments according to selected command"
72 echo
73 echo "OPTIONS can be any of the following (variable names and values are case sensitive)"
74 echo " -d : don't run, just print what shall be executed"
75 echo " Variable for configuration file: \`DEBUG'"
76 echo " Values: \`true' or \`false'"
77 echo " -e DATABASE : the database engine to connect (one of \`postgis' (default) or \`monetdb')"
78 echo " Variable for configuration file: \`DATABASE'"
79 echo " Values: \`postgis' or \`monetdb'"
80 echo " -db DB : the database to connect to (defaults to \`${DB}')"
81 echo " Variable for configuration file: \`DB'"
82 echo " -p PORT : the port to use for the database connection"
83 echo " : (defaults to 5432 for postgis and 50000 for monetdb)"
84 echo " Variable for configuration file: \`PORT'"
85 echo " -h HOSTNAME : the hostname to use for the database connection (defaults to \`${HOST}')"
86 echo " Variable for configuration file: \`HOST'"
87 echo " -u USERNAME : the username for the database connection"
88 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
89 echo " Variable for configuration file: \`DBUSER'"
90 echo " -pass PASS : the password for the database connection"
91 echo " (defaults to \`postgres' for postgis and \`monetdb' for monetdb)"
92 echo " Variable for configuration file: \`DBPASS'"
93 echo " -c FILE : configuration file to use for the connection (defaults to \`${STRABON_CONF}')."
94 echo " If the default configuration file exists, it is read. Options specified in the"
95 echo " command line override their values already set by the configuration file."
96 }
98 function help_query() {
99 echo "Usage: ${CMD} query SPARQL_QUERY"
100 echo
101 echo "Execute a SPARQL query on Strabon."
102 echo
103 echo " SPARQL_QUERY : the SPARQL query to execute or an alias name such as the following:"
104 echo " size: returns the number of triples"
105 }
107 function help_update() {
108 echo "Usage: ${CMD} update SPARQL_UPDATE"
109 echo
110 echo "Execute a SPARQL Update query on Strabon."
111 echo
112 echo " SPARQL_UPDATE : the SPARQL update query to execute or an alias name such as the"
113 echo " the following:"
114 echo " clear: deletes all triples"
115 }
117 function help_store() {
118 echo "Usage: ${CMD} store [OPTIONS] FILE..."
119 echo
120 echo "Store RDF documents in Strabon."
121 echo
122 echo " FILE : the file containing the RDF document to store. It can be a filename or a URL,"
123 echo " (i.e., file:///tmp/file.nt, http://www.example.org/file.nt,"
124 echo " ftp://www.example.org/file.nt, etc.)."
125 echo
126 echo "OPTIONS can be one of the following"
127 echo " -f FORMAT : the RDF format of the files to store. The format can be one of the following:"
128 echo " \`ntriples' (default), \`n3', \`rdfxml', or \`turtle'."
129 }
132 # read configuration script first
133 if test -e "${STRABON_CONF}"; then
134 . ${STRABON_CONF}
135 fi
137 if ${DEBUG}; then
138 echo "${CMD}: debug is ON (from configuration file ${STRABON_CONF})"
139 fi
141 # read script options
142 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
143 case "${1}" in
144 -d)
145 shift
146 if ! ${DEBUG}; then
147 echo "${CMD}: debug is ON"
148 fi
149 DEBUG=true
150 DEBUG_SAVE=true
151 ;;
152 -e)
153 shift
154 if ! test $# -gt 1; then
155 help
156 exit 1
157 fi
159 DATABASE=${1}
160 case "${DATABASE}" in
161 monetdb|postgis)
162 shift
163 ;;
164 postgis)
165 shift
166 ;;
167 *)
168 echo "${CMD}: invalid database engine"
169 echo "${CMD}: valid database engines are \`postgis' or \`monetdb'"
170 exit 2
171 ;;
172 esac
173 ;;
174 -db)
175 shift
176 if ! test $# -gt 1; then
177 help
178 exit 1
179 fi
180 DB="${1}"
181 shift
182 ;;
183 -p)
184 shift
185 if ! test $# -gt 1; then
186 help
187 exit 1
188 fi
189 PORT=${1}
190 shift
191 ;;
192 -h)
193 shift
194 if ! test $# -gt 1; then
195 help
196 exit 1
197 fi
198 HOST=${1}
199 shift
200 ;;
201 -u)
202 shift
203 if ! test $# -gt 1; then
204 help
205 exit 1
206 fi
207 DBUSER=${1}
208 shift
209 ;;
210 -pass)
211 shift
212 if ! test $# -gt 1; then
213 help
214 exit 1
215 fi
216 DBPASS=${1}
217 shift
218 ;;
219 -c)
220 shift
221 if ! test $# -gt 1; then
222 help
223 exit 1
224 fi
226 STRABON_CONF="${1}"
227 if ! test -e "${STRABON_CONF}"; then
228 echo "${CMD}: configuration file \"${1}\" does not exist"
229 exit 1
230 else
231 # if true, then debug has been set ON in command line
232 if $DEBUG; then
233 echo "${CMD}: Reading configuration file \"${STRABON_CONF}\""
234 fi
236 . ${STRABON_CONF}
238 if $DEBUG_SAVE; then
239 # restore debug value set on command line
240 DEBUG=true
241 fi
242 shift
243 fi
244 ;;
245 -*)
246 echo "${CMD}: unknown option \"${1}\""
247 help
248 exit 1
249 ;;
250 esac
251 done
253 # set defaults
254 case "${DATABASE}" in
255 postgis)
256 if test -z "${PORT}";
257 then
258 PORT=5432
259 fi
261 if test -z "${DBUSER}";
262 then
263 DBUSER="postgres"
264 fi
266 if test -z "${DBPASS}";
267 then
268 DBPASS="postgres"
269 fi
270 ;;
271 monetdb)
272 if test -z "${PORT}";
273 then
274 PORT=50000
275 fi
277 if test -z "${DBUSER}";
278 then
279 DBUSER="monetdb"
280 fi
282 if test -z "${DBPASS}";
283 then
284 DBPASS="monetdb"
285 fi
286 ;;
287 esac
289 # print configuration/options
290 if ${DEBUG}; then
291 echo "${CMD}: printing database connection details"
292 echo " Database Engine : ${DATABASE}"
293 echo " Database Name : ${DB}"
294 echo " Hostname : ${HOST}"
295 echo " Port : ${PORT}"
296 echo " Username : ${DBUSER}"
297 echo " Password : ${DBPASS}"
298 fi
300 # determine command to execute
301 case "${1}" in
302 help)
303 shift
304 if test $# -ge 1; then
305 case "${1}" in
306 query)
307 help_query
308 ;;
309 update)
310 help_update
311 ;;
312 store)
313 help_store
314 ;;
315 *)
316 help
317 ;;
318 esac
319 exit 1
320 fi
321 help
322 exit 0
323 ;;
324 query)
325 CLASS="QueryOp"
326 shift
327 if ! test $# -eq 1; then
328 help_query
329 exit 1
330 fi
331 QUERY="${1}"
333 # check for predefined queries
334 case "${QUERY}" in
335 size)
336 QUERY="${QUERY_SIZE}"
337 ;;
338 esac
339 ;;
340 update)
341 CLASS="UpdateOp"
342 shift
343 if ! test $# -eq 1; then
344 help_update
345 exit 1
346 fi
347 QUERY="${1}"
349 # check for predefined queries
350 case "${QUERY}" in
351 clear)
352 QUERY="${QUERY_DELETEALL}"
353 ;;
354 esac
355 ;;
356 store)
357 CLASS="StoreOp"
358 shift
359 if ! test $# -ge 1; then
360 help_store
361 exit 1
362 fi
363 # check whether format is specified
364 if test "${1}" = "-f"; then
365 shift
366 if ! test $# -ge 1; then
367 echo "${CMD}: Option -f requires an RDF format (\`ntriples', \`n3', \`rdfxml', or \`turtle')"
368 exit 2
369 else
370 FORMAT="${1}"
371 case "${FORMAT}" in
372 ntriples|n3|rdfxml|turtle)
373 shift
374 ;;
375 *)
376 echo "${CMD}: invalid RDF format \"${FORMAT}\"."
377 echo "${CMD}: valid RDF formats are \`ntriples', \`n3', \`rdfxml', or \`turtle'"
378 exit 2
379 ;;
380 esac
381 fi
382 fi
383 # if no files are given
384 if ! test $# -ge 1; then
385 help_store
386 exit 1
387 fi
388 # do not make an assignment of the files to QUERY
389 # handle the case of storing multiple files afterwards
390 # QUERY="${@}"
391 ;;
392 "")
393 help
394 exit 1
395 ;;
396 *)
397 help
398 echo
399 echo "${CMD}: unknown command \"${1}\"."
400 exit 1
401 ;;
402 esac
404 # compile command to execute
405 if test "${CLASS}" = "StoreOp"; then
406 STRABON_EXEC=
407 for file in "${@}"; do
408 STRABON_EXEC="${STRABON_EXEC}(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${file}\" ${FORMAT});
409 "
410 done
411 else
412 STRABON_EXEC="(cd ${RUNTIME} && java -cp ./target/\*:. ${PKG}.${DATABASE}.${CLASS} ${HOST} ${PORT} ${DB} ${DBUSER} ${DBPASS} \"${QUERY}\")"
413 fi
415 # execute command or debug
416 if ${DEBUG}; then
417 echo "${CMD}: printing command for execution"
418 echo "${STRABON_EXEC}"
419 else
420 eval ${STRABON_EXEC}
421 fi