com.google.code.rees.scope.session.DefaultSessionConfigurationProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of struts2-conversation-scope-plugin Show documentation
Show all versions of struts2-conversation-scope-plugin Show documentation
A Struts2 plugin for leveraging conversation scopes in Struts2
/*******************************************************************************
*
* Struts2-Conversation-Plugin - An Open Source Conversation- and Flow-Scope Solution for Struts2-based Applications
* =================================================================================================================
*
* Copyright (C) 2012 by Rees Byars
* http://code.google.com/p/struts2-conversation/
*
* **********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* **********************************************************************************************************************
*
* $Id: DefaultSessionConfigurationProvider.java reesbyars $
******************************************************************************/
package com.google.code.rees.scope.session;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.code.rees.scope.ActionProvider;
import com.google.code.rees.scope.container.Component;
import com.google.code.rees.scope.util.ReflectionUtil;
/**
*
* The default implementation of the {@link SessionConfigurationProvider}.
*
* @author rees.byars
*/
public class DefaultSessionConfigurationProvider implements SessionConfigurationProvider {
private static final long serialVersionUID = 3703684121698557461L;
private static final Logger LOG = LoggerFactory.getLogger(DefaultSessionConfigurationProvider.class);
protected transient SessionConfiguration configuration = new SessionConfiguration();
protected Set> classesProcessed = new HashSet>();
protected ActionProvider actionProvider;
/**
* {@inheritDoc}
*/
@Override
@Component
public void setActionProvider(ActionProvider actionProvider) {
this.actionProvider = actionProvider;
}
/**
* {@inheritDoc}
*/
@Override
public void init() {
try {
this.processClasses(actionProvider.getActionClasses());
} catch (Exception e) {
LOG.warn("The was an error attempting to retrieve the action classes. This could be due to an older version of Struts2. Configurations will be built on the fly");
}
}
/**
* {@inheritDoc}
*/
@Override
public SessionConfiguration getSessionConfiguration(Class> clazz) {
if (this.configuration == null) {
synchronized (this.classesProcessed) {
this.initConfig();
}
}
if (!this.classesProcessed.contains(clazz)) {
this.processClass(clazz);
}
return this.configuration;
}
protected synchronized void initConfig() {
if (this.configuration == null) {
this.configuration = new SessionConfiguration();
this.processClasses(this.classesProcessed);
}
}
protected void processClasses(Set> classes) {
for (Class> clazz : classes) {
this.processClass(clazz);
}
}
protected synchronized void processClass(Class> clazz) {
if (!this.classesProcessed.contains(clazz)) {
if (!clazz.isInterface()
&& !Modifier.isAbstract(clazz.getModifiers())
&& !clazz.isAnnotation()) {
LOG.info("Loading @SessionField fields from " + clazz.getName());
addFields(clazz);
this.classesProcessed.add(clazz);
}
}
}
protected void addFields(Class> clazz) {
for (Field field : ReflectionUtil.getFields(clazz)) {
if (field.isAnnotationPresent(SessionField.class)) {
LOG.debug("Adding @SessionField " + field.getName() + " from class " + clazz.getName() + " to the SessionConfiguration");
SessionField sessionField = (SessionField) field.getAnnotation(SessionField.class);
String name = sessionField.name();
if (name.equals(SessionField.DEFAULT)) {
name = field.getName();
}
String key = SessionUtil.buildKey(name, field.getType());
ReflectionUtil.makeAccessible(field);
this.configuration.addField(clazz, key, field);
}
}
}
}