<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Port Component Link Test</title>
  </head>

  <body>
    <h1>Port Component Link Test</h1>

    <%@ page contentType="text/html" %>
    <%@ page import="javax.naming.InitialContext" %>
    <%@ page import="javax.xml.rpc.Service" %>
    <%@ page import="javax.xml.rpc.Call" %>
    <%@ page import="javax.xml.rpc.Stub" %>
    <%@ page import="java.rmi.Remote" %>
    <%@ page import="javax.xml.namespace.QName" %>
    <%@ page import="org.objectweb.jonas.jtests.servlets.portlink.Address" %>
    <%

    // new InitialContext
    InitialContext ic = new InitialContext();

    // Lookup
    Service s = (Service) ic.lookup("java:comp/env/service/AddressBook");

    // QNames preparation
    QName port = new QName("jonas:AddressBook", "AddressBookPort");
    QName opReset = new QName("jonas:AddressBook", "reset");
    QName opAddAddress = new QName("jonas:AddressBook", "addAddress");
    QName opGetAddresses = new QName("jonas:AddressBook", "getAddresses");
    QName opGetAddress = new QName("jonas:AddressBook", "getAddress");
    QName opIsPresent = new QName("jonas:AddressBook", "isPresent");

    // Call creation
    Call call = s.createCall(port);
    call.setProperty(Call.SESSION_MAINTAIN_PROPERTY, new Boolean(true));


    Remote remote = s.getPort(org.objectweb.jonas.jtests.servlets.portlink.AddressBook.class);

    %>
    <br/>
    Config : <br/>
    <a id="location" href="index.jsp">
    get.getTargetEndpointAddress() : '<%= call.getTargetEndpointAddress() %>'
    </a>
    <a id="stub" href="index.jsp">
    getPort return a Stub : '<%= (remote instanceof Stub) %>'<br/>
    </a>
    <%

    // preInvoke
    // reset the base
    call.setOperationName(opReset);
    call.invoke(new Object[] {});

    // Call execution
    // 1: getAddress
    call.setOperationName(opGetAddress);
    Address jonas = (Address) call.invoke(new Object[] {"JOnAS"});

    // 2: addAddress
    Address jonasTeam = new Address();
    jonasTeam.setName("JOnAS-Team");
    jonasTeam.setCompany("ObjectWeb Consortium");
    jonasTeam.setVersion(1.0f);
    call.setOperationName(opAddAddress);
    call.invoke(new Object[] {jonasTeam});

    // 3: getAddresses
    call.setOperationName(opGetAddresses);
    // return a Address[] but ClassCastException is thrown when direct casting
    Object[] objects = (Object[]) call.invoke(new Object[] {});

    // So we cast Addresses one by one in a new Array.
    Address[] addresses = new Address[objects.length];
    for (int i = 0; i < objects.length; i++ )  {
        addresses[i] = (Address) objects[i];
    }
    %>
    <a id="classname" href="index.jsp">
      return  type : <%= addresses[0].getClass().getName() %>
    </a>
    <%
    
    // 4: isPresent
    call.setOperationName(opIsPresent);
    Boolean presence = (Boolean) call.invoke(new Object[] {jonasTeam.getName()});
    %>
    <br/>
    <br/>
    Results : <br/>
    <br/>
    <a id="get" href="index.jsp">
    getAddress("JOnAS") : <br/>
    name : '<%= jonas.getName() %>'<br/>
    company : '<%= jonas.getCompany() %>'<br/>
    version : '<%= jonas.getVersion() %>'<br/>
    </a>

    <a id="getAll" href="index.jsp">
      getAddresses() length : <%= addresses.length %><br/>
      <%
      for (int i = 0; i < addresses.length; i++) {
      out.println("<a id=\"add_" + i + "\">");
      %>
      

      name[<%= i %>] : '<%= addresses[i].getName() %>'<br/>
      company[<%= i %>] : '<%= addresses[i].getCompany() %>'<br/>
      version[<%= i %>] : '<%= addresses[i].getVersion() %>'<br/>
    </a>
      <%
      }
      %>
      </a>

    <br/>
    <a id="present" href="index.jsp">
    isPresent("JOnAS-Team") : '<%= presence %>'<br/>
    </a>

  </body>
</html>
