modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigdefRecipeCommand.java
| 14 -
modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/DeployRecipeCommand.java
| 17 +
modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeContext.java
| 37 +++
modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java
| 29 +-
modules/common/filetemplate-bundle/src/test/java/org/rhq/bundle/filetemplate/recipe/RecipeParserTest.java
| 101 ++++++++++
modules/enterprise/server/plugins/filetemplate-bundle/src/main/java/org/rhq/enterprise/server/plugins/filetemplate/BundleServerPluginComponent.java
| 2
6 files changed, 180 insertions(+), 20 deletions(-)
New commits:
commit c0c6ae1ec0cfe537d9980b9015b5b757de7ac4d6
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Mon Mar 1 15:10:29 2010 -0500
get configdef and deploy to parse - create unit test - support blank/comment lines
diff --git
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigdefRecipeCommand.java
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigdefRecipeCommand.java
index 057d474..029222b 100644
---
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigdefRecipeCommand.java
+++
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigdefRecipeCommand.java
@@ -45,11 +45,11 @@ public class ConfigdefRecipeCommand implements RecipeCommand {
switch (code) {
case ':':
case '?': {
- throw new IllegalArgumentException("Bad recipe command [" +
getName() + "]: " + args);
+ throw new IllegalArgumentException("Bad recipe argument.");
}
case 1: {
- throw new IllegalArgumentException("Bad recipe command [" +
getName() + "]: " + args);
+ throw new IllegalArgumentException("Bad recipe argument!");
}
case 'f': {
@@ -58,12 +58,16 @@ public class ConfigdefRecipeCommand implements RecipeCommand {
}
default: {
- throw new IllegalArgumentException("Unexpected error in recipe
command [" + getName() + "]: " + args);
+ throw new IllegalArgumentException("Unexpected error in recipe
command");
}
-
}
}
- // TODO : do something with filename
+ if (filename == null) {
+ throw new IllegalArgumentException("Missing the -f argument to indicate
what the config def filename is");
+ }
+
+ context.setConfigurationDefinitionFilename(filename);
+ return;
}
}
diff --git
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/DeployRecipeCommand.java
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/DeployRecipeCommand.java
index 145398c..5f14f2f 100644
---
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/DeployRecipeCommand.java
+++
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/DeployRecipeCommand.java
@@ -47,11 +47,11 @@ public class DeployRecipeCommand implements RecipeCommand {
switch (code) {
case ':':
case '?': {
- throw new IllegalArgumentException("Bad recipe command [" +
getName() + "]: " + args);
+ throw new IllegalArgumentException("Bad recipe command.");
}
case 1: {
- throw new IllegalArgumentException("Bad recipe command [" +
getName() + "]: " + args);
+ throw new IllegalArgumentException("Bad recipe command!");
}
case 'f': {
@@ -65,12 +65,21 @@ public class DeployRecipeCommand implements RecipeCommand {
}
default: {
- throw new IllegalArgumentException("Unexpected error in recipe
command [" + getName() + "]: " + args);
+ throw new IllegalArgumentException("Unexpected error in recipe
command");
}
}
}
- // TODO : do something with filename and directory
+ if (filename == null) {
+ throw new IllegalArgumentException("Did not specify the name of the file
to deploy");
+ }
+
+ if (directory == null) {
+ throw new IllegalArgumentException("Did not specify the directory where
the file should be deployed");
+ }
+
+ context.addDeployFile(filename, directory);
+ return;
}
}
diff --git
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeContext.java
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeContext.java
index da96ceb..92c204e 100644
---
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeContext.java
+++
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeContext.java
@@ -23,15 +23,24 @@
package org.rhq.bundle.filetemplate.recipe;
+import java.util.HashMap;
+import java.util.Map;
+
/**
+ * Contains information that is gleened from a recipe after it is parsed.
+ *
* @author John Mazzitelli
*/
public class RecipeContext {
private final String recipe;
+ private String configurationDefinitionFilename;
+ private Map<String, String> deployFiles;
public RecipeContext(String recipe) {
this.recipe = recipe;
+ this.configurationDefinitionFilename = null;
+ this.deployFiles = new HashMap<String, String>();
}
/**
@@ -42,4 +51,32 @@ public class RecipeContext {
public String getRecipe() {
return recipe;
}
+
+ /**
+ * A recipe can have an optional configuration definition file associated with it.
+ * If this returns non-<code>null</code>, it refers to this configuration
definition file.
+ *
+ * @return config def filename
+ */
+ public String getConfigurationDefinitionFilename() {
+ return configurationDefinitionFilename;
+ }
+
+ public void setConfigurationDefinitionFilename(String filename) {
+ this.configurationDefinitionFilename = filename;
+ }
+
+ /**
+ * Returns all the files that are to be deployed. The key is the bundle file's
name, the
+ * value is the directory where the bundle is to be deployed to.
+ *
+ * @return map of all files to be deployed and the location where they are to be
deployed
+ */
+ public Map<String, String> getDeployFiles() {
+ return this.deployFiles;
+ }
+
+ public void addDeployFile(String filename, String directory) {
+ this.deployFiles.put(filename, directory);
+ }
}
diff --git
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java
index ff9b91f..b52e807 100644
---
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java
+++
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java
@@ -52,21 +52,30 @@ public class RecipeParser {
BufferedReader recipeReader = new BufferedReader(new StringReader(recipe));
String line = recipeReader.readLine();
while (line != null) {
- String[] commandLineArray = splitCommandLine(line);
- String commandName = commandLineArray[0];
- String[] arguments = extractArguments(commandLineArray);
+ parseRecipeCommandLine(context, line);
+ line = recipeReader.readLine();
+ }
- RecipeCommand recipeCommand = recipeCommands.get(commandName);
- if (recipeCommand == null) {
- throw new Exception("Unknown command in recipe: " +
commandName);
- }
+ return context;
+ }
- recipeCommand.parse(context, arguments);
+ public void parseRecipeCommandLine(RecipeContext context, String line) throws
Exception {
+ // ignore blank lines or comment lines that start with #
+ if (line == null || line.trim().length() == 0 || line.startsWith("#"))
{
+ return;
+ }
- line = recipeReader.readLine();
+ String[] commandLineArray = splitCommandLine(line);
+ String commandName = commandLineArray[0];
+ String[] arguments = extractArguments(commandLineArray);
+
+ RecipeCommand recipeCommand = recipeCommands.get(commandName);
+ if (recipeCommand == null) {
+ throw new Exception("Unknown command in recipe: " + commandName);
}
- return context;
+ recipeCommand.parse(context, arguments);
+ return;
}
private void setupRecipeCommands() {
diff --git
a/modules/common/filetemplate-bundle/src/test/java/org/rhq/bundle/filetemplate/recipe/RecipeParserTest.java
b/modules/common/filetemplate-bundle/src/test/java/org/rhq/bundle/filetemplate/recipe/RecipeParserTest.java
new file mode 100644
index 0000000..d573072
--- /dev/null
+++
b/modules/common/filetemplate-bundle/src/test/java/org/rhq/bundle/filetemplate/recipe/RecipeParserTest.java
@@ -0,0 +1,101 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2010 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation, and/or the GNU Lesser
+ * General Public License, version 2.1, also as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License and the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the GNU Lesser General Public License along with this program;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.rhq.bundle.filetemplate.recipe;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Test
+public class RecipeParserTest {
+ private StringBuilder recipe;
+
+ @BeforeMethod
+ public void cleanRecipeBeforeMethod() {
+ cleanRecipe();
+ }
+
+ public void testSimpleRecipe() throws Exception {
+ addRecipeCommand("deploy -f jboss.tar -d /opt/jboss");
+ addRecipeCommand("configdef -f config.xml");
+ RecipeParser parser = new RecipeParser();
+ RecipeContext context = parser.parseRecipe(getRecipe());
+ assert
context.getConfigurationDefinitionFilename().equals("config.xml");
+ assert context.getDeployFiles().containsKey("jboss.tar");
+ assert
context.getDeployFiles().get("jboss.tar").equals("/opt/jboss");
+ }
+
+ public void testSimpleRecipeError() throws Exception {
+ addRecipeCommand("deploy -f jboss.tar");
+ RecipeParser parser = new RecipeParser();
+
+ try {
+ parser.parseRecipe(getRecipe());
+ assert false : "This should have failed - need to provide a -d to the
deploy command";
+ } catch (Exception ok) {
+ // to be expected
+ }
+
+ cleanRecipe();
+ addRecipeCommand("configdef");
+
+ try {
+ parser.parseRecipe(getRecipe());
+ assert false : "This should have failed - need to provide a -f to the
configdef command";
+ } catch (Exception ok) {
+ // to be expected
+ }
+ }
+
+ public void testSimpleRecipeComments() throws Exception {
+ addRecipeCommand("");
+ addRecipeCommand("#");
+ addRecipeCommand("### comment here");
+ addRecipeCommand("deploy -f jboss.tar -d /opt/jboss");
+ addRecipeCommand("");
+ addRecipeCommand("#");
+ addRecipeCommand("### comment here");
+ addRecipeCommand("configdef -f config.xml");
+ addRecipeCommand("");
+ addRecipeCommand("#");
+ addRecipeCommand("### comment here");
+ addRecipeCommand("");
+ RecipeParser parser = new RecipeParser();
+ RecipeContext context = parser.parseRecipe(getRecipe());
+ assert
context.getConfigurationDefinitionFilename().equals("config.xml");
+ assert context.getDeployFiles().containsKey("jboss.tar");
+ assert
context.getDeployFiles().get("jboss.tar").equals("/opt/jboss");
+ }
+
+ private void cleanRecipe() {
+ this.recipe = new StringBuilder();
+ }
+
+ private void addRecipeCommand(String commandLine) {
+ this.recipe.append(commandLine).append("\n");
+ }
+
+ private String getRecipe() {
+ return this.recipe.toString();
+ }
+}
diff --git
a/modules/enterprise/server/plugins/filetemplate-bundle/src/main/java/org/rhq/enterprise/server/plugins/filetemplate/BundleServerPluginComponent.java
b/modules/enterprise/server/plugins/filetemplate-bundle/src/main/java/org/rhq/enterprise/server/plugins/filetemplate/BundleServerPluginComponent.java
index 96f7070..a67fc1a 100644
---
a/modules/enterprise/server/plugins/filetemplate-bundle/src/main/java/org/rhq/enterprise/server/plugins/filetemplate/BundleServerPluginComponent.java
+++
b/modules/enterprise/server/plugins/filetemplate-bundle/src/main/java/org/rhq/enterprise/server/plugins/filetemplate/BundleServerPluginComponent.java
@@ -68,7 +68,7 @@ public class BundleServerPluginComponent implements
ServerPluginComponent, Bundl
RecipeParser parser = new RecipeParser();
RecipeContext parserContext = parser.parseRecipe(recipe);
- // TODO convert the context to the results object
+ // TODO convert the context into the proper data that the parse results wants
ConfigurationDefinition configDef = null;
Set<String> bundleFileNames = null;