/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.jtests.appclients.timeclient;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;

/**
 * @author Guillaume Sauthier
 */
public class TimeClient {

    /**
     * Service Ref Name
     */
    private static final String SERVICE_REF_NAME = "java:comp/env/service/time";

    /**
     * InitialContext
     */
    private InitialContext ic;

    /**
     *  
     */
    public TimeClient() throws Exception {
        init();
    }

    /**
     *  
     */
    private void init() throws NamingException {
        ic = new InitialContext();
    }

    public boolean hasBindedService() throws NamingException {
        Object o = lookupService();
        if (o != null) {
            if (o instanceof Service) {
                return true;
            }
        }
        return false;
    }
    
    /*
    public boolean isServiceWellConfigured() throws NamingException {
        Service s = (Service) lookupService();
        if (s instanceof XXX) {
            return true;
        }
        return false;
    }
    */

    private Object lookupService() throws NamingException {
        return ic.lookup(SERVICE_REF_NAME);
    }

    public static void main(String[] args) throws Exception {
        TimeClient tc = new TimeClient();

        if (!tc.hasBindedService()) {
            throw new Exception("TimeClient should have a Service instance binded to '" + SERVICE_REF_NAME + "'");
        }
        
        tc.handlersInvoked();
    }

    /**
     * @return
     */
    private boolean handlersInvoked() throws Exception {
        Service s = (Service) lookupService();
        Call call = s.createCall(new QName("TimePort"), new QName("jonas:Time", "getDate"));
        call.invoke(new Object[] {});
        StaticPassValue spv = StaticPassValue.getInstance();
        String init = spv.getInit();
        String req = spv.getRequest();
        String resp = spv.getResponse();
        if (init == null) {
            throw new Exception("Handler.init not invoked");
        }
        if (req == null) {
            throw new Exception("Handler.handleRequest not invoked");
        }
        if (resp == null) {
            throw new Exception("Handler.handlerResponse not invoked");
        }
        return true;
    }
}