1 22 23 package org.xquark.bridge; 24 25 import java.io.File ; 26 import java.io.PrintStream ; 27 import java.net.MalformedURLException ; 28 import java.sql.Connection ; 29 import java.sql.SQLException ; 30 import java.util.HashMap ; 31 32 import javax.sql.DataSource ; 33 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 import org.xquark.extractor.Extractor; 37 import org.xquark.extractor.ExtractorConnection; 38 import org.xquark.schema.SchemaManager; 39 import org.xquark.xml.xdbc.XMLDBCException; 40 41 48 public class XQBridge { 49 private static final String RCSRevision = "$Revision: 1.22 $"; 50 private static final String RCSName = "$Name: $"; 51 52 private Connection jdbcConnection; 54 private DataSource dataSource; 55 private String configURI; 56 private File configFile; 57 private String jdbcURL; 58 private String userName; 59 private String password; 60 61 private Extractor extractor; 62 private HashMap mappingMetadata = new HashMap (); 63 private SchemaManager schemaManager = null; 64 private PrintStream log = null; 65 private ClassLoader classLoader = null; 66 67 80 public XQBridge(String URI) { 81 if (URI == null) 82 throw new NullPointerException ("Illegal null value for configuration file URI"); 83 configURI = URI; 84 } 85 86 94 public XQBridge(File confFile) { 95 if (confFile == null) 96 throw new NullPointerException ("Illegal null value for configuration file"); 97 configFile = confFile; 98 } 99 100 108 public XQBridge(DataSource dataSource) { 109 if (dataSource == null) 110 throw new NullPointerException ("Illegal null value for data source"); 111 this.dataSource = dataSource; 112 } 113 114 126 public XQBridge(DataSource dataSource, File confFile) { 127 if (dataSource == null) 128 throw new NullPointerException ("Illegal null value for data source"); 129 if (confFile == null) 130 throw new NullPointerException ("Illegal null value for configuration file"); 131 this.dataSource = dataSource; 132 this.configFile = confFile; 133 } 134 135 147 public XQBridge(String url, String userName, String password) { 148 if (url == null) 149 throw new NullPointerException ("Illegal null value for URL"); 150 if (userName == null) 151 throw new NullPointerException ("Illegal null value for user name"); 152 if (password == null) 153 throw new NullPointerException ("Illegal null value for user password"); 154 this.jdbcURL = url; 155 this.userName = userName; 156 this.password = password; 157 } 158 159 169 public XQBridge(Connection jdbcConn) { 170 if (jdbcConn == null) 171 throw new NullPointerException ("Illegal null value for connection"); 172 jdbcConnection = jdbcConn; 173 } 174 175 188 public XQBridge(Connection jdbcConn, String confURI) { 189 if (jdbcConn == null) 190 throw new NullPointerException ("Illegal null value for connection"); 191 if (confURI == null) 192 throw new NullPointerException ("Illegal null value for configuration file URI"); 193 jdbcConnection = jdbcConn; 194 configURI = confURI; 195 } 196 197 private Extractor getExtractor() throws XMLDBCException { 198 if (extractor == null) { 199 if (configURI == null && configFile != null) { 200 try { 201 configURI = configFile.toURL().toString(); 202 } catch (MalformedURLException ex) { 203 throw new XMLDBCException("Invalid configuration file name " + configFile, ex); 204 } 205 } 206 if (dataSource != null) { 207 extractor = new Extractor(dataSource, configURI); 208 } else if (jdbcConnection != null) { 209 extractor = new Extractor(jdbcConnection, configURI); 210 } else if (jdbcURL != null) { 211 extractor = new Extractor(jdbcURL, userName, password); 212 } else { 213 extractor = new Extractor(configURI, getClassLoader()); 214 } 215 } 216 return extractor; 217 } 218 219 227 public Connection getConnection() throws XMLDBCException { 228 Connection conn = null; 229 try { 230 conn = getExtractor().getJdbcDataSource().getConnection(); 231 } catch (SQLException ex) { 232 throw new XMLDBCException("Could not create connection", ex); 233 } 234 return conn; 235 } 236 237 243 public ExtractorConnection getXMLConnection() throws XMLDBCException { 244 return getExtractor().getExtractorConnection(); 245 } 246 247 253 public void close() throws XMLDBCException { 254 if (extractor != null) 255 extractor.close(); 256 } 257 258 262 public void setClassLoader(ClassLoader loader) { 263 classLoader = loader; 264 } 265 266 269 public ClassLoader getClassLoader() { 270 return classLoader != null ? classLoader : this.getClass().getClassLoader(); 271 } 272 273 287 public Mapping getMapping(InputSource source) throws XMLDBCException { 288 return loadMapping(source, false); 289 } 290 291 306 public Mapping getMapping(InputSource source, boolean reload) throws XMLDBCException { 307 return loadMapping(source, reload); 308 } 309 310 321 public Mapping getMapping(String fileURI) throws XMLDBCException { 322 return loadMapping(new InputSource (fileURI), false); 323 } 324 325 337 public Mapping getMapping(String fileURI, boolean reload) throws XMLDBCException { 338 return loadMapping(new InputSource (fileURI), reload); 339 } 340 341 private Mapping loadMapping(InputSource source, boolean reload) throws XMLDBCException { 342 return new Mapping(getExtractor().getJdbcDataSource(), source, reload, getClassLoader(), getSchemaManager(), mappingMetadata); 343 } 344 345 private SchemaManager getSchemaManager() { 346 if (schemaManager == null) 347 resetSchemas(); 348 return schemaManager; 349 } 350 351 private void resetSchemas() { 352 schemaManager = new SchemaManager(); 353 String resourceLegacy = getClass().getResource("/org/xquark/bridge/resources/mapping_1_0.xsd").toString(); 354 String resource = getClass().getResource("/org/xquark/bridge/resources/mapping.xsd").toString(); 355 356 try { 357 schemaManager.loadSchema(new InputSource (resourceLegacy)); 358 schemaManager.loadSchema(new InputSource (resource)); 359 } catch (SAXException e) { 360 } 362 } 363 364 368 public void loadSchema(InputSource source) throws SAXException { 369 getSchemaManager().loadSchema(source); 370 } 371 372 377 public void resetMappings(boolean resetSchemas) { 378 mappingMetadata.clear(); 379 if (resetSchemas) 380 resetSchemas(); 381 } 382 383 } 384 | Popular Tags |