1 package org.splat.simer;
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;
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;
21 public class Converter implements MessageListener {
26 final static private Logger LOG = Logger.getLogger(Converter.class);
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
34 // ==============================================================================================================================
36 // ==============================================================================================================================
38 protected Converter(final String type, final String from, final String to,
40 // ----------------------------------------------------------------------
49 // ==============================================================================================================================
50 // Public member functions
51 // ==============================================================================================================================
53 public void converts(final Publication source) throws MismatchException {
54 // -----------------------------------------
55 Document sdoc = source.value();
57 if (!sdoc.getType().getName().equals(_type)) {
58 throw new MismatchException();
60 if (!sdoc.getFormat().equals(_from)) {
61 throw new MismatchException();
63 Connection connex = null;
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);
76 // Listen for arriving messages
77 consum.setMessageListener(this);
80 // Start the conversion
81 String command = ApplicationSettings.getApplicationPluginPath()
83 String option = "-Dresource.dir=\""
84 + ApplicationSettings.getApplicationResourcePath() + "\"";
85 File executable = new File(command);
86 if (!executable.exists()) {
87 throw new NoSuchMethodException();
90 File sfile = sdoc.getSourceFile().asFile();
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 "
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);
106 if (connex != null) {
109 } catch (JMSException e) {
110 LOG.error("Can't close the connection: ", e);
117 // ==============================================================================================================================
119 // ==============================================================================================================================
121 public void onMessage(final Message msg) {
122 String result = msg.toString();
123 LOG.info("Notification of availability of " + result);