Remove checked exceptions from @PostConstructs

Update all @PostConstruct methods to ensure that they don't throw
checked exceptions. Required to allow deployment of Spring Boot
applications on Glassfish.

Fixes gh-868
This commit is contained in:
Phillip Webb 2014-05-15 15:47:44 +01:00
parent 08a6efff46
commit fa7da5925b
5 changed files with 43 additions and 19 deletions

View File

@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -214,7 +213,7 @@ public class CrshAutoConfiguration {
} }
@PostConstruct @PostConstruct
public void init() throws Exception { public void init() {
FS commandFileSystem = createFileSystem(this.properties FS commandFileSystem = createFileSystem(this.properties
.getCommandPathPatterns()); .getCommandPathPatterns());
FS configurationFileSystem = createFileSystem(this.properties FS configurationFileSystem = createFileSystem(this.properties
@ -232,8 +231,7 @@ public class CrshAutoConfiguration {
start(context); start(context);
} }
protected FS createFileSystem(String[] pathPatterns) throws IOException, protected FS createFileSystem(String[] pathPatterns) {
URISyntaxException {
Assert.notNull(pathPatterns, "PathPatterns must not be null"); Assert.notNull(pathPatterns, "PathPatterns must not be null");
FS fileSystem = new FS(); FS fileSystem = new FS();
for (String pathPattern : pathPatterns) { for (String pathPattern : pathPatterns) {

View File

@ -87,10 +87,15 @@ public class BasicBatchConfigurer implements BatchConfigurer {
} }
@PostConstruct @PostConstruct
public void initialize() throws Exception { public void initialize() {
this.transactionManager = createTransactionManager(); try {
this.jobRepository = createJobRepository(); this.transactionManager = createTransactionManager();
this.jobLauncher = createJobLauncher(); this.jobRepository = createJobRepository();
this.jobLauncher = createJobLauncher();
}
catch (Exception ex) {
throw new IllegalStateException("Unable to initialize Spring Batch", ex);
}
} }
private JobLauncher createJobLauncher() throws Exception { private JobLauncher createJobLauncher() throws Exception {

View File

@ -28,6 +28,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -58,10 +59,9 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
} }
@PostConstruct @PostConstruct
protected void initialize() throws Exception { protected void initialize() {
if (this.enabled) { if (this.enabled) {
String platform = DatabaseType.fromMetaData(this.dataSource).toString() String platform = getDatabaseType();
.toLowerCase();
if ("hsql".equals(platform)) { if ("hsql".equals(platform)) {
platform = "hsqldb"; platform = "hsqldb";
} }
@ -78,4 +78,13 @@ public class BatchDatabaseInitializer implements EnvironmentAware {
} }
} }
private String getDatabaseType() {
try {
return DatabaseType.fromMetaData(this.dataSource).toString().toLowerCase();
}
catch (MetaDataAccessException ex) {
throw new IllegalStateException("Unable to detect database type", ex);
}
}
} }

View File

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -86,7 +87,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
} }
@PostConstruct @PostConstruct
protected void initialize() throws Exception { protected void initialize() {
boolean initialize = this.datasourceProperties.getProperty("initialize", boolean initialize = this.datasourceProperties.getProperty("initialize",
Boolean.class, true); Boolean.class, true);
if (this.dataSource == null || !initialize) { if (this.dataSource == null || !initialize) {
@ -101,11 +102,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
+ ".sql,classpath*:schema.sql,classpath*:data.sql"; + ".sql,classpath*:schema.sql,classpath*:data.sql";
} }
List<Resource> resources = new ArrayList<Resource>(); List<Resource> resources = getSchemaResources(schema);
for (String schemaLocation : StringUtils.commaDelimitedListToStringArray(schema)) {
resources.addAll(Arrays.asList(this.applicationContext
.getResources(schemaLocation)));
}
boolean continueOnError = this.datasourceProperties.getProperty( boolean continueOnError = this.datasourceProperties.getProperty(
"continueOnError", Boolean.class, false); "continueOnError", Boolean.class, false);
@ -125,6 +122,21 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
} }
} }
private List<Resource> getSchemaResources(String schema) {
List<Resource> resources = new ArrayList<Resource>();
for (String schemaLocation : StringUtils.commaDelimitedListToStringArray(schema)) {
try {
resources.addAll(Arrays.asList(this.applicationContext
.getResources(schemaLocation)));
}
catch (IOException ex) {
throw new IllegalStateException("Unable to load resource from "
+ schemaLocation, ex);
}
}
return resources;
}
/** /**
* Determines if the {@code dataSource} being used by Spring was created from * Determines if the {@code dataSource} being used by Spring was created from
* {@link EmbeddedDataSourceConfiguration}. * {@link EmbeddedDataSourceConfiguration}.

View File

@ -32,7 +32,7 @@ import com.mongodb.Mongo;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Mongo. * {@link EnableAutoConfiguration Auto-configuration} for Mongo.
* *
* @author Dave Syer * @author Dave Syer
* @author Oliver Gierke * @author Oliver Gierke
* @author Phillip Webb * @author Phillip Webb
@ -48,7 +48,7 @@ public class MongoAutoConfiguration {
private Mongo mongo; private Mongo mongo;
@PreDestroy @PreDestroy
public void close() throws UnknownHostException { public void close() {
if (this.mongo != null) { if (this.mongo != null) {
this.mongo.close(); this.mongo.close();
} }