@DataSourceDefinition( name = "java:app/TestDataSource", className = "org.h2.jdbcx.JdbcDataSource", url = "jdbc:h2:tcp://database:1521/opt/h2-data/test", user = "sa", password = "sa" ) @Stateless public class UserRepository { @PersistenceContext private EntityManager em; public void store(User user) { em.persist(user); } }
public HammockDataSource(DataSourceDefinition dataSourceDefinition) { this.name = dataSourceDefinition.name(); this.delegate = createDelegate(dataSourceDefinition); }
private void createDataSource(@Nonnull DataSourceDefinitions dsDefs) { for (DataSourceDefinition dsDef : dsDefs.value()) { createDataSource(dsDef); if (ds != null) { return; } } }
desc.setName(defn.name()); desc.setClassName(defn.className()); if (defn.description() != null && !defn.description().equals("")) { desc.setDescription(defn.description()); if (defn.serverName() != null && !defn.serverName().equals("localhost")) { desc.setServerName(defn.serverName()); if (defn.portNumber() != -1) { desc.setPortNumber(defn.portNumber()); if (defn.databaseName() != null && !defn.databaseName().equals("")) { desc.setDatabaseName(defn.databaseName()); if (defn.url() != null && !defn.url().equals("")) { desc.setUrl(defn.url()); if (defn.user() != null && !defn.user().equals("")) { desc.setUser(defn.user()); if (defn.password() != null /*ALLOW EMPTY PASSWORDS && !defn.password().equals("")*/) { desc.setPassword(defn.password()); if (defn.isolationLevel() != -1) { desc.setIsolationLevel(String.valueOf(defn.isolationLevel())); if (defn.transactional()) { desc.setTransactional(true);
private DataSource createDelegate(DataSourceDefinition dataSourceDefinition) { HikariConfig config = new HikariConfig(); if (dataSourceDefinition.url() != null) { config.setJdbcUrl(dataSourceDefinition.url()); } if (dataSourceDefinition.user() != null) { config.setUsername(dataSourceDefinition.user()); config.setPassword(dataSourceDefinition.password()); } if (dataSourceDefinition.maxPoolSize() > 0) { config.setMaximumPoolSize(dataSourceDefinition.maxPoolSize()); } config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); return wrap(new HikariDataSource(config)); }
private void setDataSourcePropertiesFromConfiguredValues(@Nonnull DataSourceDefinition dsDef) { try { BeanInfo beanInfo = Introspector.getBeanInfo(dsClass, Object.class); PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); setProperty(properties, "url", dsDef.url()); setProperty(properties, "user", dsDef.user()); setProperty(properties, "password", dsDef.password()); } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }
protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException { DataSourceDefinitions defns = (DataSourceDefinitions) ainfo.getAnnotation(); DataSourceDefinition values[] = defns.value(); Set duplicates = new HashSet(); if(values != null && values.length >0){ for(DataSourceDefinition defn : values){ String defnName = DataSourceDefinitionDescriptor.getJavaName(defn.name()); if(duplicates.contains(defnName)){ String localString = localStrings.getLocalString( "enterprise.deployment.annotation.handlers.datasourcedefinitionsduplicates", "@DataSourceDefinitions cannot have multiple definitions with same name : '{0}'", defnName); throw new IllegalStateException(localString); /* //TODO V3 should we throw exception or return failure result ? return getFailureResult(ainfo, "@DataSourceDefinitions cannot have multiple" + " definitions with same name [ "+defnName+" ]", true ); */ }else{ duplicates.add(defnName); } DataSourceDefinitionHandler handler = new DataSourceDefinitionHandler(); handler.processAnnotation(defn, ainfo, rcContexts); } duplicates.clear(); } return getDefaultProcessedResult(); }
private void instantiateConfiguredDataSourceClass(@Nonnull DataSourceDefinition dsDef) { String className = dsDef.className(); try { //noinspection unchecked dsClass = (Class<? extends CommonDataSource>) Class.forName(className); //noinspection ClassNewInstance ds = dsClass.newInstance(); } catch (ClassNotFoundException | IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e.getCause()); } }
if (desc.getName().equals(defn.name())) { desc.setClassName(defn.className()); if (defn.description() != null && !defn.description().equals("")) { desc.setDescription(defn.description()); if (defn.serverName() != null && !defn.serverName().equals("localhost")) { desc.setServerName(defn.serverName()); if (defn.portNumber() != -1) { desc.setPortNumber(defn.portNumber()); if (defn.databaseName() != null && !defn.databaseName().equals("")) { desc.setDatabaseName(defn.databaseName()); !(desc.getPortNumber() != -1 && desc.getServerName() != null && (desc.getDatabaseName() != null))) { if (defn.url() != null && !defn.url().equals("")) { desc.setUrl(defn.url()); if (defn.user() != null && !defn.user().equals("")) { desc.setUser(defn.user()); if (defn.password() != null /*ALLOW EMPTY PASSWORDS && !defn.password().equals("")*/) { desc.setPassword(defn.password()); if (defn.isolationLevel() != -1) { desc.setIsolationLevel(String.valueOf(defn.isolationLevel()));
private void introspectClass(String location, DataSourceDefinition def) { String name = def.name(); ValueGenerator gen = bindGenerator(location, def); if (name != null && ! "".equals(name)) { bindJndi(name, gen, name); } }
public void findDataSourceDefinitions(@Observes @WithAnnotations(DataSourceDefinitions.class) ProcessAnnotatedType<?> pat) { DataSourceDefinitions annotation = pat.getAnnotatedType().getJavaClass().getAnnotation(DataSourceDefinitions.class); dataSourceDefinitions.addAll(asList(annotation.value())); }
@DataSourceDefinition( name = "java:app/TestDataSource", className = "org.h2.jdbcx.JdbcDataSource", url = "jdbc:h2:tcp://database:1521/opt/h2-data/test", user = "sa", password = "sa" ) @Stateless public class UserRepository { @PersistenceContext private EntityManager em; public void store(User user) { em.persist(user); } }
private void createDataSource(@Nonnull DataSourceDefinition dsDef) { String configuredDataSourceName = InjectionPoint.getNameFromJNDILookup(dsDef.name()); if (configuredDataSourceName.equals(dsName)) { instantiateConfiguredDataSourceClass(dsDef); setDataSourcePropertiesFromConfiguredValues(dsDef); } }
@DataSourceDefinition( className = "org.apache.derby.jdbc.EmbeddedDataSource", name = "java:global/jdbc/chapter08DS",
private DataSource findDataSource(DataSourceDefinition dsDefinition, JndiConsumer annotatedApp) { String dsDefinitionName = getJndiName(dsDefinition.name().trim()); Collection<DataSource> dataSources = annotatedApp.getDataSource(); for (DataSource ds : dataSources) { String dsName = getJndiName(ds.getName().trim()); if (dsDefinitionName.equals(dsName)) { return ds; } } return null; }
public void buildNaming(JndiConsumer specDD, XmlObject plan, Module module, Map<EARContext.Key, Object> sharedContext) throws DeploymentException { // step 1: process annotations and update deployment descriptor if ((module != null) && (module.getClassFinder() != null)) { List<Class<?>> classes; classes = module.getClassFinder().findAnnotatedClasses(DataSourceDefinitions.class); if (classes != null) { for (Class<?> clazz : classes) { DataSourceDefinitions dsDefinitions = clazz.getAnnotation(DataSourceDefinitions.class); for (DataSourceDefinition dsDefinition : dsDefinitions.value()) { processDefinition(dsDefinition, specDD); } } } classes = module.getClassFinder().findAnnotatedClasses(DataSourceDefinition.class); if (classes != null) { for (Class<?> clazz : classes) { DataSourceDefinition dsDefinition = clazz.getAnnotation(DataSourceDefinition.class); processDefinition(dsDefinition, specDD); } } } // step 2: bind all defined data sources into jndi Collection<DataSource> dataSources = specDD.getDataSource(); if (dataSources != null) { for (DataSource dataSource: dataSources) { addDataSourceGBean(module, sharedContext, dataSource); } } }
private ConfigProgram generateProgram(AnnotatedField<?> field, DataSourceDefinition def) throws ConfigException { String name = def.name(); Field javaField = field.getJavaMember(); String location = getLocation(javaField); Class<?> bindType = javaField.getType(); ValueGenerator gen = bindGenerator(location, def); if (name != null && ! "".equals(name)) bindJndi(name, gen, name); bindJndi(javaField, gen); return new FieldGeneratorProgram(javaField, gen); }
private ConfigProgram generateProgram(AnnotatedMethod<?> method, DataSourceDefinition def) throws ConfigException { String name = def.name(); Method javaMethod = method.getJavaMember(); String location = getLocation(javaMethod); ValueGenerator gen = bindGenerator(location, def); if (name != null && ! "".equals(name)) bindJndi(name, gen, name); bindJndi(javaMethod, gen); return new MethodGeneratorProgram(javaMethod, gen); }
public void registerDataSources(@Observes AfterBeanDiscovery afterBeanDiscovery) { DataSourceDefinition defaultDataSource = dataSourceDefinitions.stream() .filter(dsd -> dsd.name().equals(EntityManagerFactoryProvider.DEFAULT_EMF)) .findFirst() .orElse(null); Bean<ws.ament.hammock.jpa.DataSourceDefinition> defaultBean = beanDelegates .stream() .filter(b -> b.getName().equals(EntityManagerFactoryProvider.DEFAULT_EMF)) .findFirst() .orElse(null); dataSourceDefinitions.stream().map(dataSourceDefinitionDataSourceDefinitionFunction) .map(dataSourceDefinition -> new DataSourceDelegateBean(dataSourceDefinition.getName(), () -> dataSourceDefinition)) .forEach(afterBeanDiscovery::addBean); beanDelegates.stream().map(bean -> new DataSourceDelegateBean(bean.getName(), () -> bean.create(null))) .forEach(afterBeanDiscovery::addBean); databaseProducers.stream().map(producer -> new DataSourceDelegateBean(producer, create(producer))) .forEach(afterBeanDiscovery::addBean); if (defaultBean == null && defaultDataSource == null) { afterBeanDiscovery.addBean(new DefaultDataSourceBean()); } }