All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.bristleback.server.bristle.config.ActionInformationResolver Maven / Gradle / Ivy

// Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
// This library is distributed in the hope that it will be useful,
// but without any warranty; without even the implied warranty of merchantability
// or fitness for a particular purpose.
// You should have received a copy of the GNU Lesser General Public License along
// with this program; if not, see .
// ---------------------------------------------------------------------------
package pl.bristleback.server.bristle.config;

import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.actions.AnnotatedRemoteAction;
import pl.bristleback.server.bristle.actions.RemoteAction;
import pl.bristleback.server.bristle.actions.RemoteActionInformation;
import pl.bristleback.server.bristle.exceptions.BristleRuntimeException;
import pl.bristleback.server.bristle.exceptions.handlers.MissingRightsErrorHandler;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Class used to resolve and bind information about annotated action class.
 * Provides check of annotation appearance in action class.
 * It is a helper class for {@link ActionAnnotationsProcessor} implementations.
 * 

* Created on: 2010-09-03 16:04:52
* * @author Wojciech Niemiec */ public final class ActionInformationResolver { private static Logger log = Logger.getLogger(ActionInformationResolver.class.getName()); /** * Gets action information wrapper for given action class. * * @param actionClass remote action class. * @return action information wrapper if everything is fine, * null if action class is not annotated, exception if instance of action class cannot be created. */ public RemoteActionInformation getActionInformation(Class actionClass) { AnnotatedRemoteAction actionAnnotation = actionClass.getAnnotation(AnnotatedRemoteAction.class); if (actionAnnotation == null) { if (log.isDebugEnabled()) { log.debug("Cannot create action information object from class " + actionClass + ", " + "action class is not signed with " + AnnotatedRemoteAction.class.getSimpleName() + " annotation."); } return null; } return createActionInformationObject(actionClass, actionAnnotation); } private RemoteActionInformation createActionInformationObject(Class actionClass, AnnotatedRemoteAction actionAnnotation) { RemoteAction action = initAction(actionClass); String actionName = getActionName(actionAnnotation); Set requiredRights = getActionRequiredRights(actionAnnotation); boolean actionHandlingMissingRightsError = isActionHandlingMissingRightsError(action); return bind(action, actionName, requiredRights, actionHandlingMissingRightsError); } private boolean isActionHandlingMissingRightsError(RemoteAction action) { return (action instanceof MissingRightsErrorHandler); } private RemoteActionInformation bind(RemoteAction action, String actionName, Set requiredRights, boolean actionHandlingMissingRightsError) { RemoteActionInformation actionInformation = new RemoteActionInformation(); actionInformation.setAction(action); actionInformation.setActionName(actionName); actionInformation.setRequiredRights(requiredRights); actionInformation.setActionHandlingErrors(actionHandlingMissingRightsError); return actionInformation; } private RemoteAction initAction(Class actionClass) { try { return actionClass.newInstance(); } catch (Exception e) { throw new BristleRuntimeException("Cannot init action class instance."); } } private String getActionName(AnnotatedRemoteAction actionAnnotation) { return actionAnnotation.actionName(); } private Set getActionRequiredRights(AnnotatedRemoteAction actionAnnotation) { String[] rightsArray = actionAnnotation.requiredRights(); return new HashSet(Arrays.asList(rightsArray)); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy