Salome HOME
Remove the scenario functionality is implemented
[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.JMSException;
8 import javax.jms.Message;
9 import javax.jms.MessageConsumer;
10 import javax.jms.MessageListener;
11 import javax.jms.Queue;
12 import javax.jms.Session;
13 import javax.naming.Context;
14 import javax.naming.InitialContext;
15
16 import org.apache.log4j.Logger;
17 import org.splat.dal.bo.som.Document;
18 import org.splat.dal.bo.som.Publication;
19 import org.splat.kernel.MismatchException;
20
21 public class Converter implements MessageListener {
22
23         /**
24          * Converter logger.
25          */
26         final static private Logger LOG = Logger.getLogger(Converter.class);
27
28         private final String _type; // Type of document to be converted (e.g. geometry, model)
29         private final String _from; // Source format (e.g. py, sldprt)
30         private final String _to; // Target format (e.g. brep)
31         private final 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(final String type, final String from, final String to,
39                         final String exec) {
40                 // ----------------------------------------------------------------------
41                 this._type = type;
42                 this._from = from;
43                 this._to = to;
44                 this._exec = exec;
45                 this._fname = null;
46
47         }
48
49         // ==============================================================================================================================
50         // Public member functions
51         // ==============================================================================================================================
52
53         public void converts(final Publication source) throws MismatchException {
54                 // -----------------------------------------
55                 Document sdoc = source.value();
56
57                 if (!sdoc.getType().getName().equals(_type)) {
58                         throw new MismatchException();
59                 }
60                 if (!sdoc.getFormat().equals(_from)) {
61                         throw new MismatchException();
62                 }
63                 Connection connex = null;
64                 try {
65                         // Initialization of the asynchronous communication with the actual converter
66                         Context context = new InitialContext(ApplicationSettings
67                                         .getNamingProperties());
68                         ConnectionFactory factory = (javax.jms.QueueConnectionFactory) context
69                                         .lookup("QueueConnectionFactory");
70                         connex = factory.createConnection();
71                         Session session = connex.createSession(false,
72                                         Session.AUTO_ACKNOWLEDGE);
73                         Queue queue = session.createQueue("conversion");
74                         MessageConsumer consum = session.createConsumer(queue);
75
76                         // Listen for arriving messages
77                         consum.setMessageListener(this);
78                         connex.start();
79
80                         // Start the conversion
81                         String command = ApplicationSettings.getApplicationPluginPath()
82                                         + "converter.jar";
83                         String option = "-Dresource.dir=\""
84                                         + ApplicationSettings.getApplicationResourcePath() + "\"";
85                         File executable = new File(command);
86                         if (!executable.exists()) {
87                                 throw new NoSuchMethodException();
88                         }
89
90                         File sfile = sdoc.getSourceFile().asFile();
91                         String args;
92
93                         _fname = sfile.getAbsolutePath();
94                         args = "\"" + _exec + "\" \"" + _fname + "\"";
95                         if (LOG.isInfoEnabled()) {
96                                 LOG.info("Launching the conversion of " + sfile.getName()
97                                                 + " to " + _to.toUpperCase() + " format using "
98                                                 + command);
99                         }
100                         Runtime.getRuntime().exec(
101                                         "\"C:/Program Files/Java/jre6/bin/java.exe\" -jar "
102                                                         + option + " \"" + command + "\" " + args);
103                 } catch (Exception error) {
104                         LOG.error("Reason: ", error);
105                 } finally {
106                         if (connex != null) {
107                                 try {
108                                         connex.close();
109                                 } catch (JMSException e) {
110                                         LOG.error("Can't close the connection: ", e);
111                                 }
112                         }
113                 }
114
115         }
116
117         // ==============================================================================================================================
118         // Messages
119         // ==============================================================================================================================
120
121         public void onMessage(final Message msg) {
122                 String result = msg.toString();
123                 LOG.info("Notification of availability of " + result);
124         }
125 }