modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigDefRecipeCommand.java | 81 ++++++++++ modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/RecipeParser.java | 1 modules/common/filetemplate-bundle/src/test/java/org/rhq/bundle/filetemplate/recipe/RecipeParserTest.java | 36 ++++ modules/core/util/src/main/java/org/rhq/core/template/TemplateEngine.java | 8 4 files changed, 125 insertions(+), 1 deletion(-)
New commits: commit 716420b870b9eb6ce9a6b39cd6973cafbf0267e1 Merge: 0eb9524... 9b6a941... Author: John Mazzitelli mazz@redhat.com Date: Tue Mar 23 09:20:21 2010 -0400
Merge branch 'gwt' of ssh://git.fedorahosted.org/git/rhq/rhq into gwt
commit 0eb9524ead03b9323a27aa6dc3c18f8fe49526b4 Author: John Mazzitelli mazz@redhat.com Date: Tue Mar 23 09:19:40 2010 -0400
add new configdef recipe command so we can specify a replacement variable that may exist in bundle files that are to be realized
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 new file mode 100644 index 0000000..54f297c --- /dev/null +++ b/modules/common/filetemplate-bundle/src/main/java/org/rhq/bundle/filetemplate/recipe/ConfigDefRecipeCommand.java @@ -0,0 +1,81 @@ +/* + * 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; + +import java.util.HashSet; +import java.util.Set; + +public class ConfigDefRecipeCommand implements RecipeCommand { + + public String getName() { + return "configdef"; + } + + public void parse(RecipeParser parser, RecipeContext context, String[] args) { + String sopts = ":n:"; + LongOpt[] lopts = { new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null, 'n') }; + + String replacementVariableName = null; + + Getopt getopt = new Getopt(getName(), 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 'n': { + replacementVariableName = getopt.getOptarg(); + break; + } + + default: { + throw new IllegalArgumentException("Unexpected error in recipe command"); + } + + } + } + + if (replacementVariableName == null) { + throw new IllegalArgumentException( + "Did not specify the name of the configuration definition replacement variable"); + } + + Set<String> replacementVariableNames = new HashSet<String>(1); + replacementVariableNames.add(replacementVariableName); + context.addReplacementVariables(replacementVariableNames); + + return; + } +} 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 d48c3ba..ea9464d 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 @@ -129,6 +129,7 @@ public class RecipeParser { HashMap<String, RecipeCommand> commands = new HashMap<String, RecipeCommand>();
RecipeCommand[] knownCommands = new RecipeCommand[] { new ScriptRecipeCommand(), // + new ConfigDefRecipeCommand(), // new CommandRecipeCommand(), // new FileRecipeCommand(), // new RealizeRecipeCommand(), // 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 bfa58e6..d36831b 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,16 @@ public class RecipeParserTest { cleanRecipe(); }
+ public void testConfigDefRecipe() throws Exception { + addRecipeCommand("configdef -n custom.prop"); + RecipeParser parser = new RecipeParser(); + RecipeContext context = new RecipeContext(getRecipe()); + parser.parseRecipe(context); + Set<String> vars = context.getReplacementVariables(); + assert vars.size() == 1 : vars; + assert vars.contains("custom.prop"); + } + public void testRealizeRecipe() throws Exception { addRecipeCommand("realize --file=<%opt.dir%>/config.ini"); RecipeParser parser = new RecipeParser(); @@ -114,6 +124,32 @@ public class RecipeParserTest { assert files.get("jboss3.tar").equals(SystemInfoFactory.createSystemInfo().getHostname() + "/jboss") : files; }
+ public void testSimpleRecipeReplaceJavaSystemPropertyReplacementVariables() throws Exception { + addRecipeCommand("deploy -f jboss1.tar -d <%java.io.tmpdir%>"); + addRecipeCommand("deploy -f jboss2.tar -d <%custom.sysprop%>"); + RecipeParser parser = new RecipeParser(); + parser.setReplaceReplacementVariables(true); + RecipeContext context = new RecipeContext(getRecipe()); + parser.parseRecipe(context); + Map<String, String> files = context.getDeployFiles(); + assert files.containsKey("jboss1.tar") : files; + assert files.get("jboss1.tar").equals(System.getProperty("java.io.tmpdir")) : files; + assert files.containsKey("jboss2.tar") : files; + assert files.get("jboss2.tar").equals("<%custom.sysprop%>") : files; + + // now set our custom system property and see that it gets replaced properly + System.setProperty("custom.sysprop", "MY/CUSTOM/PROPERTY/HERE"); + parser = new RecipeParser(); + parser.setReplaceReplacementVariables(true); + context = new RecipeContext(getRecipe()); + parser.parseRecipe(context); + files = context.getDeployFiles(); + assert files.containsKey("jboss1.tar") : files; + assert files.get("jboss1.tar").equals(System.getProperty("java.io.tmpdir")) : files; + assert files.containsKey("jboss2.tar") : files; + assert files.get("jboss2.tar").equals("MY/CUSTOM/PROPERTY/HERE") : files; + } + public void testSimpleRecipe() throws Exception { addRecipeCommand("deploy -f jboss.tar -d /opt/jboss"); addRecipeCommand("deploy -f tomcat.tar -d /opt/tomcat");
commit c165b0103c5171b004700db50363d9bc56632931 Author: John Mazzitelli mazz@redhat.com Date: Tue Mar 23 09:18:44 2010 -0400
all for recipe replacement variables to represent Java system properties
diff --git a/modules/core/util/src/main/java/org/rhq/core/template/TemplateEngine.java b/modules/core/util/src/main/java/org/rhq/core/template/TemplateEngine.java index 113771d..6f2a3e0 100644 --- a/modules/core/util/src/main/java/org/rhq/core/template/TemplateEngine.java +++ b/modules/core/util/src/main/java/org/rhq/core/template/TemplateEngine.java @@ -5,7 +5,7 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern;
-public class TemplateEngine implements Serializable{ +public class TemplateEngine implements Serializable { /** * */ @@ -48,6 +48,12 @@ public class TemplateEngine implements Serializable{ String value = tokens.get(key); if (value != null) { next = value; + } else { + // fallback is to see if the replacement key represents a Java system prop + value = System.getProperty(key); + if (value != null) { + next = value; + } } } //If we didn't find a replacement for the key
rhq-commits@lists.fedorahosted.org