微软 改变授权文本允许用户迁移Office
2013-03-08 20:31
323 查看
public class ProcessBuilder {
/**
* 进程运行标记文件名称
*/
public final String SIGN_RUN_FILE_NAME = "run.do";
private static Logger log = Logger.getLogger(ProcessBuilder.class);
/**
* 创建进程
*
* @param cmd
* 命令行
* @param workDir
* 指定的工作目录
* @throws IOException
*/
public Process create(String[] cmd, File dir) throws Exception {
// 执行命令行,创建新的进程
Process ps = Runtime.getRuntime().exec(cmd, null, dir);
if (ps != null) {
// (非常重要)输出进程流中的信息,防止运行过程中因为进程流的阻塞,导致被迫挂起
new ProcessStreamUtil(ps.getErrorStream(), "ERROR").start();
new ProcessStreamUtil(ps.getInputStream(), "OUTPUT").start();
return ps;
}
return null;
}
/**
* 标记进程的运行 (注:在运行主目录下创建一个目标文件,并加上文件锁, 用于当外部程通过文件锁来判断程序是否正在运行)
*
* @return
*/
public boolean signRunning() throws Exception {
// 运行目录
String lockPath = System.getProperty("user.dir");
File dir = new File(lockPath);
if (!dir.exists()) {
dir.mkdirs();
}
// 待锁定的目标文件路径
lockPath = lockPath + File.separator + SIGN_RUN_FILE_NAME;
log.debug("标记文件:" + lockPath);
return this.tryLockFile(lockPath);
}
/**
* 通过文件锁来判断程序是否正在运行
*
* @return 如果正在运行返回true,否则返回false
*/
public boolean isRunning(String lockFilePath) throws Exception {
log.debug("嗅探文件:" + lockFilePath);
int i = 0;
// 延时60秒,嗅探目标文件是否已被锁上
do {
if (i++ > 20) {
return false;
}
if (!this.tryLockFile(lockFilePath)) {
return true;
} else {
Thread.sleep(3 * 1000L);
}
} while (true);
}
/**
* 尝试对目标文件进行加锁操作
*
* @param lockFilePath
* 目标文件
* @return
*/
private boolean tryLockFile(String lockFilePath) throws Exception {
// 待锁定的目标文件路径
log.debug(lockFilePath);
RandomAccessFile fis = null;
FileLock flock = null;
FileChannel lockfc = null;
try {
fis = new RandomAccessFile(lockFilePath, "rw");
lockfc = fis.getChannel();
// 尝试加锁
flock = lockfc.tryLock();
if (flock != null) {
return true;
}
} finally {
// 释放资源
if (fis != null)
fis.close();
if (flock != null)
lockfc.close();
}
return false;
}
}
// 进程流输出工具类
public class ProcessStreamUtil extends Thread {
private Logger log = Logger.getLogger(ProcessStreamUtil.class);
private InputStream is = null;
private String type = null;
public ProcessStreamUtil(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
log.debug(type + "......");
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
log.debug(type + ">" + line);
}
} catch (Exception ioe) {
log.error(ioe);
} finally {
try {
if (isr != null)
isr.close();
if (br != null)
br.close();
} catch (Exception er) {
log.error(er);
}
}
}
}
相关文章推荐
- 微软Office Live Workplace用户过百万
- 微软开始向批量授权客户分发Office 2010
- MySQL-给新建用户授权以允许远程登录数据库
- Sun出卖Open Office 微软被允许起诉用户
- 微软开发的两个动态库存在后门允许用户查看ASP文件源程序和下载整个网站
- 很希望看到微软来一次“因为QQ未经用户允许扫描硬盘,在所有window平台拒绝运行QQ”
- 微软对企业版Office 365新用户提价15%
- Win2003今年7月停止支持 微软发文鼓励用户尽快迁移
- Mysql允许用户远程连接及授权
- 微软不允许用户绕开Windows 8 Metro界面
- js控制URL地址的改变,以微信服务号的授权获取用户的昵称和头像为例
- 微软将公布网络版Office 对用户免费
- 微软允许PC用户在windows 7中关闭IE浏览器
- 09年博客迁移:诺基亚与微软将组成“邪恶”联盟将出售预装 office手机
- 微软允许用户输入win7序列号后下载ISO镜像 win10降级Win7很简单
- 09年博客迁移:诺基亚与微软将组成“邪恶”联盟将出售预装 office手机
- 微软扩大Office365多重认证功能至所有用户
- 微软 Office 同步插件 Google Cloud Connect 向所有用户开放
- 金山是微软VBA的正式授权用户
- 请 MAS 版 Dash 用户紧急下载授权迁移工具