1
1
package aya ;
2
2
3
+ import java .io .File ;
4
+ import java .io .IOException ;
5
+ import java .net .MalformedURLException ;
6
+ import java .net .URL ;
7
+ import java .net .URLClassLoader ;
3
8
import java .util .ArrayList ;
9
+ import java .util .HashMap ;
10
+ import java .util .Map ;
11
+ import java .util .ServiceLoader ;
12
+ import java .util .stream .StreamSupport ;
4
13
14
+ import aya .exceptions .runtime .IOError ;
5
15
import aya .ext .color .ColorInstructionStore ;
6
16
import aya .ext .date .DateInstructionStore ;
7
17
import aya .ext .debug .DebugInstructionStore ;
11
21
import aya .ext .image .ImageInstructionStore ;
12
22
import aya .ext .json .JSONInstructionStore ;
13
23
import aya .ext .la .LinearAlgebraInstructionStore ;
24
+ import aya .ext .library .LibraryInstructionStore ;
14
25
import aya .ext .plot .PlotInstructionStore ;
15
26
import aya .ext .socket .SocketInstructionStore ;
16
27
import aya .ext .sys .SystemInstructionStore ;
@@ -48,60 +59,52 @@ public class StaticData {
48
59
// All calls to modify this data will need to be thread safe
49
60
//
50
61
private static StaticData _instance ;
51
-
62
+
63
+
52
64
//
53
65
// Data loaded in the parser
54
66
//
55
67
private StringSearch _helpData ;
56
68
57
-
69
+
58
70
//
59
71
// Data Loaded on Start-up
60
72
//
61
- private ArrayList < NamedInstructionStore > _namedInstructionStores ;
62
-
63
-
73
+ private final Map < String , NamedOperator > _namedInstructions = new HashMap <>() ;
74
+
75
+
64
76
private StaticData () {
65
77
_helpData = null ; // initHelpData will create
66
- _namedInstructionStores = new ArrayList <NamedInstructionStore >();
67
78
}
68
-
79
+
69
80
public static StaticData getInstance () {
70
81
if (_instance == null ) {
71
82
_instance = new StaticData ();
72
83
}
73
84
return _instance ;
74
85
}
75
-
86
+
76
87
public void init () {
77
88
initHelpData ();
78
89
initNamedInstructions ();
79
90
}
80
-
81
- public void addNamedInstructionStore (NamedInstructionStore is ) {
82
- _namedInstructionStores .add (is );
83
- is .initHelpData (this );
84
- }
85
-
86
-
91
+
92
+
87
93
///////////////
88
94
// Help Data //
89
95
///////////////
90
-
96
+
91
97
private void initHelpData () {
92
- if (_helpData == null ) {
93
-
98
+ if (_helpData == null ) {
99
+
94
100
//Make sure all classes are loaded
95
- try
96
- {
97
- loadOps (Ops .OPS );
98
- loadOps (Ops .EXTRA_OPS );
99
- loadOps (MiscOps .MATH_OPS );
100
- loadOps (ColonOps .COLON_OPS );
101
- loadOps (DotOps .DOT_OPS );
102
- }
103
- catch (Exception e )
104
- {
101
+ try {
102
+ loadOps (Ops .OPS );
103
+ loadOps (Ops .EXTRA_OPS );
104
+ loadOps (MiscOps .MATH_OPS );
105
+ loadOps (ColonOps .COLON_OPS );
106
+ loadOps (DotOps .DOT_OPS );
107
+ } catch (Exception e ) {
105
108
e .printStackTrace ();
106
109
}
107
110
ArrayList <String > searchList = new ArrayList <String >();
@@ -113,20 +116,20 @@ private void initHelpData() {
113
116
_helpData = new StringSearch (searchList );
114
117
}
115
118
}
116
-
119
+
117
120
public StringSearch getHelpData () {
118
121
initHelpData ();
119
122
return _helpData ;
120
123
}
121
-
124
+
122
125
public void addHelpText (String in ) {
123
126
getHelpData ().addUnique (in );
124
127
}
125
128
126
129
public String [] getQuickSearchData () {
127
130
return getHelpData ().getAllItems ();
128
131
}
129
-
132
+
130
133
/* This function does nothing but force java to load
131
134
* the operators and call the static blocks
132
135
*/
@@ -141,36 +144,68 @@ private void loadOps(Operator[] ops) {
141
144
////////////////////////
142
145
143
146
private void initNamedInstructions () {
144
- _namedInstructionStores .add (new DebugInstructionStore ());
145
- _namedInstructionStores .add (new JSONInstructionStore ());
146
- _namedInstructionStores .add (new ImageInstructionStore ());
147
- _namedInstructionStores .add (new GraphicsInstructionStore ());
148
- _namedInstructionStores .add (new FStreamInstructionStore ());
149
- _namedInstructionStores .add (new SystemInstructionStore ());
150
- _namedInstructionStores .add (new DialogInstructionStore ());
151
- _namedInstructionStores .add (new PlotInstructionStore ());
152
- _namedInstructionStores .add (new DateInstructionStore ());
153
- _namedInstructionStores .add (new SocketInstructionStore ());
154
- _namedInstructionStores .add (new ColorInstructionStore ());
155
- _namedInstructionStores .add (new LinearAlgebraInstructionStore ());
156
- _namedInstructionStores .add (new ThreadInstructionStore ());
147
+ addNamedInstructionStore (new DebugInstructionStore ());
148
+ addNamedInstructionStore (new JSONInstructionStore ());
149
+ addNamedInstructionStore (new ImageInstructionStore ());
150
+ addNamedInstructionStore (new GraphicsInstructionStore ());
151
+ addNamedInstructionStore (new FStreamInstructionStore ());
152
+ addNamedInstructionStore (new SystemInstructionStore ());
153
+ addNamedInstructionStore (new DialogInstructionStore ());
154
+ addNamedInstructionStore (new PlotInstructionStore ());
155
+ addNamedInstructionStore (new DateInstructionStore ());
156
+ addNamedInstructionStore (new SocketInstructionStore ());
157
+ addNamedInstructionStore (new ColorInstructionStore ());
158
+ addNamedInstructionStore (new LinearAlgebraInstructionStore ());
159
+ addNamedInstructionStore (new ThreadInstructionStore ());
160
+ addNamedInstructionStore (new LibraryInstructionStore ());
161
+ }
162
+
163
+ public ArrayList <NamedInstructionStore > loadLibrary (File path ) {
164
+ ArrayList <NamedInstructionStore > loaded = new ArrayList <NamedInstructionStore >();
157
165
158
- for (NamedInstructionStore x : _namedInstructionStores ) {
159
- x .initHelpData (this );
166
+ try {
167
+ URL [] urls = {path .toURI ().toURL ()};
168
+
169
+ try (URLClassLoader libClassLoader = new URLClassLoader (urls )) {
170
+ StreamSupport .stream (
171
+ ServiceLoader .load (NamedInstructionStore .class , libClassLoader ).spliterator (),
172
+ false
173
+ ).forEach (store -> {
174
+ //IO.out().println("found store: " + store.getClass().getName());
175
+ addNamedInstructionStore (store );
176
+ loaded .add (store );
177
+ });
178
+ } catch (IOException e ) {
179
+ throw new IOError ("library.load" , path .getPath (), e );
180
+ }
181
+
182
+ } catch (MalformedURLException e ) {
183
+ throw new IOError ("library.load" , path .getPath (), e );
160
184
}
185
+
186
+ return loaded ;
161
187
}
162
-
163
-
164
- public NamedOperator getNamedInstruction (String name ) {
165
- for (NamedInstructionStore x : _namedInstructionStores ) {
166
- NamedOperator i = x .getInstruction (name );
167
- if (i != null ) {
168
- return i ;
188
+
189
+ public void addNamedInstructionStore (NamedInstructionStore store ) {
190
+ for (NamedOperator instruction : store .getNamedInstructions ()) {
191
+ String iName = instruction .getName ();
192
+ NamedOperator previous = _namedInstructions .put (iName , instruction );
193
+ if (previous != null ) {
194
+ IO .err ().println ("NamedInstruction '" + iName + "' has multiple implementations:\n "
195
+ + " " + previous .getClass ().getName () + "\n "
196
+ + " " + instruction .getClass ().getName ()
197
+ );
198
+ }
199
+
200
+ String doc = instruction .getDoc ();
201
+ if (doc != null && !doc .isEmpty ()) {
202
+ addHelpText (instruction .opName () + "\n " + doc );
169
203
}
170
204
}
171
- return null ;
172
205
}
173
206
174
-
207
+ public NamedOperator getNamedInstruction (String name ) {
208
+ return _namedInstructions .get (name );
209
+ }
175
210
176
211
}
0 commit comments