213 shaares
1 résultat
taggé
string
Ce binding a été créé pour le cas d'une appli Spring-Boot, Spring-security, se lançant dans un Websphere 8.5.5.
Bean de configuration de l'application récupérant les variables JNDI
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
/**
* The Spring configuration for the application.
*/
@Configuration
@PropertySource(
encoding = "UTF-8",
value = "classpath:application.properties"
)
public class ApplicationConfig extends SpringConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfig.class.getCanonicalName());
private static final String JAVA_COMP_ENV = "java:comp/env";
private static final String PATH_STORAGE_FOLDER = "path/toto";
private static final String URL_SERVICE_URL = "url/ServiceUrl";
private static final String MY_DATASOURCE_JNDI_NAME = "my.datasource.jndi.name";
/**
* Load the configuration.
*
* @param environment the Spring environment.
*/
public ApplicationConfig(Environment environment) {
super(environment);
}
/**
* My database datasource.
*
* @return the datasource JNDI name.
*/
@Bean
public String myDatasourceJndiName() {
LOGGER.info("Database JNDI name set to {}", super.getProperty(MY_DATASOURCE_JNDI_NAME, String.class));
return super.getProperty(MY_DATASOURCE_JNDI_NAME, String.class);
}
/**
* Return the JNDI value associated a simple string.
*/
@Bean
public String theNameOfMyBean() {
LOGGER.info("Lonading simple string JNDI variable");
String jndiFoundPath = null;
try {
Context envEntryContext = (Context) new InitialContext().lookup(JAVA_COMP_ENV);
String jndiFoundVariable = (String) envEntryContext.lookup(PATH_STORAGE_FOLDER);
LOGGER.info("Binding JNDI {} gave {}", PATH_STORAGE_FOLDER, jndiFoundVariable);
return jndiFoundVariable;
} catch (NamingException e) {
LOGGER.info("Binding JNDI {} gave null (Naming exception).", PATH_STORAGE_FOLDER);
LOGGER.trace("raised error", e);
//Here you can default value or throw exception
}
LOGGER.info("Binding JNDI Bel storage folder path gave {}", jndiFoundPath);
if (jndiFoundPath == null || "".equals(jndiFoundPath)) {
LOGGER.info("Binding JNDI Bel storage folder path gave null.");
//Here you can default value or throw exception
}
return jndiFoundPath;
}
/**
* Return the JNDI value associated a URL.
*/
@Bean
public String servicesUrl() {
LOGGER.info("Lonading service URL JNDI variable");
String jndiFoundPath = null;
try {
Context envEntryContext = (Context) new InitialContext().lookup(JAVA_COMP_ENV);
URL jndiFoundUrl = (URL) envEntryContext.lookup(URL_SERVICE_URL);
LOGGER.info("Binding JNDI service url gave {}", jndiFoundUrl);
jndiFoundPath = jndiFoundUrl.toString();
} catch (NamingException e) {
LOGGER.info("Binding JNDI ws url gave null.");
LOGGER.trace("raised error", e);
//Here you can default value or throw exception
}
if (jndiFoundPath == null || "".equals(jndiFoundPath)) {
LOGGER.info("Binding JNDI Bel storage folder path gave null.");
//Here you can default value or throw exception
}
return jndiFoundPath;
}
}
Sample pour la datasource :
/**
* Load the Data source for the database.
*
* @return the data source.
*/
@Bean
@Primary
public DataSource dataSource() {
LOGGER.info("Load datasource");
try {
DataSource jndiDataSource = new JndiDataSourceLookup().getDataSource(this.getContext().getBean("myDatasourceJndiName", String.class));
if (jndiDataSource == null) {
LOGGER.info("DB - JNDI datasource returned null object - Default datasource");
jndiDataSource = new BoneCPDataSource(this.connectionPoolConfig());
}
LOGGER.info("DB - JNDI datasource is OK.");
return jndiDataSource;
} catch (BeansException | DataSourceLookupFailureException e) {
LOGGER.info("Error when lookup JNDI datasource => Bone-CP DataSource loaded");
return new BoneCPDataSource(this.connectionPoolConfig());
}
}
Le web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="reviewit-poc">
<display-name>TheNameYouWant</display-name>
<resource-ref>
<res-ref-name>url/ServiceUrl</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
<resource-env-ref>
<description>BEL Storage Folder</description>
<resource-env-ref-name>path/toto</resource-env-ref-name>
<resource-env-ref-type>java.lang.String</resource-env-ref-type>
</resource-env-ref>
<resource-ref>
<res-ref-name>jdbc/myDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
</web-app>