将Tapestry框架打包的实现

来源:互联网 发布:资海网络集团骗局 编辑:程序博客网 时间:2024/06/10 15:25

1-     首先在Tapestry框架会在类路径下,导入的jar包里,搜索META-INF目录,

(在应用启动的时候, Hivemind会在类路径下寻找 /META-INF/hivemodule.xml  

类路径包括

1-${webapp-context}/WEB-INF/classes,

2-${webapp-context}/WEB-INF/lib 中所有的jar包。

3-或者容器的对所有应用共享的jar

 

另外tapestry.jar中的${tapestry.jar}/META-INF/hivemodule.xml

一般情况下只读这两个位置的hivemodule.xml

2-     这样你就可以把Hivemodule.xml打进一个jar包,而不需要在源代码目录编辑它。

 

3- 在Hivemodule.xml中添加如下内容

<!-- 定位jar包中的页面、组件 -->

<implementation service-id="tapestry.page.SpecificationResolverDelegate">

        <invoke-factory model="threaded">

            <construct class=

"cn.com.agree.web.tapestry.presentation.plugin.AgreeSpecificationResolverDelegate">

                <set-object property=

"specificationSource" value="infrastructure:specificationSource"/>

                <set-object property=

"classResolver" value="infrastructure:classResolver"/>

                <set-object property="pageRootDirList" value=

"app-property:org.apache.tapestry.mlrain.spec-resolver-delegate.page-root-dir"/>

                <set-object property="componentRootDirList" value=

"app-property:org.apache.tapestry.mlrain.spec-resolver-delegate.component-root-dir"/>

            </construct>

        </invoke-factory>

    </implementation>

4- AgreeSpecificationResolverDelegate类的内容

import org.apache.hivemind.ClassResolver;

import org.apache.hivemind.Resource;

import org.apache.hivemind.util.ClasspathResource;

import org.apache.tapestry.INamespace;

import org.apache.tapestry.IRequestCycle;

import org.apache.tapestry.engine.ISpecificationSource;

import org.apache.tapestry.resolver.ISpecificationResolverDelegate;

import org.apache.tapestry.spec.IComponentSpecification;

import org.mlstudio.util.StringUtil;

 

/*

 * ISpecificationResolverDelegate 当页面规范文件或者组件规范文件不能通过正常方式获得的时候

 * (比如被打进jar包中去了)该接口运行指定这些文件到一个非常规位置,或者临时生成。

 * 必须在线程安全的模式下实现。

 *

 */

public class AgreeSpecificationResolverDelegate

    implements ISpecificationResolverDelegate

{

    public static final String AGREE_NAMESPACE = "agree";

    

    //用来返回,解析组件规范

    private ISpecificationSource _specificationSource;

    private ClassResolver _classResolver;

    //这两个属性会被Hivemodule运行时候依赖注入

    //.application文件中指定的目录,比如/WEB-INF/

    //可以指定多个目录所以是个数组

    private String _pageRootDirList[];

    private String _componentRootDirList[];

 

    public AgreeSpecificationResolverDelegate()

    {

        _pageRootDirList = new String[0];

        _componentRootDirList = new String[0];

}

       /**

     * 用来返回一个组件规范文件,Tapestry会自动调用该方法来寻找一个组件规范

     */

public IComponentSpecification findComponentSpecification

(IRequestCycle cycle, INamespace namespace, String type)

    {

        for(int i = 0; i < _componentRootDirList.length; i++)

        {

            //ClasspathResource指明了是类路径

//_componentRootDirList[i]指明了类路径下相对的目录

            Resource resource =

new ClasspathResource(_classResolver, _componentRootDirList[i]);

            //通过得到的类路径资源,寻找需要的规范

            Resource componentResource = resource.getRelativeResource(getExpectedComponentName(type));

            //如果得到了就返回

            if(componentResource.getResourceURL() != null)

                return _specificationSource.getComponentSpecification(componentResource);

        }

return null;

}

/**

     * 用来返回一个页面规范文件,Tapestry会自动调用该方法来寻找一个页面规范

     */

    public IComponentSpecification findPageSpecification(IRequestCycle cycle, INamespace namespace, String simplePageName)

    {

        for(int i = 0; i < _pageRootDirList.length; i++)

        {

            Resource resource =

new ClasspathResource(_classResolver, _pageRootDirList[i]);

            Resource pageResource = resource.getRelativeResource(getExpectedPageName(simplePageName));

            if(pageResource.getResourceURL() != null)

                return _specificationSource.getPageSpecification(pageResource);

        }

        return null;

}

。。省略GetterSetter

 

}

 

5-      接着在.application文件总添加如下内容

 

<!-- jar包中的页面/模板的存放根目录 -->

    <meta key=

"org.apache.tapestry.mlrain.spec-resolver-delegate.page-root-dir"

value="/WEB-INF/" />

 

<meta key=

"org.apache.tapestry.mlrain.spec-resolver-delegate.component-root-dir"

       value="/WEB-INF/" />

 

 
原创粉丝点击