KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jena > DBcmd


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6  
7 package jena;
8 import jena.cmdline.* ;
9 import com.hp.hpl.jena.db.* ;
10 //import com.hp.hpl.jena.rdf.model.* ;
11
import java.util.* ;
12  
13 /** Framework for the database commands.
14  *
15  * @author Andy Seaborne
16  * @version $Id: DBcmd.java,v 1.6 2005/02/21 11:49:04 andy_seaborne Exp $
17  */

18  
19 abstract class DBcmd
20 {
21     // Standardised names.
22
protected final ArgDecl argDeclDbURL = new ArgDecl(true, "db");
23     protected final ArgDecl argDeclDbType = new ArgDecl(true, "dbType");
24     protected final ArgDecl argDeclDbUser = new ArgDecl(true, "dbUser", "user");
25     protected final ArgDecl argDeclDbPassword = new ArgDecl(true, "dbPassword", "password", "pw");
26     protected final ArgDecl argDeclModelName = new ArgDecl(true, "model");
27     protected final ArgDecl argDeclDbDriver = new ArgDecl(true, "driver");
28
29     protected final ArgDecl argDeclVerbose = new ArgDecl(false, "v", "verbose");
30     protected boolean verbose = false ;
31
32     protected final ArgDecl argDeclDebug = new ArgDecl(false, "debug");
33     protected boolean debug = false ;
34
35     protected final ArgDecl argDeclHelp = new ArgDecl(false, "help", "h");
36
37
38     // The values of these arguments
39
protected String JavaDoc argDbURL = null;
40     protected String JavaDoc argDbType = null;
41     protected String JavaDoc argDbUser = null;
42     protected String JavaDoc argDbPassword = null;
43     protected String JavaDoc argModelName = null;
44
45     // DB types to driver
46
static Map drivers = new HashMap();
47     static {
48         drivers.put("mysql", "com.mysql.jdbc.Driver");
49         drivers.put("postgresql", "org.postgresql.Driver");
50         drivers.put("postgres", "org.postgresql.Driver");
51         drivers.put("postgresql", "org.postgresql.Driver");
52         drivers.put("PostgreSQL", "org.postgresql.Driver") ; // Thanks to Joshua Moore [j.moore@dkfz-heidelberg.de]
53
}
54
55     boolean takesPositionalArgs = false ;
56     String JavaDoc cmdName = "DB" ;
57     CommandLine cmdLine = new CommandLine();
58     private IDBConnection jdbcConnection = null;
59     private ModelRDB dbModel = null ;
60     
61     private String JavaDoc [] usage = new String JavaDoc[]{ "Complain to jena-dev: someone forgot the usage string" } ;
62
63     DBcmd(String JavaDoc n, boolean posArgs)
64     {
65         cmdName = n ;
66         takesPositionalArgs = posArgs ;
67         cmdLine.add(argDeclDbURL);
68         cmdLine.add(argDeclDbType);
69         cmdLine.add(argDeclDbUser);
70         cmdLine.add(argDeclDbPassword);
71         cmdLine.add(argDeclModelName);
72         cmdLine.add(argDeclVerbose) ;
73         cmdLine.add(argDeclDebug) ;
74         cmdLine.add(argDeclHelp) ;
75     }
76
77     protected void init(String JavaDoc[] args)
78     {
79         try {
80             cmdLine.process(args);
81         } catch (IllegalArgumentException JavaDoc ex)
82         {
83             usage() ;
84             System.exit(1) ;
85         }
86         
87         if ( cmdLine.contains(argDeclHelp) )
88         {
89             usage() ;
90             System.exit(0) ;
91         }
92         
93         verbose = cmdLine.contains(argDeclVerbose) ;
94         debug = cmdLine.contains(argDeclDebug) ;
95         if ( debug )
96             verbose = true ;
97         
98         if (cmdLine.contains(argDeclDbURL))
99             argDbURL = cmdLine.getArg(argDeclDbURL).getValue();
100
101         if (cmdLine.contains(argDeclDbType))
102             argDbType = cmdLine.getArg(argDeclDbType).getValue();
103
104         if (cmdLine.contains(argDeclDbUser))
105             argDbUser = cmdLine.getArg(argDeclDbUser).getValue();
106
107         if (cmdLine.contains(argDeclDbPassword))
108             argDbPassword = cmdLine.getArg(argDeclDbPassword).getValue();
109
110         if (cmdLine.contains(argDeclModelName))
111             argModelName = cmdLine.getArg(argDeclModelName).getValue();
112
113         if ( verbose )
114         {
115             System.out.println("URL = " + argDbURL);
116             System.out.println("User = " + argDbUser);
117             System.out.println("Password = " + argDbPassword);
118             System.out.println("Type = " + argDbType);
119             System.out.println("Name = " + argModelName);
120         }
121
122         // Mandatory arguments
123
if (argDbURL == null || argDbType == null || argDbUser == null || argDbPassword == null)
124         {
125             System.err.println("Missing a required argument (need JDBC URL, user, password and DB type)");
126             System.exit(9);
127         }
128
129         if ( ! takesPositionalArgs && cmdLine.items().size() != 0 )
130         {
131             System.err.println(cmdName+": No positional arguments allowed") ;
132             usage() ;
133             System.exit(9) ;
134         }
135         
136         if ( takesPositionalArgs && cmdLine.items().size() == 0 )
137         {
138             System.err.println(cmdName+": Positional argument required") ;
139             usage() ;
140             System.exit(9) ;
141         }
142
143         String JavaDoc driverClass = (String JavaDoc)drivers.get(argDbType);
144         if (cmdLine.contains(argDeclDbDriver))
145             driverClass = cmdLine.getArg(argDeclDbDriver).getValue();
146
147         if (driverClass == null)
148         {
149             System.err.println("No driver: please say which JDBC driver to use");
150             System.exit(9);
151         }
152
153         try
154         {
155             Class.forName(driverClass).newInstance();
156         }
157         catch (Exception JavaDoc ex)
158         {
159             System.err.println("Couldn't load the driver class: " + driverClass);
160             System.err.println("" + ex);
161             System.exit(9);
162         }
163
164     }
165     
166 protected ModelRDB getRDBModel()
167     {
168         if ( dbModel == null )
169         {
170             try
171             {
172                 
173             if ( argModelName == null )
174                 dbModel = ModelRDB.open(getConnection()) ;
175             else
176                 try {
177                     dbModel = ModelRDB.open(getConnection(), argModelName) ;
178                 } catch (com.hp.hpl.jena.shared.DoesNotExistException ex)
179                 {
180                     System.out.println("No model '"+argModelName+"' in that database") ;
181                     System.exit(9) ;
182                 }
183             }
184             catch (com.hp.hpl.jena.db.RDFRDBException dbEx)
185             {
186                 Throwable JavaDoc t = dbEx.getCause() ;
187                 if ( t == null )
188                     t = dbEx ;
189                 System.out.println("Failed to connect to the database: "+t.getMessage()) ;
190                 System.exit(9) ;
191             }
192         }
193         
194         return dbModel ;
195     }
196
197
198     protected IDBConnection getConnection()
199     {
200         if ( jdbcConnection == null )
201         {
202             try {
203                 jdbcConnection = new DBConnection(argDbURL, argDbUser, argDbPassword, argDbType);
204             } catch (Exception JavaDoc ex)
205             {
206                 System.out.println("Exception making connection: "+ex.getMessage()) ;
207                 System.exit(9) ;
208             }
209         }
210         return jdbcConnection ;
211     }
212
213
214     protected void exec()
215     {
216         if ( cmdLine.items().size() == 0 )
217         {
218             exec0() ;
219             return ;
220         }
221         
222         
223         boolean inTransaction = false ;
224         try
225         {
226             if ( getRDBModel().supportsTransactions() )
227             {
228                 inTransaction = true ;
229                 getRDBModel().begin() ;
230             }
231
232             for ( Iterator iter = cmdLine.items().iterator() ; iter.hasNext() ; )
233             {
234                 String JavaDoc arg = (String JavaDoc)iter.next() ;
235                 boolean contTrans = false ;
236                 try {
237                     contTrans = exec1(arg) ;
238                 }
239                 catch (Exception JavaDoc ex)
240                 {
241                     System.out.println(ex.getMessage()) ;
242                     ex.printStackTrace(System.out) ;
243                     if ( inTransaction )
244                     {
245                         getRDBModel().abort() ;
246                         inTransaction = false ;
247                     }
248                     dbModel.close() ;
249                     dbModel = null ;
250                     System.exit(9);
251                 }
252                                         
253                     
254                 if ( !contTrans && inTransaction )
255                 {
256                     getRDBModel().commit() ;
257                     if ( iter.hasNext() )
258                     {
259                         inTransaction = true ;
260                         getRDBModel().begin() ;
261                     }
262                 }
263             }
264         }
265         finally
266         {
267             if ( inTransaction )
268                 getRDBModel().commit() ;
269         }
270         
271         dbModel.close() ;
272         dbModel = null ;
273     }
274
275     /** Called if there are no psoitional arguments
276      */

277     abstract void exec0() ;
278     
279     /** Called for each postional argument, inside a transaction.
280      * Return true to continue this transaction, false to end it and start a new
281      * one if there are any more args
282      */

283     abstract boolean exec1(String JavaDoc arg) ;
284     
285     
286     
287     protected void setUsage(String JavaDoc a)
288     {
289         String JavaDoc[] aa = new String JavaDoc[]{a} ;
290         setUsage(aa) ;
291     }
292
293
294     /** Usage message: one line per entry*/
295     
296     protected void setUsage(String JavaDoc[] a)
297     {
298         usage = a ;
299     }
300     
301     protected void usage()
302     {
303         for ( int i = 0 ; i < usage.length ; i++ )
304         {
305             System.err.println(usage[i]) ;
306         }
307     }
308 }
309
310 /*
311  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
312  * All rights reserved.
313  *
314  * Redistribution and use in source and binary forms, with or without
315  * modification, are permitted provided that the following conditions
316  * are met:
317  * 1. Redistributions of source code must retain the above copyright
318  * notice, this list of conditions and the following disclaimer.
319  * 2. Redistributions in binary form must reproduce the above copyright
320  * notice, this list of conditions and the following disclaimer in the
321  * documentation and/or other materials provided with the distribution.
322  * 3. The name of the author may not be used to endorse or promote products
323  * derived from this software without specific prior written permission.
324  *
325  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
326  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
327  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
328  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
329  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
330  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
331  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
332  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
333  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
334  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
335  */

336
Popular Tags