« Bashj programming guide » : différence entre les versions

De Lillois Fractale Wiki
Aller à la navigation Aller à la recherche
Contenu ajouté Contenu supprimé
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 13 : Ligne 13 :
The following slide explains the global flows of bashj
The following slide explains the global flows of bashj


[[File:Bashj overview.png|upright]]
[[File:Bashj overview.png|upright|Bashj overview.png]]


But the developper of a bashs script should be aware of the fact that any global component of the JVM (static variables, Threads,...) will be shared with all other bashj clients launched.
But the developper of a bashs script should be aware of the fact that any global component of the JVM (static variables, Threads,...) will be shared with all other bashj clients launched.


For instance if a client modifies a static variable during the execution of a bashj script, the next execution of the same script will start with the modified value of that variable. This may be convenient ... or not, depending on siuations. 
For instance if a client modifies a static variable during the execution of a bashj script, the next execution of the same script will start with the modified value of that variable. This may be convenient ... or not, depending on siuations.

Similarly, if a bashj client launches a Thread without clsing it, this Thread will survive in the server JVM (which may not be the intention).


== Using bashj as a pure java 'interpreter' ==
== Using bashj as a pure java 'interpreter' ==


It is possible to create a bashj page without nay bash line. In this case, the interpreter will automatically call and execute the method&nbsp; <span style="font-family:courier new,courier,monospace;">static void main(String... a)</span>&nbsp;, as in this example:
It is possible to create a bashj page without any bash line. In this case, the interpreter will automatically call and execute the method&nbsp; <span style="font-family:courier new,courier,monospace;">static void main(String... a)</span>&nbsp;, as in this example:
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">#!/usr/bin/bashj<br/> #@java</div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">static void main(String... a)<br/> {u.p("Hello world, welcome to bashj");}</div>
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">#!/usr/bin/bashj<br/> #@java</div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">static void main(String... a)<br/> {u.p("Hello world, welcome to bashj");}</div>
This minimal but complete code will execute normally.
This minimal but complete code will execute as expected.


== Convenience u methods ==
== Convenience u methods ==
Ligne 33 : Ligne 35 :
== Swinging bash ==
== Swinging bash ==


As any other java package, it is possible to take advantage of swing, and this provides (through bashj) a windowing interface to bash.
As any other java package, it is possible to take advantage of Swing UI components, and this provides (through bashj) a windowing interface to bash.


Here is a minimal example:
Here is a minimal example:
Ligne 39 : Ligne 41 :
== Inter process communication using bashj ==
== Inter process communication using bashj ==


Given the persistence faetures of the bashj server, it is easy to set up fast IPC between bashj scripts.
Given the persistence features of the bashj server, it is easy to set up fast IPC between bashj scripts.


The following u methods allow to set and get inter process variables:
The following u methods allow to set() and get() inter process variables:
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">public static void set(String key,String value,boolean scriptSpecific,boolean persist)<br/> public static String get(String key,boolean scriptSpecific,boolean persist)</div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">public static void set(String key,String value)<br/> public static String get(String key)</div>
<div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">public static void set(String key,String value,boolean scriptSpecific,boolean persist)<br/> public static String get(String key,boolean scriptSpecific,boolean persist)</div> <div style="background:#eee;border:1px solid #ccc;padding:5px 10px;">public static void set(String key,String value)<br/> public static String get(String key)</div>
The <span style="font-family:courier new,courier,monospace;">scriptSpecific</span> flag indicates if this value is shared or not with other scripts. These values will be kept during successive calls of the calling scripts, until the server is stopped.
The <span style="font-family:courier new,courier,monospace;">scriptSpecific</span> flag indicates whether&nbsp;this value is shared or not with other scripts. These values will be kept during successive calls of the calling scripts, until the server is stopped.


The <span style="font-family:courier new,courier,monospace;">persist</span> flag indicates whether the values should persist in&nbsp;the host&nbsp; file system. These values will be saved when the server is stopped.
The <span style="font-family:courier new,courier,monospace;">persist</span> flag indicates whether the values should persist in&nbsp;the host&nbsp; file system. These values will be saved when the server is stopped.

Version du 17 juin 2018 à 12:44

This is a complement to the general bashj presentation page.

Understanding the architecture of bashj

The key idea behind bashj is the execution of a bashj server. It is JVM performing essentially two tasks

  1. translating bashj code into bash code
  2. execute any java method call for any running bashj client

The benefit of this is the extreme efficiency of this minimal architecture, taking advantage of combined strengths of java & bash.

The following slide explains the global flows of bashj

Bashj overview.png

But the developper of a bashs script should be aware of the fact that any global component of the JVM (static variables, Threads,...) will be shared with all other bashj clients launched.

For instance if a client modifies a static variable during the execution of a bashj script, the next execution of the same script will start with the modified value of that variable. This may be convenient ... or not, depending on siuations.

Similarly, if a bashj client launches a Thread without clsing it, this Thread will survive in the server JVM (which may not be the intention).

Using bashj as a pure java 'interpreter'

It is possible to create a bashj page without any bash line. In this case, the interpreter will automatically call and execute the method  static void main(String... a) , as in this example:

#!/usr/bin/bashj
#@java
static void main(String... a)
{u.p("Hello world, welcome to bashj");}

This minimal but complete code will execute as expected.

Convenience u methods

Various convenience methods are include with bashj. It is recommended to cheks their list using the command

bashj -u

or their javadoc.

Swinging bash

As any other java package, it is possible to take advantage of Swing UI components, and this provides (through bashj) a windowing interface to bash.

Here is a minimal example:

#!/usr/bin/bashj
#@java
import javax.swing.JFrame;
import javax.swing.JOptionPane;
static void main(String... a)
{JOptionPane.showMessageDialog​(new JFrame(),"Welcome to bashj swing UI");}

Inter process communication using bashj

Given the persistence features of the bashj server, it is easy to set up fast IPC between bashj scripts.

The following u methods allow to set() and get() inter process variables:

public static void set(String key,String value,boolean scriptSpecific,boolean persist)
public static String get(String key,boolean scriptSpecific,boolean persist)
public static void set(String key,String value)
public static String get(String key)

The scriptSpecific flag indicates whether this value is shared or not with other scripts. These values will be kept during successive calls of the calling scripts, until the server is stopped.

The persist flag indicates whether the values should persist in the host  file system. These values will be saved when the server is stopped.

The simple calls (without boolean parameters) assume scriptSpecific is true, persist is false.