WSO2 ESB / EI / AM 汉化方法
Posted 菠萝蚊鸭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WSO2 ESB / EI / AM 汉化方法相关的知识,希望对你有一定的参考价值。
WSO2 ESB / EI / AM 汉化方法
一、参考文章
最近看了文章 WSO2 ESB企业服务总线汉化源码分享和实现原理,想着能不能把 WSO2 EI 6.6.0 也给汉化了,看了这篇博客发现 ESB 和 EI 是一样的。整理了一下代码,重新写了使用百度翻译来翻译 WSO2 EI 6.6.0,同时增加了一些术语和常用词的翻译(原博客的代码使用的是谷歌翻译,该博客原源代码:updjarutils)。
百度翻译 API :百度翻译开放平台,普通版免费,但是 QPS 只有 1,高级版每月前一百万字符免费。
目前验证过使用该方法汉化后的 jar 包,WSO2 ESB 5.0.0、 WSO2 EI 6.6.0 和 WSO2 AM 4.0.0 可以正常运行
二、依赖
commons-codec-1.15.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
fastjson-1.2.83.jar
log4j-1.2.17.jar
slf4j-api-1.7.30.jar
slf4j-log4j12-1.7.30.jar
三、代码
WSO2 EI 6.6.0 的 ui 包在 WSO2 EI 6.6.0 根目录下的 wso2ei_home\\wso2\\components\\plugins
文件夹下
1、获取名称带有 ui 的 jar 文件列表和请求翻译 API
package com.yoodb.wso2ei;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.yoodb.wso2ei.util.LoadTerminology;
/**
* 解压jar包,读取jar包里的Resource.properties文件,读取到配置文件内容,请求百度翻译API,获取翻译后的内容
* 把翻译后的内容更新到Resource.properties文件,然后再把解压后的文件夹压缩成jar包
*
* @author mrwang
*
*/
public class UpdJarContent
private static Logger log = LoggerFactory.getLogger(UpdJarContent.class);
public JSONObject updJarContent(SignUtils sign, FileOutputStream logfile, String... proName) throws Exception
JSONObject tempJsonObject = new JSONObject();
if (!isExists(sign.getJarNewPath()))
List<String> filePathlist = new ArrayList<String>();
HashMap<String, String> terminology = LoadTerminology.getTerminology();
for (int i = 0; i < proName.length; i++)
Decompression.uncompress(new File(sign.getJarPath()), new File(sign.getFilePath()));
ArrayList<String> filelist = new ArrayList<String>();
getFiles(sign.getFilePath(), filelist);
for (String string : filelist)
if (string.contains(proName[i]))
logfile.write((string).getBytes());
logfile.write("\\r\\n".getBytes());
filePathlist.add(string);
Properties pro = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(string));
pro.load(in);
Iterator<String> it = pro.stringPropertyNames().iterator();
FileOutputStream yfile = new FileOutputStream(string);
FileOutputStream originalfile = new FileOutputStream(
string.replace(proName[i], "original_file"));
pro.store(originalfile, "file update...");
while (it.hasNext())
String key = it.next();
String value = pro.getProperty(key);
log.info("key-->" + key + " value-->" + value);
String cntext = null;
if (terminology.containsKey(value.toLowerCase()))
cntext = terminology.get(value.toLowerCase());
else
try
if (!isEmpty(value))
cntext = TranslateByBaiDuUtil.translate(value, TranslateByBaiDuUtil.ENGLISH, TranslateByBaiDuUtil.CHINA);
// Thread.sleep(1000);
catch (Exception e)
// TODO: handle exception
log.error(e.getMessage());
Thread.sleep(20000);
if (!isEmpty(value))
cntext = TranslateByBaiDuUtil.translate(value, TranslateByBaiDuUtil.ENGLISH, TranslateByBaiDuUtil.CHINA);
log.info("key-->" + key + " value-->" + cntext);
if (isEmpty(cntext))
pro.put(key, "");
else
pro.put(key, cntext);
if(!isEmpty(value) && !isEmpty(cntext))
tempJsonObject.put(value, cntext);
Thread.sleep(10);
pro.store(yfile, "file update...");
yfile.close();
originalfile.close();
in.close();
Compressor zc = new Compressor(sign.getJarNewPath());
zc.setOriginalUrl(sign.getOriginalUrl());
zc.compress(sign.getFilePath());
log.info("恭喜修改压缩文件成功!!!!,修改文件如下:");
for (String string : filePathlist)
log.info("update file path is [" + string + "]");
log.info("翻译后的值:" + tempJsonObject.toJSONString());
else
log.info("jar包已经存在--->" + sign.getJarNewPath());
return tempJsonObject;
/**
* 将文件夹下所有文件存储
*
* @param filePath
* @param filelist
*/
public static ArrayList<String> getFiles(String filePath, ArrayList<String> filelist)
File root = new File(filePath);
File[] files = root.listFiles();
for (File file : files)
if (file.isDirectory())
getFiles(file.getAbsolutePath(), filelist);
else
filelist.add(file.getAbsolutePath());
return filelist;
/**
* 判断字符串是否为空
* @param string 输入字符串
* @return 为空返回true,不为空返回false
*/
public boolean isEmpty(String string)
if (null == string || "".equals(string) || string.isEmpty() || string.length() == 0)
return true;
else
return false;
/**
* 将文件夹下所有jar存储
*
* @param filePath
*/
public static ArrayList<File> searchJarFiles(String filePath)
ArrayList<File> filelist = new ArrayList<File>();
File f = new File(filePath);
if (!f.exists())
log.info(filePath + " not exists");
return null;
File fa[] = f.listFiles();
for (int i = 0; i < fa.length; i++)
File file = fa[i];
if (file.isDirectory())
log.info(file.getName() + " [目录]");
else
String path = file.getAbsolutePath();
if (path.contains("ui_") && path.contains(".jar") && !path.contains("_1.jar"))
filelist.add(file);
if (path.indexOf("org.wso2.carbon.i18n") != -1)
filelist.add(file);
return filelist;
public static void createFile(String path, String fileName)
File f = new File(path);
if (!f.exists())
f.mkdirs();
File file = new File(f, fileName);
if (!file.exists())
try
file.createNewFile();
catch (IOException e)
e.printStackTrace();
public static boolean isExists(String jarNewPath)
File file = new File(jarNewPath);
if (file.exists())
return true;
else
return false;
2、解压 jar包
package com.yoodb.wso2ei;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 解压工具
*
* @author mrwang
*/
public class Decompression
protected static Logger log = LoggerFactory.getLogger(Decompression.class);
@SuppressWarnings("resource")
public static void uncompress(File jarFile, File tarDir) throws IOException
JarFile jfInst = new JarFile(jarFile);
Enumeration<JarEntry> enumEntry = jfInst.entries();
while (enumEntry.hasMoreElements())
JarEntry jarEntry = enumEntry.nextElement();
File tarFile = new File(tarDir, jarEntry.getName());
/*
* if(jarEntry.getName().contains("META-INF")) File miFile = new File(tarDir,
* "META-INF"); if(!miFile.exists()) miFile.mkdirs();
*
*
*/
log.info("原文件路径 -->" + tarFile.getPath());
String path = tarFile.getPath().substring(0, tarFile.getPath().lastIndexOf("\\\\") + 1);
log.info("原文件所属目录 -->" + tarFile.getPath());
File file = new File(path);
if ((!file.exists() && !file.isDirectory()))
file.mkdirs();
makeFile(jarEntry, tarFile);
if (jarEntry.isDirectory())
continue;
FileChannel fileChannel = new FileOutputStream(tarFile).getChannel();
InputStream ins = jfInst.getInputStream(jarEntry);
transferStream(ins, fileChannel);
/**
* 流交换操作
*
* @param ins
* 输入流
* @param channel
* 输出流
*/
private static void transferStream(InputStream ins, FileChannel channel)
ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10);
ReadableByteChannel rbcInst = Channels.newChannel(ins);
try
while (-1 != (rbcInst.read(byteBuffer)))
byteBuffer.flip();
channel.write(byteBuffer);
byteBuffer.clear();
catch (IOException ioe)
ioe.printStackTrace();
finally
if (null != rbcInst)
try
rbcInst.close();
catch (IOException e)
e.printStackTrace();
if (null != channel)
try WSO2 ESB / EI / AM 汉化方法