| 1 |  |  package liquibase.database; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.ArrayList; | 
  | 4 |  |  import java.util.Comparator; | 
  | 5 |  |  import java.util.List; | 
  | 6 |  |  import java.util.SortedSet; | 
  | 7 |  |  import java.util.TreeSet; | 
  | 8 |  |   | 
  | 9 |  |  import liquibase.database.core.UnsupportedDatabase; | 
  | 10 |  |  import liquibase.exception.DatabaseException; | 
  | 11 |  |  import liquibase.exception.UnexpectedLiquibaseException; | 
  | 12 |  |  import liquibase.logging.LogFactory; | 
  | 13 |  |  import liquibase.servicelocator.ServiceLocator; | 
  | 14 |  |   | 
  | 15 |  |  public class DatabaseFactory { | 
  | 16 |  |      private static DatabaseFactory instance; | 
  | 17 | 1 |      private List<Database> implementedDatabases = new ArrayList<Database>(); | 
  | 18 |  |   | 
  | 19 | 1 |      protected DatabaseFactory() { | 
  | 20 |  |          try { | 
  | 21 | 1 |              Class[] classes = ServiceLocator.getInstance().findClasses(Database.class); | 
  | 22 |  |   | 
  | 23 |  |               | 
  | 24 | 18 |              for (Class<? extends Database> clazz : classes) { | 
  | 25 | 17 |                  register(clazz.getConstructor().newInstance()); | 
  | 26 |  |              } | 
  | 27 |  |   | 
  | 28 | 0 |          } catch (Exception e) { | 
  | 29 | 0 |              throw new RuntimeException(e); | 
  | 30 | 1 |          } | 
  | 31 |  |   | 
  | 32 | 1 |      } | 
  | 33 |  |   | 
  | 34 |  |      public static DatabaseFactory getInstance() { | 
  | 35 | 3 |          if (instance == null) { | 
  | 36 | 1 |              instance = new DatabaseFactory(); | 
  | 37 |  |          } | 
  | 38 | 3 |          return instance; | 
  | 39 |  |      } | 
  | 40 |  |   | 
  | 41 |  |      public static void reset() { | 
  | 42 | 0 |          instance = new DatabaseFactory(); | 
  | 43 | 0 |      } | 
  | 44 |  |   | 
  | 45 |  |       | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |      public List<Database> getImplementedDatabases() { | 
  | 49 | 2 |          return implementedDatabases; | 
  | 50 |  |      } | 
  | 51 |  |   | 
  | 52 |  |      public void register(Database database) { | 
  | 53 | 17 |          implementedDatabases.add(0, database); | 
  | 54 | 17 |      } | 
  | 55 |  |   | 
  | 56 |  |      public Database findCorrectDatabaseImplementation(DatabaseConnection connection) throws DatabaseException { | 
  | 57 |  |   | 
  | 58 | 0 |          SortedSet<Database> foundDatabases = new TreeSet<Database>(new Comparator<Database>() { | 
  | 59 |  |              @Override | 
  | 60 |  |              public int compare(Database o1, Database o2) { | 
  | 61 | 0 |                  return -1 * new Integer(o1.getPriority()).compareTo(o2.getPriority()); | 
  | 62 |  |              } | 
  | 63 |  |          }); | 
  | 64 |  |   | 
  | 65 | 0 |          for (Database implementedDatabase : getImplementedDatabases()) { | 
  | 66 | 0 |              if (implementedDatabase.isCorrectDatabaseImplementation(connection)) { | 
  | 67 | 0 |                  foundDatabases.add(implementedDatabase); | 
  | 68 |  |              } | 
  | 69 |  |          } | 
  | 70 |  |   | 
  | 71 | 0 |          if (foundDatabases.size() == 0) { | 
  | 72 | 0 |              LogFactory.getLogger().warning("Unknown database: " + connection.getDatabaseProductName()); | 
  | 73 | 0 |              return new UnsupportedDatabase(); | 
  | 74 |  |          } | 
  | 75 |  |   | 
  | 76 |  |          Database returnDatabase; | 
  | 77 |  |          try { | 
  | 78 | 0 |              returnDatabase = foundDatabases.iterator().next().getClass().newInstance(); | 
  | 79 | 0 |          } catch (Exception e) { | 
  | 80 | 0 |              throw new UnexpectedLiquibaseException(e); | 
  | 81 | 0 |          } | 
  | 82 |  |   | 
  | 83 | 0 |          returnDatabase.setConnection(connection); | 
  | 84 | 0 |          return returnDatabase; | 
  | 85 |  |      } | 
  | 86 |  |   | 
  | 87 |  |      public String findDefaultDriver(String url) { | 
  | 88 | 0 |          for (Database database : this.getImplementedDatabases()) { | 
  | 89 | 0 |              String defaultDriver = database.getDefaultDriver(url); | 
  | 90 | 0 |              if (defaultDriver != null) { | 
  | 91 | 0 |                  return defaultDriver; | 
  | 92 |  |              } | 
  | 93 | 0 |          } | 
  | 94 |  |   | 
  | 95 | 0 |          return null; | 
  | 96 |  |      } | 
  | 97 |  |   | 
  | 98 |  |       | 
  | 99 |  |   | 
  | 100 |  |   | 
  | 101 |  |      public void clearRegistry() { | 
  | 102 | 0 |          implementedDatabases.clear(); | 
  | 103 | 0 |      } | 
  | 104 |  |  } |