2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.core
;
18 import java
.beans
.PropertyDescriptor
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
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
;
30 * Executes with a system authentication the instantiation and initialization
31 * methods of the application context where it has been defined.
33 public class AuthenticatedApplicationContextInitialization
extends
34 AbstractSystemExecution
implements InstantiationAwareBeanPostProcessor
,
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
>();
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();
56 public boolean postProcessAfterInstantiation(Object bean
, String beanName
)
57 throws BeansException
{
61 public PropertyValues
postProcessPropertyValues(PropertyValues pvs
,
62 PropertyDescriptor
[] pds
, Object bean
, String beanName
)
63 throws BeansException
{
67 public Object
postProcessBeforeInitialization(Object bean
, String beanName
)
68 throws BeansException
{
69 // authenticateAsSystem();
73 public Object
postProcessAfterInitialization(Object bean
, String beanName
)
74 throws BeansException
{
75 // NOTE: in case there was an exception in on the initialization method
76 // we expect the underlying thread to die and thus the system
77 // authentication to be lost. We have currently no way to catch the
78 // exception and perform the deauthentication by ourselves.
79 // deauthenticateAsSystem();
83 public void onApplicationEvent(ApplicationEvent event
) {
84 if (event
instanceof ContextRefreshedEvent
) {
85 // make sure that we have deauthenticated after the application
86 // context was initialized/refreshed
87 // deauthenticateAsSystem();
91 public void setBeanNames(List
<String
> beanNames
) {
92 this.beanNames
= beanNames
;