public void 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().loadJavaClassQuiet(className); state.library = new ClassExtensionLibrary(theClass); } catch (Exception ee) { state.library = null; runtime.getGlobalVariables().set("$!", runtime.getNil()); } // If there was a good library before, we go back to that if (state.library == null && oldLibrary != null) { state.library = oldLibrary; } }
public void 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) { throw runtime.newLoadError("no such file to load -- " + state.searchFile); } state.library = new ScriptClassLibrary(script); }
protected Library findLibraryBySearchState(SearchState state) { Library library = super.findLibraryBySearchState(state); if (library == null) { library = findLibraryWithClassloaders(state, state.searchFile, state.suffixType); if (library != null) { state.library = library; } } return library; }
public void trySearch(SearchState state) { state.library = findLibraryWithClassloaders(state, state.searchFile, state.suffixType); }
public void trySearch(SearchState state) { state.library = findLibraryWithoutCWD(state, state.searchFile, state.suffixType); }