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