1 package org.splat.simer;
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;
15 import org.splat.kernel.MismatchException;
16 import org.splat.som.Document;
17 import org.splat.som.Publication;
18 import org.apache.log4j.Logger;
21 public class Converter implements MessageListener {
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
29 final static Logger logger = Logger.getLogger(Converter.class);
31 // ==============================================================================================================================
33 // ==============================================================================================================================
35 protected Converter (String type, String from, String to, String exec) {
36 // ----------------------------------------------------------------------
45 // ==============================================================================================================================
46 // Public member functions
47 // ==============================================================================================================================
49 public void converts (Publication source) throws MismatchException {
50 // -----------------------------------------
51 Document sdoc = source.value();
53 if (!sdoc.getType().getName().equals(type)) throw new MismatchException();
54 if (!sdoc.getFormat().equals(from)) throw new MismatchException();
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);
65 // Listen for arriving messages
66 consum.setMessageListener(this);
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();
75 File sfile = sdoc.getSourceFile().asFile();
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);
83 Runtime.getRuntime().exec("\"C:/Program Files/Java/jre6/bin/java.exe\" -jar " + option + " \"" + command + "\" " + args);
85 catch (Exception error) {
86 logger.error("Reason: ", error);
90 // ==============================================================================================================================
92 // ==============================================================================================================================
94 public void onMessage (Message msg) {
95 // -----------------------------------
96 String result = msg.toString();
97 logger.info("Notification of availability of " + result);