技术开发 频道

基于RSA开发SOA Pattern

    步骤二:创建Pattern

    1. 选择Window'show view'Modeling'Pattern Authoring, 打开Pattern authoring视图.

    2. 新建Pattern,在Pattern Authoring视图中,选择我们刚刚建立的Project, 在上面击右键弹出菜单,选择New Pattern。弹出如下窗口,命名为Cache Pattern, 则对应Class Name为CachePattern,选择Pattern Type为Collaboration,并为该Pattern创建一个组,称之为Tutorial Pattern.

    3. 创建Pattern参数,在上图的Parameters栏中,点击Add…按钮,弹出下图的对话框。分别定义前面的四个参数,下图为Service参数的建立,由于该参数绑定的类型可能为Interface或是Class,指定其类型为Classifier。下面第二图为getEntry参数建立Parameter Dependency,指定其Supplier parameter为Service。这种设定为两个参数建立了依赖关系,如果没有绑定Service,则不能绑定getEntry,因为getEntry所绑定的operation实现是Service参数的元素。

    4. 完成Pattern 参数定义后,转到Detail Tab,可以定义一些Pattern的描述信息。Keywords为Pattern在RAS中进行搜索是提供方便。

     5. 单击OK,在Pattern Authoring视图下,生成的结构如下

    在Package Explorer下生成的结构如下:

    步骤三:扩展点编程

    在生成的代码框架中我们可以看到,在CachePattern中添加了四个内部类,这四个类分别对应我们所添加的四个模式参数,我们展开其中一个内部类可以看到内部类由PARAMETER_ID,构造函数和两个基本扩展方法所构成,如上图红圈所标注,对于GetEntry内部类对应getEntry参数,由于在参数构建时设定了依赖关系,所以在getEntry内部类中又产生一个内部类GetEntry_ServiceDependency,在其内部产生了若干个update()扩展方法。下面说明在该例中如何扩展这些扩展方法。

    1. 两个基本扩展点:

    public boolean expand(PatternParameterValue value) 在参数被绑定时触发,完成绑定参数的动作,同时可以观察到该方法返回值为boolean类型,但是不要以为返回false该动作不会发生,即无论扩展后该方法返回何值,bind事件都会被触发。若想改变这种状况,则必须进行验证,验证方法后面会提到。下面代码为在Service内部类扩展该方法的代码:


            		public boolean expand(PatternParameterValue value) {
            Object obj = value.getValue();
            if( obj instanceof Classifier ) {
            Classifier classifier = (Classifier) obj;
            // instance -- instance of the pattern in the model which
            gives values specified for this pattern.
            com.ibm.xtools.patterns.framework.AbstractPatternInstance patternInstance =
            value.getOwningInstance();
            CachePatternImplementation impl;
            impl = (CachePatternImplementation) instanceToImplMap.get(patternInstance);
            if (impl == null) {
            impl = new CachePatternImplementation (patternInstance);
            instanceToImplMap.put(patternInstance, impl);
            }
            impl.expandService(classifier);
            }
            return true;
            }
            

    该段代码完成在绑定Service参数时,根据绑定的类生成相应的UML2 元素,其中主要的实现在CachePatternImplementation类中完成,以减少项目主类中的代码数量。

  • public boolean expand(PatternParameterValue.Removed value) 该方法在解除参数绑定时所触发,只要解除动作发生了,那么该方法将被执行。下面代码仍为在Service内部类扩展该方法的代码:比如,我们以在该例中的Service的expand方法编程,我们在该例子中为该Pattern的Service方法添加代码如下:

    
                		public boolean expand(PatternParameterValue.Removed value) {
                Object obj = value.getValue();
                if( obj instanceof Classifier ) {
                Classifier classifier = (Classifier) obj;
                // instance -- instance of the pattern in the model which gives
                values specified for this pattern.
                com.ibm.xtools.patterns.framework.AbstractPatternInstance patternInstance =
                value.getOwningInstance();
                CachePatternImplementation impl;
                impl = (CachePatternImplementation) instanceToImplMap.get(patternInstance);
                if (impl == null) {
                impl = new CachePatternImplementation (patternInstance);
                instanceToImplMap.put(patternInstance, impl);
                }
                impl.removeService(classifier);
                }
                return true;
                }
                

     
  • 0
    相关文章