Skip to content
Permalink
Browse files

Added 'connect_on_startup' settings. Fixes #1

  • Loading branch information
aalmiray committed Dec 19, 2014
1 parent 689c952 commit 40035286c23b6d5831b0377b532bbab963ac5e38
@@ -10,10 +10,10 @@ buildscript {
classpath 'org.codehaus.griffon:gradle-griffon-build-plugin:2.0.0'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1'
classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'org.ajoberstar:gradle-git:0.12.0'
classpath 'org.kordamp.gradle:stats-gradle-plugin:0.1.3'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.5'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.6'
}
}

@@ -89,7 +89,7 @@ subprojects { subproj ->
}

dependencies {
testCompile 'junit:junit:4.11'
testCompile 'junit:junit:4.12'
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
testCompile 'org.slf4j:slf4j-simple:1.7.7'
}
@@ -1,5 +1,5 @@
group=org.codehaus.griffon.plugins
version=1.0.0
version=1.1.0
pluginBaseName=griffon-datasource
griffonVersion=2.0.0
griffonPlugin=true
@@ -13,7 +13,7 @@ dependencies {
compile('commons-dbcp:commons-dbcp:1.4') { transitive = false }
compile('commons-pool:commons-pool:1.6') { transitive = false }

testRuntime('com.h2database:h2:1.4.182') { transitive = false }
testRuntime('com.h2database:h2:1.4.183') { transitive = false }
testRuntime "org.slf4j:slf4j-simple:1.7.7"

testRuntime "org.codehaus.griffon:griffon-guice:${griffon.version}"
@@ -17,10 +17,18 @@

import griffon.core.storage.ObjectFactory;

import javax.annotation.Nonnull;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Set;

/**
* @author Andres Almiray
*/
public interface DataSourceFactory extends ObjectFactory<DataSource> {
@Nonnull
Set<String> getDataSourceNames();

@Nonnull
Map<String, Object> getConfigurationFor(@Nonnull String dataSourceName);
}
@@ -0,0 +1,64 @@
/*
* Copyright 2014 the original author or authors.
*
* 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.
*/
package org.codehaus.griffon.runtime.datasource;

import griffon.core.GriffonApplication;
import griffon.plugins.datasource.DataSourceCallback;
import griffon.plugins.datasource.DataSourceFactory;
import griffon.plugins.datasource.DataSourceHandler;
import org.codehaus.griffon.runtime.core.addon.AbstractGriffonAddon;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;

import static griffon.util.ConfigUtils.getConfigValueAsBoolean;

/**
* @author Andres Almiray
* @since 1.1.0
*/
@Named("datasource")
public class DataSourceAddon extends AbstractGriffonAddon {
@Inject
private DataSourceHandler dataSourceHandler;

@Inject
private DataSourceFactory dataSourceFactory;

public void onBootstrapEnd(@Nonnull GriffonApplication application) {
for (String dataSourceName : dataSourceFactory.getDataSourceNames()) {
Map<String, Object> config = dataSourceFactory.getConfigurationFor(dataSourceName);
if (getConfigValueAsBoolean(config, "connect_on_startup", false)) {
dataSourceHandler.withDataSource(dataSourceName, new DataSourceCallback<Void>() {
@Override
public Void handle(@Nonnull String dataSourceName, @Nonnull DataSource dataSource) throws SQLException {
return null;
}
});
}
}
}

public void onShutdownStart(@Nonnull GriffonApplication application) {
for (String dataSourceName : dataSourceFactory.getDataSourceNames()) {
dataSourceHandler.closeDataSource(dataSourceName);
}
}
}
@@ -16,6 +16,7 @@
package org.codehaus.griffon.runtime.datasource;

import griffon.core.Configuration;
import griffon.core.addon.GriffonAddon;
import griffon.core.injection.Module;
import griffon.plugins.datasource.DataSourceFactory;
import griffon.plugins.datasource.DataSourceHandler;
@@ -59,6 +60,10 @@ protected void doConfigure() {
bind(DataSourceHandler.class)
.to(DefaultDataSourceHandler.class)
.asSingleton();

bind(GriffonAddon.class)
.to(DataSourceAddon.class)
.asSingleton();
// end::bindings[]
}
}
@@ -40,8 +40,10 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import static griffon.core.GriffonExceptionHandler.sanitize;
import static griffon.core.env.Environment.getEnvironmentShortName;
@@ -58,10 +60,31 @@
*/
public class DefaultDataSourceFactory extends AbstractObjectFactory<DataSource> implements DataSourceFactory {
private static final Logger LOG = LoggerFactory.getLogger(DefaultDataSourceFactory.class);
private static final String ERROR_DATASOURCE_BLANK = "Argument 'dataSourceName' must not be blank";
private final Set<String> dataSourceNames = new LinkedHashSet<>();

@Inject
public DefaultDataSourceFactory(@Nonnull @Named("datasource") Configuration configuration, @Nonnull GriffonApplication application) {
super(configuration, application);
dataSourceNames.add(KEY_DEFAULT);

if (configuration.containsKey(getPluralKey())) {
Map<String, Object> datasources = (Map<String, Object>) configuration.get(getPluralKey());
dataSourceNames.addAll(datasources.keySet());
}
}

@Nonnull
@Override
public Set<String> getDataSourceNames() {
return dataSourceNames;
}

@Nonnull
@Override
public Map<String, Object> getConfigurationFor(@Nonnull String dataSourceName) {
requireNonBlank(dataSourceName, ERROR_DATASOURCE_BLANK);
return narrowConfig(dataSourceName);
}

@Nonnull
@@ -79,6 +102,7 @@ protected String getPluralKey() {
@Nonnull
@Override
public DataSource create(@Nonnull String name) {
requireNonBlank(name, ERROR_DATASOURCE_BLANK);
Map<String, Object> config = narrowConfig(name);
event("DataSourceConnectStart", asList(name, config));
DataSource dataSource = createDataSource(config, name);
@@ -92,6 +116,7 @@ public DataSource create(@Nonnull String name) {

@Override
public void destroy(@Nonnull String name, @Nonnull DataSource instance) {
requireNonBlank(name, ERROR_DATASOURCE_BLANK);
requireNonNull(instance, "Argument 'instance' must not be null");
Map<String, Object> config = narrowConfig(name);
event("DataSourceDisconnectStart", asList(name, config, instance));

0 comments on commit 4003528

Please sign in to comment.
You can’t perform that action at this time.