KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > JavaEmbedUtils


1 package org.jruby.javasupport;
2
3 /***** BEGIN LICENSE BLOCK *****
4  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Common Public
7  * License Version 1.0 (the "License"); you may not use this file
8  * except in compliance with the License. You may obtain a copy of
9  * the License at http://www.eclipse.org/legal/cpl-v10.html
10  *
11  * Software distributed under the License is distributed on an "AS
12  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * rights and limitations under the License.
15  *
16  * Copyright (C) 2006 Thomas E Enebo <enebo@acm.org>
17  *
18  * Alternatively, the contents of this file may be used under the terms of
19  * either of the GNU General Public License Version 2 or later (the "GPL"),
20  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21  * in which case the provisions of the GPL or the LGPL are applicable instead
22  * of those above. If you wish to allow use of your version of this file only
23  * under the terms of either the GPL or the LGPL, and not to allow others to
24  * use your version of this file under the terms of the CPL, indicate your
25  * decision by deleting the provisions above and replace them with the notice
26  * and other provisions required by the GPL or the LGPL. If you do not delete
27  * the provisions above, a recipient may use your version of this file under
28  * the terms of any one of the CPL, the GPL or the LGPL.
29  ***** END LICENSE BLOCK *****/

30
31 import java.util.List JavaDoc;
32
33 import org.jruby.Ruby;
34 import org.jruby.runtime.Block;
35 import org.jruby.runtime.ThreadContext;
36 import org.jruby.runtime.builtin.IRubyObject;
37
38 /**
39  * Utility functions to help embedders out. These function consolidate logic that is
40  * used between BSF and JSR 223. People who are embedding JRuby 'raw' should use these
41  * as well. If at a later date, we discover a flaw or change how we do things, this
42  * utility class should provide some insulation.
43  *
44  */

45 public class JavaEmbedUtils {
46     /**
47      * Get an instance of a JRuby runtime. Provide any loadpaths you want used at startup.
48      *
49      * @param loadPaths to specify where to look for Ruby modules.
50      * @return an instance
51      */

52     public static Ruby initialize(List JavaDoc loadPaths) {
53         Ruby runtime = Ruby.getDefaultInstance();
54         runtime.getLoadService().init(loadPaths);
55         runtime.getLoadService().require("java");
56         
57         return runtime;
58     }
59
60     /**
61      * Dispose of the runtime you initialized.
62      *
63      * @param runtime to be disposed of
64      */

65     public static void terminate(Ruby runtime) {
66         runtime.tearDown();
67         runtime.getThreadService().disposeCurrentThread();
68     }
69     
70     /**
71      * Convenience function for embedders
72      *
73      * @param runtime environment where the invoke will occur
74      * @param receiver is the instance that will receive the method call
75      * @param method is method to be called
76      * @param args are the arguments to the method
77      * @param returnType is the type we want it to conform to
78      * @return the result of the invocation.
79      */

80     public static Object JavaDoc invokeMethod(Ruby runtime, Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args,
81             Class JavaDoc returnType) {
82         IRubyObject rubyReceiver = receiver != null ?
83                 JavaUtil.convertJavaToRuby(runtime, receiver) : runtime.getTopSelf();
84
85         IRubyObject[] rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args);
86
87         // Create Ruby proxies for any input arguments that are not primitives.
88
IRubyObject javaUtilities = runtime.getObject().getConstant("JavaUtilities");
89         ThreadContext context = runtime.getCurrentContext();
90         for (int i = 0; i < rubyArgs.length; i++) {
91             IRubyObject obj = rubyArgs[i];
92
93             if (obj instanceof JavaObject) {
94                 rubyArgs[i] = javaUtilities.callMethod(context, "wrap", obj);
95             }
96         }
97
98         IRubyObject result = rubyReceiver.callMethod(context, method, rubyArgs);
99
100         return rubyToJava(runtime, result, returnType);
101     }
102     
103     /**
104      * Convert a Ruby object to a Java object.
105      *
106      */

107     public static Object JavaDoc rubyToJava(Ruby runtime, IRubyObject value, Class JavaDoc type) {
108         return JavaUtil.convertArgument(Java.ruby_to_java(runtime.getObject(), value, Block.NULL_BLOCK), type);
109     }
110
111     /**
112      * Convert a java object to a Ruby object.
113      */

114     public static IRubyObject javaToRuby(Ruby runtime, Object JavaDoc value) {
115         if (value instanceof IRubyObject) {
116             return (IRubyObject) value;
117         }
118         IRubyObject result = JavaUtil.convertJavaToRuby(runtime, value);
119         if (result instanceof JavaObject) {
120             return runtime.getModule("JavaUtilities").callMethod(runtime.getCurrentContext(), "wrap", result);
121         }
122         return result;
123     }
124
125     public static IRubyObject javaToRuby(Ruby runtime, boolean value) {
126         return javaToRuby(runtime, value ? Boolean.TRUE : Boolean.FALSE);
127     }
128     public static IRubyObject javaToRuby(Ruby runtime, byte value) {
129         return javaToRuby(runtime, new Byte JavaDoc(value));
130     }
131     public static IRubyObject javaToRuby(Ruby runtime, char value) {
132         return javaToRuby(runtime, new Character JavaDoc(value));
133     }
134     public static IRubyObject javaToRuby(Ruby runtime, double value) {
135         return javaToRuby(runtime, new Double JavaDoc(value));
136     }
137     public static IRubyObject javaToRuby(Ruby runtime, float value) {
138         return javaToRuby(runtime, new Float JavaDoc(value));
139     }
140     public static IRubyObject javaToRuby(Ruby runtime, int value) {
141         return javaToRuby(runtime, new Integer JavaDoc(value));
142     }
143     public static IRubyObject javaToRuby(Ruby runtime, long value) {
144         return javaToRuby(runtime, new Long JavaDoc(value));
145     }
146     public static IRubyObject javaToRuby(Ruby runtime, short value) {
147         return javaToRuby(runtime, new Short JavaDoc(value));
148     }
149 }
150
Popular Tags