Salome HOME
91048c0b9f86a7c2a4eb33e801f25cbbe04b3e23
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / Converter.java
1 package org.splat.simer;
2
3 import java.io.File;
4
5 import javax.jms.Connection;
6 import javax.jms.ConnectionFactory;
7 import javax.jms.Message;
8 import javax.jms.MessageConsumer;
9 import javax.jms.MessageListener;
10 import javax.jms.Queue;
11 import javax.jms.Session;
12 import javax.naming.Context;
13 import javax.naming.InitialContext;
14
15 import org.splat.kernel.MismatchException;
16 import org.splat.som.Document;
17 import org.splat.som.Publication;
18 import org.apache.log4j.Logger;
19
20
21 public class Converter implements MessageListener {
22
23     private String  type;     // Type of document to be converted (e.g. geometry, model)
24     private String  from;     // Source format (e.g. py, sldprt)
25     private String  to;       // Target format (e.g. brep)
26     private String  exec;     // Command line launching the actual converter
27     private String  fname;    // Absolute path of the source file to be converted
28
29     final static Logger logger = Logger.getLogger(Converter.class);
30
31 //  ==============================================================================================================================
32 //  Constructor
33 //  ==============================================================================================================================
34
35     protected Converter (String type, String from, String to, String exec) {
36 //  ----------------------------------------------------------------------
37       this.type  = type;
38       this.from  = from;
39       this.to    = to;
40       this.exec  = exec;
41       this.fname = null;
42       
43     }
44
45 //  ==============================================================================================================================
46 //  Public member functions
47 //  ==============================================================================================================================
48
49     public void converts (Publication source) throws MismatchException {
50 //  -----------------------------------------
51       Document  sdoc = source.value();
52
53       if (!sdoc.getType().getName().equals(type)) throw new MismatchException();
54       if (!sdoc.getFormat().equals(from))         throw new MismatchException();
55       try {
56 //      Initialization of the asynchronous communication with the actual converter
57         ApplicationSettings settings = ApplicationSettings.getMe();
58         Context             context  = new InitialContext(settings.getNamingProperties());
59         ConnectionFactory   factory  = (javax.jms.QueueConnectionFactory)context.lookup("QueueConnectionFactory");
60         Connection          connex   = factory.createConnection();
61         Session             session  = connex.createSession(false, Session.AUTO_ACKNOWLEDGE);
62         Queue               queue    = session.createQueue("conversion");
63         MessageConsumer     consum   = session.createConsumer(queue);
64
65 //      Listen for arriving messages
66         consum.setMessageListener(this);
67         connex.start();
68
69 //      Start the conversion
70         String command    = ApplicationSettings.getApplicationPluginPath() + "converter.jar";
71         String option     = "-Dresource.dir=\"" + ApplicationSettings.getApplicationResourcePath() + "\"";
72         File   executable = new File(command);
73         if   (!executable.exists())   throw new NoSuchMethodException();
74
75         File   sfile = sdoc.getSourceFile().asFile();
76         String args;
77
78         fname = sfile.getAbsolutePath();
79         args  = "\"" + exec + "\" \"" + fname + "\"";
80         if (logger.isInfoEnabled()) {
81           logger.info("Launching the conversion of " + sfile.getName() + " to " + to.toUpperCase() + " format using " + command);
82         }
83         Runtime.getRuntime().exec("\"C:/Program Files/Java/jre6/bin/java.exe\" -jar " + option + " \"" + command + "\" " + args);
84       }
85       catch (Exception error) {
86         logger.error("Reason: ", error);
87       }
88     }
89
90 //  ==============================================================================================================================
91 //  Messages
92 //  ==============================================================================================================================
93
94     public void onMessage (Message msg) {
95 //  -----------------------------------
96       String  result = msg.toString();
97       logger.info("Notification of availability of " + result);
98         }
99 }