forked from jruby/jruby
/
LoadService.java
1350 lines (1185 loc) · 57.7 KB
/
LoadService.java
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: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.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/cpl-v10.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) 2002-2010 JRuby Community
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2006 Ola Bini <ola@ologix.com>
*
* 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 CPL, 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 CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.runtime.load;
import org.jruby.util.collections.StringArraySet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipException;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFile;
import org.jruby.RubyHash;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyString;
import org.jruby.ast.executable.Script;
import org.jruby.exceptions.MainExitException;
import org.jruby.exceptions.RaiseException;
import org.jruby.platform.Platform;
import org.jruby.runtime.Block;
import org.jruby.runtime.Constants;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.JRubyFile;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
import static org.jruby.util.URLUtil.getPath;
/**
* <b>How require works in JRuby</b>
* When requiring a name from Ruby, JRuby will first remove any file extension it knows about,
* thereby making it possible to use this string to see if JRuby has already loaded
* the name in question. If a .rb extension is specified, JRuby will only try
* those extensions when searching. If a .so, .o, .dll, or .jar extension is specified, JRuby
* will only try .so or .jar when searching. Otherwise, JRuby goes through the known suffixes
* (.rb, .rb.ast.ser, .so, and .jar) and tries to find a library with this name. The process for finding a library follows this order
* for all searchable extensions:
* <ol>
* <li>First, check if the name starts with 'jar:', then the path points to a jar-file resource which is returned.</li>
* <li>Second, try searching for the file in the current dir</li>
* <li>Then JRuby looks through the load path trying these variants:
* <ol>
* <li>See if the current load path entry starts with 'jar:', if so check if this jar-file contains the name</li>
* <li>Otherwise JRuby tries to construct a path by combining the entry and the current working directy, and then see if
* a file with the correct name can be reached from this point.</li>
* </ol>
* </li>
* <li>If all these fail, try to load the name as a resource from classloader resources, using the bare name as
* well as the load path entries</li>
* <li>When we get to this state, the normal JRuby loading has failed. At this stage JRuby tries to load
* Java native extensions, by following this process:
* <ol>
* <li>First it checks that we haven't already found a library. If we found a library of type JarredScript, the method continues.</li>
* <li>The first step is translating the name given into a valid Java Extension class name. First it splits the string into
* each path segment, and then makes all but the last downcased. After this it takes the last entry, removes all underscores
* and capitalizes each part separated by underscores. It then joins everything together and tacks on a 'Service' at the end.
* Lastly, it removes all leading dots, to make it a valid Java FWCN.</li>
* <li>If the previous library was of type JarredScript, we try to add the jar-file to the classpath</li>
* <li>Now JRuby tries to instantiate the class with the name constructed. If this works, we return a ClassExtensionLibrary. Otherwise,
* the old library is put back in place, if there was one.
* </ol>
* </li>
* <li>When all separate methods have been tried and there was no result, a LoadError will be raised.</li>
* <li>Otherwise, the name will be added to the loaded features, and the library loaded</li>
* </ol>
*
* <b>How to make a class that can get required by JRuby</b>
* <p>First, decide on what name should be used to require the extension.
* In this purely hypothetical example, this name will be 'active_record/connection_adapters/jdbc_adapter'.
* Then create the class name for this require-name, by looking at the guidelines above. Our class should
* be named active_record.connection_adapters.JdbcAdapterService, and implement one of the library-interfaces.
* The easiest one is BasicLibraryService, where you define the basicLoad-method, which will get called
* when your library should be loaded.</p>
* <p>The next step is to either put your compiled class on JRuby's classpath, or package the class/es inside a
* jar-file. To package into a jar-file, we first create the file, then rename it to jdbc_adapter.jar. Then
* we put this jar-file in the directory active_record/connection_adapters somewhere in JRuby's load path. For
* example, copying jdbc_adapter.jar into JRUBY_HOME/lib/ruby/site_ruby/1.8/active_record/connection_adapters
* will make everything work. If you've packaged your extension inside a RubyGem, write a setub.rb-script that
* copies the jar-file to this place.</p>
* <p>If you don't want to have the name of your extension-class to be prescribed, you can also put a file called
* jruby-ext.properties in your jar-files META-INF directory, where you can use the key <full-extension-name>.impl
* to make the extension library load the correct class. An example for the above would have a jruby-ext.properties
* that contained a ruby like: "active_record/connection_adapters/jdbc_adapter=org.jruby.ar.JdbcAdapter". (NOTE: THIS
* FEATURE IS NOT IMPLEMENTED YET.)</p>
*
* @author jpetersen
*/
public class LoadService {
private static final Logger LOG = LoggerFactory.getLogger("LoadService");
private final LoadTimer loadTimer;
public enum SuffixType {
Source, Extension, Both, Neither;
public static final String[] sourceSuffixes = { ".class", ".rb" };
public static final String[] extensionSuffixes = { ".jar", ".so", ".bundle", ".dll" };
private static final String[] allSuffixes = { ".class", ".rb", ".jar", ".so", ".bundle", ".dll" };
private static final String[] emptySuffixes = { "" };
public String[] getSuffixes() {
switch (this) {
case Source:
return sourceSuffixes;
case Extension:
return extensionSuffixes;
case Both:
return allSuffixes;
case Neither:
return emptySuffixes;
}
throw new RuntimeException("Unknown SuffixType: " + this);
}
}
protected static final Pattern sourcePattern = Pattern.compile("\\.(?:rb)$");
protected static final Pattern extensionPattern = Pattern.compile("\\.(?:so|o|dll|bundle|jar)$");
protected RubyArray loadPath;
protected StringArraySet loadedFeatures;
protected final Map<String, Library> builtinLibraries = new HashMap<String, Library>();
protected final Map<String, JarFile> jarFiles = new HashMap<String, JarFile>();
protected final Ruby runtime;
public LoadService(Ruby runtime) {
this.runtime = runtime;
if (RubyInstanceConfig.DEBUG_LOAD_TIMINGS) {
loadTimer = new TracingLoadTimer();
} else {
loadTimer = new LoadTimer();
}
}
public void init(List additionalDirectories) {
loadPath = RubyArray.newArray(runtime);
String jrubyHome = runtime.getJRubyHome();
loadedFeatures = new StringArraySet(runtime);
// add all startup load paths to the list first
for (Iterator iter = additionalDirectories.iterator(); iter.hasNext();) {
addPath((String) iter.next());
}
// add $RUBYLIB paths
RubyHash env = (RubyHash) runtime.getObject().getConstant("ENV");
RubyString env_rubylib = runtime.newString("RUBYLIB");
if (env.has_key_p(env_rubylib).isTrue()) {
String rubylib = env.op_aref(runtime.getCurrentContext(), env_rubylib).toString();
String[] paths = rubylib.split(File.pathSeparator);
for (int i = 0; i < paths.length; i++) {
addPath(paths[i]);
}
}
// wrap in try/catch for security exceptions in an applet
try {
if (jrubyHome != null) {
char sep = '/';
String rubyDir = jrubyHome + sep + "lib" + sep + "ruby" + sep;
// If we're running in 1.9 compat mode, add Ruby 1.9 libs to path before 1.8 libs
if (runtime.is1_9()) {
addPath(rubyDir + "site_ruby" + sep + Constants.RUBY1_9_MAJOR_VERSION);
addPath(rubyDir + "site_ruby" + sep + "shared");
addPath(rubyDir + "site_ruby" + sep + Constants.RUBY_MAJOR_VERSION);
addPath(rubyDir + Constants.RUBY1_9_MAJOR_VERSION);
} else {
// Add 1.8 libs
addPath(rubyDir + "site_ruby" + sep + Constants.RUBY_MAJOR_VERSION);
addPath(rubyDir + "site_ruby" + sep + "shared");
addPath(rubyDir + Constants.RUBY_MAJOR_VERSION);
}
}
} catch(SecurityException ignore) {}
// "." dir is used for relative path loads from a given file, as in require '../foo/bar'
if (!runtime.is1_9() && runtime.getSafeLevel() == 0) {
addPath(".");
}
}
protected void addLoadedFeature(String name) {
loadedFeatures.append(RubyString.newString(runtime, name));
}
protected void addPath(String path) {
// Empty paths do not need to be added
if (path == null || path.length() == 0) return;
synchronized(loadPath) {
loadPath.append(runtime.newString(path.replace('\\', '/')));
}
}
public void load(String file, boolean wrap) {
if(!runtime.getProfile().allowLoad(file)) {
throw runtime.newLoadError("no such file to load -- " + file);
}
SearchState state = new SearchState(file);
state.prepareLoadSearch(file);
Library library = findBuiltinLibrary(state, state.searchFile, state.suffixType);
if (library == null) library = findLibraryWithoutCWD(state, state.searchFile, state.suffixType);
if (library == null) {
library = findLibraryWithClassloaders(state, state.searchFile, state.suffixType);
if (library == null) {
throw runtime.newLoadError("no such file to load -- " + file);
}
}
try {
library.load(runtime, wrap);
} catch (IOException e) {
if (runtime.getDebug().isTrue()) e.printStackTrace(runtime.getErr());
throw newLoadErrorFromThrowable(runtime, file, e);
}
}
public SearchState findFileForLoad(String file) {
if (Platform.IS_WINDOWS) {
file = file.replace('\\', '/');
}
// Even if we don't support .so, some stdlib require .so directly.
// Replace it with .jar to look for a java extension
// JRUBY-5033: The ExtensionSearcher will locate C exts, too, this way.
if (file.endsWith(".so")) {
file = file.replaceAll(".so$", ".jar");
}
SearchState state = new SearchState(file);
state.prepareRequireSearch(file);
for (LoadSearcher searcher : searchers) {
if (searcher.shouldTrySearch(state)) {
if (!searcher.trySearch(state)) {
return null;
}
}
}
return state;
}
public boolean require(String requireName) {
return requireCommon(requireName, true) == RequireState.LOADED;
}
public boolean autoloadRequire(String requireName) {
return requireCommon(requireName, false) != RequireState.CIRCULAR;
}
private enum RequireState {
LOADED, ALREADY_LOADED, CIRCULAR
};
private RequireState requireCommon(String requireName, boolean circularRequireWarning) {
ReentrantLock requireLock = null;
try {
requireLock = acquireRequireLock(requireName);
if (requireLock == null) {
if (circularRequireWarning && runtime.isVerbose() && runtime.is1_9()) {
warnCircularRequire(requireName);
}
return RequireState.CIRCULAR;
}
if (!runtime.getProfile().allowRequire(requireName)) {
throw runtime.newLoadError("no such file to load -- " + requireName);
}
// check for requiredName without extension.
if (featureAlreadyLoaded(requireName)) {
return RequireState.ALREADY_LOADED;
}
// numbers from loadTimer does not include lock waiting time.
long startTime = loadTimer.startLoad(requireName);
try {
boolean loaded = smartLoadInternal(requireName);
return loaded ? RequireState.LOADED : RequireState.ALREADY_LOADED;
} finally {
loadTimer.endLoad(requireName, startTime);
}
} finally {
if (requireLock != null) {
releaseRequireLock(requireName, requireLock);
}
}
}
private ReentrantLock acquireRequireLock(String requireName) {
ReentrantLock requireLock;
synchronized (requireLocks) {
requireLock = requireLocks.get(requireName);
if (requireLock == null) {
requireLock = new ReentrantLock();
requireLocks.put(requireName, requireLock);
} else if (requireLock.isHeldByCurrentThread()) {
return null;
}
}
requireLock.lock();
return requireLock;
}
private void releaseRequireLock(String requireName, ReentrantLock requireLock) {
synchronized (requireLocks) {
if (requireLock.isLocked()) {
requireLock.unlock();
}
requireLocks.remove(requireName);
}
}
protected void warnCircularRequire(String requireName) {
runtime.getWarnings().warn("loading in progress, circular require considered harmful - " + requireName);
// it's a hack for c:rb_backtrace impl.
// We should introduce new method to Ruby.TraceType when rb_backtrace is widely used not only for this purpose.
RaiseException ex = new RaiseException(runtime, runtime.getRuntimeError(), null, false);
String trace = runtime.getInstanceConfig().getTraceType().printBacktrace(ex.getException());
// rb_backtrace dumps to stderr directly.
System.err.print(trace.replaceFirst("[^\n]*\n", ""));
}
/**
* This method did require the specified file without getting a lock.
* Now we offer safe version only. Use {@link LoadService#require(String)} instead.
*/
@Deprecated
public boolean smartLoad(String file) {
return require(file);
}
protected Map<String, ReentrantLock> requireLocks = new HashMap<String, ReentrantLock>();
private boolean smartLoadInternal(String file) {
checkEmptyLoad(file);
SearchState state = findFileForLoad(file);
if (state == null) {
return false;
}
if (state.library == null) {
throw runtime.newLoadError("no such file to load -- " + state.searchFile);
}
// check with long name
if (featureAlreadyLoaded(state.loadName)) {
return false;
}
boolean loaded = tryLoadingLibraryOrScript(runtime, state);
if (loaded) {
addLoadedFeature(state.loadName);
}
return loaded;
}
private static class LoadTimer {
public long startLoad(String file) { return 0L; }
public void endLoad(String file, long startTime) {}
}
private static class TracingLoadTimer extends LoadTimer {
private final AtomicInteger indent = new AtomicInteger(0);
private String getIndentString() {
StringBuilder buf = new StringBuilder();
int i = indent.get();
for (int j = 0; j < i; j++) {
buf.append(" ");
}
return buf.toString();
}
@Override
public long startLoad(String file) {
indent.incrementAndGet();
LOG.info(getIndentString() + "-> " + file);
return System.currentTimeMillis();
}
@Override
public void endLoad(String file, long startTime) {
LOG.info(getIndentString() + "<- " + file + " - "
+ (System.currentTimeMillis() - startTime) + "ms");
indent.decrementAndGet();
}
}
/**
* Load the org.jruby.runtime.load.Library implementation specified by
* className. The purpose of using this method is to avoid having static
* references to the given library class, thereby avoiding the additional
* classloading when the library is not in use.
*
* @param runtime The runtime in which to load
* @param libraryName The name of the library, to use for error messages
* @param className The class of the library
* @param classLoader The classloader to use to load it
* @param wrap Whether to wrap top-level in an anonymous module
*/
public static void reflectedLoad(Ruby runtime, String libraryName, String className, ClassLoader classLoader, boolean wrap) {
try {
if (classLoader == null && Ruby.isSecurityRestricted()) {
classLoader = runtime.getInstanceConfig().getLoader();
}
Object libObject = classLoader.loadClass(className).newInstance();
if (libObject instanceof Library) {
Library library = (Library)libObject;
library.load(runtime, false);
} else if (libObject instanceof BasicLibraryService) {
BasicLibraryService service = (BasicLibraryService)libObject;
service.basicLoad(runtime);
} else {
// invalid type of library, raise error
throw runtime.newLoadError("library `" + libraryName + "' is not of type Library or BasicLibraryService");
}
} catch (RaiseException re) {
throw re;
} catch (Throwable e) {
if (runtime.getDebug().isTrue()) e.printStackTrace();
throw runtime.newLoadError("library `" + libraryName + "' could not be loaded: " + e);
}
}
public IRubyObject getLoadPath() {
return loadPath;
}
public IRubyObject getLoadedFeatures() {
return loadedFeatures;
}
public void addBuiltinLibrary(String name, Library library) {
builtinLibraries.put(name, library);
}
public void removeBuiltinLibrary(String name) {
builtinLibraries.remove(name);
}
public void removeInternalLoadedFeature(String name) {
RubyString nameRubyString = runtime.newString(name);
loadedFeatures.delete(runtime.getCurrentContext(), nameRubyString, Block.NULL_BLOCK);
}
protected boolean featureAlreadyLoaded(String name) {
return loadedFeatures.containsString(name);
}
protected boolean isJarfileLibrary(SearchState state, final String file) {
return state.library instanceof JarredScript && file.endsWith(".jar");
}
protected void reraiseRaiseExceptions(Throwable e) throws RaiseException {
if (e instanceof RaiseException) {
throw (RaiseException) e;
}
}
public interface LoadSearcher {
/**
* @param state
* @return true if trySearch should be called.
*/
public boolean shouldTrySearch(SearchState state);
/**
* @param state
* @return false if loadSearch must be bail-out.
*/
public boolean trySearch(SearchState state);
}
public class BailoutSearcher implements LoadSearcher {
public boolean shouldTrySearch(SearchState state) {
return state.library == null;
}
protected boolean trySearch(String file, SuffixType suffixType) {
for (String suffix : suffixType.getSuffixes()) {
String searchName = file + suffix;
if (featureAlreadyLoaded(searchName)) {
return false;
}
}
return true;
}
public boolean trySearch(SearchState state) {
return trySearch(state.searchFile, state.suffixType);
}
}
public class SourceBailoutSearcher extends BailoutSearcher {
public boolean shouldTrySearch(SearchState state) {
// JRUBY-5032: Load extension files if they are required
// explicitly, and even if an rb file of the same name
// has already been loaded (effectively skipping the search for a source file).
return !extensionPattern.matcher(state.loadName).find();
}
// According to Rubyspec, source files should be loaded even if an equally named
// extension is loaded already. So we use the bailout search twice, once only
// for source files and once for whatever suffix type the state determines
public boolean trySearch(SearchState state) {
return super.trySearch(state.searchFile, SuffixType.Source);
}
}
public class NormalSearcher implements LoadSearcher {
public boolean shouldTrySearch(SearchState state) {
return state.library == null;
}
public boolean trySearch(SearchState state) {
state.library = findLibraryWithoutCWD(state, state.searchFile, state.suffixType);
return true;
}
}
public class ClassLoaderSearcher implements LoadSearcher {
public boolean shouldTrySearch(SearchState state) {
return state.library == null;
}
public boolean trySearch(SearchState state) {
state.library = findLibraryWithClassloaders(state, state.searchFile, state.suffixType);
return true;
}
}
public class ExtensionSearcher implements LoadSearcher {
public boolean shouldTrySearch(SearchState state) {
return (state.library == null || state.library instanceof JarredScript) && !state.searchFile.equalsIgnoreCase("");
}
public boolean trySearch(SearchState state) {
// This code exploits the fact that all .jar files will be found for the JarredScript feature.
// This is where the basic extension mechanism gets fixed
Library oldLibrary = state.library;
// Create package name, by splitting on / and joining all but the last elements with a ".", and downcasing them.
String[] all = state.searchFile.split("/");
StringBuilder finName = new StringBuilder();
for(int i=0, j=(all.length-1); i<j; i++) {
finName.append(all[i].toLowerCase()).append(".");
}
try {
// Make the class name look nice, by splitting on _ and capitalize each segment, then joining
// the, together without anything separating them, and last put on "Service" at the end.
String[] last = all[all.length-1].split("_");
for(int i=0, j=last.length; i<j; i++) {
finName.append(Character.toUpperCase(last[i].charAt(0))).append(last[i].substring(1));
}
finName.append("Service");
// We don't want a package name beginning with dots, so we remove them
String className = finName.toString().replaceAll("^\\.*","");
// If there is a jar-file with the required name, we add this to the class path.
if(state.library instanceof JarredScript) {
// It's _really_ expensive to check that the class actually exists in the Jar, so
// we don't do that now.
runtime.getJRubyClassLoader().addURL(((JarredScript)state.library).getResource().getURL());
}
// quietly try to load the class
Class theClass = runtime.getJavaSupport().loadJavaClass(className);
state.library = new ClassExtensionLibrary(className + ".java", theClass);
} catch (ClassNotFoundException cnfe) {
if (runtime.isDebug()) cnfe.printStackTrace();
// we ignore this and assume the jar is not an extension
} catch (UnsupportedClassVersionError ucve) {
if (runtime.isDebug()) ucve.printStackTrace();
throw runtime.newLoadError("JRuby ext built for wrong Java version in `" + finName + "': " + ucve);
} catch (IOException ioe) {
if (runtime.isDebug()) ioe.printStackTrace();
throw runtime.newLoadError("IOException loading extension `" + finName + "`: " + ioe);
} catch (Exception e) {
if (runtime.isDebug()) e.printStackTrace();
throw runtime.newLoadError("Exception loading extension `" + finName + "`: " + e);
}
// If there was a good library before, we go back to that
if(state.library == null && oldLibrary != null) {
state.library = oldLibrary;
}
return true;
}
}
public class ScriptClassSearcher implements LoadSearcher {
public class ScriptClassLibrary implements Library {
private Script script;
public ScriptClassLibrary(Script script) {
this.script = script;
}
public void load(Ruby runtime, boolean wrap) {
runtime.loadScript(script, wrap);
}
}
public boolean shouldTrySearch(SearchState state) {
return state.library == null;
}
public boolean trySearch(SearchState state) throws RaiseException {
// no library or extension found, try to load directly as a class
Script script;
String className = buildClassName(state.searchFile);
int lastSlashIndex = className.lastIndexOf('/');
if (lastSlashIndex > -1 && lastSlashIndex < className.length() - 1 && !Character.isJavaIdentifierStart(className.charAt(lastSlashIndex + 1))) {
if (lastSlashIndex == -1) {
className = "_" + className;
} else {
className = className.substring(0, lastSlashIndex + 1) + "_" + className.substring(lastSlashIndex + 1);
}
}
className = className.replace('/', '.');
try {
Class scriptClass = Class.forName(className);
script = (Script) scriptClass.newInstance();
} catch (Exception cnfe) {
return true;
}
state.library = new ScriptClassLibrary(script);
return true;
}
}
public static class SearchState {
public Library library;
public String loadName;
public SuffixType suffixType;
public String searchFile;
public SearchState(String file) {
loadName = file;
}
public void prepareRequireSearch(final String file) {
// if an extension is specified, try more targetted searches
if (file.lastIndexOf('.') > file.lastIndexOf('/')) {
Matcher matcher = null;
if ((matcher = sourcePattern.matcher(file)).find()) {
// source extensions
suffixType = SuffixType.Source;
// trim extension to try other options
searchFile = file.substring(0, matcher.start());
} else if ((matcher = extensionPattern.matcher(file)).find()) {
// extension extensions
suffixType = SuffixType.Extension;
// trim extension to try other options
searchFile = file.substring(0, matcher.start());
} else {
// unknown extension, fall back to search with extensions
suffixType = SuffixType.Both;
searchFile = file;
}
} else {
// try all extensions
suffixType = SuffixType.Both;
searchFile = file;
}
}
public void prepareLoadSearch(final String file) {
// if a source extension is specified, try all source extensions
if (file.lastIndexOf('.') > file.lastIndexOf('/')) {
Matcher matcher = null;
if ((matcher = sourcePattern.matcher(file)).find()) {
// source extensions
suffixType = SuffixType.Source;
// trim extension to try other options
searchFile = file.substring(0, matcher.start());
} else {
// unknown extension, fall back to exact search
suffixType = SuffixType.Neither;
searchFile = file;
}
} else {
// try only literal search
suffixType = SuffixType.Neither;
searchFile = file;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getName()).append(": ");
sb.append("library=").append(library.toString());
sb.append(", loadName=").append(loadName);
sb.append(", suffixType=").append(suffixType.toString());
sb.append(", searchFile=").append(searchFile);
return sb.toString();
}
}
protected boolean tryLoadingLibraryOrScript(Ruby runtime, SearchState state) {
// attempt to load the found library
try {
state.library.load(runtime, false);
return true;
} catch (MainExitException mee) {
// allow MainExitException to propagate out for exec and friends
throw mee;
} catch (Throwable e) {
if(isJarfileLibrary(state, state.searchFile)) {
return true;
}
reraiseRaiseExceptions(e);
if(runtime.getDebug().isTrue()) e.printStackTrace(runtime.getErr());
RaiseException re = newLoadErrorFromThrowable(runtime, state.searchFile, e);
re.initCause(e);
throw re;
}
}
private static RaiseException newLoadErrorFromThrowable(Ruby runtime, String file, Throwable t) {
return runtime.newLoadError(String.format("load error: %s -- %s: %s", file, t.getClass().getName(), t.getMessage()));
}
// Using the BailoutSearch twice, once only for source files and once for state suffixes,
// in order to adhere to Rubyspec
protected final List<LoadSearcher> searchers = new ArrayList<LoadSearcher>();
{
searchers.add(new SourceBailoutSearcher());
searchers.add(new NormalSearcher());
searchers.add(new ClassLoaderSearcher());
searchers.add(new BailoutSearcher());
searchers.add(new ExtensionSearcher());
searchers.add(new ScriptClassSearcher());
}
protected String buildClassName(String className) {
// Remove any relative prefix, e.g. "./foo/bar" becomes "foo/bar".
className = className.replaceFirst("^\\.\\/", "");
if (className.lastIndexOf(".") != -1) {
className = className.substring(0, className.lastIndexOf("."));
}
className = className.replace("-", "_minus_").replace('.', '_');
return className;
}
protected void checkEmptyLoad(String file) throws RaiseException {
if (file.equals("")) {
throw runtime.newLoadError("no such file to load -- " + file);
}
}
protected void debugLogTry(String what, String msg) {
if (RubyInstanceConfig.DEBUG_LOAD_SERVICE) {
LOG.info( "LoadService: trying " + what + ": " + msg );
}
}
protected void debugLogFound(String what, String msg) {
if (RubyInstanceConfig.DEBUG_LOAD_SERVICE) {
LOG.info( "LoadService: found " + what + ": " + msg );
}
}
protected void debugLogFound( LoadServiceResource resource ) {
if (RubyInstanceConfig.DEBUG_LOAD_SERVICE) {
String resourceUrl;
try {
resourceUrl = resource.getURL().toString();
} catch (IOException e) {
resourceUrl = e.getMessage();
}
LOG.info( "LoadService: found: " + resourceUrl );
}
}
protected Library findBuiltinLibrary(SearchState state, String baseName, SuffixType suffixType) {
for (String suffix : suffixType.getSuffixes()) {
String namePlusSuffix = baseName + suffix;
debugLogTry( "builtinLib", namePlusSuffix );
if (builtinLibraries.containsKey(namePlusSuffix)) {
state.loadName = namePlusSuffix;
Library lib = builtinLibraries.get(namePlusSuffix);
debugLogFound( "builtinLib", namePlusSuffix );
return lib;
}
}
return null;
}
protected Library findLibraryWithoutCWD(SearchState state, String baseName, SuffixType suffixType) {
Library library = null;
switch (suffixType) {
case Both:
library = findBuiltinLibrary(state, baseName, SuffixType.Source);
if (library == null) library = createLibrary(state, tryResourceFromJarURL(state, baseName, SuffixType.Source));
if (library == null) library = createLibrary(state, tryResourceFromLoadPathOrURL(state, baseName, SuffixType.Source));
// If we fail to find as a normal Ruby script, we try to find as an extension,
// checking for a builtin first.
if (library == null) library = findBuiltinLibrary(state, baseName, SuffixType.Extension);
if (library == null) library = createLibrary(state, tryResourceFromJarURL(state, baseName, SuffixType.Extension));
if (library == null) library = createLibrary(state, tryResourceFromLoadPathOrURL(state, baseName, SuffixType.Extension));
break;
case Source:
case Extension:
// Check for a builtin first.
library = findBuiltinLibrary(state, baseName, suffixType);
if (library == null) library = createLibrary(state, tryResourceFromJarURL(state, baseName, suffixType));
if (library == null) library = createLibrary(state, tryResourceFromLoadPathOrURL(state, baseName, suffixType));
break;
case Neither:
library = createLibrary(state, tryResourceFromJarURL(state, baseName, SuffixType.Neither));
if (library == null) library = createLibrary(state, tryResourceFromLoadPathOrURL(state, baseName, SuffixType.Neither));
break;
}
return library;
}
protected Library findLibraryWithClassloaders(SearchState state, String baseName, SuffixType suffixType) {
for (String suffix : suffixType.getSuffixes()) {
String file = baseName + suffix;
LoadServiceResource resource = findFileInClasspath(file);
if (resource != null) {
state.loadName = resolveLoadName(resource, file);
return createLibrary(state, resource);
}
}
return null;
}
protected Library createLibrary(SearchState state, LoadServiceResource resource) {
if (resource == null) {
return null;
}
String file = state.loadName;
if (file.endsWith(".so") || file.endsWith(".dll") || file.endsWith(".bundle")) {
if (runtime.getInstanceConfig().isCextEnabled()) {
return new CExtension(resource);
} else {
throw runtime.newLoadError("C extensions are disabled, can't load `" + resource.getName() + "'");
}
} else if (file.endsWith(".jar")) {
return new JarredScript(resource);
} else if (file.endsWith(".class")) {
return new JavaCompiledScript(resource);
} else {
return new ExternalScript(resource, file);
}
}
protected LoadServiceResource tryResourceFromCWD(SearchState state, String baseName,SuffixType suffixType) throws RaiseException {
LoadServiceResource foundResource = null;
for (String suffix : suffixType.getSuffixes()) {
String namePlusSuffix = baseName + suffix;
// check current directory; if file exists, retrieve URL and return resource
try {
JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix));
debugLogTry("resourceFromCWD", file.toString());
if (file.isFile() && file.isAbsolute() && file.canRead()) {
boolean absolute = true;
foundResource = new LoadServiceResource(file, getFileName(file, namePlusSuffix), absolute);
debugLogFound(foundResource);
state.loadName = resolveLoadName(foundResource, namePlusSuffix);
break;
}
} catch (IllegalArgumentException illArgEx) {
} catch (SecurityException secEx) {
}
}
return foundResource;
}
protected LoadServiceResource tryResourceFromHome(SearchState state, String baseName, SuffixType suffixType) throws RaiseException {
LoadServiceResource foundResource = null;
RubyHash env = (RubyHash) runtime.getObject().getConstant("ENV");
RubyString env_home = runtime.newString("HOME");
if (env.has_key_p(env_home).isFalse()) {
return null;
}
String home = env.op_aref(runtime.getCurrentContext(), env_home).toString();
String path = baseName.substring(2);
for (String suffix : suffixType.getSuffixes()) {
String namePlusSuffix = path + suffix;
// check home directory; if file exists, retrieve URL and return resource
try {
JRubyFile file = JRubyFile.create(home, RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix));
debugLogTry("resourceFromHome", file.toString());
if (file.isFile() && file.isAbsolute() && file.canRead()) {
boolean absolute = true;
state.loadName = file.getPath();
foundResource = new LoadServiceResource(file, state.loadName, absolute);
debugLogFound(foundResource);
break;
}
} catch (IllegalArgumentException illArgEx) {
} catch (SecurityException secEx) {
}
}
return foundResource;
}
protected LoadServiceResource tryResourceFromJarURL(SearchState state, String baseName, SuffixType suffixType) {
// if a jar or file URL, return load service resource directly without further searching
LoadServiceResource foundResource = null;
if (baseName.startsWith("jar:")) {
for (String suffix : suffixType.getSuffixes()) {
String namePlusSuffix = baseName + suffix;
try {
URL url = new URL(namePlusSuffix);
debugLogTry("resourceFromJarURL", url.toString());
if (url.openStream() != null) {
foundResource = new LoadServiceResource(url, namePlusSuffix);
debugLogFound(foundResource);
}
} catch (FileNotFoundException e) {
} catch (MalformedURLException e) {
throw runtime.newIOErrorFromException(e);
} catch (IOException e) {
throw runtime.newIOErrorFromException(e);
}
if (foundResource != null) {
state.loadName = resolveLoadName(foundResource, namePlusSuffix);
break; // end suffix iteration
}
}
} else if(baseName.startsWith("file:") && baseName.indexOf("!/") != -1) {
for (String suffix : suffixType.getSuffixes()) {
String namePlusSuffix = baseName + suffix;
try {
String jarFile = namePlusSuffix.substring(5, namePlusSuffix.indexOf("!/"));
JarFile file = new JarFile(jarFile);
String expandedFilename = expandRelativeJarPath(namePlusSuffix.substring(namePlusSuffix.indexOf("!/") + 2));
debugLogTry("resourceFromJarURL", expandedFilename.toString());
if(file.getJarEntry(expandedFilename) != null) {
foundResource = new LoadServiceResource(new URL("jar:file:" + jarFile + "!/" + expandedFilename), namePlusSuffix);
debugLogFound(foundResource);
}