Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * JBoss, Home of Professional Open Source 
   * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
   * as indicated by the @author tags. All rights reserved. 
   * See the copyright.txt in the distribution for a 
   * full listing of individual contributors.
   *
   * This copyrighted material is made available to anyone wishing to use, 
   * modify, copy, or redistribute it subject to the terms and conditions 
  * of the GNU Lesser General Public License, v. 2.1. 
  * This program is distributed in the hope that it will be useful, but WITHOUT A 
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details. 
  * You should have received a copy of the GNU Lesser General Public License, 
  * v.2.1 along with this distribution; if not, write to the Free Software 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
  * MA  02110-1301, USA.
  */
 package org.switchyard.tools.forge.rules;
 
 import static org.switchyard.component.rules.config.model.RulesComponentImplementationModel.DEFAULT_NAMESPACE;
 
 import java.io.File;
 
 
 import  org.switchyard.component.common.knowledge.config.model.ActionModel;
 import  org.switchyard.component.common.knowledge.config.model.v1.V1ActionsModel;
 import  org.switchyard.component.common.knowledge.config.model.v1.V1ManifestModel;
Forge plugin for Rules component commands.
 
 @Alias("rules-service")
 @Topic("SOA")
 @Help("Provides commands related to rules services in SwitchYard.")
 public class RulesServicePlugin implements Plugin {
 
     // rule definition template
     private static final String RULES_TEMPLATE = "RulesTemplate.drl";
     // rule definition file extension
     private static final String RULES_EXTENSION = ".drl";
     // rule definition directory
     private static final String RULES_DIR = "META-INF";
     
     @Inject
     private Project _project;
     
     @Inject
     private Shell _shell;
    
    
Create a new rules service interface and implementation.

Parameters:
argServiceName service name
out shell output
argInterfaceClass class name of Java service interface
argRuleFilePath path to the rule definition
Throws:
java.io.IOException error with file resources
 
     @Command(value = "create", help = "Created a new service backed by business rules.")
     public void newRules(
             @Option(required = true,
                      name = "serviceName",
                      description = "The service name"
              final String argServiceName,
              @Option(required = false,
                      name = "interfaceClass",
                     description = "The Java service interface"
             final String argInterfaceClass,
             @Option(required = false,
                     name = "ruleDefinition",
                     description = "The business rule definition"
             final String argRuleFilePath,
             final PipeOut out)
    throws java.io.IOException {
      
        String pkgName = .getFacet(MetadataFacet.class).getTopLevelPackage();
        String interfaceClass = argInterfaceClass;
        
        if (interfaceClass == null) {
            // Figure out the Java package name for the interface
            if (pkgName == null) {
                pkgName = .promptCommon(
                    "Java package for service interface:",
                    .);
            }
        
            // Create the service interface
            JavaInterface ruleInterface = JavaParser.create(JavaInterface.class)
                .setPackage(pkgName)
                .setName(argServiceName)
                .setPublic();
            java.saveJavaSource(ruleInterface);
            interfaceClass = ruleInterface.getQualifiedName();
            out.println("Created service interface [" + interfaceClass + "]");
        }
        
        String ruleDefinitionPath = argRuleFilePath;
        if (ruleDefinitionPath == null) {
            // Create an empty rule definition
            ruleDefinitionPath =  + . + argServiceName + ;
            TemplateResource template = new TemplateResource()
                .serviceName(argServiceName)
                .packageName(pkgName);
            template.writeResource(.getFacet(ResourceFacet.class).getResource(ruleDefinitionPath));
            
            out.println("Created rule definition [" + ruleDefinitionPath + "]");
        }
        
        // Add the SwitchYard config
        createImplementationConfig(argServiceNameinterfaceClassruleDefinitionPath);
          
        // Notify user of success
        out.println("Rule service " + argServiceName + " has been created.");
    }
    
    private void createImplementationConfig(String serviceName,
            String interfaceName,
            String rulesDefinition) {
        
        SwitchYardFacet switchYard = .getFacet(SwitchYardFacet.class);
        // Create the component service model
        V1ComponentModel component = new V1ComponentModel();
        component.setName(serviceName);
        V1ComponentServiceModel service = new V1ComponentServiceModel();
        service.setName(serviceName);
        InterfaceModel csi = new V1InterfaceModel(.);
        csi.setInterface(interfaceName);
        service.setInterface(csi);
        component.addService(service);
        
        // Create the Rules implementation model and add it to the component model
        V1ActionsModel actions = new V1ActionsModel();
        ActionModel action = new V1RulesActionModel().setOperation("operation").setType(.);
        actions.addAction(action);
        rules.setActions(actions);
        V1ManifestModel manifest = new V1ManifestModel();
        V1ResourcesModel resources = new V1ResourcesModel();
        resources.addResource(new V1ResourceModel().setLocation(rulesDefinition).setType(ResourceType.valueOf("DRL")));
        manifest.setResources(resources);
        rules.setManifest(manifest);
        component.setImplementation(rules);
        
        // Add the new component service to the application config
        SwitchYardModel syConfig = switchYard.getSwitchYardConfig();
        syConfig.getComposite().addComponent(component);
        switchYard.saveConfig();
    }
New to GrepCode? Check out our FAQ X