]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/core/AuthenticatedApplicationContextInitialization.java
Improve login
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / core / AuthenticatedApplicationContextInitialization.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.core;
17
18 import java.beans.PropertyDescriptor;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.springframework.beans.BeansException;
23 import org.springframework.beans.PropertyValues;
24 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
25 import org.springframework.context.ApplicationEvent;
26 import org.springframework.context.ApplicationListener;
27 import org.springframework.context.event.ContextRefreshedEvent;
28
29 /**
30 * Executes with a system authentication the instantiation and initialization
31 * methods of the application context where it has been defined.
32 */
33 public class AuthenticatedApplicationContextInitialization extends
34 AbstractSystemExecution implements InstantiationAwareBeanPostProcessor,
35 ApplicationListener<ApplicationEvent> {
36 // private Log log = LogFactory
37 // .getLog(AuthenticatedApplicationContextInitialization.class);
38 /** If non empty, restricts to these beans */
39 private List<String> beanNames = new ArrayList<String>();
40
41 @SuppressWarnings("rawtypes")
42 public Object postProcessBeforeInstantiation(Class beanClass,
43 String beanName) throws BeansException {
44 // we authenticate when any bean is instantiated
45 // we will deauthenticate only when the application context has been
46 // refreshed in order to be able to deal with factory beans has well
47 if (!isAuthenticatedBySelf()) {
48 if (beanNames.size() == 0)
49 authenticateAsSystem();
50 else if (beanNames.contains(beanName))
51 authenticateAsSystem();
52 }
53 return null;
54 }
55
56 public boolean postProcessAfterInstantiation(Object bean, String beanName)
57 throws BeansException {
58 return true;
59 }
60
61 public PropertyValues postProcessPropertyValues(PropertyValues pvs,
62 PropertyDescriptor[] pds, Object bean, String beanName)
63 throws BeansException {
64 return pvs;
65 }
66
67 public Object postProcessBeforeInitialization(Object bean, String beanName)
68 throws BeansException {
69 return bean;
70 }
71
72 public Object postProcessAfterInitialization(Object bean, String beanName)
73 throws BeansException {
74 // NOTE: in case there was an exception in on the initialization method
75 // we expect the underlying thread to die and thus the system
76 // authentication to be lost. We have currently no way to catch the
77 // exception and perform the deauthentication by ourselves.
78 // deauthenticateAsSystem();
79 return bean;
80 }
81
82 public void onApplicationEvent(ApplicationEvent event) {
83 if (event instanceof ContextRefreshedEvent) {
84 // make sure that we have deauthenticated after the application
85 // context was initialized/refreshed
86 // deauthenticateAsSystem();
87 }
88 }
89
90 public void setBeanNames(List<String> beanNames) {
91 this.beanNames = beanNames;
92 }
93
94 }