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.HashMap;
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.ScilabList;
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, ScilabType> readContext() {
121 LOG.entering("ScilabDirectHandler", "readContext");
122 final Map<String, ScilabType> result = new HashMap<String, ScilabType>();
124 final ScilabType keys;
125 final ScilabType values;
127 keys = Scilab.getInCurrentScilabSession(CONTEXT + "_names");
128 values = Scilab.getInCurrentScilabSession(CONTEXT + "_values");
129 } catch (JavasciException e) {
130 throw new RuntimeException(e);
132 final ScilabString k;
134 if (keys instanceof ScilabString && values instanceof ScilabList) {
135 k = (ScilabString) keys;
136 v = (ScilabList) values;
137 LOG.finer("data available");
139 LOG.finer("data unavailable");
143 for (int i = 0; i < Math.min(k.getWidth(), v.size()); i++) {
144 result.put(k.getData()[0][i], v.get(i));
147 LOG.exiting("ScilabDirectHandler", "readContext");
152 public void writeContext(final String[] context) {
153 LOG.entering("ScilabDirectHandler", "writeContext");
156 Scilab.putInCurrentScilabSession(CONTEXT, new ScilabString(context));
157 } catch (JavasciException e) {
158 throw new RuntimeException(e);
161 LOG.exiting("ScilabDirectHandler", "writeContext");