-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathRubyInstanceConfig.java
More file actions
1825 lines (1543 loc) · 54.7 KB
/
RubyInstanceConfig.java
File metadata and controls
1825 lines (1543 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2007-2011 Nick Sieger <nicksieger@gmail.com>
* Copyright (C) 2009 Joseph LaFata <joe@quibb.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import jnr.posix.util.Platform;
import org.jruby.common.RubyWarnings;
import org.jruby.exceptions.MainExitException;
import org.jruby.runtime.Constants;
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.load.LoadService;
import org.jruby.runtime.profile.builtin.ProfileOutput;
import org.jruby.util.ClassesLoader;
import org.jruby.util.ClasspathLauncher;
import org.jruby.util.FileResource;
import org.jruby.util.Loader;
import org.jruby.util.InputStreamMarkCursor;
import org.jruby.util.JRubyFile;
import org.jruby.util.KCode;
import org.jruby.util.SafePropertyAccessor;
import org.jruby.util.StringSupport;
import org.jruby.util.UriLikePathHelper;
import org.jruby.util.cli.ArgumentProcessor;
import org.jruby.util.cli.Options;
import org.jruby.util.cli.OutputStrings;
import static org.jruby.util.StringSupport.EMPTY_STRING_ARRAY;
import org.objectweb.asm.Opcodes;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern;
/**
* A structure used to configure new JRuby instances. All publicly-tweakable
* aspects of Ruby can be modified here, including those settable by command-
* line options, those available through JVM properties, and those suitable for
* embedding.
*/
public class RubyInstanceConfig {
public RubyInstanceConfig() {
this(Ruby.isSecurityRestricted());
}
public RubyInstanceConfig(boolean isSecurityRestricted) {
this.isSecurityRestricted = isSecurityRestricted;
currentDirectory = isSecurityRestricted ? "/" : JRubyFile.getFileProperty("user.dir");
if (isSecurityRestricted) {
compileMode = CompileMode.OFF;
jitLogging = false;
jitLoggingVerbose = false;
jitLogEvery = 0;
jitThreshold = -1;
jitMax = 0;
jitMaxSize = -1;
managementEnabled = false;
} else {
if (COMPILE_EXCLUDE != null) {
excludedMethods.addAll(StringSupport.split(COMPILE_EXCLUDE, ','));
}
managementEnabled = Options.MANAGEMENT_ENABLED.load();
compileMode = Options.COMPILE_MODE.load();
jitLogging = Options.JIT_LOGGING.load();
jitLoggingVerbose = Options.JIT_LOGGING_VERBOSE.load();
jitLogEvery = Options.JIT_LOGEVERY.load();
jitThreshold = Options.JIT_THRESHOLD.load();
jitMax = Options.JIT_MAX.load();
jitMaxSize = Options.JIT_MAXSIZE.load();
}
initEnvironment();
}
public RubyInstanceConfig(RubyInstanceConfig parentConfig) {
isSecurityRestricted = parentConfig.isSecurityRestricted;
currentDirectory = parentConfig.getCurrentDirectory();
compileMode = parentConfig.getCompileMode();
jitLogging = parentConfig.jitLogging;
jitLoggingVerbose = parentConfig.jitLoggingVerbose;
jitLogEvery = parentConfig.jitLogEvery;
jitThreshold = parentConfig.jitThreshold;
jitMax = parentConfig.jitMax;
jitMaxSize = parentConfig.jitMaxSize;
managementEnabled = parentConfig.managementEnabled;
excludedMethods = parentConfig.excludedMethods;
updateNativeENVEnabled = parentConfig.updateNativeENVEnabled;
profilingService = parentConfig.profilingService;
profilingMode = parentConfig.profilingMode;
initEnvironment();
}
private void initEnvironment() {
try {
setEnvironment(System.getenv());
}
catch (SecurityException se) { /* ignore missing getenv permission */ }
}
public RubyInstanceConfig(final InputStream in, final PrintStream out, final PrintStream err) {
this();
setInput(in);
setOutput(out);
setError(err);
}
public LoadService createLoadService(Ruby runtime) {
return creator.create(runtime);
}
public void processArguments(String[] arguments) {
new ArgumentProcessor(arguments, this).processArguments();
tryProcessArgumentsWithRubyopts();
}
public void tryProcessArgumentsWithRubyopts() {
try {
processArgumentsWithRubyopts();
} catch (SecurityException se) {
// ignore and do nothing
}
}
public void processArgumentsWithRubyopts() {
if (disableRUBYOPT) return;
// environment defaults to System.getenv normally
Object rubyoptObj = environment.get("RUBYOPT");
if (rubyoptObj == null) return;
// Our argument processor bails if an arg starts with space, so we trim the RUBYOPT line
// See #4849
String rubyopt = rubyoptObj.toString().trim();
if (rubyopt.length() == 0) return;
String[] rubyoptArgs = rubyopt.split("\\s+");
if (rubyoptArgs.length != 0) {
new ArgumentProcessor(rubyoptArgs, false, true, true, this).processArguments();
}
}
// This method does not work like previous version in verifying it is
// a Ruby shebang line. Looking for ruby before \n is possible to add,
// but I wanted to keep this short.
private boolean isShebang(InputStreamMarkCursor cursor) throws IOException {
if (cursor.read() == '#') {
int c = cursor.read();
if (c == '!') {
cursor.endPoint(-2);
return true;
} else if (c == '\n') {
cursor.rewind();
}
} else {
cursor.rewind();
}
return false;
}
private boolean skipToNextLine(InputStreamMarkCursor cursor) throws IOException {
int c = cursor.read();
do {
if (c == '\n') return true;
} while ((c = cursor.read()) != -1);
return false;
}
private void eatToShebang(InputStream in) {
InputStreamMarkCursor cursor = new InputStreamMarkCursor(in, 8192);
try {
do {
if (isShebang(cursor)) break;
} while (skipToNextLine(cursor));
} catch (IOException e) {
} finally {
try { cursor.finish(); } catch (IOException e) {}
}
}
/**
* The intent here is to gather up any options that might have
* been specified in the shebang line and return them so they can
* be merged into the ones specified on the command-line. This is
* kind of a hopeless task because it's impossible to figure out
* where the command invocation stops and the parameters start.
* We try to work with the common scenarios where /usr/bin/env is
* used to invoke the JRuby shell script, and skip any parameters
* it might have. Then we look for the interpreter invocation and
* assume that the binary will have the word "ruby" in the name.
* This is error prone but should cover more cases than the
* previous code.
*/
public String[] parseShebangOptions(InputStream in) {
String[] result = EMPTY_STRING_ARRAY;
if (in == null) return result;
if (isXFlag()) eatToShebang(in);
BufferedReader reader;
try {
InputStreamMarkCursor cursor = new InputStreamMarkCursor(in, 8192);
try {
if (!isShebang(cursor)) return result;
} finally {
cursor.finish();
}
in.mark(8192);
reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.ISO_8859_1), 8192);
String firstLine = reader.readLine();
boolean usesEnv = false;
if (firstLine.length() > 2 && firstLine.charAt(0) == '#' && firstLine.charAt(1) == '!') {
String[] options = firstLine.substring(2).split("\\s+");
int i;
for (i = 0; i < options.length; i++) {
// Skip /usr/bin/env if it's first
if (i == 0 && options[i].endsWith("/env")) {
usesEnv = true;
continue;
}
// Skip any assignments if /usr/bin/env is in play
if (usesEnv && options[i].indexOf('=') > 0) continue;
// Skip any commandline args if /usr/bin/env is in play
if (usesEnv && options[i].startsWith("-")) continue;
String basename = (new File(options[i])).getName();
if (basename.indexOf("ruby") > 0) break;
}
setHasShebangLine(true);
System.arraycopy(options, i, result, 0, options.length - i);
} else {
// No shebang line found
setHasShebangLine(false);
}
} catch (Exception ex) {
// ignore error
} finally {
try {
in.reset();
} catch (IOException ex) {}
}
return result;
}
private static final Pattern RUBY_SHEBANG = Pattern.compile("#!.*ruby.*");
protected static boolean isRubyShebangLine(String line) {
return RUBY_SHEBANG.matcher(line).matches();
}
private String calculateJRubyHome() {
String newJRubyHome = null;
// try the normal property first
if (!isSecurityRestricted) {
newJRubyHome = SafePropertyAccessor.getProperty("jruby.home");
}
if (newJRubyHome == null && getLoader().getResource("META-INF/jruby.home/.jrubydir") != null) {
newJRubyHome = "uri:classloader://META-INF/jruby.home";
}
if (newJRubyHome != null) {
// verify it if it's there
newJRubyHome = verifyHome(newJRubyHome, error);
} else {
try {
newJRubyHome = SafePropertyAccessor.getenv("JRUBY_HOME");
} catch (Exception e) {}
if (newJRubyHome != null) {
// verify it if it's there
newJRubyHome = verifyHome(newJRubyHome, error);
} else {
// otherwise fall back on system temp location
newJRubyHome = SafePropertyAccessor.getProperty("java.io.tmpdir");
}
}
// RegularFileResource absolutePath will canonicalize resources so that will change c: paths to C:.
// We will cannonicalize on windows so that jruby.home is also C:.
// assume all those uri-like pathnames are already in absolute form
if (Platform.IS_WINDOWS && !RubyFile.PROTOCOL_PATTERN.matcher(newJRubyHome).matches()) {
try {
newJRubyHome = new File(newJRubyHome).getCanonicalPath();
}
catch (IOException e) {} // just let newJRubyHome stay the way it is if this fails
}
return newJRubyHome == null ? null : JRubyFile.normalizeSeps(newJRubyHome);
}
// We require the home directory to be absolute
private static String verifyHome(String home, PrintStream error) {
if ("uri:classloader://META-INF/jruby.home".equals(home) || "uri:classloader:/META-INF/jruby.home".equals(home)) {
return home;
}
if (".".equals(home)) {
home = SafePropertyAccessor.getProperty("user.dir");
}
else if (home.startsWith("cp:")) {
home = home.substring(3);
}
if (home.startsWith("jar:") || ( home.startsWith("file:") && home.contains(".jar!/") ) ||
home.startsWith("classpath:") || home.startsWith("uri:")) {
error.println("Warning: JRuby home with uri like paths may not have full functionality - use at your own risk");
}
// do not normalize on plain jar like paths coming from jruby-rack
else if (!home.contains(".jar!/") && !home.startsWith("uri:")) {
File file = new File(home);
if (!file.exists()) {
final String tmpdir = SafePropertyAccessor.getProperty("java.io.tmpdir");
error.println("Warning: JRuby home \"" + file + "\" does not exist, using " + tmpdir);
return tmpdir;
}
if (!file.isAbsolute()) {
home = file.getAbsolutePath();
}
}
return home;
}
/** Indicates whether the JVM process' native environment will be updated when ENV[...] is set from Ruby. */
public boolean isUpdateNativeENVEnabled() {
return updateNativeENVEnabled;
}
/** Ensure that the JVM process' native environment will be updated when ENV is modified .*/
public void setUpdateNativeENVEnabled(boolean updateNativeENVEnabled) {
this.updateNativeENVEnabled = updateNativeENVEnabled;
}
public byte[] inlineScript() {
return inlineScript.toString().getBytes();
}
public InputStream getScriptSource() {
try {
// KCode.NONE is used because KCODE does not affect parse in Ruby 1.8
// if Ruby 2.0 encoding pragmas are implemented, this will need to change
if (hasInlineScript) {
return new ByteArrayInputStream(inlineScript());
} else if (isForceStdin() || getScriptFileName() == null) {
// can't use -v and stdin
if (isShowVersion()) {
return null;
}
return getInput();
} else {
final String script = getScriptFileName();
FileResource resource = JRubyFile.createRestrictedResource(getCurrentDirectory(), getScriptFileName());
if (resource != null && resource.exists()) {
if (resource.canRead() && !resource.isDirectory()) {
if (isXFlag()) {
// search for a shebang line and
// return the script between shebang and __END__ or CTRL-Z (0x1A)
return findScript(resource.openInputStream());
}
return resource.openInputStream();
}
else {
throw new FileNotFoundException(script + " (Not a file)");
}
}
else {
throw new FileNotFoundException(script + " (No such file or directory)");
}
}
} catch (IOException e) {
throw new MainExitException(1, "Error opening script file: " + e.getMessage());
}
}
private static InputStream findScript(InputStream is) throws IOException {
StringBuilder buf = new StringBuilder(64);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
boolean foundRubyShebang = false;
String currentLine;
while ((currentLine = br.readLine()) != null) {
if (isRubyShebangLine(currentLine)) {
foundRubyShebang = true;
break;
}
}
if (!foundRubyShebang) {
throw new MainExitException(1, "jruby: no Ruby script found in input (LoadError)");
}
buf.append(currentLine).append('\n');
do {
currentLine = br.readLine();
if (currentLine != null) {
buf.append(currentLine).append('\n');
}
} while (!(currentLine == null || currentLine.contains("__END__") || currentLine.contains("\026")));
return new BufferedInputStream(new ByteArrayInputStream(buf.toString().getBytes()), 8192);
}
public String displayedFileName() {
if (hasInlineScript) {
if (scriptFileName != null) {
return scriptFileName;
} else {
return "-e";
}
} else if (isForceStdin() || getScriptFileName() == null) {
return "-";
} else {
return getScriptFileName();
}
}
////////////////////////////////////////////////////////////////////////////
// Static utilities and global state management methods.
////////////////////////////////////////////////////////////////////////////
public static boolean hasLoadedNativeExtensions() {
return loadedNativeExtensions;
}
public static void setLoadedNativeExtensions(boolean loadedNativeExtensions) {
RubyInstanceConfig.loadedNativeExtensions = loadedNativeExtensions;
}
////////////////////////////////////////////////////////////////////////////
// Getters and setters for config settings.
////////////////////////////////////////////////////////////////////////////
public LoadServiceCreator getLoadServiceCreator() {
return creator;
}
public void setLoadServiceCreator(LoadServiceCreator creator) {
this.creator = creator;
}
public String getJRubyHome() {
if (jrubyHome == null) {
jrubyHome = calculateJRubyHome();
}
return jrubyHome;
}
public void setJRubyHome(String home) {
jrubyHome = home != null ? verifyHome(home, error) : null;
resetEnvRuby();
}
public CompileMode getCompileMode() {
return compileMode;
}
public void setCompileMode(CompileMode compileMode) {
this.compileMode = compileMode;
}
/**
* @see Options#JIT_LOGGING
*/
public boolean isJitLogging() {
return jitLogging;
}
/**
* @see Options#JIT_LOGGING_VERBOSE
*/
public boolean isJitLoggingVerbose() {
return jitLoggingVerbose;
}
/**
* @see Options#JIT_LOGEVERY
*/
public int getJitLogEvery() {
return jitLogEvery;
}
/**
* @see Options#JIT_LOGEVERY
*/
public void setJitLogEvery(int jitLogEvery) {
this.jitLogEvery = jitLogEvery;
}
/**
* @see Options#JIT_THRESHOLD
*/
public int getJitThreshold() {
return jitThreshold;
}
/**
* @see Options#JIT_THRESHOLD
*/
public void setJitThreshold(int jitThreshold) {
this.jitThreshold = jitThreshold;
}
/**
* @see Options#JIT_MAX
*/
public int getJitMax() {
return jitMax;
}
/**
* @see Options#JIT_MAX
*/
public void setJitMax(int jitMax) {
this.jitMax = jitMax;
}
/**
* @see Options#JIT_MAXSIZE
*/
public int getJitMaxSize() {
return jitMaxSize;
}
/**
* @see Options#JIT_MAXSIZE
*/
public void setJitMaxSize(int jitMaxSize) {
this.jitMaxSize = jitMaxSize;
}
/**
* @return true if JIT compilation is enabled
*/
public boolean isJitEnabled() {
return getJitThreshold() >= 0 && getCompileMode().shouldJIT();
}
public void setInput(InputStream newInput) {
input = newInput;
}
public InputStream getInput() {
return input;
}
public void setOutput(PrintStream newOutput) {
output = newOutput;
}
public PrintStream getOutput() {
return output;
}
public void setError(PrintStream newError) {
error = newError;
}
public PrintStream getError() {
return error;
}
public void setCurrentDirectory(String newCurrentDirectory) {
currentDirectory = newCurrentDirectory;
}
public String getCurrentDirectory() {
return currentDirectory;
}
public void setProfile(Profile newProfile) {
profile = newProfile;
}
public Profile getProfile() {
return profile;
}
/**
* @see Options#OBJECTSPACE_ENABLED
*/
public void setObjectSpaceEnabled(boolean newObjectSpaceEnabled) {
objectSpaceEnabled = newObjectSpaceEnabled;
}
/**
* @see Options#OBJECTSPACE_ENABLED
*/
public boolean isObjectSpaceEnabled() {
return objectSpaceEnabled;
}
/**
* @see Options#SIPHASH_ENABLED
*/
public void setSiphashEnabled(boolean newSiphashEnabled) {
siphashEnabled = newSiphashEnabled;
}
/**
* @see Options#SIPHASH_ENABLED
*/
public boolean isSiphashEnabled() {
return siphashEnabled;
}
public void setEnvironment(Map<String, String> newEnvironment) {
environment = new HashMap<>();
if (newEnvironment != null) {
environment.putAll(newEnvironment);
}
}
public Map<String, String> getEnvironment() {
if (!environment.containsKey("RUBY") && RubyFile.PROTOCOL_PATTERN.matcher(getJRubyHome()).matches()) {
// assumption: if JRubyHome is not a regular file than jruby got launched in an embedded fashion
environment.put("RUBY", ClasspathLauncher.jrubyCommand(defaultClassLoader()));
setEnvRuby = true;
}
return environment;
}
private transient boolean setEnvRuby;
private void resetEnvRuby() { // when jruby-home changes, we might need to recompute
if (setEnvRuby) environment.remove("RUBY");
}
public ClassLoader getLoader() {
return loader;
}
public void setLoader(ClassLoader loader) {
this.loader = loader;
}
private final List<String> extraLoadPaths = new CopyOnWriteArrayList<>();
public List<String> getExtraLoadPaths() {
return extraLoadPaths;
}
private final List<String> extraGemPaths = new CopyOnWriteArrayList<>();
public List<String> getExtraGemPaths() {
return extraGemPaths;
}
private final List<Loader> extraLoaders = new CopyOnWriteArrayList<>();
public List<Loader> getExtraLoaders() {
return extraLoaders;
}
/**
* adds a given ClassLoader to jruby. i.e. adds the root of
* the classloader to the LOAD_PATH so embedded ruby scripts
* can be found. dito for embedded gems.
*
* since classloaders do not provide directory information (some
* do and some do not) the source of the classloader needs to have
* a '.jrubydir' in each with the list of files and directories of the
* same directory. (see jruby-stdlib.jar or jruby-complete.jar inside
* META-INF/jruby.home for examples).
*
* these files can be generated by <code>jruby -S generate_dir_info {path/to/ruby/files}</code>
*
* @param loader
*/
public void addLoader(ClassLoader loader) {
addLoader(new ClassesLoader(loader));
}
/**
* adds a given "bundle" to jruby. an OSGi bundle and a classloader
* both have common set of method but do not share a common interface.
* for adding a bundle or classloader to jruby is done via the base URL of
* the classloader/bundle. all we need is the 'getResource'/'getResources'
* method to do so.
* @param bundle
*/
public void addLoader(Loader bundle) {
// loader can be a ClassLoader or an Bundle from OSGi
UriLikePathHelper helper = new UriLikePathHelper(bundle);
String uri = helper.getUriLikePath();
if (uri != null) extraLoadPaths.add(uri);
uri = helper.getUriLikeGemPath();
if (uri != null) extraGemPaths.add(uri);
extraLoaders.add(bundle);
}
public String[] getArgv() {
return argv;
}
public void setArgv(String[] argv) {
this.argv = argv;
}
public StringBuffer getInlineScript() {
return inlineScript;
}
public void setHasInlineScript(boolean hasInlineScript) {
this.hasScriptArgv = true;
this.hasInlineScript = hasInlineScript;
}
public boolean hasInlineScript() {
return hasInlineScript;
}
public Collection<String> getRequiredLibraries() {
return requiredLibraries;
}
public List<String> getLoadPaths() {
return loadPaths;
}
public void setLoadPaths(List<String> loadPaths) {
this.loadPaths = loadPaths;
}
/**
* @see Options#CLI_HELP
*/
public void setShouldPrintUsage(boolean shouldPrintUsage) {
this.shouldPrintUsage = shouldPrintUsage;
}
/**
* @see Options#CLI_HELP
*/
public boolean getShouldPrintUsage() {
return shouldPrintUsage;
}
/**
* @see Options#CLI_PROPERTIES
*/
public void setShouldPrintProperties(boolean shouldPrintProperties) {
this.shouldPrintProperties = shouldPrintProperties;
}
/**
* @see Options#CLI_PROPERTIES
*/
public boolean getShouldPrintProperties() {
return shouldPrintProperties;
}
public boolean isInlineScript() {
return hasInlineScript;
}
/**
* True if we are only using source from stdin and not from a -e or file argument.
*/
public boolean isForceStdin() {
return forceStdin;
}
/**
* Set whether we should only look at stdin for source.
*/
public void setForceStdin(boolean forceStdin) {
this.forceStdin = forceStdin;
}
public void setScriptFileName(String scriptFileName) {
this.hasScriptArgv = true;
this.scriptFileName = scriptFileName;
}
public String getScriptFileName() {
return scriptFileName;
}
/**
* @see Options#CLI_ASSUME_LOOP
*/
public void setAssumeLoop(boolean assumeLoop) {
this.assumeLoop = assumeLoop;
}
/**
* @see Options#CLI_ASSUME_LOOP
*/
public boolean isAssumeLoop() {
return assumeLoop;
}
/**
* @see Options#CLI_ASSUME_PRINT
*/
public void setAssumePrinting(boolean assumePrinting) {
this.assumePrinting = assumePrinting;
}
/**
* @see Options#CLI_ASSUME_PRINT
*/
public boolean isAssumePrinting() {
return assumePrinting;
}
/**
* @see Options#CLI_PROCESS_LINE_ENDS
*/
public void setProcessLineEnds(boolean processLineEnds) {
this.processLineEnds = processLineEnds;
}
/**
* @see Options#CLI_PROCESS_LINE_ENDS
*/
public boolean isProcessLineEnds() {
return processLineEnds;
}
/**
* @see Options#CLI_AUTOSPLIT
*/
public void setSplit(boolean split) {
this.split = split;
}
/**
* @see Options#CLI_AUTOSPLIT
*/
public boolean isSplit() {
return split;
}
/**
* @see Options#CLI_WARNING_LEVEL
*/
public Verbosity getVerbosity() {
return verbosity;
}
/**
* @see Options#CLI_WARNING_LEVEL
*/
public void setVerbosity(Verbosity verbosity) {
this.verbosity = verbosity;
}
public void setBacktraceLimit(Integer limit) {
this.backtraceLimit = limit;
}
public Integer getBacktraceLimit() {
return this.backtraceLimit;
}
/**
* @see Options#CLI_VERBOSE
*/
public boolean isVerbose() {
return verbosity == Verbosity.TRUE;
}
/**
* @see Options#CLI_DEBUG
*/
public boolean isDebug() {
return debug;
}
/**
* @see Options#CLI_DEBUG
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
/**
* Get the set of enabled warning categories.
*
* @return the set of enabled warning categories
*/
public Set<RubyWarnings.Category> getWarningCategories() {
return warningCategories;
}
/**
* @see Options#CLI_PARSER_DEBUG
*/
public boolean isParserDebug() {
return parserDebug;
}
/**
* @see Options#CLI_PARSER_DEBUG
*/
public void setParserDebug(boolean parserDebug) {
this.parserDebug = parserDebug;
}
/**
* @see Options#CLI_PARSER_DEBUG
*/
public boolean getParserDebug() {
return parserDebug;
}
/**
* @see Options#CLI_VERSION
*/
public void setShowVersion(boolean showVersion) {
this.showVersion = showVersion;
}
/**
* @see Options#CLI_VERSION
*/
public boolean isShowVersion() {
return showVersion;
}
/**
* @see Options#CLI_BYTECODE
*/
public void setShowBytecode(boolean showBytecode) {
this.showBytecode = showBytecode;
}
/**
* @see Options#CLI_BYTECODE
*/
public boolean isShowBytecode() {
return showBytecode;
}
/**
* @see Options#CLI_COPYRIGHT
*/
public void setShowCopyright(boolean showCopyright) {
this.showCopyright = showCopyright;
}
/**
* @see Options#CLI_COPYRIGHT
*/
public boolean isShowCopyright() {
return showCopyright;
}
public void setShouldRunInterpreter(boolean shouldRunInterpreter) {
this.shouldRunInterpreter = shouldRunInterpreter;
}
public boolean getShouldRunInterpreter() {
return shouldRunInterpreter && (hasScriptArgv || !showVersion);
}