Java.pm

filter: Statementstmt 18.5% 47 / 254 Branchbran 3.4% 3 / 86 Conditioncond 8.5% 3 / 35 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 30.9% 13 / 42 Podpod 7.4% 2 / 27 totaltotal 14.7% 68 / 462 CC 59 SCAR 39.9
CC
59
Cov
14%
pretty_args
240.0
_init
73.9
send_command_and_get_response
42.0
CRAP 54.1
↑ Summary blib/lib/java_server_install.pl »
1 # $Header: /cvsroot/javaserver/javaserver/JavaServer/perl/Java.pm,v 1.3 2004/02/25 07:52:15 zzo Exp $
2 # $Revision: 1.3 $
3 # $Log: Java.pm,v $
4 # Revision 1.3 2004/02/25 07:52:15 zzo
5 # - Fix test scripts to use local Java.pm & Test & Test2 classes in the
6 # com.zzo.javaserver package
7 # - added 'destroy' method to Java.pm so y'all can hand destroy objects
8 #
9 # Revision 1.2 2004/01/19 23:28:59 zzo
10 # Moved the java files around to put them in 'com.zzo.javaserver' package
11 # Fixed test script
12 #
13 # Revision 1.1.1.1 2003/11/17 22:08:07 zzo
14 # Initial import.
15 #
16 # Revision 1.16 2003/06/24 17:28:48 mark
17 # - Correctly encode parameter value for static set_field calls
18 # - 'die' instead of exit on authentication failure
19 # - anchor 'false:b' regex in pretty_args
20 # - try to make install more platform independent
21 #
22 # Revision 1.15 2003/04/14 22:50:51 mark
23 # allow negative integers, test fix
24 #
25 # Revision 1.14 2002/09/03 17:26:43 mark
26 # bump version #
27 #
28 # Revision 1.13 2002/09/03 17:21:03 mark
29 # Fix Makefile.PL so EXE_FILES are an anon array
30 # When checking for ERROR make sure it must begin on beginning of line!
31 # Better perldocs
32 #
33 # Revision 1.12 2001/11/30 19:18:52 mark
34 # Windows fix
35 #
36 # Revision 1.11 2001/08/13 18:36:08 mark
37 # Make the auth secret stuff work w/Winblows & Krapple by getting rid of
38 # '\015' chars @ end of line
39 #
40 # Revision 1.10 2001/08/02 16:05:14 mark
41 # Fix for Windows authentication
42 #
43 # Revision 1.9 2001/08/01 21:53:08 mark
44 # version to 4.1.1
45 #
46 # Revision 1.8 2001/08/01 21:51:38 mark
47 # Fixed generic handling of pulling out instance data from 'java' object
48 #
49 # Revision 1.7 2001/07/29 21:18:37 mark
50 # VERSION up to 4.1
51 #
52 # Revision 1.6 2001/07/20 22:39:44 mark
53 # Allow blank lines for callbacks
54 #
55 # Revision 1.5 2001/07/17 15:50:25 mark
56 # Made sure exceptions are stored in the 'java' object & not the
57 # instantiated object. added some convenience functions for this too
58 #
59 # Revision 1.4 2001/07/13 14:59:51 mark
60 # Put eval'ed callbacks in package main by default like it should have been
61 # in the first place
62 #
63 # Revision 1.3 2001/07/10 18:47:36 mark
64 # Changed '\r' to more portable '\015'
65 #
66 # Revision 1.2 2001/07/09 23:05:51 mark
67 # Clean up
68 #
69 # Revision 1.1.1.1 2001/07/09 22:33:57 mark
70 # Initial Toss In
71 #
72 # Revision 1.2 2000/05/15 21:24:37 markt
73 # This is da Big Daddy
74 #
75  
76 package Java;
77  
78 # Perl5 is good enough for me I think
79 1 require 5;
80  
81 ##
82 # If you 'use strict' you have to do 'no strict 'subs'' 'cuz all Java
83 # function calls are AUTOLOAD'ed - sorry.
84 ##
85  
86 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 # NOTE - you may have to 'use IO::Socket::INET' if yer perl install
91 # is cracked...
92 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 # Fancy pants array stuff
95 1 ▶use JavaArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 # Now allow '==' to mimic 'same' functionality
98 1 ▶use overload '==' => "same", 'fallback' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 1 ▶use vars qw ($AUTOLOAD @ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 1 require Exporter;
103 1 @ISA = qw(Exporter);
104  
105 1 $VERSION = '4.7';
106  
107 # Items to export into callers namespace by default. Note: do not export
108 # names by default without a very good reason. Use EXPORT_OK instead.
109 # Do not simply export all your public functions/methods/constants.
110  
111 # This allows declaration use Java ':all';
112 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
113 # will save memory.
114  
115 # Extremely cheesy
116 1 ▶use constant PARAMETER_SEPARATOR => "";
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 1 ▶use constant NULL_TOKEN => "___NULL___";
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 # Preloaded methods go here.
121 sub new
122 1 ▶{
Subroutine new: called CC=1 SCAR=0.2
Pod undocumented
123 1 my $self = {};
124 1 bless $self, shift;
125 1 $self->_init(@_);
126 0 return $self;
127 }
128  
129 ##
130 # When ya first create one of these monsters it'll try to connect
131 # to JavaServer running on
132 # host => host
133 # port => port
134 #
135 # And the JavaServer will attempt to connect to US on event_port...
136 # event_port => event port
137 #
138 # supplied in 'new' arguments or use defaults
139 # 'localhost', 2000 & 2001
140 #
141 # Also it'll use old-style arrays if specified.
142 sub _init
143 1 ▶{
Subroutine _init: called CC=12 SCAR=43.0
144 1 my($self,%attrs) = @_;
145  
146 1 ▶ $self->{port} = $attrs{port} || 2000;
Condition50%1/2
$attrs{'port'} || 2000
l!l
01
MC/DC0%0/1
$attrs{'port'} || 2000
$attrs{'port'}
Truth table50%1/2
A
A= $attrs{'port'}
Aresult
11
00
147 1 ▶ $self->{host} = $attrs{host} || "localhost";
Condition50%1/2
$attrs{'host'} || 'localhost'
l!l
01
MC/DC0%0/1
$attrs{'host'} || 'localhost'
$attrs{'host'}
Truth table50%1/2
A
A= $attrs{'host'}
Aresult
11
00
148 1 ▶ $self->{event_port} = $attrs{event_port} || 2001;
Condition50%1/2
$attrs{'event_port'} || 2001
l!l
01
MC/DC0%0/1
$attrs{'event_port'} || 2001
$attrs{'event_port'}
Truth table50%1/2
A
A= $attrs{'event_port'}
Aresult
11
00
149  
150 # It's now the default!
151 1 ▶ $self->{use_tied_arrays} = 1 unless $attrs{use_old_style_arrays};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"use_old_style_arrays"} 1 0 1
152  
153 ##
154 # Set up them sockets!
155 ##
156  
157 # Client/control socket
158 $self->{socket} = IO::Socket::INET->new
159 (
160 PeerAddr => $self->{host},
161 PeerPort => $self->{port}
162 1 );
163  
164 1 ▶ if (!$self->{socket})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"socket"}) 1 0 1
165 {
166 1 croak("Client socket error: $!");
167 }
168 # Make sure we're autoflushed
169 0 $self->{socket}->autoflush(1);
170  
171 0 my $authSecret = "";
172  
173 # Check for authorization file & use it!
174 0 ▶ if (defined($attrs{authfile}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs{"authfile"}) 0 0 0
175 {
176 0 ▶ open(AUTH,$attrs{authfile}) or croak("Could not open ".$attrs{authfile}.": $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open AUTH, $attrs{"authfile"} or croak('Could not open ' . $attrs{"authfile"} . ": $!\n") 0 0 0
177 0 $authSecret=<AUTH>;
178 0 close(AUTH);
179 0 $authSecret =~ s/\015//g; # clean up input from Winblows
180 0 chomp($authSecret);
181 }
182  
183 # Send Auth token
184 0 $self->{socket}->print("AUTH: ".$authSecret."\n");
185  
186 # Check response
187 0 my $line = $self->{socket}->getline;
188 0 $line =~ s/\015//g; # clean up input from Winblows
189 0 chomp $line;
190  
191 0 ▶ die("Authentication Failed: $line") unless ($line =~ 'OK');
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /OK/ 0 0 0
192  
193 # Set to '-1' to disable events
194 0 ▶ if ($self->{event_port} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"event_port"} > 0) 0 0 0
195 {
196 #
197 # Set up our event_socket server
198 #
199 $self->{event_server} = IO::Socket::INET->new
200 (
201 Listen => 5,
202 LocalPort => $self->{event_port},
203 0 Proto => 'tcp',
204 Reuse => 1
205 );
206
207 0 ▶ if (!$self->{event_server})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"event_server"}) 0 0 0
208 {
209 0 croak("Couldn't create event_server socket: $!");
210 }
211 }
212  
213 ## Tell JavaServer what port we want our events on...
214 0 $self->{socket}->print($self->{event_port}."\n");
215  
216 0 ▶ if ($self->{event_port} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"event_port"} > 0) 0 0 0
217 {
218 # Don't wanna do nuthin' until we hear from JavaServer on our
219 # event_server port
220 0 my $peer_address;
221 0 ($self->{event_socket}, $peer_address) = $self->{event_server}->accept;
222
223 #my($port, $iaddr) = sockaddr_in($peer_address);
224 #$iaddr = inet_ntoa($iaddr);
225 #print STDERR "Event port connexion from $iaddr:$port!\n";
226
227 # Don't wanna accept any more event_server connexions
228 0 undef $self->{event_server};
229  
230 # AutoFlush this bad boy
231 # We're only gonna use this monster to read events
232 0 $self->{event_socket}->autoflush(1);
233 }
234 }
235  
236 ###
237 # This is used to create a new Java object
238 ###
239 sub create_object
240 0 ▶{
Subroutine create_object: not called CC=1 SCAR=6.9
Pod undocumented
241 0 my($self,$what,@args) = @_;
242  
243 # pretty up the arguments for java-land
244 0 @args = pretty_args(@args);
245  
246 0 local($") = PARAMETER_SEPARATOR;
247 0 my $resp = $self->send_command_and_get_response("NEW $what(@args)");
248 #
249 # Create a new java object
250 #
251 0 $self->new_java_object($resp);
252 }
253  
254 # Get callback object so server side can make perl calls
255 sub get_callback_object
256 0 ▶{
Subroutine get_callback_object: not called CC=1 SCAR=6.9
Pod undocumented
257 0 my($self) = @_;
258  
259 0 my $resp = $self->send_command_and_get_response("BCK");
260  
261 0 $self->new_java_object($resp);
262 }
263  
264 # Guess what this does...
265 sub create_array
266 0 ▶{
Subroutine create_array: not called CC=1 SCAR=6.9
Pod undocumented
267 0 my($self,$what,@indicies) = @_;
268
269 # We don't need to pretty args here.... all assumed to be ints
270 # and need to be separated by commans NOT PARAMETER_SEPARATOR
271 0 local($") = ",";
272 0 my $resp = $self->send_command_and_get_response("NEW [L$what;(@indicies)");
273 0 $self->new_java_object($resp);
274 }
275  
276 # Hmmm... wonder what this does?
277 sub set_field
278 0 ▶{
Subroutine set_field: not called CC=2 SCAR=17.9
Pod undocumented
279 0 my($self,$index,@args) = @_;
280
281 # pretty up the arguments for java-land
282 0 @args = pretty_args(@args);
283  
284 0 local($") = PARAMETER_SEPARATOR;
285 0 my $line;
286  
287 # Figure out what we're dealing with...
288 0 ▶ if ($self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_java) { } 0 0 0
289 {
290 # static object
291 0 $line = "SET $_[1]#$_[2](@args[1..$#args])";
292 0 print "STATIC LINE: $line\n";
293 }
294 else
295 {
296 # instantiated object
297 0 $line = "SET $self->{name}#$index(@args)";
298 }
299
300 0 my $resp = $self->send_command_and_get_response($line);
301 0 $self->new_java_object($resp);
302 }
303  
304 sub get_length
305 0 ▶{
Subroutine get_length: not called CC=1 SCAR=6.9
Pod undocumented
306 # Get an array's length
307 0 my($self) = @_;
308 0 my $line = "GET $self->{name}#LEN";
309 0 my $resp = $self->send_command_and_get_response($line);
310 # We're just getting a raw integer back here...
311 0 chomp $resp;
312 0 $resp;
313 }
314  
315  
316  
317 ###
318 # Bless the returned string into a 'Java' object
319 # & be mindful of if this 'self' already points to a
320 # java object or not
321 ###
322 sub new_java_object
323 0 ▶{
Subroutine new_java_object: not called CC=4 SCAR=30.0
Pod undocumented
324 0 my($self, $line) = @_;
325 0 chomp $line;
326  
327 # NULL!
328 0 ▶ return 0 if ($line eq 'NUL');
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "NUL" 0 0 0
329  
330 #
331 # If we're creating this object from another one
332 # i.e. from a method call
333 # pull the 'java' portion outta there...
334 0 my $java = $self->_get_in_java('java');
335  
336 #print STDERR "Created Java Object $line\n";
337 0 my $obj = (bless { name => $line, java => $java }, ref $self);
338  
339 #
340 # If we're using the tied array syntax convert this guy into
341 # a tied array IF INDEED it is an array that is!
342 #
343 0 ▶ if ($java->{use_tied_arrays})
Branch: 1 branch
BranchTrueFalseTotal
if ($java->{"use_tied_arrays"}) 0 0 0
344 {
345 0 ▶ if ($line =~ /^\[/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\[/) 0 0 0
346 {
347 0 my @java_object;
348 0 tie @java_object, 'JavaArray', $obj;
349 0 return \@java_object;
350 }
351 }
352  
353 # Otherwise just return the thang
354 0 return $obj;
355 }
356  
357 # Gets the client socket
358 sub get_socket
359 0 ▶{
Subroutine get_socket: not called CC=1 SCAR=6.9
Pod undocumented
360 0 my ($self) = shift;
361 0 $self->_get_in_java('socket');
362 }
363  
364 # Gets the incoming event socket
365 sub get_event_socket
366 0 ▶{
Subroutine get_event_socket: not called CC=1 SCAR=6.9
Pod undocumented
367 0 my ($self) = shift;
368 0 $self->_get_in_java('event_socket');
369 }
370  
371 sub send_line
372 0 ▶{
Subroutine send_line: not called CC=4 SCAR=30.0
Pod undocumented
373 0 my($self,$line) = @_;
374 0 ▶ return if (!$self || !defined($line));
Branch: 1 branch
BranchTrueFalseTotal
unless $self and defined $line 0 0 0
Condition0%0/3
$self and defined $line
!ll&&!rl&&r
000
MC/DC0%0/2
$self and defined $line
$self defined $line
Truth table0%0/3
A and B
A= $selfB= defined $line
ABresult
0X0
100
111
375 0 ▶ print "Sending line: $line\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
376 0 $self->get_socket->print("$line\n\n");
377 }
378
379 sub send_command_and_get_response
380 0 ▶{
Subroutine send_command_and_get_response: not called CC=6 SCAR=37.4
Pod undocumented
381 0 my($self,$line) = @_;
382 0 my $resp = "";
383 0 ▶ if ($self->send_line($line))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->send_line($line)) { } 0 0 0
384 {
385 # The response ends w/a  on a line by itself
386 0 while (1)
387 {
388 0 my $line;
389 0 $line = $self->get_socket->getline;
390 0 ▶ if (!defined $line)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
391 {
392 0 croak("Error receiving response");
393 }
394 0 $line =~ s/\015//g; # clean up input from Winblows
395  
396 # Check for end of response
397 0 ▶ last if ($line =~ /^$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\001$/ 0 0 0
398  
399 0 $resp .= $line;
400 }
401  
402 0 chomp $resp; # clean up last newline
403  
404 # Pull out the Exception object if it's there
405 0 ▶ if ($resp =~ /^ERROR/)
Branch: 1 branch
BranchTrueFalseTotal
if ($resp =~ /^ERROR/) 0 0 0
406 {
407 # Peel off Exception object if it's there
408 0 ▶ if ($resp =~ s/%%%(.*)$//)
Branch: 1 branch
BranchTrueFalseTotal
if ($resp =~ s/%%%(.*)$//) 0 0 0
409 {
410 # & keep track of it in the Java object
411 0 my $ex_obj = $self->new_java_object($1);
412 0 $self->_set_in_java('last_exception',$ex_obj);
413 }
414 0 croak($resp);
415 }
416  
417 0 return $resp;
418 }
419 else
420 {
421 0 croak("Error sending $line");
422 }
423 }
424  
425 # Gets the most recent Exception object
426 sub get_exception
427 0 ▶{
Subroutine get_exception: not called CC=1 SCAR=6.9
Pod documented
428 0 my($self) = @_;
429 0 $self->_get_in_java('last_exception');
430 }
431  
432 #
433 # Does the nasty work for ya to get the Stack Trace for
434 # the most recent Exception
435 # Returns array of stack trace lines
436 #
437 sub get_stack_trace
438 0 ▶{
Subroutine get_stack_trace: not called CC=1 SCAR=6.9
Pod documented
439 0 my ($self) = @_;
440  
441 0 my $exception_object = $self->get_exception;
442  
443 # Get the Stack Trace - blame Java for this mess!
444 0 my $string_writer = $self->create_object("java.io.StringWriter");
445 0 my $print_writer = $self->create_object("java.io.PrintWriter", $string_writer);
446 0 $exception_object->printStackTrace($print_writer);
447  
448 0 my $line = $string_writer->toString->get_value;
449 0 $line =~ s/\015//g; # Get rid of Windows/Krapple nastiness
450 0 split(/\n/, $line);
451 }
452  
453 ## This'll return an objectified field from a static or instantiated reference
454 # you ken this call 'get_value' on this monster to string-ify it...
455 # Also gets array elements...
456 sub get_field
457 0 ▶{
Subroutine get_field: not called CC=2 SCAR=17.9
Pod undocumented
458 0 my($self) = shift;
459 0 my $resp;
460 0 ▶ if ($self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_java) { } 0 0 0
461 {
462 # Get static field
463 0 $resp = $self->send_command_and_get_response("GET $_[0]#$_[1]");
464 }
465 else
466 {
467 # Get instantiated field
468 0 $resp = $self->send_command_and_get_response("GET $self->{name}#$_[0]");
469 }
470  
471 # Objectify it
472 0 return $self->new_java_object($resp);
473 }
474  
475 ## This'll return an STRING value from a static or instantiated reference
476 sub get_value
477 0 ▶{
Subroutine get_value: not called CC=2 SCAR=17.9
Pod undocumented
478 0 my($self) = shift;
479 0 ▶ if ($self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_java) { } 0 0 0
480 {
481 # Get static value
482 0 $self->send_command_and_get_response("VAL $_[0]");
483 }
484 else
485 {
486 # Get instantiated field value
487 0 $self->send_command_and_get_response("VAL $self->{name}");
488 }
489 }
490  
491 # Convert 'perl' type args to 'java' type args
492 sub pretty_args
493 0 ▶{
Subroutine pretty_args: not called CC=15 SCAR=54.8
Pod undocumented
494 ##
495 # Append type name to each primitive arg
496 # Gotta add all them other primitive types here...
497 ##
498 0 foreach (@_)
499 {
500 0 ▶ if (!defined)
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
elsif (/^-?\d+$/) { } 0 0 0
elsif (/^true:b$/i or /^false:b$/i) { } 0 0 0
elsif (/:char$/i or /:short$/ or /:float$/ or /:double$/ or /:byte$/ or /:long$/) { } 0 0 0
elsif (ref $_) { } 0 0 0
501 {
502 # Wanna pass 'null' in
503 0 $_ = NULL_TOKEN;
504 }
505 elsif (/^-?\d+$/)
506 {
507 # If it looks like an INT it is an int...
508 0 $_ .= ":int";
509 }
510 ▶ elsif (/^true:b$/i || /^false:b$/i)
Condition0%0/3
/^true:b$/i or /^false:b$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
/^true:b$/i or /^false:b$/i
/^true:b$/i /^false:b$/i
Truth table0%0/3
A or B
A= /^true:b$/iB= /^false:b$/i
ABresult
1X1
011
000
511 {
512 # Stick that 'oolean' @ the end of ':b'!
513 0 $_ .= "oolean";
514 }
515 ▶ elsif (/:char$/i || /:short$/ || /:float$/ || /:double$/
Condition0%0/15
/:char$/i or /:short$/ or /:float$/ or /:double$/ or /:byte$/ or /:long$/
l!l&&r!l&&!r
000
/:char$/i or /:short$/ or /:float$/ or /:double$/ or /:byte$/
l!l&&r!l&&!r
000
/:char$/i or /:short$/ or /:float$/ or /:double$/
l!l&&r!l&&!r
000
/:char$/i or /:short$/ or /:float$/
l!l&&r!l&&!r
000
/:char$/i or /:short$/
l!l&&r!l&&!r
000
MC/DC0%0/6
/:char$/i or /:short$/ or /:float$/ or /:double$/ or /:byte$/ or /:long$/
/:char$/i /:short$/ /:float$/ /:double$/ /:byte$/ /:long$/
Truth table0%0/7
A or B or C or D or E or F
A= /:char$/iB= /:short$/C= /:float$/D= /:double$/E= /:byte$/F= /:long$/
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
516 || /:byte$/ || /:long$/)
517 {
518 #leave it alone
519 }
520 elsif (ref $_)
521 {
522 # It's a JavaArray - we get the actual underlying
523 # 'Java' object by pop'ing the array...
524 # who knew?
525 0 ▶ if (ref $_ eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "ARRAY") 0 0 0
526 {
527 0 $_ = pop @$_;
528 }
529  
530 # It's a Java object already
531 0 $_ = $_->{name};
532 }
533 else
534 {
535 # It's a string
536 # in case it's an integer w/a ':string' already at the
537 # end of it
538 # Or it is an encoding string like "Unicdoe:string_UTF"
539  
540 # Either way we gotta put quotes around it & append
541 # it w/:string or w/:string_<ENCODING>
542  
543 # Put quotes around it
544 0 $_ = "\"".$_;
545 0 ▶ unless (s/:string/\":string/)
Branch: 1 branch
BranchTrueFalseTotal
unless (s/:string/":string/) 0 0 0
546 {
547 # Regular string
548 0 $_ .= "\":string";
549 }
550
551 }
552 }
553 0 @_;
554 }
555  
556 sub get_chars_from_dec
557 0 ▶{
Subroutine get_chars_from_dec: not called CC=1 SCAR=6.9
Pod undocumented
558 # Makes 2-byte hex ints...
559 0 unpack("H*",pack("n",shift));
560 }
561  
562 sub create_raw_string
563 0 ▶{
Subroutine create_raw_string: not called CC=2 SCAR=17.9
Pod undocumented
564 # This is dicey!!!
565  
566 0 my($self,$encoding,$string) = @_;
567  
568 0 my @all_bytes;
569 0 my @chars = split //, $string;
570 0 foreach (@chars)
571 {
572 # Makes integers outta chars...
573 0 push @all_bytes, unpack("C",$_);
574 }
575  
576 ## @all_bytes is now an array of integer bytes values representing
577 # the unicode string
578 ##
579  
580 0 my $len = @all_bytes;
581 0 my $line = "BYTE java.lang.String $encoding $len";
582 0 $self->send_line($line);
583  
584 # Wait for response
585 0 my $resp = $self->get_socket->getline;
586  
587 # Send bytes
588 0 local($") = " ";
589 0 $resp = $self->send_command_and_get_response("@all_bytes");
590 0 $self->new_java_object($resp);
591 }
592  
593 sub do_event
594 0 ▶{
Subroutine do_event: not called CC=2 SCAR=17.9
Pod undocumented
595 0 my($self,$object,$func,$callback) = @_;
596  
597 0 my $object_name = $object->{name};
598 0 my $resp = $self->send_command_and_get_response("EVT $object_name(\"$func\")");
599 0 $func =~ s/listener//i;
600  
601 ##
602 # So this'll look like
603 # $self->events->java.awt.Frame^234->Window = $callback
604 # so all 'Window' events for java.awt.Frame^234 will point
605 # to a 2-keyed hash containing the object itself & the callback
606 # to callback...
607 #
608 0 ▶ if ($func =~ s/^add//)
Branch: 1 branch
BranchTrueFalseTotal
if ($func =~ s/^add//) { } 0 0 0
609 {
610 0 $self->{events}->{$object_name}->{$func} =
611 {
612 obj => $object,
613 callback => $callback
614 };
615 }
616 else
617 {
618 0 $func =~ s/remove//;
619 0 delete $self->{events}->{$object_name}->{$func};
620 }
621 }
622  
623 ##
624 # One of our java objects has gone out of scope so tell JavaServer about it
625 # OR the main java object is gone & we're done...
626 sub DESTROY
627 1 ▶{
Subroutine DESTROY: called CC=4 SCAR=22.3
628 1 my($self) = shift;
629 1 ▶ if ($self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_java) { } 0 1 1
630 {
631 # Entire Java hash going out of scope
632 0 ▶ $self->{socket}->close() if ($self->{socket});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 0 0 0
633 0 ▶ $self->{event_socket}->close() if ($self->{event_socket});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"event_socket"} 0 0 0
634 0 undef $self;
635 }
636 else
637 {
638 # Plain old scalar - java object
639 # Tell JavaServer we're done w/it...
640 my $resp =
641 1 $self->{java}->send_command_and_get_response("BYE $self->{name}");
642 0 undef $self;
643 }
644 }
645  
646 sub destroy
647 0 ▶{
Subroutine destroy: not called CC=2 SCAR=17.9
Pod undocumented
648 0 my($self) = shift;
649 0 ▶ if (!$self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_is_java) 0 0 0
650 {
651 my $resp =
652 0 $self->{java}->send_command_and_get_response("BYE $self->{name}");
653 0 undef $self;
654 }
655 }
656  
657 ##
658 # This'll capture all function calls...
659 ##
660 sub AUTOLOAD
661 0 ▶{
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
662 0 my($self,@args) = @_;
663 0 my ($func) = $Java::AUTOLOAD =~ /::(.+)$/;
664 0 my @goo;
665  
666 # it's a static call UNLESS $self is an instantiated class
667 0 ▶ if ($func =~ /_/ && $self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($func =~ /_/ and $self->_is_java) { } 0 0 0
Condition0%0/3
$func =~ /_/ and $self->_is_java
!ll&&!rl&&r
000
MC/DC0%0/2
$func =~ /_/ and $self->_is_java
$func =~ /_/ $self->_is_java
Truth table0%0/3
A and B
A= $func =~ /_/B= $self->_is_java
ABresult
0X0
100
111
668 {
669 # called like $java->java_lang_Class("forName","java.lang.String");
670 # Pull out object name
671 0 my $obj;
672 0 ($obj = $func) =~ s/_/\./g;
673 0 push @goo, $obj;
674 }
675 else
676 {
677 # regular method call
678 # called like $frame->setSize(200,200);
679 # Pull out object name & function name
680 0 push @goo, $self->{name}, $func;
681 }
682  
683 0 return base_call($self,@goo,@args);
684 }
685  
686 #
687 # Make a static function call if yer object ain't in a package...
688 #
689 # Called like
690 # $java->static_call("MyStaticClass","function_name","param1","param2"....);
691 #
692 sub static_call
693 0 ▶{
Subroutine static_call: not called CC=1 SCAR=6.9
Pod undocumented
694 0 return base_call(@_);
695 }
696  
697 # wrapper routine for instantiated calls
698 sub call
699 0 ▶{
Subroutine call: not called CC=1 SCAR=6.9
Pod undocumented
700 0 my $self = shift;
701 0 return base_call($self,$self->{name},@_);
702 }
703  
704 sub base_call
705 0 ▶{
Subroutine base_call: not called CC=4 SCAR=30.0
Pod undocumented
706 0 my($self,$obj,$func,@args) = @_;
707 0 local($") = PARAMETER_SEPARATOR;
708  
709 # Make args java-friendly
710 0 @args = pretty_args(@args);
711  
712 0 my $resp=$self->send_command_and_get_response("CAL $obj%$func(@args)");
713  
714 # Handle a callback request
715 0 ▶ while ($resp =~ s/^CALLBACK //)
Branch: 1 branch
BranchTrueFalseTotal
if ($resp =~ s/^CALLBACK //) 0 0 0
716 {
717 # eval it - put it in package main if they don't
718 # specify one themselves
719 0 my $ret = eval("package main;$resp");
720 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
721 {
722 0 chomp $@;
723  
724 # Something went wrong - send a response back
725 0 $resp = $self->send_command_and_get_response($@);
726  
727 # & tell someone
728 0 print STDERR "Remote callback failed: $@";
729 }
730  
731 0 ▶ $ret ||= "";
Condition0%0/2
$ret ||= ""
l!l
00
MC/DC0%0/1
$ret ||= ""
$ret
Truth table0%0/2
A
A= $ret
Aresult
11
00
732 0 $resp = $self->send_command_and_get_response($ret);
733 }
734  
735 0 return $self->new_java_object($resp);
736 }
737  
738 ##
739 # This compares two objects to see if they're the same one!
740 # pretty much only useful for event handling I think...
741 ##
742 sub same
743 0 ▶{
Subroutine same: not called CC=1 SCAR=6.9
Pod undocumented
744 0 my($self,$other_obj) = @_;
745 0 $self->{name} eq $other_obj->{name};
746 }
747  
748 ###
749 # The Event Loop
750 # Just sit around & wait for 1 line from the JVM
751 # return undef is there's a problem
752 # return whatever the event handler returned if alls kool
753 ##
754 sub go
755 0 ▶{
Subroutine go: not called CC=4 SCAR=30.0
Pod undocumented
756 0 my $self = shift;
757  
758 # Mite not be using events
759 0 ▶ return if (!$self->get_event_socket);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_event_socket 0 0 0
760  
761 0 my $READBITS = 0;
762 0 vec($READBITS,$self->get_event_socket->fileno,1) = 1;
763 0 my $nf = select(my $rb = $READBITS,undef,undef,0);
764  
765 0 ▶ return if (!$nf);
Branch: 1 branch
BranchTrueFalseTotal
unless $nf 0 0 0
766  
767 0 my $line = $self->get_event_socket->getline;
768 0 ▶ return if (!defined $line); # lost somebody
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
769  
770 0 $self->decipher_event($line);
771 }
772  
773 ##
774 # Decipher & Dispatch this event
775 ##
776 sub decipher_event
777 0 ▶{
Subroutine decipher_event: not called CC=1 SCAR=6.9
Pod undocumented
778 0 my($self,$line) = @_;
779  
780 0 chomp $line;
781 0 $line =~ s/\015//g; # Clean up input from Winblows...
782
783 # figure out who wanted this event
784 0 $line =~ s/^EVE:\s+//;
785  
786 # Get the two strings
787 0 my($event_object,$object_name) = split / /,$line;
788
789 # Get rid of 'Event'
790 0 my($event) = $event_object =~ /\.(\w+)Event/;
791  
792 # Get the hash for this event object
793 0 my $hash = $self->{events}->{$object_name}->{$event};
794  
795 # Make the Event object a 'blessed' java object
796 0 my $new_event_obj = $self->new_java_object($event_object);
797  
798 # Call the event callback
799 0 $hash->{callback}->($hash->{obj},$new_event_obj);
800 }
801  
802 ##
803 # In case someone wants to do the event loop themselves...
804 ##
805 sub get_event_FH
806 0 ▶{
Subroutine get_event_FH: not called CC=1 SCAR=6.9
Pod undocumented
807 0 my $self = shift;
808 0 $self->get_event_socket;
809 }
810
811 sub _get_in_java
812 0 ▶{
Subroutine _get_in_java: not called CC=5 SCAR=34.0
813 0 my($self,$what) = @_;
814 0 ▶ if ($self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_java) { } 0 0 0
815 {
816 0 ▶ return $self if ($what eq 'java' && !$self->{java});
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "java" and not $self->{"java"} 0 0 0
Condition0%0/3
$what eq "java" and not $self->{"java"}
!ll&&!rl&&r
000
MC/DC0%0/2
$what eq "java" and not $self->{"java"}
$what eq "java" not $self->{"java"}
Truth table0%0/3
A and not(B)
A= $what eq "java"B= not $self->{"java"}
ABresult
0X0
100
111
817 0 return $self->{$what};
818 }
819 else
820 {
821 0 ▶ return $self->{java} if ($what eq 'java');
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "java" 0 0 0
822 0 return $self->{java}->{$what};
823 }
824 }
825  
826 sub _set_in_java
827 0 ▶{
Subroutine _set_in_java: not called CC=2 SCAR=17.9
828 0 my($self,$what,$value) = @_;
829 0 ▶ if ($self->_is_java)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_java) { } 0 0 0
830 {
831 0 return $self->{$what} = $value
832 }
833 else
834 {
835 0 $self->{java}->{$what} = $value;
836 }
837 }
838  
839 sub _is_java
840 1 ▶{
Subroutine _is_java: called CC=1 SCAR=0.0
841 1 shift->{socket};
842 }
843  
844 # Autoload methods go after =cut, and are processed by the autosplit program.
845  
846 1 1;
847 __END__
↑ Summary blib/lib/java_server_install.pl »