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  
54  @Configuration
55  @EnableScheduling
56  @EnableJpaRepositories(basePackages = "ca.uhn.fhir.jpa.dao.data")
57  public abstract class BaseConfig implements SchedulingConfigurer {
58  
59  	public static final String TASK_EXECUTOR_NAME = "hapiJpaTaskExecutor";
60  
61  	@Autowired
62  	protected Environment myEnv;
63  
64  	@Override
65  	public void configureTasks(@Nonnull ScheduledTaskRegistrar theTaskRegistrar) {
66  		theTaskRegistrar.setTaskScheduler(taskScheduler());
67  	}
68  
69  	@Bean(autowire = Autowire.BY_TYPE)
70  	public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
71  		return new DatabaseBackedPagingProvider();
72  	}
73  
74  	/**
75  	 * This method should be overridden to provide an actual completed
76  	 * bean, but it provides a partially completed entity manager
77  	 * factory with HAPI FHIR customizations
78  	 */
79  	protected LocalContainerEntityManagerFactoryBean entityManagerFactory() {
80  		LocalContainerEntityManagerFactoryBean retVal = new LocalContainerEntityManagerFactoryBean();
81  		configureEntityManagerFactory(retVal, fhirContext());
82  		return retVal;
83  	}
84  
85  	public abstract FhirContext fhirContext();
86  
87  	@Bean
88  	public HibernateExceptionTranslator hibernateExceptionTranslator() {
89  		return new HibernateExceptionTranslator();
90  	}
91  
92  	@Bean
93  	public HibernateJpaDialect hibernateJpaDialectInstance() {
94  		return new HibernateJpaDialect();
95  	}
96  
97  	@Bean
98  	public IReindexController reindexController() {
99  		return new ReindexController();
100 	}
101 
102 	@Bean()
103 	public ScheduledExecutorFactoryBean scheduledExecutorService() {
104 		ScheduledExecutorFactoryBean b = new ScheduledExecutorFactoryBean();
105 		b.setPoolSize(5);
106 		return b;
107 	}
108 
109 	@Bean(autowire = Autowire.BY_TYPE)
110 	public ISearchCoordinatorSvc searchCoordinatorSvc() {
111 		return new SearchCoordinatorSvcImpl();
112 	}
113 
114 	@Bean
115 	public ISearchParamPresenceSvc searchParamPresenceSvc() {
116 		return new SearchParamPresenceSvcImpl();
117 	}
118 
119 	@Bean(autowire = Autowire.BY_TYPE)
120 	public IStaleSearchDeletingSvc staleSearchDeletingSvc() {
121 		return new StaleSearchDeletingSvcImpl();
122 	}
123 
124 	/**
125 	 * Note: If you're going to use this, you need to provide a bean
126 	 * of type {@link ca.uhn.fhir.jpa.subscription.email.IEmailSender}
127 	 * in your own Spring config
128 	 */
129 	@Bean
130 	@Lazy
131 	public SubscriptionEmailInterceptor subscriptionEmailInterceptor() {
132 		return new SubscriptionEmailInterceptor();
133 	}
134 
135 	@Bean
136 	@Lazy
137 	public SubscriptionRestHookInterceptor subscriptionRestHookInterceptor() {
138 		return new SubscriptionRestHookInterceptor();
139 	}
140 
141 	@Bean
142 	@Lazy
143 	public SubscriptionWebsocketInterceptor subscriptionWebsocketInterceptor() {
144 		return new SubscriptionWebsocketInterceptor();
145 	}
146 
147 	@Bean(name = TASK_EXECUTOR_NAME)
148 	public TaskScheduler taskScheduler() {
149 		ConcurrentTaskScheduler retVal = new ConcurrentTaskScheduler();
150 		retVal.setConcurrentExecutor(scheduledExecutorService().getObject());
151 		retVal.setScheduledExecutor(scheduledExecutorService().getObject());
152 		return retVal;
153 	}
154 
155 	public static void configureEntityManagerFactory(LocalContainerEntityManagerFactoryBean theFactory, FhirContext theCtx) {
156 		theFactory.setJpaDialect(hibernateJpaDialect(theCtx.getLocalizer()));
157 		theFactory.setPackagesToScan("ca.uhn.fhir.jpa.entity");
158 		theFactory.setPersistenceProvider(new HibernatePersistenceProvider());
159 	}
160 
161 	private static HibernateJpaDialect hibernateJpaDialect(HapiLocalizer theLocalizer) {
162 		return new HapiFhirHibernateJpaDialect(theLocalizer);
163 	}
164 
165 	/**
166 	 * This lets the "@Value" fields reference properties from the properties file
167 	 */
168 	@Bean
169 	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
170 		return new PropertySourcesPlaceholderConfigurer();
171 	}
172 
173 }