View Javadoc
1   package ca.uhn.fhir.jpa.config;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.context.FhirContext;
24  import ca.uhn.fhir.i18n.HapiLocalizer;
25  import ca.uhn.fhir.jpa.search.*;
26  import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc;
27  import ca.uhn.fhir.jpa.sp.SearchParamPresenceSvcImpl;
28  import ca.uhn.fhir.jpa.subscription.email.SubscriptionEmailInterceptor;
29  import ca.uhn.fhir.jpa.subscription.resthook.SubscriptionRestHookInterceptor;
30  import ca.uhn.fhir.jpa.subscription.websocket.SubscriptionWebsocketInterceptor;
31  import ca.uhn.fhir.jpa.util.IReindexController;
32  import ca.uhn.fhir.jpa.util.ReindexController;
33  import org.hibernate.jpa.HibernatePersistenceProvider;
34  import org.springframework.beans.factory.annotation.Autowire;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.context.annotation.Bean;
37  import org.springframework.context.annotation.Configuration;
38  import org.springframework.context.annotation.Lazy;
39  import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
40  import org.springframework.core.env.Environment;
41  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
42  import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
43  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
44  import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
45  import org.springframework.scheduling.TaskScheduler;
46  import org.springframework.scheduling.annotation.EnableScheduling;
47  import org.springframework.scheduling.annotation.SchedulingConfigurer;
48  import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
49  import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
50  import org.springframework.scheduling.config.ScheduledTaskRegistrar;
51  
52  import javax.annotation.Nonnull;
53  import java.util.concurrent.ScheduledExecutorService;
54  
55  @Configuration
56  @EnableScheduling
57  @EnableJpaRepositories(basePackages = "ca.uhn.fhir.jpa.dao.data")
58  public abstract class BaseConfig implements SchedulingConfigurer {
59  
60  	public static final String TASK_EXECUTOR_NAME = "hapiJpaTaskExecutor";
61  
62  	@Autowired
63  	protected Environment myEnv;
64  
65  	@Override
66  	public void configureTasks(@Nonnull ScheduledTaskRegistrar theTaskRegistrar) {
67  		theTaskRegistrar.setTaskScheduler(taskScheduler());
68  	}
69  
70  	@Bean(autowire = Autowire.BY_TYPE)
71  	public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
72  		return new DatabaseBackedPagingProvider();
73  	}
74  
75  	/**
76  	 * This method should be overridden to provide an actual completed
77  	 * bean, but it provides a partially completed entity manager
78  	 * factory with HAPI FHIR customizations
79  	 */
80  	protected LocalContainerEntityManagerFactoryBean entityManagerFactory() {
81  		LocalContainerEntityManagerFactoryBean retVal = new LocalContainerEntityManagerFactoryBean();
82  		configureEntityManagerFactory(retVal, fhirContext());
83  		return retVal;
84  	}
85  
86  	public abstract FhirContext fhirContext();
87  
88  	@Bean
89  	public HibernateExceptionTranslator hibernateExceptionTranslator() {
90  		return new HibernateExceptionTranslator();
91  	}
92  
93  	@Bean
94  	public HibernateJpaDialect hibernateJpaDialectInstance() {
95  		return new HibernateJpaDialect();
96  	}
97  
98  	@Bean
99  	public IReindexController reindexController() {
100 		return new ReindexController();
101 	}
102 
103 	@Bean()
104 	public ScheduledExecutorService scheduledExecutorService() {
105 		ScheduledExecutorFactoryBean b = new ScheduledExecutorFactoryBean();
106 		b.setPoolSize(5);
107 		b.afterPropertiesSet();
108 		return b.getObject();
109 	}
110 
111 	@Bean(autowire = Autowire.BY_TYPE)
112 	public ISearchCoordinatorSvc searchCoordinatorSvc() {
113 		return new SearchCoordinatorSvcImpl();
114 	}
115 
116 	@Bean
117 	public ISearchParamPresenceSvc searchParamPresenceSvc() {
118 		return new SearchParamPresenceSvcImpl();
119 	}
120 
121 	@Bean(autowire = Autowire.BY_TYPE)
122 	public IStaleSearchDeletingSvc staleSearchDeletingSvc() {
123 		return new StaleSearchDeletingSvcImpl();
124 	}
125 
126 	/**
127 	 * Note: If you're going to use this, you need to provide a bean
128 	 * of type {@link ca.uhn.fhir.jpa.subscription.email.IEmailSender}
129 	 * in your own Spring config
130 	 */
131 	@Bean
132 	@Lazy
133 	public SubscriptionEmailInterceptor subscriptionEmailInterceptor() {
134 		return new SubscriptionEmailInterceptor();
135 	}
136 
137 	@Bean
138 	@Lazy
139 	public SubscriptionRestHookInterceptor subscriptionRestHookInterceptor() {
140 		return new SubscriptionRestHookInterceptor();
141 	}
142 
143 	@Bean
144 	@Lazy
145 	public SubscriptionWebsocketInterceptor subscriptionWebsocketInterceptor() {
146 		return new SubscriptionWebsocketInterceptor();
147 	}
148 
149 	@Bean(name = TASK_EXECUTOR_NAME)
150 	public TaskScheduler taskScheduler() {
151 		ConcurrentTaskScheduler retVal = new ConcurrentTaskScheduler();
152 		retVal.setConcurrentExecutor(scheduledExecutorService());
153 		retVal.setScheduledExecutor(scheduledExecutorService());
154 		return retVal;
155 	}
156 
157 	public static void configureEntityManagerFactory(LocalContainerEntityManagerFactoryBean theFactory, FhirContext theCtx) {
158 		theFactory.setJpaDialect(hibernateJpaDialect(theCtx.getLocalizer()));
159 		theFactory.setPackagesToScan("ca.uhn.fhir.jpa.entity");
160 		theFactory.setPersistenceProvider(new HibernatePersistenceProvider());
161 	}
162 
163 	private static HibernateJpaDialect hibernateJpaDialect(HapiLocalizer theLocalizer) {
164 		return new HapiFhirHibernateJpaDialect(theLocalizer);
165 	}
166 
167 	/**
168 	 * This lets the "@Value" fields reference properties from the properties file
169 	 */
170 	@Bean
171 	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
172 		return new PropertySourcesPlaceholderConfigurer();
173 	}
174 
175 }