1 5 6 7 package jena; 8 import jena.cmdline.* ; 9 import com.hp.hpl.jena.db.* ; 10 import java.util.* ; 12 13 18 19 abstract class DBcmd 20 { 21 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 protected String argDbURL = null; 40 protected String argDbType = null; 41 protected String argDbUser = null; 42 protected String argDbPassword = null; 43 protected String argModelName = null; 44 45 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") ; } 54 55 boolean takesPositionalArgs = false ; 56 String cmdName = "DB" ; 57 CommandLine cmdLine = new CommandLine(); 58 private IDBConnection jdbcConnection = null; 59 private ModelRDB dbModel = null ; 60 61 private String [] usage = new String []{ "Complain to jena-dev: someone forgot the usage string" } ; 62 63 DBcmd(String 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 [] args) 78 { 79 try { 80 cmdLine.process(args); 81 } catch (IllegalArgumentException 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 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 driverClass = (String )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 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 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 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 arg = (String )iter.next() ; 235 boolean contTrans = false ; 236 try { 237 contTrans = exec1(arg) ; 238 } 239 catch (Exception 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 277 abstract void exec0() ; 278 279 283 abstract boolean exec1(String arg) ; 284 285 286 287 protected void setUsage(String a) 288 { 289 String [] aa = new String []{a} ; 290 setUsage(aa) ; 291 } 292 293 294 295 296 protected void setUsage(String [] 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 336 | Popular Tags |