samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java
branchlivedb
changeset 358 afdd66815ee3
parent 357 837370f791ba
child 361 6507a9474b6d
     1.1 --- a/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java	Wed Jul 14 20:56:33 2010 +0200
     1.2 +++ b/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java	Thu Jul 15 00:40:37 2010 +0200
     1.3 @@ -15,7 +15,11 @@
     1.4  import java.sql.ResultSetMetaData;
     1.5  import java.sql.SQLException;
     1.6  import java.util.Enumeration;
     1.7 +import java.util.Properties;
     1.8 +import java.util.ServiceLoader;
     1.9  import java.util.Set;
    1.10 +import java.util.logging.Level;
    1.11 +import java.util.logging.Logger;
    1.12  import javax.annotation.processing.AbstractProcessor;
    1.13  import javax.annotation.processing.Processor;
    1.14  import javax.annotation.processing.RoundEnvironment;
    1.15 @@ -46,7 +50,7 @@
    1.16              try {
    1.17                  JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe);
    1.18                  Writer w = src.openWriter();
    1.19 -                Connection c = DriverManager.getConnection(db.url(), db.user(), db.password());
    1.20 +                Connection c = getConnection(db.url(), db.user(), db.password());
    1.21                  CallableStatement s = c.prepareCall(db.query());
    1.22                  ResultSet rs = s.executeQuery();
    1.23                  ResultSetMetaData md = rs.getMetaData();
    1.24 @@ -103,12 +107,19 @@
    1.25          return true;
    1.26      }
    1.27  
    1.28 -    static {
    1.29 -        // init drivers
    1.30 -        Enumeration<Driver> en;
    1.31 -        en = DriverManager.getDrivers();
    1.32 -        while (en.hasMoreElements()) {
    1.33 -            Driver driver = en.nextElement();
    1.34 +    private static Connection getConnection(String url, String user, String password) 
    1.35 +    throws SQLException {
    1.36 +        final ClassLoader cl = LiveDBProcessor.class.getClassLoader();
    1.37 +        for (Driver d : ServiceLoader.load(Driver.class, cl)) {
    1.38 +//            System.out.println("looked up: " + d);
    1.39 +            if (d.acceptsURL(url)) {
    1.40 +                //System.out.println("accepts: " + d);
    1.41 +                Properties p = new Properties();
    1.42 +                p.put("user", user);
    1.43 +                p.put("password", password);
    1.44 +                return d.connect(url, p);
    1.45 +            }
    1.46          }
    1.47 +        throw new SQLException("No driver found for " + url);
    1.48      }
    1.49  }