📄 raconfig.java
字号:
}
}
// extract the fileName from InputStream
// in the tmp directory.
if (res != null)
createFile(fileName,res);
zipFile.close();
}
}
/**
* Extract fileName from the JAR file.
* @param fileName file name
* @param reader Input stream
* @throws Exception to throw if an Exception occurs
*/
private InputStream extractFromJAR(String fileName, InputStream reader)
throws Exception {
if (debug)
System.out.println("RAConfig.extractFromJAR(" + fileName + "," + reader + ")");
ZipInputStream stream = new ZipInputStream(reader);
ZipEntry currEntry = (ZipEntry) stream.getNextEntry();
while (stream.available() > 0) {
if (currEntry == null) break;
if (currEntry.getName().equalsIgnoreCase(fileName)) {
// the fileName is found, return the InputStream.
if (debug)
System.out.println("RAConfig.extractFromJAR : currEntry = " + currEntry);
else if (verbose)
System.out.println("extract \"" + fileName + "\" from JAR.");
return stream;
}
currEntry = (ZipEntry) stream.getNextEntry();
}
// close the stream and return null if file not found.
stream.close();
return null;
}
/**
* Extract config files from the JAR file.
* @param jarName JAR file name
* @param fileName file to be extract
* @throws Exception to throw if an Exception occurs
*/
private void extractFromJAR(String jarName, String fileName)
throws Exception {
if (debug)
System.out.println("RAConfig.extractFromJAR(" + jarName + "," + fileName + ")");
else if (verbose)
System.out.println("extract \"" + fileName + "\" from \"" + jarName + "\"");
JarFile jar = new JarFile(jarName);
ZipEntry entry = jar.getEntry(fileName);
if (debug)
System.out.println("RAConfig.extractFromJAR : entry = " + entry);
// extract the fileName from jar in the tmp directory.
if (entry != null)
createFile(fileName,jar.getInputStream(entry));
jar.close();
}
/**
* write the inputstream in outputstream.
* @param is input stream
* @param os output stream
* @throws Exception to throw if an Exception occurs
*/
private void dump(InputStream is, OutputStream os) throws Exception {
int n = 0;
byte[] buffer = new byte[BUFFER_SIZE];
n = is.read(buffer);
while (n > 0) {
os.write(buffer, 0, n);
n = is.read(buffer);
}
}
private String getFileName(String path) throws Exception {
int i = path.lastIndexOf("/");
if (i > 0)
return path.substring(i+1,path.length());
i = path.lastIndexOf("\\");
if (i > 0)
return path.substring(i+1,path.length());
return path;
}
/**
* create the filename in the tmp directory
* by writing the input stream in the file name.
* @param path new file
* @param is input stream
* @throws Exception to throw if an Exception occurs
*/
private void createFile(String path, InputStream is) throws Exception {
if (debug)
System.out.println("RAConfig.createFile(" + path + "," + is + ")");
String fileName = tmpDir + getFileName(path);
if (debug)
System.out.println("RAConfig.createFile : fileName = " + fileName);
else if (verbose)
System.out.println("create file \"" + fileName + "\"");
new File(fileName).delete();
FileOutputStream fos = new FileOutputStream(fileName);
try {
dump(is,fos);
} finally {
fos.close();
}
}
/**
* create the filename in the tmp directory,
* by writing the input in the file name.
* @param path new file
* @param input string to write
* @throws Exception to throw if an Exception occurs
*/
private void createFile(String path, String input) throws Exception {
if (debug)
System.out.println("RAConfig.createFile(" + path + "," + input + ")");
String fileName = tmpDir + getFileName(path);
if (debug)
System.out.println("RAConfig.createFile : fileName = " + fileName);
else if (verbose)
System.out.println("create file \"" + fileName + "\"");
new File(fileName).delete();
ByteArrayInputStream bis = new ByteArrayInputStream(input.getBytes());
FileOutputStream fos = new FileOutputStream(fileName);
try {
dump(bis,fos);
} finally {
fos.close();
bis.close();
}
}
/**
* parse file name.
* @param fileName file
* @throws Exception to throw if an Exception occurs
*/
private String parse(String fileName) throws Exception {
if (debug)
System.out.println("RAConfig.parse(" + fileName + ")");
else if (verbose)
System.out.println("parse \"" + fileName + "\"");
Wrapper wrapper = new Wrapper(debug);
FileInputStream fis = new FileInputStream(fileName);
return wrapper.parse(fis);
}
/**
* parse input stream.
* @param is input stream
* @throws Exception to throw if an Exception occurs
*/
private String parse(InputStream is) throws Exception {
if (debug)
System.out.println("RAConfig.parse(" + is + ")");
Wrapper wrapper = new Wrapper(debug);
return wrapper.parse(is);
}
/**
* update input stream with map value.
* @param is input stream
* @param map map value (see ra.properties)
* @throws Exception to throw if an Exception occurs
*/
private String update(InputStream is, Map map) throws Exception {
if (debug)
System.out.println("RAConfig.update(" + is + "," + map + ")");
Wrapper wrapper = new Wrapper(debug);
return wrapper.update(is,map);
}
/**
* update host/port in ra.xml and a3server.xml in RAR.
* @param rarName rar file name
* @param hostName new host name
* @param port new port
* @param serverId server Id
*/
private void updateHostPort(String rarName,
String hostName,
String port,
short serverId) throws Exception {
if (debug)
System.out.println("RAConfig.updateHostPort(" + rarName +
"," + hostName +
"," + port +
"," + serverId + ")");
else if (verbose)
System.out.println("update (ra.xml and a3server.xml) in \"" + rarName +
"\" with host=" + hostName +
" port=" + port +
" serverId=" + serverId);
// update ra.xml file
File file = new File(rarName);
StringBuffer buff = new StringBuffer();
buff.append("RAR_NAME ");
buff.append(file.getAbsolutePath());
buff.append("\n[org.objectweb.joram.client.connector.JoramAdapter]");
buff.append("\nHostName ");
buff.append(hostName);
buff.append("\nServerPort ");
buff.append(port);
buff.append("\nServerId ");
buff.append(serverId);
// create temporary raProperty file
String tempFile = "ra.properties_tmp";
createFile(tempFile,buff.toString());
updateRAR(tmpDir + tempFile);
new File(tmpDir + tempFile).delete();
}
/**
* update A3SERVERS_XML file
* @param rarName rar file name
* @param hostName new host name
* @param port new port
* @param serverId server Id
*/
private void updateA3Servers(String rarName,
String hostName,
String port,
short serverId)
throws Exception {
if (debug)
System.out.println("RAConfig.updateA3Servers(" + rarName +
"," + hostName +
"," + port +
"," + serverId + ")");
else if (verbose)
System.out.println("update (a3server.xml) in \"" + rarName +
"\" host=" + hostName +
" port=" + port +
" serverId=" + serverId);
// extract the joram-config.jar file from RAR in the temp dir
extractFromRAR(rarName,JORAM_CONFIG_JAR);
// extract the a3servers.xml file from joram-config.jar in the tmp dir
extractFromJAR(tmpDir + JORAM_CONFIG_JAR, A3SERVERS_XML);
// if present the a3server.xml source is the confdir one
// otherwise, we take the RAR one
if (confDir != null) {
File f = new File(confDir, A3SERVERS_XML);
if (f.exists()) {
copy(f.getPath(), tmpDir + A3SERVERS_XML);
}
}
// update A3SERVERS_XML
A3CMLConfig conf = A3CML.getXMLConfig(tmpDir + A3SERVERS_XML);
A3CMLServer server = conf.getServer(serverId);
server.hostname = hostName;
A3CMLService service =
server.getService("org.objectweb.joram.mom.proxies.tcp.TcpProxyService");
service.args = port;
// write changes to A3SERVERS_XML file
A3CML.toXML(conf,null, tmpDir + A3SERVERS_XML);
if (debug)
System.out.println("RAConfig.updateA3Servers : confDir=" + confDir);
// update file in conf dir.
if (confDir != null) {
File f = new File(confDir, A3SERVERS_XML);
if (f.exists())
copy(tmpDir + A3SERVERS_XML, f.getPath());
if (new File(confDir, JORAMADMIN_CFG).exists())
updateJoramAdminCfg(hostName, port);
if (new File(confDir, JORAMADMIN_XML).exists())
updateJoramAdminXml(hostName, port);
}
// update jar
updateZIP(tmpDir + JORAM_CONFIG_JAR,A3SERVERS_XML,tmpDir + A3SERVERS_XML,A3SERVERS_XML);
// update rar
updateZIP(rarName,JORAM_CONFIG_JAR,tmpDir +JORAM_CONFIG_JAR,JORAM_CONFIG_JAR);
// remove temporary file
new File(tmpDir + JORAM_CONFIG_JAR).delete();
new File(tmpDir + A3SERVERS_XML).delete();
}
private boolean copy(String file1, String file2)
throws Exception {
if (! new File(file1).exists())
return false;
new File(file2).delete();
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
try {
dump(fis,fos);
return true;
} finally {
fos.close();
fis.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -