- 浏览: 180979 次
- 性别:
- 来自: 杭州
最新评论
-
yanjingan:
通俗易懂 理解的很深 好文章!
ActiveMQ分享(一)JMS简介 -
xyc717:
我去,也不写怎么登陆
windows下的命令行svn客户端 -
少年中国:
赞一个!
Maven和Maven插件 -
springmvc-freemarker:
可以参考最新的文档:如何在eclipse jee中检出项目并转 ...
Maven和Maven插件 -
u012049463:
谢谢,如果一开始就看这篇文章,又可以少走多少弯路。很具体也很通 ...
ActiveMQ分享(一)JMS简介
在webwork2.2.6版本中,不管是用ServletDispatcher还是FilterDispatcher充当请求转发器(可在web.xml中配置),都会调用到相关Servlet的init或Filter的init方法。
在init方法中,DispatcherUtils.initialize(getServletContext())的调用完成了配置文件的读取、ObjectFactory的设置(也是根据配置文件)。类Configuration充当了读取配置文件的功能。其子类包括:
webwork提供了默认资源文件类型、property资源文件类型,还有DelegatingConfiguration资源文件类型 ,你也可以实现自己的XMLConnfiguration。
查看Configuration类代码,我们可以看到非静态方法,包括isSetImpl(String name)直接返回false,setImpl(String name, Object value)直接抛出异常,这是一种变态的abstract类设计,在需要使用该类的静态方法,却又不愿意调用者实例化该类并调用非静态方法时候,用这种变通的做法也是不错的选择。(因为我们不能同时让static和abstract修饰一个类)。
另外,该做法实现了多态,在这种多态中,子类可以被任意更换,确只保证只有一个之类实例存在。实现方法是在Configuration中建立静态变量static Configuration configurationImpl;调用端可以通过实例化不同的子类赋予configurationImpl。最后通过Configuration.getConfiguration() 获取唯一的实例。
这样的设计在webwork其他地方也经常可见。如ActionProxyFactory及其子类,但是不同的是ActionProxyFactory是absctract的,因为ActionProxyFactory没有设计有静态方法。
这种设计在策略互换且只允许单个策略的情况下相当有用,今天看了这段代码,且做一个笔记。
代码如下:::
public class Configuration { static Configuration configurationImpl; static Configuration defaultImpl; static Locale locale; // Cached locale private static final Log LOG = LogFactory.getLog(Configuration.class); /** * Sets the current configuration implementation. Can only be called once. * * @param config a Configuration implementation * @throws IllegalStateException if an error occurs when setting the configuration implementation. */ public static void setConfiguration(Configuration config) throws IllegalStateException { configurationImpl = config; locale = null; // Reset cached locale } /** * Gets the current configuration implementation. * * @return the current configuration implementation. */ public static Configuration getConfiguration() { return (configurationImpl == null) ? getDefaultConfiguration() : configurationImpl; } /** * Returns the WebWork2 locale. Keys off the property <tt>webwork.locale</tt> which should be set * as the Java {@link java.util.Locale#toString() toString()} representation of a Locale object (i.e., * "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC", etc). <p> * <p/> * If no locale is specified then the default VM locale is used ({@link java.util.Locale#getDefault()}). * * @return the WebWork2 locale if specified or the VM default locale. */ public static Locale getLocale() { if (locale == null) { try { StringTokenizer localeTokens = new StringTokenizer(getString(WebWorkConstants.WEBWORK_LOCALE), "_"); String lang = null; String country = null; if (localeTokens.hasMoreTokens()) { lang = localeTokens.nextToken(); } if (localeTokens.hasMoreTokens()) { country = localeTokens.nextToken(); } locale = new Locale(lang, country); } catch (Throwable t) { // Default LOG.warn("Setting locale to the default locale"); locale = Locale.getDefault(); } } return locale; } /** * Determines whether or not a value has been set. Useful for testing for the existance of parameter without * throwing an IllegalArgumentException. * * @param name the name of the property to test. * @return <tt>true</tt> if the property exists and has a value, <tt>false</tt> otherwise. */ public static boolean isSet(String name) { return getConfiguration().isSetImpl(name); } /** * Returns a property as a String. This will throw an <tt>IllegalArgumentException</tt> if an error occurs * while retrieveing the property or if the property doesn't exist. * * @param name the name of the property to get. * @return the property as a String * @throws IllegalArgumentException if an error occurs retrieveing the property or the property does not exist. */ public static String getString(String name) throws IllegalArgumentException { String val = get(name).toString(); return val; } /** * Returns a property as an Object. This will throw an <tt>IllegalArgumentException</tt> if an error occurs * while retrieveing the property or if the property doesn't exist. * * @param name the name of the property to get. * @return the property as an Object. * @throws IllegalArgumentException if an error occurs retrieveing the property or the property does not exist. */ public static Object get(String name) throws IllegalArgumentException { Object val = getConfiguration().getImpl(name); return val; } /** * Returns an Iterator of all properties names. * * @return an Iterator of all properties names. */ public static Iterator list() { return getConfiguration().listImpl(); } /** * Implementation of the {@link #isSet(String)} method. * * @see #isSet(String) */ public boolean isSetImpl(String name) { // this is dumb.. maybe it should just throw an unsupported op like the rest of the *Impl // methods in this class. return false; } /** * Sets a property. Throws an exception if an error occurs when setting the property or if the * Configuration implementation does not support setting properties. * * @param name the name of the property to set. * @param value the property to set. * @throws IllegalArgumentException if an error occurs when setting the property. * @throws UnsupportedOperationException if the config implementation does not support setting properties. */ public static void set(String name, Object value) throws IllegalArgumentException, UnsupportedOperationException { getConfiguration().setImpl(name, value); } /** * Implementation of the {@link #set(String, Object)} method. * * @see #set(String, Object) */ public void setImpl(String name, Object value) throws IllegalArgumentException, UnsupportedOperationException { throw new UnsupportedOperationException("This configuration does not support updating a setting"); } /** * Implementation of the {@link #get(String)} method. * * @see #get(String) */ public Object getImpl(String aName) throws IllegalArgumentException { return null; } /** * Implementation of the {@link #list()} method. * * @see #list() */ public Iterator listImpl() { throw new UnsupportedOperationException("This configuration does not support listing the settings"); } private static Configuration getDefaultConfiguration() { if (defaultImpl == null) { // Create bootstrap implementation defaultImpl = new DefaultConfiguration(); // Create default implementation try { String className = getString(WebWorkConstants.WEBWORK_CONFIGURATION); if (!className.equals(defaultImpl.getClass().getName())) { try { // singleton instances shouldn't be built accessing request or session-specific context data defaultImpl = (Configuration) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null); } catch (Exception e) { LOG.error("Could not instantiate configuration", e); } } } catch (IllegalArgumentException ex) { // ignore } } return defaultImpl; } public static void reset() { defaultImpl = null; configurationImpl = null; } }
发表评论
-
mvnDebug的配置项
2010-11-22 10:23 7372在maven上debug,经常跟jetty或tomcat插件在 ... -
jmeter性能测试入门
2010-10-08 18:31 0有机会接触了性能测试,对jmeter做一个简单的入门介绍 -
java.lang.LinkageError: loader constraint violation的错误
2010-10-08 17:26 34270java.lang.LinkageError: loader ... -
一次性能测试的总结
2010-10-08 16:02 2404有机会做了一次性能测试工作,主要是预研性质的工作。开发人员有必 ... -
JMX
2010-08-08 14:10 0JMX:Java Management Extensions( ... -
Maven和Maven插件
2010-07-31 15:18 10719人生是一张茶几,上面 ... -
ActiveMQ分享(一)JMS简介
2010-07-31 14:09 16900一、概述 Message,即消息。人与人之间通过消息传递 ... -
BigDecimal的8种舍入方式
2010-04-26 15:32 1884在银行、帐户、计费等领域,BigDecimal提供了精确的数值 ... -
持续集成hudson入门
2010-04-07 13:44 29988极限编程中一项建议实践便是持续集成,持续集成是指在开发 ... -
数值区间正则表达式写法
2010-02-04 09:21 4581数值型正则在实际应用中还是比较多见的,如判读合法IP段 ... -
线程同步模型, 生产者/消费者, 读写同步,线程池,concurrent map
2009-05-31 10:13 216生产者/消费者模型 有了信号量这个利器,我们就可以处理比较 ... -
线程同步
2009-05-31 09:47 72作者 : buaawhl 我们可以 ... -
ibatis 模糊查询单引号处理
2009-05-23 21:57 174ibatis 查询中,我们通常用#或$来获取变量或对象的值作为 ...
相关推荐
webwork 2.2.6 中文版本,然后编译生成chm格式,方面查看。
WebWork 2.2.6 API
webwork底层源码,用于webwork框架设计
webwork的源码和tomcat的源码,可以借鉴一下
是webwork中的一个jar包
webwork-2.2.5源码包下载
webwork-2.1.6.jar
比较基础的入门级webwork实例,适合初学者!
剖析 Webwork 源码-Along-Anatomy Webwork Source Code_V0.9.pdf
webwork in action 源码,简单易读,适合新手上路!
详细介绍了,webwork2的配置及action映射,验证框架等,里面有jar包,源代码
剖析 Webwork
剖析Webwork源码 深入了解Webwork
非常好的Webwork2入门资料,OpenDoc出品
博文链接:https://melet.iteye.com/blog/102700
webwork源码下载
struts2已经大有飞入寻常百姓家的趋势了,大家都知struts2其实是webwork2项目来的。所以我们还是从webwork得源码来看一下struts2的端倪~ <br>详细讲解核心类
webWork 源码, struts2 学习和提升的必需资料, 提供有详细的文档说明, 示例代码, 源码, 能够使初学者学而有味, 老手们也是学而不乏. 更是在开发过程中的帮助一把手.
webwork源码,struts2.0核心技术
xwork源代码(webwork源代码,xwork source,)xwork源代码(webwork源代码,xwork source,)