modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/FileRecipeCommand.java
| 85 ++++++++++
modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeContext.java
| 18 ++
modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java
| 7
modules/common/filetemplate-bundle/src/test/java/org/rhq/bundle/filetemplate/recipe/RecipeParserTest.java
| 14 +
modules/plugins/filetemplate-bundle/src/main/java/org/rhq/plugins/filetemplate/ProcessingRecipeContext.java
| 11 +
5 files changed, 134 insertions(+), 1 deletion(-)
New commits:
commit 3c52854a359fa4fb4fae21fc4d0b9323d0ee800b
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Tue Mar 2 19:03:52 2010 -0500
support the file recipe command
diff --git
a/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/FileRecipeCommand.java
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/FileRecipeCommand.java
new file mode 100644
index 0000000..3b74805
--- /dev/null
+++
b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/FileRecipeCommand.java
@@ -0,0 +1,85 @@
+/*
+ * 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 gnu.getopt.Getopt;
+import gnu.getopt.LongOpt;
+
+public class FileRecipeCommand implements RecipeCommand {
+
+ public String getName() {
+ return "file";
+ }
+
+ public void parse(RecipeContext context, String[] args) {
+ String sopts = ":s:d:";
+ LongOpt[] lopts = { new LongOpt("source", LongOpt.REQUIRED_ARGUMENT,
null, 's'),
+ new LongOpt("destination", LongOpt.REQUIRED_ARGUMENT, null,
'd') };
+
+ String source = null;
+ String destination = null;
+
+ Getopt getopt = new Getopt(context.toString(), args, sopts, lopts);
+ int code;
+
+ while ((code = getopt.getopt()) != -1) {
+ switch (code) {
+ case ':':
+ case '?': {
+ throw new IllegalArgumentException("Bad recipe command.");
+ }
+
+ case 1: {
+ throw new IllegalArgumentException("Bad recipe command!");
+ }
+
+ case 's': {
+ source = getopt.getOptarg();
+ break;
+ }
+
+ case 'd': {
+ destination = getopt.getOptarg();
+ break;
+ }
+
+ default: {
+ throw new IllegalArgumentException("Unexpected error in recipe
command");
+ }
+
+ }
+ }
+
+ if (source == null) {
+ throw new IllegalArgumentException("Did not specify the source file to
copy");
+ }
+
+ if (destination == null) {
+ throw new IllegalArgumentException("Did not specify the destination
where the file should be copied");
+ }
+
+ context.addFile(source, destination);
+ 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 fb911f3..dcea274 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
@@ -42,6 +42,7 @@ public class RecipeContext {
private final String recipe;
private final Map<String, String> deployFiles;
+ private final Map<String, String> files;
private final Set<String> replacementVariables;
private Configuration replacementVariableValues;
private final List<Script> scripts;
@@ -49,6 +50,7 @@ public class RecipeContext {
public RecipeContext(String recipe) {
this.recipe = recipe;
this.deployFiles = new HashMap<String, String>();
+ this.files = new HashMap<String, String>();
this.replacementVariables = new HashSet<String>();
this.scripts = new ArrayList<Script>();
}
@@ -77,6 +79,22 @@ public class RecipeContext {
}
/**
+ * Returns all the files that are strictly to be copied to some location.
+ * The key is the bundle file's name, the value is the full path and filename
where
+ * the file should be copied to. No processing of these files will occur other than
+ * they are copied.
+ *
+ * @return map of all files to be deployed and the location where they are to be
deployed
+ */
+ public Map<String, String> getFiles() {
+ return this.files;
+ }
+
+ public void addFile(String source, String destination) {
+ this.files.put(source, destination);
+ }
+
+ /**
* The names of all replacement variables that are found in the recipe.
*
* @return the replacement variables
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 d18fd4b..13644e6 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
@@ -111,7 +111,11 @@ public class RecipeParser {
context.addReplacementVariables(replacementVars);
}
- recipeCommand.parse(context, arguments);
+ try {
+ recipeCommand.parse(context, arguments);
+ } catch (Exception e) {
+ throw new Exception("Error in recipe, line [" + line +
"]", e);
+ }
return;
}
@@ -120,6 +124,7 @@ public class RecipeParser {
HashMap<String, RecipeCommand> commands = new HashMap<String,
RecipeCommand>();
RecipeCommand[] knownCommands = new RecipeCommand[] { new ScriptRecipeCommand(),
//
+ new FileRecipeCommand(), //
new DeployRecipeCommand() //
};
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
index 40bc7fb..7d5abef 100644
---
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
@@ -40,6 +40,20 @@ public class RecipeParserTest {
cleanRecipe();
}
+ public void testFileRecipe() throws Exception {
+ addRecipeCommand("file --source=run-me.sh --destination=/opt/run.sh");
+ addRecipeCommand("file -s META-INF/another.xml -d /etc/another.xml");
+ RecipeParser parser = new RecipeParser();
+ RecipeContext context = new RecipeContext(getRecipe());
+ parser.parseRecipe(context);
+ Map<String, String> files = context.getFiles();
+ assert files.size() == 2 : files;
+ assert files.get("run-me.sh") != null : files;
+ assert files.get("run-me.sh").equals("/opt/run.sh") : files;
+ assert files.get("META-INF/another.xml") != null : files;
+ assert
files.get("META-INF/another.xml").equals("/etc/another.xml") : files;
+ }
+
public void testScriptRecipe() throws Exception {
addRecipeCommand("script run-me.sh");
addRecipeCommand("script lots-o-params.sh -d /opt/jboss -- foo.txt
\"hello world\"");
diff --git
a/modules/plugins/filetemplate-bundle/src/main/java/org/rhq/plugins/filetemplate/ProcessingRecipeContext.java
b/modules/plugins/filetemplate-bundle/src/main/java/org/rhq/plugins/filetemplate/ProcessingRecipeContext.java
index 2e0d1dc..b0ae578 100644
---
a/modules/plugins/filetemplate-bundle/src/main/java/org/rhq/plugins/filetemplate/ProcessingRecipeContext.java
+++
b/modules/plugins/filetemplate-bundle/src/main/java/org/rhq/plugins/filetemplate/ProcessingRecipeContext.java
@@ -76,6 +76,17 @@ public class ProcessingRecipeContext extends RecipeContext {
}
@Override
+ public void addFile(String filename, String directory) {
+ super.addDeployFile(filename, directory);
+
+ File existingFile = new File(this.baseWorkingDirectory, filename);
+ File newFile = new File(directory, filename);
+ if (!existingFile.renameTo(newFile)) {
+ throw new RuntimeException("Failed to move file [" + existingFile +
"] to [" + newFile + "]");
+ }
+ }
+
+ @Override
public void addReplacementVariables(Set<String> replacementVariables) {
super.addReplacementVariables(replacementVariables);
}