2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - Scilab Enterprises - Clement DAVID
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.xcos.io.scicos;
18 import java.util.LinkedHashMap;
20 import java.util.concurrent.Semaphore;
21 import java.util.concurrent.TimeUnit;
22 import java.util.logging.Logger;
24 import org.scilab.modules.javasci.JavasciException;
25 import org.scilab.modules.javasci.Scilab;
26 import org.scilab.modules.types.ScilabMList;
27 import org.scilab.modules.types.ScilabString;
28 import org.scilab.modules.types.ScilabType;
31 * Scilab data direct access.
33 public class ScilabDirectHandler implements Handler {
35 * Context Scilab variable name
37 public static final String CONTEXT = "context";
39 * Diagram Scilab variable name
41 public static final String SCS_M = "scs_m";
43 * Block Scilab variable name
45 public static final String BLK = "blk";
47 private static final Logger LOG = Logger.getLogger(ScilabDirectHandler.class.getPackage().getName());
48 private static final ScilabDirectHandler INSTANCE = new ScilabDirectHandler();
50 private final Semaphore lock = new Semaphore(1, true);
52 private ScilabDirectHandler() {
56 * Lock management to avoid multiple actions
60 * Get the current instance of a ScilabDirectHandler.
62 * Please note that after calling {@link #acquire()} and performing action,
63 * you should release the instance using {@link #release()}.
66 * It is recommended practice to <em>always</em> immediately follow a call
67 * to {@code getInstance()} with a {@code try} block, most typically in a
68 * before/after construction such as:
76 * final ScilabDirectHandler handler = ScilabDirectHandler.getInstance();
87 * @return the instance or null if another operation is in progress
89 public static ScilabDirectHandler acquire() {
90 LOG.finest("lock request");
93 final boolean status = INSTANCE.lock.tryAcquire(0, TimeUnit.SECONDS);
97 } catch (InterruptedException e) {
101 LOG.finest("lock acquired");
107 * Release the instance
109 public void release() {
110 LOG.finest("lock release");
112 INSTANCE.lock.release();
116 * Handler implementation
120 public synchronized Map<String, String> readContext() {
121 LOG.entering("ScilabDirectHandler", "readContext");
122 final ScilabMList list;
123 final Map<String, String> result = new LinkedHashMap<String, String>();
125 final ScilabType data;
127 data = Scilab.getInCurrentScilabSession(CONTEXT);
128 } catch (JavasciException e) {
129 throw new RuntimeException(e);
131 if (data instanceof ScilabMList) {
132 list = (ScilabMList) data;
133 LOG.finer("data available");
135 list = new ScilabMList();
136 LOG.finer("data unavailable");
139 // We are starting at 2 because a struct is composed of
140 // - the fields names (ScilabString)
142 // - variables values...
143 for (int index = 2; index < list.size(); index++) {
144 String key = ((ScilabString) list.get(0)).getData()[0][index];
145 String value = list.get(index).toString();
147 result.put(key, value);
150 LOG.exiting("ScilabDirectHandler", "readContext");
155 public void writeContext(final String[] context) {
156 LOG.entering("ScilabDirectHandler", "writeContext");
159 Scilab.putInCurrentScilabSession(CONTEXT, new ScilabString(context));
160 } catch (JavasciException e) {
161 throw new RuntimeException(e);
164 LOG.exiting("ScilabDirectHandler", "writeContext");