]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AuthenticatedApplicationContextInitialization.java
59f6a517921e79c448e08ad342889e6c2fb5497e
[lgpl/argeo-commons.git] / AuthenticatedApplicationContextInitialization.java
1 package org.argeo.security.core;
2
3 import java.beans.PropertyDescriptor;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.springframework.beans.BeansException;
8 import org.springframework.beans.PropertyValues;
9 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
10 import org.springframework.context.ApplicationEvent;
11 import org.springframework.context.ApplicationListener;
12 import org.springframework.context.event.ContextRefreshedEvent;
13
14 /**
15 * Executes with a system authentication the instantiation and initialization
16 * methods of the application context where it has been defined.
17 */
18 public class AuthenticatedApplicationContextInitialization extends
19 AbstractSystemExecution implements InstantiationAwareBeanPostProcessor,
20 ApplicationListener {
21 private Log log = LogFactory
22 .getLog(AuthenticatedApplicationContextInitialization.class);
23
24 @SuppressWarnings("rawtypes")
25 public Object postProcessBeforeInstantiation(Class beanClass,
26 String beanName) throws BeansException {
27 // we authenticate when any beans is instantiated
28 // we will deauthenticate only when the application context has been
29 // refreshed in order to be able to deal with factory beans has well
30 if (!isAuthenticatedBySelf()) {
31 authenticateAsSystem();
32 if (log.isTraceEnabled())
33 log.trace("Application context initialization authenticated for thread "
34 + Thread.currentThread().getName());
35 }
36 return null;
37 }
38
39 public boolean postProcessAfterInstantiation(Object bean, String beanName)
40 throws BeansException {
41 return true;
42 }
43
44 public PropertyValues postProcessPropertyValues(PropertyValues pvs,
45 PropertyDescriptor[] pds, Object bean, String beanName)
46 throws BeansException {
47 return pvs;
48 }
49
50 public Object postProcessBeforeInitialization(Object bean, String beanName)
51 throws BeansException {
52 // authenticateAsSystem();
53 return bean;
54 }
55
56 public Object postProcessAfterInitialization(Object bean, String beanName)
57 throws BeansException {
58 // NOTE: in case there was an exception in on the initialization method
59 // we expect the underlying thread to die and thus the system
60 // authentication to be lost. We have currently no way to catch the
61 // exception and perform the deauthentication by ourselves.
62 // deauthenticateAsSystem();
63 return bean;
64 }
65
66 public void onApplicationEvent(ApplicationEvent event) {
67 if (event instanceof ContextRefreshedEvent) {
68 // make sure that we have deauthenticated after the application
69 // context was initialized/refreshed
70 deauthenticateAsSystem();
71 if (log.isTraceEnabled())
72 log.trace("Application context initialization deauthenticated for thread "
73 + Thread.currentThread().getName());
74 }
75 }
76
77 }