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.dal.bo.som.Document;
17 import org.splat.dal.bo.som.Publication;
18 import org.apache.log4j.Logger;
21 public class Converter implements MessageListener {
26 final static Logger logger = Logger.getLogger(Converter.class);
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
34 // ==============================================================================================================================
36 // ==============================================================================================================================
38 protected Converter (String type, String from, String to, String exec) {
39 // ----------------------------------------------------------------------
48 // ==============================================================================================================================
49 // Public member functions
50 // ==============================================================================================================================
52 public void converts (Publication source) throws MismatchException {
53 // -----------------------------------------
54 Document sdoc = source.value();
56 if (!sdoc.getType().getName().equals(type)) throw new MismatchException();
57 if (!sdoc.getFormat().equals(from)) throw new MismatchException();
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);
68 // Listen for arriving messages
69 consum.setMessageListener(this);
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();
78 File sfile = sdoc.getSourceFile().asFile();
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);
86 Runtime.getRuntime().exec("\"C:/Program Files/Java/jre6/bin/java.exe\" -jar " + option + " \"" + command + "\" " + args);
88 catch (Exception error) {
89 logger.error("Reason: ", error);
93 // ==============================================================================================================================
95 // ==============================================================================================================================
97 public void onMessage (Message msg) {
98 // -----------------------------------
99 String result = msg.toString();
100 logger.info("Notification of availability of " + result);