From 6c321b83c82913f961cf74a9af635edab4cf0ee9 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 16 Oct 2015 15:41:28 -0400 Subject: [PATCH] Get examples working with Java config --- .../ca/uhn/fhir/jpa/config/BaseConfig.java | 14 +++ .../uhn/fhir/jpa/config/BaseDstu2Config.java | 38 +----- .../fhir/jpa/config/WebsocketDstu2Config.java | 37 ++++++ ...onsRequireManualActivationInterceptor.java | 1 + .../ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java | 2 +- .../ca/uhn/fhir/jpa/demo/AppCtxConfig.java | 110 ++++++++++++++++++ .../ca/uhn/fhir/jpa/demo/JpaServerDemo.java | 5 +- .../src/main/webapp/WEB-INF/web.xml | 16 +-- .../ca/uhn/fhirtest/TestRestfulServer.java | 24 ++-- .../ca/uhn/fhirtest/config/CommonConfig.java | 24 ++++ .../uhn/fhirtest/config/DbServerConfig.java | 19 +++ .../uhn/fhirtest/config/TestDstu1Config.java | 90 ++++++++++++++ .../uhn/fhirtest/config/TestDstu2Config.java | 110 ++++++++++++++++++ ...pi-fhir-server-database-config-dstu1.xml_} | 0 ...pi-fhir-server-database-config-dstu2.xml_} | 0 .../src/main/webapp/WEB-INF/web.xml | 23 +++- 16 files changed, 446 insertions(+), 67 deletions(-) create mode 100644 hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/WebsocketDstu2Config.java create mode 100644 hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/AppCtxConfig.java create mode 100644 hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java create mode 100644 hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/DbServerConfig.java create mode 100644 hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu1Config.java create mode 100644 hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java rename hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/{hapi-fhir-server-database-config-dstu1.xml => hapi-fhir-server-database-config-dstu1.xml_} (100%) rename hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/{hapi-fhir-server-database-config-dstu2.xml => hapi-fhir-server-database-config-dstu2.xml_} (100%) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java index 383c5f1c5a3..f7feb361d22 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java @@ -2,10 +2,13 @@ package ca.uhn.fhir.jpa.config; import javax.annotation.Resource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; @@ -24,6 +27,17 @@ public class BaseConfig implements SchedulingConfigurer { private static FhirContext ourFhirContextDstu1; private static FhirContext ourFhirContextDstu2Hl7Org; + @Autowired + protected Environment myEnv; + + /** + * This lets the "@Value" fields reference properties from the properties file + */ + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + @Resource private ApplicationContext myAppCtx; diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java index f9878a3cdb6..d06555616fe 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java @@ -4,32 +4,23 @@ import org.springframework.beans.factory.annotation.Autowire; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.scheduling.TaskScheduler; -import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.config.annotation.EnableWebSocket; -import org.springframework.web.socket.config.annotation.WebSocketConfigurer; -import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; -import org.springframework.web.socket.handler.PerConnectionWebSocketHandler; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.FhirSearchDao; import ca.uhn.fhir.jpa.dao.IFhirSystemDao; import ca.uhn.fhir.jpa.dao.ISearchDao; -import ca.uhn.fhir.jpa.subscription.SubscriptionWebsocketHandler; @Configuration @EnableTransactionManagement -@EnableWebSocket() -public class BaseDstu2Config extends BaseConfig implements WebSocketConfigurer { +public class BaseDstu2Config extends BaseConfig { @Bean @Primary public FhirContext defaultFhirContext() { return fhirContextDstu2(); } - + @Bean(name = "mySystemDaoDstu2", autowire = Autowire.BY_NAME) public IFhirSystemDao systemDaoDstu2() { ca.uhn.fhir.jpa.dao.FhirSystemDaoDstu2 retVal = new ca.uhn.fhir.jpa.dao.FhirSystemDaoDstu2(); @@ -49,35 +40,10 @@ public class BaseDstu2Config extends BaseConfig implements WebSocketConfigurer { return retVal; } - @Override - public void registerWebSocketHandlers(WebSocketHandlerRegistry theRegistry) { - theRegistry.addHandler(subscriptionWebSocketHandler(), "/websocket/dstu2"); - } - - @Bean(autowire = Autowire.BY_TYPE) - public WebSocketHandler subscriptionWebSocketHandler() { - return new PerConnectionWebSocketHandler(SubscriptionWebsocketHandler.class); - } - - @Bean - public TaskScheduler websocketTaskScheduler() { - ThreadPoolTaskScheduler retVal = new ThreadPoolTaskScheduler(); - retVal.setPoolSize(5); - return retVal; - } - @Bean(autowire = Autowire.BY_TYPE) public ISearchDao searchDao() { FhirSearchDao searchDao = new FhirSearchDao(); return searchDao; } - // - } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/WebsocketDstu2Config.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/WebsocketDstu2Config.java new file mode 100644 index 00000000000..df988da814f --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/WebsocketDstu2Config.java @@ -0,0 +1,37 @@ +package ca.uhn.fhir.jpa.config; + +import org.springframework.beans.factory.annotation.Autowire; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; +import org.springframework.web.socket.handler.PerConnectionWebSocketHandler; + +import ca.uhn.fhir.jpa.subscription.SubscriptionWebsocketHandler; + +@Configuration +@EnableWebSocket() +public class WebsocketDstu2Config implements WebSocketConfigurer { + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry theRegistry) { + theRegistry.addHandler(subscriptionWebSocketHandler(), "/websocket/dstu2"); + } + + @Bean(autowire = Autowire.BY_TYPE) + public WebSocketHandler subscriptionWebSocketHandler() { + return new PerConnectionWebSocketHandler(SubscriptionWebsocketHandler.class); + } + + @Bean + public TaskScheduler websocketTaskScheduler() { + ThreadPoolTaskScheduler retVal = new ThreadPoolTaskScheduler(); + retVal.setPoolSize(5); + return retVal; + } + +} diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/SubscriptionsRequireManualActivationInterceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/SubscriptionsRequireManualActivationInterceptor.java index 0767a9ef0e2..d48bae0fc7a 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/SubscriptionsRequireManualActivationInterceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/SubscriptionsRequireManualActivationInterceptor.java @@ -74,6 +74,7 @@ public class SubscriptionsRequireManualActivationInterceptor extends Interceptor if (theProcessedRequest.getResourceType().equals("Subscription")) { verifyStatusOk(theOperation, theProcessedRequest); } + break; default: break; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java index 0b3183a3daa..d9a8917f5e0 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java @@ -69,7 +69,7 @@ import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; //@formatter:off @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes= {TestDstu2Config.class}) +@ContextConfiguration(classes= {TestDstu2Config.class, ca.uhn.fhir.jpa.config.WebsocketDstu2Config.class}) //@formatter:on public abstract class BaseJpaDstu2Test extends BaseJpaTest { diff --git a/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/AppCtxConfig.java b/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/AppCtxConfig.java new file mode 100644 index 00000000000..f013552be42 --- /dev/null +++ b/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/AppCtxConfig.java @@ -0,0 +1,110 @@ +package ca.uhn.fhir.jpa.demo; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.time.DateUtils; +import org.hibernate.jpa.HibernatePersistenceProvider; +import org.springframework.beans.factory.annotation.Autowire; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu2; +import ca.uhn.fhir.jpa.dao.DaoConfig; +import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptor; +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; +import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor; + +@Configuration +@EnableTransactionManagement() +public class AppCtxConfig extends BaseJavaConfigDstu2 { + + /** + * Configure FHIR properties around the the JPA server via this bean + */ + @Bean() + public DaoConfig daoConfig() { + DaoConfig retVal = new DaoConfig(); + retVal.setSubscriptionEnabled(true); + retVal.setSubscriptionPollDelay(5000); + retVal.setSubscriptionPurgeInactiveAfterMillis(DateUtils.MILLIS_PER_HOUR); + retVal.setAllowMultipleDelete(true); + return retVal; + } + + /** + * The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a + * directory called "jpaserver_derby_files". + * + * A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource. + */ + @Bean(destroyMethod = "close") + public DataSource dataSource() { + BasicDataSource retVal = new BasicDataSource(); + retVal.setDriver(new org.apache.derby.jdbc.EmbeddedDriver()); + retVal.setUrl("jdbc:derby:directory:target/jpaserver_derby_files;create=true"); + retVal.setUsername(""); + retVal.setPassword(""); + return retVal; + } + + @Bean() + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + LocalContainerEntityManagerFactoryBean retVal = new LocalContainerEntityManagerFactoryBean(); + retVal.setPersistenceUnitName("HAPI_PU"); + retVal.setDataSource(dataSource()); + retVal.setPackagesToScan("ca.uhn.fhir.jpa.entity"); + retVal.setPersistenceProvider(new HibernatePersistenceProvider()); + retVal.setJpaProperties(jpaProperties()); + retVal.afterPropertiesSet(); + return retVal; + } + + private Properties jpaProperties() { + Properties extraProperties = new Properties(); + extraProperties.put("hibernate.format_sql", "true"); + extraProperties.put("hibernate.show_sql", "false"); + extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.jdbc.batch_size", "20"); + extraProperties.put("hibernate.cache.use_query_cache", "false"); + extraProperties.put("hibernate.cache.use_second_level_cache", "false"); + extraProperties.put("hibernate.cache.use_structured_entries", "false"); + extraProperties.put("hibernate.cache.use_minimal_puts", "false"); + extraProperties.put("hibernate.search.default.directory_provider", "filesystem"); + extraProperties.put("hibernate.search.default.indexBase", "target/lucenefiles"); + extraProperties.put("hibernate.search.lucene_version", "LUCENE_CURRENT"); + return extraProperties; + } + + /** + * Do some fancy logging to create a nice access log that has details about each incoming request. + */ + public IServerInterceptor loggingInterceptor() { + LoggingInterceptor retVal = new LoggingInterceptor(); + retVal.setLoggerName("fhirtest.access"); + retVal.setMessageFormat( + "Path[${servletPath}] Source[${requestHeader.x-forwarded-for}] Operation[${operationType} ${operationName} ${idOrResourceName}] UA[${requestHeader.user-agent}] Params[${requestParameters}] ResponseEncoding[${responseEncodingNoDefault}]"); + retVal.setLogExceptions(true); + retVal.setErrorMessageFormat("ERROR - ${requestVerb} ${requestUrl}"); + return retVal; + } + + @Bean(autowire = Autowire.BY_TYPE) + public IServerInterceptor subscriptionSecurityInterceptor() { + return new SubscriptionsRequireManualActivationInterceptor(); + } + + @Bean() + public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + JpaTransactionManager retVal = new JpaTransactionManager(); + retVal.setEntityManagerFactory(entityManagerFactory); + return retVal; + } + +} diff --git a/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/JpaServerDemo.java b/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/JpaServerDemo.java index 30b9f331d73..b0a89460307 100644 --- a/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/JpaServerDemo.java +++ b/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/JpaServerDemo.java @@ -1,5 +1,6 @@ package ca.uhn.fhir.jpa.demo; +import java.util.Collection; import java.util.List; import javax.servlet.ServletException; @@ -123,9 +124,9 @@ public class JpaServerDemo extends RestfulServer { setPagingProvider(new FifoMemoryPagingProvider(10)); /* - * Load interceptors for the server from Spring (these are defined in hapi-fhir-server-config.xml + * Load interceptors for the server from Spring (these are defined in AppCtxConfig.java) */ - List interceptorBeans = myAppCtx.getBean("myServerInterceptors", List.class); + Collection interceptorBeans = myAppCtx.getBeansOfType(IServerInterceptor.class).values(); for (IServerInterceptor interceptor : interceptorBeans) { this.registerInterceptor(interceptor); } diff --git a/hapi-fhir-jpaserver-example/src/main/webapp/WEB-INF/web.xml b/hapi-fhir-jpaserver-example/src/main/webapp/WEB-INF/web.xml index 1c1d85bee0e..e04013d82c0 100644 --- a/hapi-fhir-jpaserver-example/src/main/webapp/WEB-INF/web.xml +++ b/hapi-fhir-jpaserver-example/src/main/webapp/WEB-INF/web.xml @@ -4,17 +4,17 @@ org.springframework.web.context.ContextLoaderListener - - + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + contextConfigLocation - classpath:hapi-fhir-server-resourceproviders-dstu1.xml - classpath:hapi-fhir-server-resourceproviders-dstu2.xml - /WEB-INF/hapi-fhir-server-database-config.xml - /WEB-INF/hapi-fhir-server-config.xml - /WEB-INF/hapi-fhir-tester-application-context.xml - /WEB-INF/hapi-fhir-tester-config.xml + ca.uhn.fhir.jpa.demo.AppCtxConfig diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java index c2b9a7b1fdc..4d4f7f4baf8 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java @@ -1,7 +1,9 @@ package ca.uhn.fhirtest; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -9,11 +11,13 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.config.TestDstu1Config; import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.dao.IFhirSystemDao; import ca.uhn.fhir.jpa.provider.JpaConformanceProviderDstu1; @@ -46,7 +50,6 @@ public class TestRestfulServer extends RestfulServer { // Get the spring context from the web container (it's declared in web.xml) WebApplicationContext parentAppCtx = ContextLoaderListener.getCurrentWebApplicationContext(); - myAppCtx = parentAppCtx; // These two parmeters are also declared in web.xml String implDesc = getInitParameter("ImplementationDescription"); @@ -67,11 +70,7 @@ public class TestRestfulServer extends RestfulServer { String baseUrlProperty; switch (fhirVersionParam.trim().toUpperCase()) { case "DSTU1": { -// String[] configLocations = new String[] { -// "/hapi-fhir-server-database-config-dstu1.xml", -// "/hapi-fhir-server-resourceproviders-dstu1.xml" -// }; -// myAppCtx = new ClassPathXmlApplicationContext(configLocations, parentAppCtx); + myAppCtx = new AnnotationConfigApplicationContext(ca.uhn.fhirtest.config.TestDstu1Config.class); setFhirContext(FhirContext.forDstu1()); beans = myAppCtx.getBean("myResourceProvidersDstu1", List.class); systemProviderDstu1 = myAppCtx.getBean("mySystemProviderDstu1", JpaSystemProviderDstu1.class); @@ -84,13 +83,7 @@ public class TestRestfulServer extends RestfulServer { break; } case "DSTU2": { -// String[] configLocations = new String[] { -// "/hapi-fhir-server-database-config-dstu2.xml", -// "/hapi-fhir-server-resourceproviders-dstu2.xml", -// "/fhir-spring-subscription-config-dstu2.xml", -// "/fhir-spring-search-config-dstu2.xml" -// }; -// myAppCtx = new ClassPathXmlApplicationContext(configLocations, parentAppCtx); + myAppCtx = parentAppCtx; setFhirContext(FhirContext.forDstu2()); beans = myAppCtx.getBean("myResourceProvidersDstu2", List.class); systemProviderDstu2 = myAppCtx.getBean("mySystemProviderDstu2", JpaSystemProviderDstu2.class); @@ -100,7 +93,6 @@ public class TestRestfulServer extends RestfulServer { confProvider.setImplementationDescription(implDesc); setServerConformanceProvider(confProvider); baseUrlProperty = "fhir.baseurl.dstu2"; - registerInterceptor(myAppCtx.getBean("mySubscriptionSecurityInterceptor", SubscriptionsRequireManualActivationInterceptor.class)); break; } default: @@ -169,9 +161,9 @@ public class TestRestfulServer extends RestfulServer { setPagingProvider(new FifoMemoryPagingProvider(10)); /* - * Load interceptors for the server from Spring (these are defined in hapi-fhir-server-config.xml + * Load interceptors for the server from Spring */ - List interceptorBeans = myAppCtx.getBean("myServerInterceptors", List.class); + Collection interceptorBeans = myAppCtx.getBeansOfType(IServerInterceptor.class).values(); for (IServerInterceptor interceptor : interceptorBeans) { this.registerInterceptor(interceptor); } diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java new file mode 100644 index 00000000000..a7d4ce6bc07 --- /dev/null +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java @@ -0,0 +1,24 @@ +package ca.uhn.fhirtest.config; + +import org.springframework.context.annotation.Configuration; + +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; +import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor; + +@Configuration +public class CommonConfig { + + /** + * Do some fancy logging to create a nice access log that has details about each incoming request. + */ + public IServerInterceptor loggingInterceptor() { + LoggingInterceptor retVal = new LoggingInterceptor(); + retVal.setLoggerName("fhirtest.access"); + retVal.setMessageFormat( + "Path[${servletPath}] Source[${requestHeader.x-forwarded-for}] Operation[${operationType} ${operationName} ${idOrResourceName}] UA[${requestHeader.user-agent}] Params[${requestParameters}] ResponseEncoding[${responseEncodingNoDefault}]"); + retVal.setLogExceptions(true); + retVal.setErrorMessageFormat("ERROR - ${requestVerb} ${requestUrl}"); + return retVal; + } + +} diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/DbServerConfig.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/DbServerConfig.java new file mode 100644 index 00000000000..85631422992 --- /dev/null +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/DbServerConfig.java @@ -0,0 +1,19 @@ +package ca.uhn.fhirtest.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import ca.uhn.fhirtest.DerbyNetworkServer; + +@Configuration +public class DbServerConfig { + + @Bean + public DerbyNetworkServer dbServer() { + return new DerbyNetworkServer(); + // For mysql + // return new ca.uhn.fhirtest.MySqlServer(); + } + + +} diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu1Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu1Config.java new file mode 100644 index 00000000000..add4252510d --- /dev/null +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu1Config.java @@ -0,0 +1,90 @@ +package ca.uhn.fhirtest.config; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.apache.commons.dbcp2.BasicDataSource; +import org.hibernate.jpa.HibernatePersistenceProvider; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.Import; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu1; +import ca.uhn.fhir.jpa.dao.DaoConfig; + +@Configuration +@Import(CommonConfig.class) +@EnableTransactionManagement() +public class TestDstu1Config extends BaseJavaConfigDstu1 { + + @Value("${fhir.db.location}") + private String myFhirDbLocation; + + /** + * This lets the "@Value" fields reference properties from the properties file + */ + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Bean() + public DaoConfig daoConfig() { + DaoConfig retVal = new DaoConfig(); + retVal.setSubscriptionEnabled(false); + retVal.setAllowMultipleDelete(false); + return retVal; + } + + @Bean(name = "myPersistenceDataSourceDstu1", destroyMethod = "close") + public DataSource dataSource() { + BasicDataSource retVal = new BasicDataSource(); + retVal.setDriver(new org.apache.derby.jdbc.ClientDriver()); + // retVal.setUrl("jdbc:derby:directory:" + myFhirDbLocation + ";create=true"); + retVal.setUrl("jdbc:derby://localhost:1527/" + myFhirDbLocation + ";create=true"); + retVal.setUsername("SA"); + retVal.setPassword("SA"); + return retVal; + } + + @Bean() + public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + JpaTransactionManager retVal = new JpaTransactionManager(); + retVal.setEntityManagerFactory(entityManagerFactory); + return retVal; + } + + @Bean() + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + LocalContainerEntityManagerFactoryBean retVal = new LocalContainerEntityManagerFactoryBean(); + retVal.setPersistenceUnitName("PU_HapiFhirJpaDstu1"); + retVal.setDataSource(dataSource()); + retVal.setPackagesToScan("ca.uhn.fhir.jpa.entity"); + retVal.setPersistenceProvider(new HibernatePersistenceProvider()); + retVal.setJpaProperties(jpaProperties()); + retVal.afterPropertiesSet(); + return retVal; + } + + private Properties jpaProperties() { + Properties extraProperties = new Properties(); + extraProperties.put("hibernate.format_sql", "true"); + extraProperties.put("hibernate.show_sql", "false"); + extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.jdbc.batch_size", "20"); + extraProperties.put("hibernate.cache.use_query_cache", "false"); + extraProperties.put("hibernate.cache.use_second_level_cache", "false"); + extraProperties.put("hibernate.cache.use_structured_entries", "false"); + extraProperties.put("hibernate.cache.use_minimal_puts", "false"); + return extraProperties; + } + +} diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java new file mode 100644 index 00000000000..49572279c3b --- /dev/null +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java @@ -0,0 +1,110 @@ +package ca.uhn.fhirtest.config; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.time.DateUtils; +import org.hibernate.jpa.HibernatePersistenceProvider; +import org.springframework.beans.factory.annotation.Autowire; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySources; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu2; +import ca.uhn.fhir.jpa.dao.DaoConfig; +import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptor; +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; + +@Configuration +@Import(CommonConfig.class) +@EnableTransactionManagement() +public class TestDstu2Config extends BaseJavaConfigDstu2 { + + @Value("${fhir.db.location.dstu2}") + private String myFhirDbLocation; + + @Value("${fhir.lucene.location.dstu2}") + private String myFhirLuceneLocation; + + /** + * This lets the "@Value" fields reference properties from the properties file + */ + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Bean() + public DaoConfig daoConfig() { + DaoConfig retVal = new DaoConfig(); + retVal.setSubscriptionEnabled(true); + retVal.setSubscriptionPollDelay(5000); + retVal.setSubscriptionPurgeInactiveAfterMillis(DateUtils.MILLIS_PER_HOUR); + retVal.setAllowMultipleDelete(true); + return retVal; + } + + @Bean(name = "myPersistenceDataSourceDstu1", destroyMethod = "close") + @DependsOn("dbServer") + public DataSource dataSource() { + BasicDataSource retVal = new BasicDataSource(); + retVal.setDriver(new org.apache.derby.jdbc.ClientDriver()); + // retVal.setUrl("jdbc:derby:directory:" + myFhirDbLocation + ";create=true"); + retVal.setUrl("jdbc:derby://localhost:1527/" + myFhirDbLocation + ";create=true"); + retVal.setUsername("SA"); + retVal.setPassword("SA"); + return retVal; + } + + @Bean() + public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + JpaTransactionManager retVal = new JpaTransactionManager(); + retVal.setEntityManagerFactory(entityManagerFactory); + return retVal; + } + + @Bean() + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + LocalContainerEntityManagerFactoryBean retVal = new LocalContainerEntityManagerFactoryBean(); + retVal.setPersistenceUnitName("PU_HapiFhirJpaDstu2"); + retVal.setDataSource(dataSource()); + retVal.setPackagesToScan("ca.uhn.fhir.jpa.entity"); + retVal.setPersistenceProvider(new HibernatePersistenceProvider()); + retVal.setJpaProperties(jpaProperties()); + retVal.afterPropertiesSet(); + return retVal; + } + + private Properties jpaProperties() { + Properties extraProperties = new Properties(); + extraProperties.put("hibernate.format_sql", "true"); + extraProperties.put("hibernate.show_sql", "false"); + extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.jdbc.batch_size", "20"); + extraProperties.put("hibernate.cache.use_query_cache", "false"); + extraProperties.put("hibernate.cache.use_second_level_cache", "false"); + extraProperties.put("hibernate.cache.use_structured_entries", "false"); + extraProperties.put("hibernate.cache.use_minimal_puts", "false"); + extraProperties.put("hibernate.search.default.directory_provider" ,"filesystem"); + extraProperties.put("hibernate.search.default.indexBase", myFhirLuceneLocation); + extraProperties.put("hibernate.search.lucene_version","LUCENE_CURRENT"); + return extraProperties; + } + + @Bean(autowire=Autowire.BY_TYPE) + public IServerInterceptor subscriptionSecurityInterceptor() { + return new SubscriptionsRequireManualActivationInterceptor(); + } + + +} diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu1.xml b/hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu1.xml_ similarity index 100% rename from hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu1.xml rename to hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu1.xml_ diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu2.xml b/hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu2.xml_ similarity index 100% rename from hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu2.xml rename to hapi-fhir-jpaserver-uhnfhirtest/src/main/resources/hapi-fhir-server-database-config-dstu2.xml_ diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/webapp/WEB-INF/web.xml b/hapi-fhir-jpaserver-uhnfhirtest/src/main/webapp/WEB-INF/web.xml index d0ef3f252fd..bb94f7332b8 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/webapp/WEB-INF/web.xml +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/webapp/WEB-INF/web.xml @@ -4,8 +4,23 @@ org.springframework.web.context.ContextLoaderListener - - + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + + ca.uhn.fhirtest.config.DbServerConfig + ca.uhn.fhirtest.config.TestDstu2Config + ca.uhn.fhir.jpa.config.WebsocketDstu2Config + + + + +