Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-2420

Be able to call drools-server without creating an agent configuration file

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

    XMLWordPrintable

Details

    • Feature Request
    • Resolution: Unresolved
    • Major
    • None
    • 5.0.1.FINAL
    • None
    • None
    • Low

    Description

      Creating snapshots can be done dynamically in guvnor.
      drools-server should allow to call such elements without human intervention.
      For now, we have to manually create an agent configuration file in the resources folder of drools-server.

      Using the servlet parameters to dynamically generate a default agent configuration could be usefull for many cases and can be done very easily.

      Here is a simple modification of some methods of the KnowledgeStatelessServlet.java.

      The behavior is simple. We assume than the URL called finish with the package name (So, the agent name should be the same as the package).
      We also assume that all released package snapshots have the same name. (For instance, for a package 'my_package1', the rules to use in production are stored in a snapshot called 'release').
      Therefore, the drools-server URL to call by the application should be

      Here is the code for doing this:

      RuleBase getRuleBase(String configName) {
      	if (cachedAgents.containsKey(configName)) {
      		return cachedAgents.get(configName).getRuleBase();
      	} else {
      		synchronized (cachedAgents) {
      			if (!cachedAgents.containsKey(configName)) {
      				//Check for agent configuration existence or create it
      				Properties configProperties = checkAgentConfigExistenz(configName);
      
      				try {
      					RuleAgent ag = RuleAgent.newRuleAgent(configProperties);
      					cachedAgents.put(configName, ag);
      				} catch (NullPointerException npe) {
      					npe.printStackTrace();
      					return null;
      				}
      			}
      			return cachedAgents.get(configName).getRuleBase();
      		}
      	}
      }
      
      protected Properties checkAgentConfigExistenz(String configName) {
      	//Get the folder where resources goes
      	URL root = KnowledgeStatelessServlet.class.getClassLoader().getResource(DEFAULT_SERVER_PROPERTIES_FILE);
      	String rootPath = root.getPath().replace(DEFAULT_SERVER_PROPERTIES_FILE, "");
      	//Get the agent file path
      	String configFilePath = rootPath + configName
      			+ ".properties";
      	File configFile = new File(configFilePath);
      	
      	Properties configProperties = null;
      	
      	//If agent config does not exists, create it using default parameters
      	if (!configFile.exists()) {
      		configProperties = createConfigFile(configName, configFilePath);
      	}
      	
      	return configProperties;
      }
      
      protected Properties createConfigFile(String configName, String configFilePath) {
      	//Get default properties and populate new config file with these guys
      	Properties defaultProperties = loadFromProperties(DEFAULT_SERVER_PROPERTIES_FILE);
      	String releaseName = (String) defaultProperties
      			.get("snapshot.release.name");
      
      	Properties ruleAgentProperties = new Properties();
      	ruleAgentProperties.put("name", configName);
      	ruleAgentProperties.put("newInstance", defaultProperties
      			.get("newInstance"));
      	ruleAgentProperties.put("poll", defaultProperties.get("poll"));
      	String url = ((String) defaultProperties.get("guvnor.url"))
      			+ configName + "/" + releaseName;
      	
      	ruleAgentProperties.put("url", url);
      
      	//Save agent configuration
      	try {
      		FileOutputStream propertiesOutput = new FileOutputStream(configFilePath);
      		
      		try {
      			ruleAgentProperties.store(propertiesOutput, null);
      		} catch (IOException e) {
      			throw new RuntimeDroolsException("Unable to store properties.",
      					e);
      		} finally {
      			try {
      				propertiesOutput.close();
      			} catch (IOException e) {
      				throw new RuntimeDroolsException(
      						"Unable to store properties. Could not close OutputStream.",
      						e);
      			}
      		}
      	} catch (FileNotFoundException e) {
      		throw new RuntimeDroolsException("Unable to store properties.", e);
      	}
      	
      	return ruleAgentProperties;
      }
      
      static Properties loadFromProperties(String propsFileName) {
      	InputStream in = KnowledgeStatelessServlet.class.getClassLoader().getResourceAsStream(propsFileName);
      
      	Properties props = new Properties();
      	try {
      		props.load(in);
      		return props;
      	} catch (IOException e) {
      		throw new RuntimeDroolsException(
      				"Unable to load properties. Needs to be the path and name of a config file on your classpath.",
      				e);
      	} finally {
      		if (null != in) {
      			try {
      				in.close();
      			} catch (IOException e) {
      				throw new RuntimeDroolsException(
      						"Unable to load properties. Could not close InputStream.",
      						e);
      			}
      		}
      	}
      
      }

      Here is a sample default property file

      guvnor.url=http://localhost:9085/drools-guvnor/org.drools.guvnor.Guvnor/package/
      snapshot.release.name=release
      newInstance=true
      poll=60

      Attachments

        Activity

          People

            mproctor@redhat.com Mark Proctor
            regis.ramillien_jira RĂ©gis Ramillien (Inactive)
            Archiver:
            rhn-support-ceverson Clark Everson

            Dates

              Created:
              Updated:
              Archived:

              PagerDuty