blob: 4f276171059300be8b71629ab4bd2e8275cad043 [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.config;
2
Michael Hanlf21773f2015-10-16 23:02:31 +02003import de.ids_mannheim.korap.interfaces.AuthenticationIface;
4import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface;
5import de.ids_mannheim.korap.interfaces.EncryptionIface;
6import de.ids_mannheim.korap.interfaces.db.*;
Michael Hanl1e18cb42015-08-06 20:57:35 +02007import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler;
8import org.springframework.beans.factory.NoSuchBeanDefinitionException;
Michael Hanlca740d72015-06-16 10:04:58 +02009import org.springframework.context.ApplicationContext;
10import org.springframework.context.support.ClassPathXmlApplicationContext;
11import org.springframework.context.support.FileSystemXmlApplicationContext;
12
Michael Hanlf0785322015-11-13 16:14:45 +010013import java.util.Arrays;
Michael Hanl1e18cb42015-08-06 20:57:35 +020014import java.util.HashSet;
15import java.util.Set;
16
Michael Hanlca740d72015-06-16 10:04:58 +020017/**
18 * User: hanl
19 * Date: 10/9/13
20 * Time: 11:20 AM
21 */
22public class BeanConfiguration {
23
Michael Hanlf0785322015-11-13 16:14:45 +010024 private static final String CONFIG_FILE = "light-config.xml";
Michael Hanl1e18cb42015-08-06 20:57:35 +020025 public static final String KUSTVAKT_DB = "kustvakt_db";
Michael Hanlca740d72015-06-16 10:04:58 +020026
Michael Hanl1e18cb42015-08-06 20:57:35 +020027 public static final String KUSTVAKT_ENCRYPTION = "kustvakt_encryption";
28 public static final String KUSTVAKT_AUDITING = "kustvakt_auditing";
29 public static final String KUSTVAKT_CONFIG = "kustvakt_config";
30
Michael Hanl482f30d2015-09-25 12:39:46 +020031 public static final String KUSTVAKT_AUTHENTICATION_MANAGER = "kustvakt_authenticationmanager";
32 public static final String KUSTVAKT_USERDB = "kustvakt_userdb";
33 public static final String KUSTVAKT_POLICIES = "kustvakt_policies";
34
Michael Hanl0f6ffd72015-08-27 19:23:15 +020035 private static BeanHolderHelper beans;
Michael Hanl1e18cb42015-08-06 20:57:35 +020036
Michael Hanl482f30d2015-09-25 12:39:46 +020037 //todo: allow this for external plugin systems that are not kustvakt specific
Michael Hanl19390652016-01-16 11:01:24 +010038 @Deprecated
Michael Hanl0f6ffd72015-08-27 19:23:15 +020039 public static void setCustomBeansHolder(BeanHolderHelper holder) {
Michael Hanl1e18cb42015-08-06 20:57:35 +020040 ApplicationContext context = beans.context;
41 holder.context = context;
42 BeanConfiguration.beans = holder;
43 }
44
Michael Hanl0f6ffd72015-08-27 19:23:15 +020045 public static BeanHolderHelper getBeans() {
Michael Hanl1e18cb42015-08-06 20:57:35 +020046 return BeanConfiguration.beans;
47 }
48
Michael Hanl0f6ffd72015-08-27 19:23:15 +020049 @Deprecated
Michael Hanl1e18cb42015-08-06 20:57:35 +020050 public static void loadAuthenticationProviders() {
51 Set<Class<? extends AuthenticationIface>> set = KustvaktClassLoader
52 .loadSubTypes(AuthenticationIface.class);
53 Set<AuthenticationIface> set2 = new HashSet<>();
54 for (Class<? extends AuthenticationIface> i : set) {
55 try {
56 set2.add(i.newInstance());
57 }catch (InstantiationException | IllegalAccessException e) {
58 e.printStackTrace();
59 }
60 }
61 try {
62 getBeans().getAuthenticationManager().setProviders(set2);
63 }catch (RuntimeException e) {
64 // do nothing
65 }
66 }
Michael Hanlca740d72015-06-16 10:04:58 +020067
Michael Hanl0f6ffd72015-08-27 19:23:15 +020068 public static boolean hasContext() {
Michael Hanlf21773f2015-10-16 23:02:31 +020069 return beans != null && beans.context != null;
Michael Hanl0f6ffd72015-08-27 19:23:15 +020070 }
71
Michael Hanlbadd79c2015-06-19 07:41:03 +020072 public static void loadClasspathContext(String... files) {
Michael Hanl19390652016-01-16 11:01:24 +010073 if (hasContext())
74 closeApplication();
Michael Hanlf21773f2015-10-16 23:02:31 +020075
Michael Hanl19390652016-01-16 11:01:24 +010076 ApplicationContext context;
77 if (files.length == 0)
78 context = new ClassPathXmlApplicationContext(CONFIG_FILE);
79 else
80 context = new ClassPathXmlApplicationContext(files);
81
82 BeanConfiguration.beans = new BeanHolderHelper(context);
83 setManualBeans();
84
Michael Hanlca740d72015-06-16 10:04:58 +020085 }
86
Michael Hanlf0785322015-11-13 16:14:45 +010087 private static void setManualBeans() {
88 if (getBeans().getPolicyDbProvider() != null
89 && getBeans().getEncryption() != null
90 && getBeans().getResourceProvider() != null)
91 de.ids_mannheim.korap.security.ac.SecurityManager
92 .setProviders(getBeans().getPolicyDbProvider(),
93 getBeans().getEncryption(),
94 Arrays.asList(getBeans().getResourceProvider()));
95 }
96
Michael Hanlbadd79c2015-06-19 07:41:03 +020097 public static void loadFileContext(String filepath) {
Michael Hanlf21773f2015-10-16 23:02:31 +020098 if (!hasContext()) {
Michael Hanl1e18cb42015-08-06 20:57:35 +020099 ApplicationContext context = new FileSystemXmlApplicationContext(
100 "file:" + filepath);
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200101 BeanConfiguration.beans = new BeanHolderHelper(context);
Michael Hanl1e18cb42015-08-06 20:57:35 +0200102 }
103 }
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200104
105 public static void closeApplication() {
Michael Hanlf21773f2015-10-16 23:02:31 +0200106 if (hasContext())
107 beans.finish();
108 beans = null;
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200109 }
110
Michael Hanl1e18cb42015-08-06 20:57:35 +0200111 //todo: set response handler
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200112 @Deprecated
Michael Hanl1e18cb42015-08-06 20:57:35 +0200113 public static KustvaktResponseHandler getResponseHandler() {
114 return null;
Michael Hanlca740d72015-06-16 10:04:58 +0200115 }
116
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200117 public static class BeanHolderHelper {
Michael Hanlca740d72015-06-16 10:04:58 +0200118
Michael Hanl1e18cb42015-08-06 20:57:35 +0200119 private ApplicationContext context = null;
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200120 private DefaultHandler handler;
Michael Hanlca740d72015-06-16 10:04:58 +0200121
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200122 private BeanHolderHelper(ApplicationContext context) {
Michael Hanlf21773f2015-10-16 23:02:31 +0200123 this.handler = new DefaultHandler();
Michael Hanl1e18cb42015-08-06 20:57:35 +0200124 this.context = context;
Michael Hanlf21773f2015-10-16 23:02:31 +0200125 // todo: better method?!
126 KustvaktResponseHandler.init(getAuditingProvider());
Michael Hanl1e18cb42015-08-06 20:57:35 +0200127 }
Michael Hanlca740d72015-06-16 10:04:58 +0200128
Michael Hanl1e18cb42015-08-06 20:57:35 +0200129 protected <T> T getBean(Class<T> clazz) {
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200130 if (context != null) {
131 try {
132 return context.getBean(clazz);
133 }catch (NoSuchBeanDefinitionException e) {
134 // do nothing
135 }
Michael Hanl1e18cb42015-08-06 20:57:35 +0200136 }
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200137 return this.handler.getDefault(clazz);
Michael Hanl1e18cb42015-08-06 20:57:35 +0200138 }
Michael Hanlca740d72015-06-16 10:04:58 +0200139
Michael Hanl1e18cb42015-08-06 20:57:35 +0200140 protected <T> T getBean(String name) {
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200141 if (context != null) {
142 try {
143 return (T) context.getBean(name);
144 }catch (NoSuchBeanDefinitionException e) {
145 // do nothing
146 }
Michael Hanl1e18cb42015-08-06 20:57:35 +0200147 }
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200148 return (T) this.handler.getDefault(name);
Michael Hanl1e18cb42015-08-06 20:57:35 +0200149 }
150
151 public AuditingIface getAuditingProvider() {
Michael Hanlf21773f2015-10-16 23:02:31 +0200152 return (AuditingIface) getBean(KUSTVAKT_AUDITING);
Michael Hanl1e18cb42015-08-06 20:57:35 +0200153 }
154
155 public <T extends KustvaktConfiguration> T getConfiguration() {
156 return (T) getBean(KUSTVAKT_CONFIG);
157 }
158
159 public PersistenceClient getPersistenceClient() {
160 return getBean(KUSTVAKT_DB);
161 }
162
Michael Hanl482f30d2015-09-25 12:39:46 +0200163 // public AuthenticationManagerIface getAuthenticationManager() {
164 // throw new RuntimeException("!Stub");
165 // }
Michael Hanl1e18cb42015-08-06 20:57:35 +0200166
Michael Hanl482f30d2015-09-25 12:39:46 +0200167 // public EntityHandlerIface getUserDBHandler() {
168 // throw new RuntimeException("!Stub");
169 // }
Michael Hanl1e18cb42015-08-06 20:57:35 +0200170
171 public EncryptionIface getEncryption() {
172 return getBean(KUSTVAKT_ENCRYPTION);
173 }
174
Michael Hanl482f30d2015-09-25 12:39:46 +0200175 public AuthenticationManagerIface getAuthenticationManager() {
176 return getBean(KUSTVAKT_AUTHENTICATION_MANAGER);
177 }
178
179 public EntityHandlerIface getUserDBHandler() {
180 return getBean(KUSTVAKT_USERDB);
181 }
182
183 public PolicyHandlerIface getPolicyDbProvider() {
184 return getBean(KUSTVAKT_POLICIES);
185 }
186
Michael Hanlf21773f2015-10-16 23:02:31 +0200187 // todo: !!!!!!!!!!!!!!!!!!!!!!!!!!
Michael Hanl482f30d2015-09-25 12:39:46 +0200188 // todo: more specific --> collection provider, document provider, etc.
189 public ResourceOperationIface getResourceProvider() {
190 return getBean("resourceProvider");
191 }
192
Michael Hanlf21773f2015-10-16 23:02:31 +0200193 private void finish() {
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200194 this.getAuditingProvider().finish();
195 context = null;
196 }
197
Michael Hanlca740d72015-06-16 10:04:58 +0200198 }
199}