?? fluentconfigurator.java
字號:
package org.directwebremoting.fluent;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.directwebremoting.AjaxFilter;import org.directwebremoting.Container;import org.directwebremoting.extend.AccessControl;import org.directwebremoting.extend.AjaxFilterManager;import org.directwebremoting.extend.Configurator;import org.directwebremoting.extend.Converter;import org.directwebremoting.extend.ConverterManager;import org.directwebremoting.extend.Creator;import org.directwebremoting.extend.CreatorManager;import org.directwebremoting.impl.SignatureParser;import org.directwebremoting.util.LocalUtil;import org.directwebremoting.util.Logger;/** * A {@link Configurator} that used the FluentInterface style as * <a href="http://www.martinfowler.com/bliki/FluentInterface.html">described by * Martin Fowler</a>. * * <p>To wire up the configuration programatically rather than having to use * <code>dwr.xml</code>. In order to use this style, you'll need to:</p> * * <ul> * <li>Create a concrete implementation of {@link FluentConfigurator} which * implements the {@link #configure()} method.</li> * <li>Add an init param '<code>customConfigurator</code>' to the DWR servlet in * <code>web.xml</code> to point at your new class.</li> * </ul> * * <p>The implementation of {@link #configure()} will look something like * this:</p> * * <pre> * public void configure() { * withConverterType("dog", "com.yourcompany.beans.Dog"); * withCreatorType("ejb", "com.yourcompany.dwr.creator.EJBCreator"); * withCreator("new", "ApartmentDAO") * .addParam("scope", "session") * .addParam("class", "com.yourcompany.dao.ApartmentDAO") * .exclude("saveApartment") * .withAuth("method", "role"); * withCreator("struts", "DogDAO") * .addParam("clas", "com.yourcompany.dao.DogDAO") * .include("getDog") * .include("getColor"); * withConverter("dog", "*.Dog") * .addParam("name", "value"); * withSignature() * .addLine("import java.util.List;") * .addLine("import com.example.Check;") * .addLine("Check.setLotteryResults(List<Integer> nos);"); * } * </pre> * @author Aaron Johnson [ajohnson at cephas dot net / <a href="http://cephas.net/blog">http://cephas.net/blog</a>] * @author Joe Walker [joe at getahead dot ltd dot uk] */public abstract class FluentConfigurator implements Configurator{ /** * This method is used to configure DWR using the fluent style. */ public abstract void configure(); /** * Add a new {@link Converter} definition. * @param id The id referred to by the {@link #withConverter(String, String)} * @param converterClassName The implementation of {@link Converter} to instansitate. * @return <code>this</code> to continue the fluency */ public FluentConfigurator withConverterType(String id, String converterClassName) { setState(STATE_INIT_CONVERT); converterManager.addConverterType(id, converterClassName); return this; } /** * Use a {@link Converter} to instansiate a class * @param newConverter A predefined {@link Converter} or one defined by * {@link #withConverterType(String, String)}. * @param newMatch The javascript name of this component * @return <code>this</code> to continue the fluency */ public FluentConfigurator withConverter(String newConverter, String newMatch) { setState(STATE_ALLOW_CONVERT); this.converter = newConverter; this.match = newMatch; return this; } /** * Add a new {@link Creator} definition. * @param id The id referred to by the {@link #withCreator(String, String)} * @param creatorClassName The implementation of {@link Creator} to instansitate. * @return <code>this</code> to continue the fluency */ public FluentConfigurator withCreatorType(String id, String creatorClassName) { setState(STATE_INIT_CREATE); creatorManager.addCreatorType(id, creatorClassName); return this; } /** * Use a {@link Creator} to instansiate a class * @param newTypeName A predefined {@link Creator} or one defined by * {@link #withCreatorType(String, String)}. * @param newScriptName The javascript name of this component * @return <code>this</code> to continue the fluency */ public FluentConfigurator withCreator(String newTypeName, String newScriptName) { setState(STATE_ALLOW_CREATE); this.typeName = newTypeName; this.scriptName = newScriptName; return this; } /** * @param newFilterClassName filter class name * @return <code>this</code> to continue the fluency */ public FluentConfigurator withFilter(String newFilterClassName) { setState(STATE_ALLOW_FILTER); this.filterClassName = newFilterClassName; return this; } /** * Add a parameter to whatever is being configured. * @param name The name of the parameter * @param value The value of the parameter * @return <code>this</code> to continue the fluency */ public FluentConfigurator addParam(String name, String value) { if (params == null) { params = new HashMap(); } params.put(name, value); return this; } /** * Add a filter to whatever is being configured. * @param newFilterClassName The class to add as a filter * @return <code>this</code> to continue the fluency */ public FluentConfigurator addFilter(String newFilterClassName) { if (filters == null) { filters = new ArrayList(); } filters.add(newFilterClassName); return this; } /** * Add an include rule to a {@link Creator}. * This should be used during a {@link #withCreator(String, String)} call. * @param methodName The method name to be allowed * @return <code>this</code> to continue the fluency */ public FluentConfigurator include(String methodName) { accessControl.addIncludeRule(scriptName, methodName); return this; } /** * Add an exclude rule to a {@link Creator} * This should be used during a {@link #withCreator(String, String)} call. * @param methodName The method name to be dis-allowed * @return <code>this</code> to continue the fluency */ public FluentConfigurator exclude(String methodName) { accessControl.addExcludeRule(scriptName, methodName); return this; } /** * Add an authorization rule to a {@link Creator} * This should be used during a {@link #withCreator(String, String)} call. * @param methodName The method name to have a required role * @param role The required role for the given method * @return <code>this</code> to continue the fluency */ public FluentConfigurator withAuth(String methodName, String role) { accessControl.addRoleRestriction(scriptName, methodName, role); return this; } /** * Add lines to a signature. * @return <code>this</code> to continue the fluency */ public FluentConfigurator withSignature() { setState(STATE_SIGNATURE); return this; } /** * Add lines to a signature. * @param line The line of text to add to the signature configuration * @return <code>this</code> to continue the fluency */ public FluentConfigurator addLine(String line) { if (null == line) { return this; } if (null == signature) { signature = new StringBuffer(); } signature.append(line); signature.append(System.getProperty("line.separator")); return this; } /** * Because some parts of the configuration require multiple steps, the instance * needs to maintain a state across invocations. Whenever the state is changed * by calling this method, the instance will 'flush' anything in the queue * applicable to that state EVEN IF the state itself doesn't change. Thus, it's * important that the child methods don't call setState() when being invoked. * @param state The new state. See the STATE_* constants. */ private void setState(int state)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -