Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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<File> 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<File>();
        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<String, String> 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<String> command = new java.util.ArrayList<String>(); 
            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;
    }
}