import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
import java.util.HashMap;
import java.util.Map;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
enum ConverterState{idle,busy,complete}
enum FileTransResult{success,server_busy,no_input_file,submit_already,converting,no_task}
class ConvertThread implements Runnable{
private PipedInputStream inputPipe;
ConvertThread(PipedInputStream pin){
this.inputPipe = pin;
}
Thread thrd;
public void set_thrd(Thread t){
thrd = t;
}
public void run()
{
int i = 0;
while(true){
int len = 0;
System.out.println("thread 1");
try{
System.out.println("Recv a task ....");
byte[] buf = new byte[1024];
len = inputPipe.read(buf); //
String request_msg= new String(buf,0,len);
System.out.println(request_msg);
JSONObject obj = null;
try{
obj = JSONObject.fromObject(request_msg);
}catch(JSONException e){
System.out.println("wrong msg!");
// TODO:
}
ConvertRequest rmsg = new ConvertRequest();
rmsg = (ConvertRequest)JSONObject.toBean(obj, ConvertRequest.class);
System.out.println("+++ rmsg:" + rmsg);
int ret = OpenOfficePdfConvert.convert2pdf(rmsg.getInputfile(), rmsg.getTargerfile());
Map<String, String> map = new HashMap<String, String>();
if(ret == 0){
map.put("type","TransOk");
//map.put("Detail","AAAAAAAAA");
} else {
map.put("type","TransError");
map.put("detail","AAAAAAAAA");
map.put("errorCode", "");
}
JSONObject jsonObject = JSONObject.fromObject( map );
String sendmsg = jsonObject.toString() + "\0";
ByteBuffer outBuffer = ByteBuffer.wrap(sendmsg.getBytes());
try{
//System.out.println("---------- write sinkChannel");
NIOClient.sinkChannel.write(outBuffer);
} catch (IOException e){
System.out.println("---------- write sinkChannel err");
if (NIOClient.sinkChannel.isOpen()) {
NIOClient.sinkChannel.close();
OpenOfficePdfConvert.stopService();
}
e.printStackTrace();
}
} catch (Exception e){
//throw new RuntimeException("");
e.printStackTrace();
}
if(len > 0){
System.out.println("++ recv msg:");
ConverterManager.CONVERTER_STATE = ConverterState.complete;
ConverterManager.CONVERTER_COMPLETE = true;
}
}
}
}
public class ConverterManager {
static ConverterState CONVERTER_STATE = ConverterState.idle;
public static boolean CONVERTER_COMPLETE = false;
private static ConvertThread cthread;// = new ConvertThread;
private static Thread thread;// = new Thread;
private static PipedInputStream pin_s;
private static PipedOutputStream pout;
public ConverterManager(){ConverterManager.CONVERTER_COMPLETE = false;CONVERTER_STATE = ConverterState.idle;}
//static public FileConverter converter = new FileConverter();
public static void StartConverter() throws IOException{
pin_s = new PipedInputStream();
pout = new PipedOutputStream();
try {
pin_s.connect(pout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cthread = new ConvertThread(pin_s);
thread = new Thread(cthread,"abc");
cthread.set_thrd(thread);
thread.start();
ConfigEnv cfg = ConfigEnv.getInstance();
int port = cfg.GetConvertImpPort();
String office_home = cfg.GetOfficeHome();
OpenOfficePdfConvert.startService(port,office_home);
}
public static void StopConverter(){
if(cthread.thrd.isAlive()){
}
OpenOfficePdfConvert.stopService();
}
public static FileTransResult SubmitTransTask(String inputFile,String targetFile){
Map<String, String> map = new HashMap<String, String>();
map.put("inputfile", inputFile);
map.put("targetfile", targetFile);
JSONObject jsonObject = JSONObject.fromObject( map );
String sendmsg = jsonObject.toString() + "\0";
try {
pout.write(sendmsg.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConverterManager.CONVERTER_STATE = ConverterState.busy;
return FileTransResult.submit_already;
}
/*
public static FileTransResult CheckTransResult(){
if(CONVERTER_COMPLETE && (CONVERTER_STATE == ConverterState.complete)){
CONVERTER_COMPLETE = false;
CONVERTER_STATE = ConverterState.idle;
return FileTransResult.success;
} else if(CONVERTER_STATE == ConverterState.busy) {
return FileTransResult.converting;
} else {
return FileTransResult.no_task;
}
}
*/
}
|