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

com.github.pmairif.weberknecht.request.view.ActionViewProcessorFactory Maven / Gradle / Ivy

Go to download

Yet another java web framework that wants to make web developers life a bit easier.

There is a newer version: 1.0.4
Show newest version
/*
 * ActionProcessorFactory.java (weberknecht)
 *
 * Copyright 2010-2012 Patrick Mairif.
 * The program is distributed under the terms of the Apache License (ALv2).
 * 
 * tabstop=4, charset=UTF-8
 */
package com.github.pmairif.weberknecht.request.view;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * create action processors
 * 
 * Custom processors can be configured via action-view-processors/action-view-processor in weberknecht.xml.
 * 
 * @author pmairif
 */
public class ActionViewProcessorFactory {
	private static final Logger log = LoggerFactory.getLogger(ActionViewProcessor.class);
	
	private final Map> suffixProcessorMap = new HashMap>();
	
	public ActionViewProcessorFactory() {
		//register default processors
		registerProcessor("do", WebActionProcessor.class);
		registerProcessor("", WebActionProcessor.class);
		registerProcessor("feed", FeedActionProcessor.class);
		registerProcessor("json", JsonActionProcessor.class);
		registerProcessor("data", DataActionProcessor.class);
	}
	
	/**
	 * @param viewProcessorName
	 * 		unique name identifying the view processor (formerly called suffix)
	 */
	public ActionViewProcessor createActionProcessor(String viewProcessorName, ServletContext servletContext) {
		ActionViewProcessor processor = null;
		
		Class clazz = suffixProcessorMap.get(viewProcessorName);
		if (clazz != null) {
			try {
				processor = clazz.newInstance();
				processor.setServletContext(servletContext);
				processor.setActionViewProcessorFactory(this);
			}
			catch (InstantiationException e) {
				log.error("createActionProcessor() - InstantiationException: "+e.getMessage(), e);	//$NON-NLS-1$
			}
			catch (IllegalAccessException e) {
				log.error("createActionProcessor() - IllegalAccessException: "+e.getMessage(), e);	//$NON-NLS-1$
			}
		}
	
		return processor;
	}
	
	@SuppressWarnings("unchecked")
	public void registerProcessor(String suffix, String className) throws ClassNotFoundException {
		Class clazz = (Class) Class.forName(className);
		registerProcessor(suffix, clazz);
	}

	public void registerProcessor(String suffix, Class clazz) {
		suffixProcessorMap.put(suffix, clazz);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy