KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyProcess


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

29 package org.jruby;
30
31 import org.jruby.runtime.Block;
32 import org.jruby.runtime.CallbackFactory;
33 import org.jruby.runtime.ObjectAllocator;
34 import org.jruby.runtime.builtin.IRubyObject;
35
36
37 /**
38  *
39  * @author enebo
40  */

41 public class RubyProcess {
42
43     public static RubyModule createProcessModule(Ruby runtime) {
44         RubyModule process = runtime.defineModule("Process");
45         
46         // TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here. Confirm. JRUBY-415
47
RubyModule process_status = process.defineClassUnder("Status", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
48
49         CallbackFactory processCallbackFactory = runtime.callbackFactory(RubyProcess.class);
50         CallbackFactory process_statusCallbackFactory = runtime.callbackFactory(RubyProcess.RubyStatus.class);
51
52 // process.defineModuleFunction("fork", processCallbackFactory.getSingletonMethod("fork"));
53
// process.defineModuleFunction("exit!", processCallbackFactory.getOptSingletonMethod("exit_bang"));
54
// process.defineModuleFunction("exit", processCallbackFactory.getOptSingletonMethod("exit"));
55
// process.defineModuleFunction("abort", processCallbackFactory.getOptSingletonMethod("abort"));
56
// process.defineModuleFunction("kill", processCallbackFactory.getOptSingletonMethod("kill"));
57
// process.defineModuleFunction("wait", processCallbackFactory.getOptSingletonMethod("wait"));
58
// process.defineModuleFunction("wait2", processCallbackFactory.getOptSingletonMethod("wait2"));
59
// process.defineModuleFunction("waitpid", processCallbackFactory.getOptSingletonMethod("waitpid"));
60
// process.defineModuleFunction("waitpid2", processCallbackFactory.getOptSingletonMethod("waitpid2"));
61
// process.defineModuleFunction("waitall", processCallbackFactory.getSingletonMethod("waitall"));
62
// process.defineModuleFunction("detach", processCallbackFactory.getSingletonMethod("detach", RubyKernel.IRUBY_OBJECT));
63
// process.defineModuleFunction("pid", processCallbackFactory.getSingletonMethod("pid"));
64
// process.defineModuleFunction("ppid", processCallbackFactory.getSingletonMethod("ppid"));
65
//
66
// process.defineModuleFunction("getpgrp", processCallbackFactory.getSingletonMethod("getprgrp"));
67
// process.defineModuleFunction("setpgrp", processCallbackFactory.getSingletonMethod("setpgrp"));
68
// process.defineModuleFunction("getpgid", processCallbackFactory.getSingletonMethod("getpgid", RubyKernel.IRUBY_OBJECT));
69
// process.defineModuleFunction("setpgid", processCallbackFactory.getSingletonMethod("setpgid", RubyKernel.IRUBY_OBJECT, RubyKernel.IRUBY_OBJECT));
70
//
71
// process.defineModuleFunction("setsid", processCallbackFactory.getSingletonMethod("setsid"));
72
//
73
// process.defineModuleFunction("getpriority", processCallbackFactory.getSingletonMethod("getpriority", RubyKernel.IRUBY_OBJECT, RubyKernel.IRUBY_OBJECT));
74
// process.defineModuleFunction("setpriority", processCallbackFactory.getSingletonMethod("setpriority", RubyKernel.IRUBY_OBJECT, RubyKernel.IRUBY_OBJECT, RubyKernel.IRUBY_OBJECT));
75

76 // #ifdef HAVE_GETPRIORITY
77
// rb_define_const(rb_mProcess, "PRIO_PROCESS", INT2FIX(PRIO_PROCESS));
78
// rb_define_const(rb_mProcess, "PRIO_PGRP", INT2FIX(PRIO_PGRP));
79
// rb_define_const(rb_mProcess, "PRIO_USER", INT2FIX(PRIO_USER));
80
// #endif
81

82 // process.defineModuleFunction("uid", processCallbackFactory.getSingletonMethod("uid"));
83
// process.defineModuleFunction("uid=", processCallbackFactory.getSingletonMethod("uid_set", RubyKernel.IRUBY_OBJECT));
84
// process.defineModuleFunction("gid", processCallbackFactory.getSingletonMethod("gid"));
85
// process.defineModuleFunction("gid=", processCallbackFactory.getSingletonMethod("gid_set", RubyKernel.IRUBY_OBJECT));
86
// process.defineModuleFunction("euid", processCallbackFactory.getSingletonMethod("euid"));
87
// process.defineModuleFunction("euid=", processCallbackFactory.getSingletonMethod("euid_set", RubyKernel.IRUBY_OBJECT));
88
// process.defineModuleFunction("egid", processCallbackFactory.getSingletonMethod("egid"));
89
// process.defineModuleFunction("egid=", processCallbackFactory.getSingletonMethod("egid_set", RubyKernel.IRUBY_OBJECT));
90
// process.defineModuleFunction("initgroups", processCallbackFactory.getSingletonMethod("initgroups", RubyKernel.IRUBY_OBJECT, RubyKernel.IRUBY_OBJECT));
91
// process.defineModuleFunction("groups", processCallbackFactory.getSingletonMethod("groups"));
92
// process.defineModuleFunction("groups=", processCallbackFactory.getSingletonMethod("groups_set", RubyKernel.IRUBY_OBJECT));
93
// process.defineModuleFunction("maxgroups", processCallbackFactory.getSingletonMethod("maxgroups"));
94
// process.defineModuleFunction("maxgroups=", processCallbackFactory.getSingletonMethod("maxgroups_set", RubyKernel.IRUBY_OBJECT));
95
process.defineModuleFunction("times", processCallbackFactory.getSingletonMethod("times"));
96         
97         // Process::Status methods
98
process_status.defineMethod("==", process_statusCallbackFactory.getMethod("op_eq", RubyKernel.IRUBY_OBJECT));
99 // process_status.defineMethod("&", process_statusCallbackFactory.getMethod("op_and"));
100
process_status.defineMethod(">>", process_statusCallbackFactory.getMethod("rightshift_op", RubyKernel.IRUBY_OBJECT));
101         process_status.defineMethod("to_i", process_statusCallbackFactory.getMethod("to_i"));
102 // process_status.defineMethod("to_int", process_statusCallbackFactory.getMethod("to_int"));
103
process_status.defineMethod("to_s", process_statusCallbackFactory.getMethod("to_s"));
104         process_status.defineMethod("inspect", process_statusCallbackFactory.getMethod("inspect"));
105 // process_status.defineMethod("pid", process_statusCallbackFactory.getMethod("pid"));
106
// process_status.defineMethod("stopped?", process_statusCallbackFactory.getMethod("stopped_p"));
107
// process_status.defineMethod("stopsig", process_statusCallbackFactory.getMethod("stopsig"));
108
// process_status.defineMethod("signaled?", process_statusCallbackFactory.getMethod("signaled_p"));
109
// process_status.defineMethod("termsig", process_statusCallbackFactory.getMethod("termsig"));
110
// process_status.defineMethod("exited?", process_statusCallbackFactory.getMethod("exited_p"));
111
process_status.defineMethod("exitstatus", process_statusCallbackFactory.getMethod("exitstatus"));
112         process_status.defineMethod("success?", process_statusCallbackFactory.getMethod("success_p"));
113 // process_status.defineMethod("coredump?", process_statusCallbackFactory.getMethod("coredump_p"));
114

115         return process;
116     }
117     
118     public static class RubyStatus extends RubyObject {
119         private long status = 0L;
120         
121         private static final long EXIT_SUCCESS = 0L;
122         public RubyStatus(Ruby runtime, RubyClass metaClass, long status) {
123             super(runtime, metaClass);
124             
125             this.status = status;
126         }
127         
128         public static RubyStatus newProcessStatus(Ruby runtime, long status) {
129             return new RubyStatus(runtime, runtime.getModule("Process").getClass("Status"), status);
130         }
131         
132         public IRubyObject exitstatus(Block block) {
133             return getRuntime().newFixnum(status);
134         }
135         
136         public IRubyObject rightshift_op(IRubyObject other, Block block) {
137             long shiftValue = other.convertToInteger().getLongValue();
138             
139             
140             return getRuntime().newFixnum(status >> shiftValue);
141         }
142         
143         public IRubyObject op_eq(IRubyObject other, Block block) {
144             return other.callMethod(getRuntime().getCurrentContext(), "==", this.to_i(block));
145         }
146
147         public IRubyObject to_i(Block unusedBlock) {
148             return exitstatus(null);
149         }
150         
151         public IRubyObject to_s(Block unusedBlock) {
152             return getRuntime().newString(String.valueOf(status));
153         }
154         
155         public IRubyObject inspect(Block unusedBlock) {
156             return to_s();
157         }
158         
159         public IRubyObject success_p(Block unusedBlock) {
160             return getRuntime().newBoolean(status == EXIT_SUCCESS);
161         }
162     }
163     
164     public static IRubyObject times(IRubyObject recv, Block unusedBlock) {
165         Ruby runtime = recv.getRuntime();
166         double currentTime = System.currentTimeMillis() / 1000.0;
167         double startTime = runtime.getStartTime() / 1000.0;
168         RubyFloat zero = runtime.newFloat(0.0);
169         return RubyStruct.newStruct(runtime.getTmsStruct(),
170                 new IRubyObject[] { runtime.newFloat(currentTime - startTime), zero, zero, zero },
171                 Block.NULL_BLOCK);
172     }
173
174     public static IRubyObject pid(IRubyObject recv) {
175         return recv.getRuntime().newFixnum(0);
176     }
177
178     public static IRubyObject kill(IRubyObject recv, IRubyObject[] args) throws Exception JavaDoc {
179         return recv.getRuntime().getNil();
180     }
181 }
182
Popular Tags