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

pl.bristleback.server.bristle.actions.ActionsInitializer 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.actions;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.config.ActionAnnotationsProcessor;
import pl.bristleback.server.bristle.config.BristleConstants;
import pl.bristleback.server.bristle.config.PackageActionAnnotationsProcessor;
import pl.bristleback.server.bristle.exceptions.BristleRuntimeException;
import pl.bristleback.server.bristle.exceptions.ImplementationResolvingException;
import pl.bristleback.server.bristle.integration.spring.SpringIntegrationUtil;
import pl.bristleback.server.bristle.utils.ResolverUtil;

import java.util.List;
import java.util.Map;

/**
 * Helper class used to retrieve annotated actions, opaqued in {@link ActionsContainer} object and to inject Spring dependencies.
 * Firstly, creator instantiates annotation processor class.
 * Then processor object method is invoked and returns result of work of that processor.
 * Method {@link ActionsInitializer#injectActionsDependencies()} is invoked by plugin to inject dependencies.
 * 

* Created on: 2010-10-04 17:15:29
* * @author Wojciech Niemiec */ public final class ActionsInitializer { private static Logger log = Logger.getLogger(ActionsInitializer.class.getName()); public static final Class ACTION_ANNOTATION_DEFAULT_PROCESSOR_CLASS = PackageActionAnnotationsProcessor.class; private ActionsContainer container; /** * Gets previously created actions container. Returns null if container was not created yet. * * @return actions container. */ public ActionsContainer getContainer() { return container; } /** * Loads actions using {@link ActionAnnotationsProcessor} implementation. * Implementation of action annotations processor can be specified by user by setting plugin property with name * {@link pl.bristleback.server.bristle.config.BristleConstants#ACTION_CLASS_RESOLVER_CLASS_PARAMETER_NAME}. * * @param pluginSettings plugin settings map */ public void loadActionsContainer(Map pluginSettings) { initialize(); String actionClassResolverName = pluginSettings.get(BristleConstants.ACTION_CLASS_RESOLVER_CLASS_PARAMETER_NAME); Class resolverClass = getResolverClass(actionClassResolverName); loadActionsByResolver(pluginSettings, resolverClass); } private void initialize() { if (container != null) { throw new BristleRuntimeException("actions already initialized"); } container = new ActionsContainer(); } private Class getResolverClass(String actionClassResolverName) { if (StringUtils.isEmpty(actionClassResolverName)) { if (log.isDebugEnabled()) { log.debug("Action class resolver is not specified by user, system will use default resolver"); } return ACTION_ANNOTATION_DEFAULT_PROCESSOR_CLASS; } else { ResolverUtil resolver = new ResolverUtil(); return resolver.getImplementation(ActionAnnotationsProcessor.class, actionClassResolverName); } } private void loadActionsByResolver(Map pluginSettings, Class actionClassResolver) { ActionAnnotationsProcessor actionAnnotationsProcessor; actionAnnotationsProcessor = getAnnotationProcessor(actionClassResolver); List actions = actionAnnotationsProcessor.getAnnotatedActions(pluginSettings); for (RemoteActionInformation action : actions) { addAction(action); } } private ActionAnnotationsProcessor getAnnotationProcessor(Class actionClassResolver) { ActionAnnotationsProcessor actionAnnotationsProcessor; try { actionAnnotationsProcessor = (ActionAnnotationsProcessor) (actionClassResolver.newInstance()); } catch (Exception e) { throw new ImplementationResolvingException(actionClassResolver.getName(), ActionAnnotationsProcessor.class); } return actionAnnotationsProcessor; } private void addAction(RemoteActionInformation actionInformation) { if (container.getAction(actionInformation.getActionName()) != null) { throw new BristleRuntimeException("action class with name " + actionInformation.getActionName() + "is already registered."); } container.addAction(actionInformation); } /** * Inject Spring dependencies using {@link pl.bristleback.server.bristle.integration.spring.SpringIntegrationUtil} class. */ public void injectActionsDependencies() { for (RemoteActionInformation actionInformation : container.getAllActions()) { SpringIntegrationUtil.injectAnnotatedBeans(actionInformation.getAction()); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy