RuleExecutionSetProvider.java :  » Rule-Engine » hammurapi-rules » biz » hammurapi » rules » jsr94 » admin » Java Open Source

Java Open Source » Rule Engine » hammurapi rules 
hammurapi rules » biz » hammurapi » rules » jsr94 » admin » RuleExecutionSetProvider.java
package biz.hammurapi.rules.jsr94.admin;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import biz.hammurapi.config.ConfigurationException;
import biz.hammurapi.xml.dom.DOMUtils;


/**
 * Rule execution set provider.
 * @author Pavel Vlasov
 *
 */
class RuleExecutionSetProvider implements javax.rules.admin.RuleExecutionSetProvider {
  
  private Map properties=new HashMap();

  /**
   * Constructor.
   * @param properties
   */
  public RuleExecutionSetProvider(Map properties) {
    if (properties!=null) {
      this.properties.putAll(properties);
    }
  }

  /**
   * Creates rule execution set from XML Element.
   * Properties are ignored.
   */
  public RuleExecutionSet createRuleExecutionSet(Element holder, Map properties) throws RuleExecutionSetCreateException, RemoteException {
    try {
      return new biz.hammurapi.rules.jsr94.admin.RuleExecutionSet(holder, properties);
    } catch (ConfigurationException e) {
      throw new RuleExecutionSetCreateException("Could not instantiate rules: "+e, e);
    } catch (TransformerException e) {
      throw new RuleExecutionSetCreateException("Could not parse XML definition: "+e, e);
    }
  }

  /**
   * Creates rule execution set from XML String
   * The first parameter shall be of type String.
   */
  public RuleExecutionSet createRuleExecutionSet(Serializable xmlString, Map properties) throws RuleExecutionSetCreateException, RemoteException {
    try {
      return createRuleExecutionSet(DOMUtils.parse(new StringReader((String) xmlString)).getDocumentElement(), properties);
    } catch (SAXException e) {
      throw new RuleExecutionSetCreateException("Could not parse XML String: "+e, e);
    } catch (IOException e) {
      throw new RuleExecutionSetCreateException("Should never happen: "+e, e);
    } catch (ParserConfigurationException e) {
      throw new RuleExecutionSetCreateException("Could not parse XML String: "+e, e);
    } catch (FactoryConfigurationError e) {
      throw new RuleExecutionSetCreateException("Could not parse XML String: "+e);
    }
  }

  /**
   * Constructs rule execution set from URI. Prefix <code>resource:</code> in uri indicates that 
   * rule set shall be loaded from classloader, otherwise it is loaded from URL.
   */
  public RuleExecutionSet createRuleExecutionSet(String uri, Map properties)
      throws RuleExecutionSetCreateException, IOException,
      RemoteException {    
    try {
      if (uri.startsWith("resource:")) {
        return new biz.hammurapi.rules.jsr94.admin.RuleExecutionSet(
            uri, 
            DOMUtils.parse(getClass().getClassLoader().getResourceAsStream(uri.substring("resource:".length()))).getDocumentElement(),
            properties);
      }
    
      return new biz.hammurapi.rules.jsr94.admin.RuleExecutionSet(
          uri, 
          DOMUtils.parse(new URL(uri).openStream()).getDocumentElement(),
          properties);
    } catch (MalformedURLException e) {
      throw new RuleExecutionSetCreateException("Malformed URI: "+uri, e);
    } catch (SAXException e) {
      throw new RuleExecutionSetCreateException("Could not parse XML definition: "+e, e);
    } catch (IOException e) {
      throw new RuleExecutionSetCreateException("Could not read XML definition: "+e, e);
    } catch (ParserConfigurationException e) {
      throw new RuleExecutionSetCreateException("Could not parse XML definition: "+e, e);
    } catch (FactoryConfigurationError e) {
      throw new RuleExecutionSetCreateException("Could not parse XML definition: "+e);
    } catch (ConfigurationException e) {
      throw new RuleExecutionSetCreateException("Could not instantiate rules: "+e, e);
    } catch (TransformerException e) {
      throw new RuleExecutionSetCreateException("Could not parse XML definition: "+e, e);
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.