package com.mandi.servlet.file.impl; import java.io.File; import java.io.InputStream; import java.net.URL; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service("flashpaper") @Scope(value="singleton") public class Flashpaper extends Thread{ private Logger log=Logger.getLogger(Flashpaper.class); private List list=null; private Lock lock=null; private Object obj=null; private boolean executeflag=false; private String flashpaperurl=null; private String findflashpaper() { @SuppressWarnings("rawtypes") Class tc=Flashpaper.class; URL turl=tc.getResource("/");//get the root path of class ,like the location where struts.xml is if(turl==null) return null; File file=new File(turl.getPath()+"flashpaper.xml"); if(!file.exists()) return null; SAXReader reader=new SAXReader(); try { Document doc=reader.read(file); Element ele=doc.getRootElement(); if(ele==null) return null; Element ele1=ele.element("flashpaher"); if(ele1==null) return null; String path=ele1.getTextTrim(); if(path!=null&&!path.isEmpty()) return path; } catch (DocumentException e) { log.error("findflashpaper error"); } return null; } public Flashpaper() { flashpaperurl=this.findflashpaper(); log.info(flashpaperurl); list=new ArrayList(); lock=new ReentrantLock(); obj=new Object(); executeflag=true; this.start(); } public boolean printFile(File file) { if(!file.exists()) return false; lock.lock(); this.list.add(file); lock.unlock(); synchronized (obj) { obj.notify(); } return true; } @Override public void run() { // TODO Auto-generated method stub log.info("flashpaper start thread!!"); // final Logger log1=this.log; ProcessBuilder processbuilder=new ProcessBuilder(); Map envmap=processbuilder.environment(); envmap.clear(); envmap.putAll(System.getenv()); while(executeflag) { File file=null; lock.lock(); if(list.size()>0) { file=list.get(0); list.remove(0); } lock.unlock(); if(file==null) { synchronized (obj) { try { log.info("start waiting ..."); obj.wait(); log.info("waking..."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } continue; } String cmd=this.flashpaperurl; List command = new java.util.ArrayList(); command.add(cmd); command.add(file.getPath()); command.add("-o"); command.add(file.getPath()+".swf"); cmd=cmd+" "+file.getPath()+" -o "+file.getPath()+".swf"; Process process=null; File fileparent=file.getParentFile(); try { if(fileparent.exists()&& fileparent.isDirectory()) processbuilder.directory(fileparent); process=processbuilder.command(command).start(); if(process==null) log.warn("can not start flashpapper"); final InputStream is=process.getInputStream(); final InputStream error=process.getErrorStream(); final ByteBuffer bytebuffer=ByteBuffer.allocate(1024); final ByteBuffer bytebuffer1=ByteBuffer.allocate(1024); Thread th=new Thread(){ @Override public void run() { // TODO Auto-generated method stub ReadableByteChannel channel=Channels.newChannel(is); try { while(channel.read(bytebuffer)!=-1) { bytebuffer.flip(); bytebuffer.clear(); } } catch (Exception e) { // TODO: handle exception log.error("can't convert"); e.printStackTrace(); } } }; Thread th1=new Thread(){ @Override public void run() { // TODO Auto-generated method stub ReadableByteChannel channel1=Channels.newChannel(error); try { while(channel1.read(bytebuffer)!=-1) { bytebuffer1.flip(); bytebuffer1.clear(); } } catch (Exception e) { // TODO: handle exception log.error("can't convert"); e.printStackTrace(); } } }; th.setDaemon(true); th.start(); th1.setDaemon(true); th1.start(); try{ int value=process.waitFor(); log.info("process waitfor code is:"+value); }catch (Exception e) { // TODO: handle exception log.error("waitfor error"+e.getMessage()); } }catch (Exception e) { // TODO: handle exception log.error("flashpaper can't convert the file"+e.getMessage()); } finally{ if(process!=null) process.destroy(); } } if(executeflag==false) { this.list.clear(); } } public void stopExecute() { this.executeflag=false; } public String getFlashpaperurl() { return flashpaperurl; } public void setFlashpaperurl(String flashpaperurl) { this.flashpaperurl = flashpaperurl; } }