git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1210504 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2011-12-05 15:45:46 +00:00
parent 712303e6ec
commit b7e31aca70
1 changed files with 59 additions and 68 deletions

View File

@ -16,35 +16,24 @@
*/ */
package org.apache.activemq.console.command; package org.apache.activemq.console.command;
import java.io.BufferedReader; import org.w3c.dom.Attr;
import java.io.File; import org.w3c.dom.Element;
import java.io.FileInputStream; import org.xml.sax.SAXException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result; import javax.xml.transform.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import java.io.*;
import org.w3c.dom.Attr; import java.nio.ByteBuffer;
import org.w3c.dom.Element; import java.nio.channels.FileChannel;
import org.xml.sax.SAXException; import java.util.List;
public class CreateCommand extends AbstractCommand { public class CreateCommand extends AbstractCommand {
@ -67,17 +56,6 @@ public class CreateCommand extends AbstractCommand {
protected String amqConf = "conf/activemq.xml"; // default conf if no conf is specified via --amqconf protected String amqConf = "conf/activemq.xml"; // default conf if no conf is specified via --amqconf
// default files to copy from activemq home to the new broker instance
protected String[][] fileCopyMap = {
{ "conf/log4j.properties", "conf/log4j.properties" },
{ "conf/broker.ks", "conf/broker.ks" },
{ "conf/broker.ts", "conf/broker.ts" },
{ "conf/camel.xml", "conf/camel.xml" },
{ "conf/jetty.xml", "conf/jetty.xml" },
{ "conf/jetty-realm.properties", "conf/jetty-realm.properties" },
{ "conf/credentials.properties", "conf/credentials.properties" }
};
// default files to create // default files to create
protected String[][] fileWriteMap = { protected String[][] fileWriteMap = {
{ "winActivemq", "bin/${brokerName}.bat" }, { "winActivemq", "bin/${brokerName}.bat" },
@ -117,7 +95,7 @@ public class CreateCommand extends AbstractCommand {
createSubDirs(targetAmqBase, BASE_SUB_DIRS); createSubDirs(targetAmqBase, BASE_SUB_DIRS);
writeFileMapping(targetAmqBase, fileWriteMap); writeFileMapping(targetAmqBase, fileWriteMap);
copyActivemqConf(amqHome, targetAmqBase, amqConf); copyActivemqConf(amqHome, targetAmqBase, amqConf);
copyFileMapping(amqHome, targetAmqBase, fileCopyMap); copyConfDirectory(new File(amqHome, "conf"), new File(targetAmqBase, "conf"));
} }
} }
@ -160,15 +138,6 @@ public class CreateCommand extends AbstractCommand {
} }
} }
protected void copyFileMapping(File srcBase, File targetBase, String[][] fileMapping) throws IOException {
for (String[] fileMap : fileMapping) {
File src = new File(srcBase, fileMap[0]);
File dest = new File(targetBase, resolveParam(BROKER_NAME_REGEX, brokerName, fileMap[1]));
context.print("Copying from: " + src.getCanonicalPath() + "\n to: " + dest.getCanonicalPath());
copyFile(src, dest);
}
}
protected void copyActivemqConf(File srcBase, File targetBase, String activemqConf) throws IOException, ParserConfigurationException, SAXException, TransformerException, XPathExpressionException { protected void copyActivemqConf(File srcBase, File targetBase, String activemqConf) throws IOException, ParserConfigurationException, SAXException, TransformerException, XPathExpressionException {
File src = new File(srcBase, activemqConf); File src = new File(srcBase, activemqConf);
@ -201,7 +170,7 @@ public class CreateCommand extends AbstractCommand {
data = resolveParam("[$][{]activemq.home[}]", amqHome.getCanonicalPath().replaceAll("[\\\\]", "/"), data); data = resolveParam("[$][{]activemq.home[}]", amqHome.getCanonicalPath().replaceAll("[\\\\]", "/"), data);
data = resolveParam("[$][{]activemq.base[}]", targetAmqBase.getCanonicalPath().replaceAll("[\\\\]", "/"), data); data = resolveParam("[$][{]activemq.base[}]", targetAmqBase.getCanonicalPath().replaceAll("[\\\\]", "/"), data);
} else if (typeName.equals("unixActivemq")) { } else if (typeName.equals("unixActivemq")) {
data = unixActivemqData; data = getUnixActivemqData();
data = resolveParam("[$][{]activemq.home[}]", amqHome.getCanonicalPath().replaceAll("[\\\\]", "/"), data); data = resolveParam("[$][{]activemq.home[}]", amqHome.getCanonicalPath().replaceAll("[\\\\]", "/"), data);
data = resolveParam("[$][{]activemq.base[}]", targetAmqBase.getCanonicalPath().replaceAll("[\\\\]", "/"), data); data = resolveParam("[$][{]activemq.base[}]", targetAmqBase.getCanonicalPath().replaceAll("[\\\\]", "/"), data);
} else { } else {
@ -217,9 +186,9 @@ public class CreateCommand extends AbstractCommand {
destinationChannel.close(); destinationChannel.close();
// Set file permissions available for Java 6.0 only // Set file permissions available for Java 6.0 only
// dest.setExecutable(true); dest.setExecutable(true);
// dest.setReadable(true); dest.setReadable(true);
// dest.setWritable(true); dest.setWritable(true);
} }
// utlity method to write an xml source to file // utlity method to write an xml source to file
@ -243,6 +212,23 @@ public class CreateCommand extends AbstractCommand {
destinationChannel.close(); destinationChannel.close();
} }
private void copyConfDirectory(File from, File dest) throws IOException {
if (from.isDirectory()) {
String files[] = from.list();
for (String file : files) {
File srcFile = new File(from, file);
if (srcFile.isFile() && !srcFile.getName().equals("activemq.xml")) {
File destFile = new File(dest, file);
context.print("Copying from: " + srcFile.getCanonicalPath() + "\n to: " + destFile.getCanonicalPath());
copyFile(srcFile, destFile);
}
}
} else {
throw new IOException(from + " is not a directory");
}
}
// replace a property place holder (paramName) with the paramValue // replace a property place holder (paramName) with the paramValue
private String resolveParam(String paramName, String paramValue, String target) { private String resolveParam(String paramName, String paramValue, String target) {
return target.replaceAll(paramName, paramValue); return target.replaceAll(paramName, paramValue);
@ -265,29 +251,34 @@ public class CreateCommand extends AbstractCommand {
+ "%ACTIVEMQ_HOME%/bin/activemq %PARAM%"; + "%ACTIVEMQ_HOME%/bin/activemq %PARAM%";
// Embedded unix script data private String getUnixActivemqData() {
private static final String unixActivemqData = "## Figure out the ACTIVEMQ_BASE from the directory this script was run from\n" StringBuffer res = new StringBuffer();
+ "PRG=\"$0\"\n" res.append("## Figure out the ACTIVEMQ_BASE from the directory this script was run from\n");
+ "progname=`basename \"$0\"`\n" res.append("PRG=\"$0\"\n");
+ "saveddir=`pwd`\n" res.append("progname=`basename \"$0\"`\n");
+ "# need this for relative symlinks\n" res.append("saveddir=`pwd`\n");
+ "dirname_prg=`dirname \"$PRG\"`\n" res.append("# need this for relative symlinks\n");
+ "cd \"$dirname_prg\"\n" res.append("dirname_prg=`dirname \"$PRG\"`\n");
+ "while [ -h \"$PRG\" ] ; do\n" res.append("cd \"$dirname_prg\"\n");
+ " ls=`ls -ld \"$PRG\"`\n" res.append("while [ -h \"$PRG\" ] ; do\n");
+ " link=`expr \"$ls\" : '.*-> \\(.*\\)$'`\n" res.append(" ls=`ls -ld \"$PRG\"`\n");
+ " if expr \"$link\" : '.*/.*' > /dev/null; then\n" res.append(" link=`expr \"$ls\" : '.*-> \\(.*\\)$'`\n");
+ " PRG=\"$link\"\n" res.append(" if expr \"$link\" : '.*/.*' > /dev/null; then\n");
+ " else\n" res.append(" PRG=\"$link\"\n");
+ " PRG=`dirname \"$PRG\"`\"/$link\"\n" res.append(" else\n");
+ " fi\n" res.append(" PRG=`dirname \"$PRG\"`\"/$link\"\n");
+ "done\n" res.append(" fi\n");
+ "ACTIVEMQ_BASE=`dirname \"$PRG\"`/..\n" res.append("done\n");
+ "cd \"$saveddir\"\n" res.append("ACTIVEMQ_BASE=`dirname \"$PRG\"`/..\n");
+ "\n" res.append("cd \"$saveddir\"\n\n");
+ "ACTIVEMQ_BASE=`cd \"$ACTIVEMQ_BASE\" && pwd`\n\n" res.append("ACTIVEMQ_BASE=`cd \"$ACTIVEMQ_BASE\" && pwd`\n\n");
+ "export ACTIVEMQ_HOME=${activemq.home}\n" res.append("## Add system properties for this instance here (if needed), e.g\n");
+ "export ACTIVEMQ_BASE=$ACTIVEMQ_BASE\n\n" res.append("#export ACTIVEMQ_OPTS_MEMORY=\"-Xms256M -Xmx1G\"\n");
+ "${ACTIVEMQ_HOME}/bin/activemq \"$*\""; res.append("#export ACTIVEMQ_OPTS=\"$ACTIVEMQ_OPTS_MEMORY -Dorg.apache.activemq.UseDedicatedTaskRunner=true -Djava.util.logging.config.file=logging.properties\"\n\n");
res.append("export ACTIVEMQ_HOME=${activemq.home}\n");
res.append("export ACTIVEMQ_BASE=$ACTIVEMQ_BASE\n\n");
res.append("${ACTIVEMQ_HOME}/bin/activemq \"$*\"");
return res.toString();
}
} }