| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.interfaces.AuthenticationIface; |
| 4 | import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface; |
| 5 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| 6 | import de.ids_mannheim.korap.interfaces.db.*; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler; |
| 8 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 9 | import org.springframework.context.ApplicationContext; |
| 10 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 11 | import org.springframework.context.support.FileSystemXmlApplicationContext; |
| 12 | |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 13 | import java.util.Arrays; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 14 | import java.util.HashSet; |
| 15 | import java.util.Set; |
| 16 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 17 | /** |
| 18 | * User: hanl |
| 19 | * Date: 10/9/13 |
| 20 | * Time: 11:20 AM |
| 21 | */ |
| 22 | public class BeanConfiguration { |
| 23 | |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 24 | private static final String CONFIG_FILE = "light-config.xml"; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 25 | public static final String KUSTVAKT_DB = "kustvakt_db"; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 26 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 27 | 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 Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 31 | 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 Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 35 | private static BeanHolderHelper beans; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 36 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 37 | //todo: allow this for external plugin systems that are not kustvakt specific |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame^] | 38 | @Deprecated |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 39 | public static void setCustomBeansHolder(BeanHolderHelper holder) { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 40 | ApplicationContext context = beans.context; |
| 41 | holder.context = context; |
| 42 | BeanConfiguration.beans = holder; |
| 43 | } |
| 44 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 45 | public static BeanHolderHelper getBeans() { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 46 | return BeanConfiguration.beans; |
| 47 | } |
| 48 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 49 | @Deprecated |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 50 | 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 Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 67 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 68 | public static boolean hasContext() { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 69 | return beans != null && beans.context != null; |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| Michael Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 72 | public static void loadClasspathContext(String... files) { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame^] | 73 | if (hasContext()) |
| 74 | closeApplication(); |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 75 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame^] | 76 | 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 Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| Michael Hanl | f078532 | 2015-11-13 16:14:45 +0100 | [diff] [blame] | 87 | 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 Hanl | badd79c | 2015-06-19 07:41:03 +0200 | [diff] [blame] | 97 | public static void loadFileContext(String filepath) { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 98 | if (!hasContext()) { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 99 | ApplicationContext context = new FileSystemXmlApplicationContext( |
| 100 | "file:" + filepath); |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 101 | BeanConfiguration.beans = new BeanHolderHelper(context); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 102 | } |
| 103 | } |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 104 | |
| 105 | public static void closeApplication() { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 106 | if (hasContext()) |
| 107 | beans.finish(); |
| 108 | beans = null; |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 111 | //todo: set response handler |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 112 | @Deprecated |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 113 | public static KustvaktResponseHandler getResponseHandler() { |
| 114 | return null; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 117 | public static class BeanHolderHelper { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 118 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 119 | private ApplicationContext context = null; |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 120 | private DefaultHandler handler; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 121 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 122 | private BeanHolderHelper(ApplicationContext context) { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 123 | this.handler = new DefaultHandler(); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 124 | this.context = context; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 125 | // todo: better method?! |
| 126 | KustvaktResponseHandler.init(getAuditingProvider()); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 127 | } |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 128 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 129 | protected <T> T getBean(Class<T> clazz) { |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 130 | if (context != null) { |
| 131 | try { |
| 132 | return context.getBean(clazz); |
| 133 | }catch (NoSuchBeanDefinitionException e) { |
| 134 | // do nothing |
| 135 | } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 136 | } |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 137 | return this.handler.getDefault(clazz); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 138 | } |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 139 | |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 140 | protected <T> T getBean(String name) { |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 141 | if (context != null) { |
| 142 | try { |
| 143 | return (T) context.getBean(name); |
| 144 | }catch (NoSuchBeanDefinitionException e) { |
| 145 | // do nothing |
| 146 | } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 147 | } |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 148 | return (T) this.handler.getDefault(name); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | public AuditingIface getAuditingProvider() { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 152 | return (AuditingIface) getBean(KUSTVAKT_AUDITING); |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 153 | } |
| 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 Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 163 | // public AuthenticationManagerIface getAuthenticationManager() { |
| 164 | // throw new RuntimeException("!Stub"); |
| 165 | // } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 166 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 167 | // public EntityHandlerIface getUserDBHandler() { |
| 168 | // throw new RuntimeException("!Stub"); |
| 169 | // } |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 170 | |
| 171 | public EncryptionIface getEncryption() { |
| 172 | return getBean(KUSTVAKT_ENCRYPTION); |
| 173 | } |
| 174 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 175 | 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 Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 187 | // todo: !!!!!!!!!!!!!!!!!!!!!!!!!! |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 188 | // todo: more specific --> collection provider, document provider, etc. |
| 189 | public ResourceOperationIface getResourceProvider() { |
| 190 | return getBean("resourceProvider"); |
| 191 | } |
| 192 | |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 193 | private void finish() { |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 194 | this.getAuditingProvider().finish(); |
| 195 | context = null; |
| 196 | } |
| 197 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 198 | } |
| 199 | } |