-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
276e31a
commit 2bf80b6
Showing
332 changed files
with
12,448 additions
and
10,745 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mossle.api.database; | ||
|
||
import java.io.Closeable; | ||
|
||
import javax.sql.DataSource; | ||
|
||
public interface Database extends Closeable { | ||
public static final String NAME_DEFAULT = "default"; | ||
|
||
DataSource getDataSource(); | ||
|
||
DataSource getDataSource(String name); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/mossle/api/database/DatabaseBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.mossle.api.database; | ||
|
||
import java.util.Properties; | ||
|
||
public abstract class DatabaseBuilder<T extends DatabaseBuilder<T>> { | ||
private Properties properties; | ||
private String defaultPrefix; | ||
|
||
public static DatabaseBuilder<?> newBuilder() { | ||
return DatabaseProvider.provider().newBuilder(); | ||
} | ||
|
||
public Properties getProperties() { | ||
return properties; | ||
} | ||
|
||
public DatabaseBuilder<?> setProperties(Properties properties) { | ||
this.properties = properties; | ||
|
||
return this; | ||
} | ||
|
||
public String getDefaultPrefix() { | ||
return defaultPrefix; | ||
} | ||
|
||
public DatabaseBuilder<?> setDefaultPrefix(String defaultPrefix) { | ||
this.defaultPrefix = defaultPrefix; | ||
|
||
return this; | ||
} | ||
|
||
public abstract Database build(); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/mossle/api/database/DatabaseProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mossle.api.database; | ||
|
||
public abstract class DatabaseProvider { | ||
public static DatabaseProvider provider() { | ||
DatabaseProvider databaseProvider = DatabaseProviderRegistry | ||
.getDefaultRegistry().provider(); | ||
|
||
if (databaseProvider == null) { | ||
throw new RuntimeException("cannot find database provider"); | ||
} | ||
|
||
return databaseProvider; | ||
} | ||
|
||
public int getPriority() { | ||
return 5; | ||
} | ||
|
||
public abstract DatabaseBuilder newBuilder(); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/mossle/api/database/DatabaseProviderComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mossle.api.database; | ||
|
||
import java.util.Comparator; | ||
|
||
public class DatabaseProviderComparator implements Comparator<DatabaseProvider> { | ||
public int compare(DatabaseProvider o1, DatabaseProvider o2) { | ||
return o1.getPriority() - o2.getPriority(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/mossle/api/database/DatabaseProviderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.mossle.api.database; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
public class DatabaseProviderRegistry { | ||
private static DatabaseProviderRegistry instance; | ||
private List<DatabaseProvider> providers = new ArrayList<DatabaseProvider>(); | ||
|
||
public static synchronized DatabaseProviderRegistry getDefaultRegistry() { | ||
if (instance == null) { | ||
instance = new DatabaseProviderRegistry(); | ||
instance.refreshProviders(); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
public void refreshProviders() { | ||
ServiceLoader<DatabaseProvider> serviceLoader = ServiceLoader | ||
.load(DatabaseProvider.class); | ||
Iterator<DatabaseProvider> iterator = serviceLoader.iterator(); | ||
List<DatabaseProvider> databaseProviders = new ArrayList<DatabaseProvider>(); | ||
|
||
while (iterator.hasNext()) { | ||
databaseProviders.add(iterator.next()); | ||
} | ||
|
||
Collections.sort(databaseProviders, new DatabaseProviderComparator()); | ||
this.providers = databaseProviders; | ||
} | ||
|
||
public DatabaseProvider provider() { | ||
if (providers.isEmpty()) { | ||
return null; | ||
} | ||
|
||
return providers.get(0); | ||
} | ||
} |
Oops, something went wrong.