Skip to content

Commit

Permalink
Allow inject SqlDbService and DataSource #23; update to act-1.8.25
Browse files Browse the repository at this point in the history
  • Loading branch information
greenlaw110 committed Jul 2, 2019
1 parent 10ab52c commit d3ec812
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# act-sql-common CHANGE LOG


1.5.0
* update to act-1.8.25
* Allow inject SqlDbService and DataSource #23

1.4.6 - 16/Jun/2019
* Error starting with db.default.datasource.provider configuration #22

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<artifactId>act-sql-common</artifactId>
<packaging>jar</packaging>
<version>1.4.7-SNAPSHOT</version>
<version>1.5.0-SNAPSHOT</version>

<name>ACT SQL Common Module</name>
<description>Provides Authentication/Authorization/Accounting service for Actframework based on osgl-aaa</description>
Expand All @@ -35,7 +35,7 @@
<parent>
<groupId>org.actframework</groupId>
<artifactId>parent</artifactId>
<version>1.8.23</version>
<version>1.8.25</version>
</parent>

<properties>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/act/db/sql/SqlDbPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import act.app.App;
import act.app.event.SysEventId;
import act.db.DbPlugin;
import act.db.sql.inject.SqlDbProviders;
import act.db.sql.tx.TxContext;
import act.event.ActEventListenerBase;
import act.handler.event.BeforeResultCommit;
Expand All @@ -32,7 +33,7 @@ public abstract class SqlDbPlugin extends DbPlugin {
private static boolean registered;

@Override
protected final void applyTo(App app) {
protected final void applyTo(final App app) {
if (registered) {
return;
}
Expand All @@ -47,6 +48,7 @@ public void run() {
app.jobManager().on(SysEventId.PRE_START, new Runnable() {
@Override
public void run() {
SqlDbProviders.classInit(app);
TxContext.reset();
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/act/db/sql/SqlDbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public DataSource dataSourceReadOnly() {
return dsReadOnly;
}

public DataSource newDataSource() {
return dataSourceProvider().createDataSource(dataSourceConfig());
}

public DataSourceProvider dataSourceProvider() {
return config.dataSourceProvider();
}
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/act/db/sql/inject/SqlDbProviders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package act.db.sql.inject;

/*-
* #%L
* ACT SQL Common Module
* %%
* Copyright (C) 2015 - 2019 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import act.Act;
import act.app.App;
import act.db.DB;
import act.db.sql.DataSourceProvider;
import act.db.sql.SqlDbService;
import org.osgl.inject.Genie;
import org.osgl.inject.NamedProvider;

import javax.inject.Provider;
import javax.sql.DataSource;

public class SqlDbProviders {

private static Provider<SqlDbService> SQL_DB_SVC_PROVIDER = new Provider<SqlDbService>() {
@Override
public SqlDbService get() {
return NAMED_SQL_DB_SVC_PROVIDER.get(DB.DEFAULT);
}
};

private static NamedProvider<SqlDbService> NAMED_SQL_DB_SVC_PROVIDER = new NamedProvider<SqlDbService>() {
@Override
public SqlDbService get(String name) {
return Act.app().dbServiceManager().dbService(name);
}
};

private static Provider<DataSource> DATA_SRC_PROVIDER = new Provider<DataSource>() {
@Override
public DataSource get() {
return NAMED_DATA_SRC_PROVIDER.get(DB.DEFAULT);
}
};

private static NamedProvider<DataSource> NAMED_DATA_SRC_PROVIDER = new NamedProvider<DataSource>() {
@Override
public DataSource get(String name) {
SqlDbService sqlDbService = NAMED_SQL_DB_SVC_PROVIDER.get(name);
return sqlDbService.dataSource();
}
};

private static Provider<DataSourceProvider> DATA_SRC_PROVIDER_PROVIDER = new Provider<DataSourceProvider>() {
@Override
public DataSourceProvider get() {
return NAMED_DATA_SRC_PROVIDER_PROVIDER.get(DB.DEFAULT);
}
};

private static NamedProvider<DataSourceProvider> NAMED_DATA_SRC_PROVIDER_PROVIDER = new NamedProvider<DataSourceProvider>() {
@Override
public DataSourceProvider get(String name) {
SqlDbService sqlDbService = NAMED_SQL_DB_SVC_PROVIDER.get(name);
return sqlDbService.dataSourceProvider();
}
};

public static void classInit(App app) {
Genie genie = app.getInstance(Genie.class);
genie.registerProvider(SqlDbService.class, SQL_DB_SVC_PROVIDER);
genie.registerNamedProvider(SqlDbService.class, NAMED_SQL_DB_SVC_PROVIDER);
genie.registerProvider(DataSource.class, DATA_SRC_PROVIDER);
genie.registerNamedProvider(DataSource.class, NAMED_DATA_SRC_PROVIDER);
genie.registerProvider(DataSourceProvider.class, DATA_SRC_PROVIDER_PROVIDER);
genie.registerNamedProvider(DataSourceProvider.class, NAMED_DATA_SRC_PROVIDER_PROVIDER);
}

}
90 changes: 90 additions & 0 deletions testapps/ghissues/src/main/java/sql_common/Gh23.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package sql_common;

import act.controller.annotation.UrlContext;
import act.db.beetlsql.BeetlSqlService;
import act.db.eclipselink.EclipseLinkService;
import act.db.hibernate.HibernateService;
import act.db.jpa.JPAService;
import act.db.sql.SqlDbService;
import org.osgl.mvc.annotation.GetAction;

import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;

@UrlContext("23")
public class Gh23 {

@Inject
@Named("el")
private JPAService elService;

@Inject
@Named("hi")
private JPAService hiService;

@Inject
@Named("be")
private SqlDbService beService;

@Inject
@Named("el")
private DataSource elDs;

@Inject
@Named("hi")
private DataSource hiDs;

@Inject
@Named("be")
private DataSource beDs;

@GetAction("~elService~")
public boolean testElService() {
if (null == elService) {
return false;
}
return elService instanceof EclipseLinkService;
}

@GetAction("~hiService~")
public boolean testHiService() {
if (null == hiService) {
return false;
}
return hiService instanceof HibernateService;
}

@GetAction("~beService~")
public boolean testBeService() {
if (null == beService) {
return false;
}
return beService instanceof BeetlSqlService;
}

@GetAction("~elDataSource~")
public boolean elDataSource() {
if (null == elDs) {
return false;
}
return elDs == elService.dataSource();
}

@GetAction("~hiDataSource~")
public boolean hiDataSource() {
if (null == hiDs) {
return false;
}
return hiDs == hiService.dataSource();
}

@GetAction("~beDataSource~")
public boolean beDataSource() {
if (null == beDs) {
return false;
}
return beDs == beService.dataSource();
}

}
10 changes: 10 additions & 0 deletions testapps/ghissues/src/main/resources/conf/db.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
db.instances=el,hi,be

db.el.url=jdbc:h2:./test-el
db.el.impl=act.db.eclipselink.EclipseLinkPlugin

db.hi.url=jdbc:h2:./test-hi
db.hi.impl=act.db.hibernate.HibernatePlugin

db.be.url=jdbc:h2:./test-el
db.be.impl=act.db.beetlsql.BeetlSqlPlugin

0 comments on commit d3ec812

Please sign in to comment.