1// BEGIN LICENSE BLOCK
2// Version: CMPL 1.1
3//
4// The contents of this file are subject to the Cisco-style Mozilla Public
5// License Version 1.1 (the "License"); you may not use this file except
6// in compliance with the License.  You may obtain a copy of the License
7// at www.eclipse-clp.org/license.
8//
9// Software distributed under the License is distributed on an "AS IS"
10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11// the License for the specific language governing rights and limitations
12// under the License.
13//
14// The Original Code is  The ECLiPSe Constraint Logic Programming System.
15// The Initial Developer of the Original Code is  Cisco Systems, Inc.
16// Portions created by the Initial Developer are
17// Copyright (C) 2000 - 2006 Cisco Systems, Inc.  All Rights Reserved.
18//
19// Contributor(s): Stefano Novello / Josh Singer, Parc Technologies
20//
21// END LICENSE BLOCK
22
23//Title:        Java/ECLiPSe interface
24//Version:      $Id: EclipseEngineOptions.java,v 1.1 2006/09/23 01:54:09 snovello Exp $
25//Author:       Josh Singer / Stefano Novello
26//Company:      Parc Technologies
27//Description:  Set of options used to initialise a new ECLiPSe engine.
28package com.parctechnologies.eclipse;
29import java.io.*;
30import java.util.Properties;
31
32
33/** Encapsulates a set of options which can be used to initialise a new ECLiPSe
34 * engine. When constructing an instance of <i>EclipseEngineOptions</i> the
35 * options settings can be read from an instance of <i>java.util.Properties</i>
36 * using a standard key string for each option:
37 *
38 * <ul>
39 * <li> <b>eclipse.default-module</b> (<i>String</i>) for the module to use
40 * initially for executing goals. Defaults to "eclipse".
41 * <li> <b>eclipse.directory</b> (<i>String</i>) for the path of the ECLiPSe installation. This
42 * option has no default and must be explicitly set either using a parameter
43 * <i>File</i> or a property.
44 * <li> <b>eclipse.global-size</b> (<code>int</code>) for the size of the global
45 * stack in megabytes.
46 * <li> <b>eclipse.local-size</b> (<code>int</code>) for the size of the local
47 * stack in megabytes.
48 * <li> <b>eclipse.peer-name</b> (<i>String</i>) for the name of the peer which represents this
49 * <i>EclipseEngine</i> in ECLiPSe. Defaults to "host".
50 * <li> <b>eclipse.use-queues</b> (<code>boolean</code>) for the "use queues" flag. Defaults to false.
51 * </ul>
52 * There are also set methods to explicitly set each option after the
53 * <i>EclipseEngineOptions</i> object has been created.
54 *
55 *
56 *
57 */
58public class EclipseEngineOptions
59{
60  // The eclipse module in which goals will be executed by default
61  private String defaultModule;
62
63  // The location of the Eclipse installation
64  private String eclipseDir;
65
66  // size of local stack in megabytes
67  private int localSize;
68
69  // size of global stack in megabytes
70  private int globalSize;
71
72  // Flag indicating whether queues are used to represent standard streams
73  private boolean useQueues = false;
74
75  // set of command-line options to be passed to the eclipse
76  private String[] commandLineOptions = null;
77
78  // the string to be used as the peer name
79  private String _peerName = "host";
80
81  // Size of a megabyte
82  static final int MEGABYTE = 1024 * 1024;
83
84  /**
85   * Construct a set of EclipseEngineOptions using a specified ECLiPSe
86   * installation and looking up all other settings in the system properties. If
87   * a setting is not found in this properties list, the defaults are as listed
88   * above.
89   *
90   * @throws IllegalArgumentException if any of the system properties does not
91   * parse to the correct type.
92   *
93   */
94  public EclipseEngineOptions(File eclipseDirectory)
95  {
96    setEclipseDir(eclipseDirectory);
97    getOptionsFromProperties(System.getProperties());
98  }
99
100  /**
101   * Construct a set of EclipseEngineOptions looking up all settings in
102   * the system properties. If
103   * a setting is not found in this properties list, the defaults are as listed
104   * above.
105   *
106   * @throws IllegalArgumentException if there is not an
107   * <b>eclipse.directory</b> property in the system properties, or if any of
108   * the system properties does not parse to the correct type.
109   *
110   */
111  public EclipseEngineOptions()
112  {
113    this(System.getProperties());
114  }
115
116  /**
117   * Construct a set of EclipseEngineOptions looking up all settings in
118   * the parameter set of properties. If
119   * a setting is not found in this properties list, the defaults are as listed
120   * above.
121   *
122   * @throws IllegalArgumentException if there is not an
123   * <b>eclipse.directory</b> property in the parameter <i>Properties</i>, or if any of
124   * the properties does not parse to the correct type.
125   *
126   */
127  public EclipseEngineOptions(Properties properties)
128  {
129    getOptionsFromProperties(properties);
130  }
131
132  private void getOptionsFromProperties(Properties properties)
133  {
134    if(eclipseDir == null)
135    {
136      getEclipseDirFromProperties(properties);
137    }
138    getDefaultModuleFromProperties(properties);
139    getPeerNameFromProperties(properties);
140    getGlobalSizeFromProperties(properties);
141    getLocalSizeFromProperties(properties);
142    getUseQueuesFromProperties(properties);
143  }
144
145  private void getEclipseDirFromProperties(Properties properties)
146  {
147    if(!properties.containsKey("eclipse.directory"))
148    {
149      throw new IllegalArgumentException("Missing eclipse.directory property");
150    }
151    else
152    {
153      setEclipseDir(new File(properties.getProperty("eclipse.directory")));
154    }
155  }
156
157  private void getDefaultModuleFromProperties(Properties properties)
158  {
159    if(properties.containsKey("eclipse.default-module"))
160    {
161      setDefaultModule(properties.getProperty("eclipse.default-module"));
162    }
163  }
164
165  private void getPeerNameFromProperties(Properties properties)
166  {
167    if(properties.containsKey("eclipse.peer-name"))
168    {
169      setPeerName(properties.getProperty("eclipse.peer-name"));
170    }
171  }
172
173  private void getGlobalSizeFromProperties(Properties properties)
174  {
175    if(properties.containsKey("eclipse.global-size"))
176    {
177      try
178      {
179        setGlobalSize(Integer.parseInt(properties.getProperty("eclipse.global-size")));
180      }
181      catch(NumberFormatException e)
182      {
183        throw new IllegalArgumentException("Property eclipse.global-size not an integer");
184      }
185    }
186  }
187  private void getLocalSizeFromProperties(Properties properties)
188  {
189    if(properties.containsKey("eclipse.local-size"))
190    {
191      try
192      {
193        setLocalSize(Integer.parseInt(properties.getProperty("eclipse.local-size")));
194      }
195      catch(NumberFormatException e)
196      {
197        throw new IllegalArgumentException("Property eclipse.local-size not an integer");
198      }
199    }
200  }
201  private void getUseQueuesFromProperties(Properties properties)
202  {
203    if(properties.containsKey("eclipse.use-queues"))
204    {
205      if(properties.getProperty("eclipse.use-queues").equalsIgnoreCase("true"))
206      {
207        setUseQueues(true);
208        return;
209      }
210      if(properties.getProperty("eclipse.use-queues").equalsIgnoreCase("false"))
211      {
212        setUseQueues(false);
213        return;
214      }
215      throw new IllegalArgumentException("Property eclipse.use-queues not a boolean");
216    }
217  }
218
219
220  /**
221   * Return default module
222   */
223  String getDefaultModule()
224  {
225    return(defaultModule);
226  }
227
228  /**
229   * Return the array of command line options
230   */
231  String[] getCommandLineOptions()
232  {
233    return(commandLineOptions);
234  }
235
236
237  /**
238   * Return the ECLiPSe installation directory.
239   */
240  String getEclipseDir()
241  {
242    return(eclipseDir);
243  }
244
245  /**
246   * Return size of local stack
247   */
248  int getLocalSize()
249  {
250    return(localSize);
251  }
252
253  /**
254   * Return size of global stack
255   */
256  int getGlobalSize()
257  {
258    return(globalSize);
259  }
260
261  /**
262   * Set the default ECLiPSe module where goals are called. If none is set,
263   * goals are called in the module 'eclipse'.
264   */
265  public void setDefaultModule(String defaultModule)
266  {
267    this.defaultModule = defaultModule;
268  }
269
270  /**
271   * Set the peer name by which the Java side will be referenced in ECLiPSe.
272   * The default setting is "host".
273   */
274  public void setPeerName(String peerName)
275  {
276    _peerName = peerName;
277  }
278
279  /**
280   * get the peer name
281   */
282  String getPeerName()
283  {
284    return(_peerName);
285  }
286
287
288  /**
289   * Set the directory where ECLiPSe is installed. This may correspond to
290   * the ECLIPSEDIR environment variable/registry entry when using the
291   * ECLiPSe development environment.
292   */
293  public void setEclipseDir(File eclipseDir)
294  {
295    this.eclipseDir = eclipseDir.getAbsolutePath();
296  }
297
298  /**
299   * Set size of the ECLiPSe local stack in megabytes.
300   */
301  public void setLocalSize(int localSize)
302  {
303    this.localSize = localSize;
304  }
305
306  /**
307   * Set size of the ECLiPSe global stack in megabytes.
308   */
309  public void setGlobalSize(int globalSize)
310  {
311    this.globalSize = globalSize;
312  }
313
314  /**
315   * Set the "use queues" flag. If true, links ECLiPSe's standard streams
316   * (stdin, stdout and stderr)
317   * to FromEclipseQueue/ToEclipseQueue objects: if false, links them to
318   * the operating system standard streams. In the false (default) case, the methods
319   * {@link EclipseEngine#getEclipseStdin()}, {@link EclipseEngine#getEclipseStdout()}
320   * and {@link EclipseEngine#getEclipseStderr()} will all return <code>null</code>.
321   */
322  public void setUseQueues(boolean useQueues)
323  {
324    this.useQueues = useQueues;
325  }
326
327  /**
328   * Set an array of strings which will be passed to the ECLiPSe engine as command-
329   * line arguments. These will then be accessible in Eclipse using the built-in
330   * predicates argc/1 and argv/2. Note that by default the options will not be interpreted
331   * unless the goal sepia_kernel:standalone_toplevel is executed.
332   * Also note that conventionally, element 0 is the command-line command (e.g.
333   * "eclipse") and the other options begin at element 1.
334   *
335   * Note: the access modifier of this should not be public. This method is for
336   * internal package use only.
337   */
338  void setCommandLineOptions(String[] args)
339  {
340    this.commandLineOptions = args;
341  }
342
343
344  boolean isUsingQueues()
345  {
346    return useQueues;
347  }
348
349
350}
351