Loading ...
Sorry, an error occurred while loading the content.

SOAP::Lite/GLUE Examples

Expand Messages
  • gwzoller
    Hello... Below you ll find code for common examples for calling a GLUE server with a perl (SOAP::Lite) client. I wrote an earlier version of this but GLUE
    Message 1 of 1 , Jun 4, 2002
    • 0 Attachment
      Hello...

      Below you'll find code for common examples for calling
      a GLUE server with a perl (SOAP::Lite) client.

      I wrote an earlier version of this but GLUE and/or
      SOAP::Lite have changed a lot since then.

      All but two of the examples work. If you can help fix
      the broken ones, please append. :-)

      The problem examples are: sending user-written object
      (Hey) to GLUE and sending a collection (Vector) to GLUE.
      Sending a HashMap to GLUE doesn't work in 2.3.x but seems
      to be ok in 3.0beta1.

      Take care,
      Greg

      =====< ITest.java >=======================================
      import java.util.*;
      public interface ITest
      {
      // Returns
      public String hello();
      public HashMap namesMap();
      public ArrayList getList();
      public int[] getIntArray();
      public Hey getHey();
      public boolean isTrue();
      // Params
      public void echo( String msg );
      public void showList( Vector list ); // *** BROKEN!
      public void showArray( int[] arr );
      public void showHash( Hashtable ht );
      public void showHey( Hey hey ); // *** BROKEN!
      public void setTrue( boolean truth );
      }

      =====< Hey.java >============================================
      public class Hey implements Serializable
      {
      private String name = null;
      private double balance = 0.0;
      private int id = 0;
      public void setName( String name ) { this.name = name; }
      public void setBalance( double balance ) { this.balance =
      balance; }
      public void setId( int id ) { this.id = id; }
      public String getName() { return name; }
      public double getBalance() { return balance; }
      public int getId() { return id; }
      }

      =====< Server.java >========================================
      import electric.registry.Registry;
      import electric.server.http.HTTP;
      import java.util.*;
      public class Server implements ITest
      {
      private boolean truth = false;
      public String hello() { return "Hello ya'll!"; }
      public HashMap namesMap() {
      HashMap ret = new HashMap();
      HashMap n1 = new HashMap();
      n1.put("Age","79");
      n1.put("Status","married");
      n1.put("Middle","Harold");
      ret.put("Mike", n1);
      ret.put("Sarah", "Mae");
      ret.put("Patricia", "Ashley");
      return ret;
      }
      public ArrayList getList()
      {
      ArrayList ret = new ArrayList();
      ret.add("Peter");
      ret.add("Paul");
      ret.add("Mary");
      return ret;
      }
      public int[] getIntArray()
      {
      int[] ret = {2,4,6};
      return ret;
      }
      public Hey getHey()
      {
      Hey hey = new Hey();
      hey.setName("Hey!");
      hey.setBalance(250.51);
      hey.setId(7);
      return hey;
      }
      public boolean isTrue() { truth = !truth; return truth; }
      public void echo( String msg ) { System.out.println("Test
      7: " + msg); }
      public void showList( Vector list ) {
      if( list == null )
      {
      System.out.println("Test 9: String array
      is null!");
      return;
      }
      System.out.print("showList: ");
      for( int i=0; i<list.size(); i++ )
      System.out.print(((String)list.get(i))+"
      ");
      System.out.println(" ");
      }
      public void showArray( int[] arr ) {
      if( arr == null )
      {
      System.out.println("Test 10: Array is
      null!");
      return;
      }
      System.out.print("Test 10 (int array): ");
      for(int i=0; i<arr.length; i++ )
      System.out.print(arr[i]+ " ");
      System.out.println(" ");
      }
      public void showHash( Hashtable ht ) {
      if( ht == null )
      {
      System.out.println("Test 8: Hash is
      null!");
      return;
      }
      System.out.println("Test 8 (Hashtable):");
      Set keys = ht.keySet();
      for( Iterator scan=keys.iterator();
      scan.hasNext(); )
      {
      String key = (String) scan.next();
      System.out.println("\t"+key+" -->
      "+(String)ht.get(key));
      }
      }
      public void showHey( Hey hey ) {
      if( hey == null )
      {
      System.out.println("Test 11: Null Hey
      object!");
      return;
      }
      System.out.println("Test 11:");
      System.out.println("\tName = " + hey.getName());
      System.out.println("\tId = " + hey.getId());
      System.out.println("\tBalance= " +
      hey.getBalance());
      }
      public void setTrue( boolean truth )
      {
      System.out.println("Test 12: Truth is --> " +
      truth);
      }
      public static void main( String[] args ) throws Exception
      {
      HTTP.startup("http://localhost:8004/glue");
      Registry.publish( "urn:test", new Server() );
      }
      }

      =====< perltest.pl >============================================
      #!/usr/bin/perl
      # define serializer
      BEGIN {
      package Hey;
      sub new {
      my $class = shift;
      my $self = {};
      $self->{'name'} = shift;
      $self->{'id'} = shift;
      $self->{'balance'} = shift;
      bless $self, $class;
      #$self->initialize();
      return $self;
      }
      sub show {
      my $self = shift;
      print $self->{'name'}."
      (".$self->{'id'}.") has \$".$self->{'balance'}."\n";
      }
      1;
      }
      use SOAP::Lite +autodispatch =>
      uri => 'urn:test.wsdl',
      proxy => 'http://localhost:8004/glue/urn:test',
      # on_debug => sub{print@_},
      on_fault => sub { my($soap, $res) = @_;
      print "ERROR: ".$res->faultstring."\n";
      eval { die ref $res ? $res->faultstring :
      $soap->transport->status };
      return ref $res ? $res : new SOAP::SOM;
      }
      ;
      #-------------------------------------------------
      ------------------------------\
      ------
      print "::: Test Suite #1 -- Return Values From GLUE :::\n";
      print "Test 1: Simple string return (string has embedded \'
      char)\n";
      my $helloResult = &cleanString(hello());
      print "\thello() says: $helloResult\n";
      print "\n";
      print "Test 2: Return HashMap or HashTable of values\n";
      my $hashRef = &makeHashRef( namesMap() );
      foreach( keys %{$hashRef} ) {
      if( $_ eq "Gregory" ) {
      print "\t(Key/Value): $_/[";
      $z = &makeHashRef($hashRef->{$_});
      foreach( keys %{$z} ) {
      print "$_/$z->{$_} ";
      }
      print("]\n");
      } else {
      print "\t(Key/Value): $_/$hashRef->{$_}\n";
      }
      }
      print "\n";
      print "Test 3: Return an array of int\n";
      my $intlist = getIntArray();
      foreach(@{$intlist})
      {
      print"\tItem: $_\n";
      }
      print "\n";
      print "Test 4: Return an ArrayList of string\n";
      my $arrayRef = makeArrayRef(getList());
      foreach( @{$arrayRef} )
      {
      print "\tItem: $_\n";
      }
      print "\n";
      print "Test 5: Return a Hey object\n";
      my $heyRef = getHey();
      foreach( keys %{$heyRef})
      {
      print"\tMember: $_ = ".$heyRef->{$_}."\n";
      }
      print "\n";
      print "Test 6: Return a boolean\n";
      my $truth = isTrue();
      print "\tTruth: $truth\n";
      my $truth = isTrue();
      print "\tTruth: $truth\n";
      print "\n";
      print "::: Test Suite #2 -- Pass Values To GLUE :::\n";
      print "\n";
      print "Test 7: Pass a String\n";
      print "\tSending: $helloResult\n";
      my $out = SOAP::Data->name(msg => $helloResult);
      echo($out);
      #
      # BROKEN in GLUE 2.3.x. Seems to work ok in 3.0beta1
      #
      print "\n";
      print "Test 8: Pass a HashMap\n";
      my %hash;
      $hash{"one"} = "ONE";
      $hash{"two"} = "TWO";
      $hash{"three"} = "THREE";
      my $var = SOAP::Data->type('map' => \%hash)->name('ht');
      showHash( $var );
      #
      # BROKEN! Please Fix
      #
      print "\n";
      print "Test 9: Pass Vector of String\n";
      my @beatles = qw(John Ringo Paul George);
      my $vector = SOAP::Data->type(Vector => [ map
      {SOAP::Data->name(list => $_)->type('string')} @beatles ]);
      showList($vector);
      print "\n";
      print "Test 10: Pass array of int\n";
      my @arr;
      $arr[0] = 1;
      $arr[1] = 2;
      $arr[2] = 3;
      my $val = \@arr;
      my $out = SOAP::Data->name("arr" => $val);
      showArray($out);
      #
      # BROKEN! Please Fix
      #
      print "\n";
      print "Test 11: Pass Hey object\n";
      my $hey = Hey->new("Greg", 1, 1000.51);
      #my $var = SOAP::Data->name(Hey => [ map {SOAP::Data->name(list
      => $_)->type('string')} $hey ]);
      #my $var = SOAP::Data->type(Hey => $hey)->name('hey');
      my $var = SOAP::Data->name('hey' => $hey);
      print "\tPassing Hey reference: ";
      $hey->show();
      showHey( $hey );
      print "\n";
      print "Test 12: Pass a boolean\n";
      $truth = 1; # 0 = false;
      my $var = SOAP::Data->type('boolean' => $truth)->name('truth');
      setTrue($var);
      #-------------------------------------------------
      ------------------------------\
      --
      sub makeHashRef
      {
      my $resultRef = shift;
      my %hash = %{$resultRef};
      my $keyRef = $hash{'keys'};
      my $valRef = $hash{'values'};
      my $i = 0;
      my $ret;
      foreach( @{$keyRef} )
      {
      $ret->{ $_ } = $valRef->[$i++];
      }
      return $ret;
      }
      sub makeArrayRef
      {
      my $arrayListHashRef = shift;
      return $arrayListHashRef->{"elements"};
      }
      sub cleanString
      {
      my $str = shift;
      $str =~ s/\&apos\;/\'/g;
      $str =~ s/\<\;/\</g;
      $str =~ s/\>\;/\>/g;
      return $str;
      }
    Your message has been successfully submitted and would be delivered to recipients shortly.