/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2009 INRIA/University of Nice-Sophia Antipolis
 * Contact: proactive@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.objectweb.proactive.core.remoteobject.http.message;

import java.io.Serializable;

import org.objectweb.proactive.core.body.request.Request;
import org.objectweb.proactive.core.remoteobject.InternalRemoteRemoteObject;
import org.objectweb.proactive.core.remoteobject.http.util.HTTPRegistry;
import org.objectweb.proactive.core.remoteobject.http.util.HttpMessage;


public class HTTPRemoteObjectRequest extends HttpMessage implements Serializable {
    private Request request;

    public HTTPRemoteObjectRequest(Request request, String url) {
        super(url);
        this.request = request;
    }

    public Object getReturnedObject() { //throws Exception {
        //        if (this.returnedObject instanceof Exception) {
        //            throw (Exception) this.returnedObject;
        //        }
        return this.returnedObject;
    }

    @Override
    public boolean isOneWay() {
        return this.request.isOneWay();
    }

    /**
     *
     */
    @Override
    public Object processMessage() {
        try {
            InternalRemoteRemoteObject ro = HTTPRegistry.getInstance().lookup(url);
            int max_retry = 10;
            while ((ro == null) && (max_retry > 0)) {
                try {
                    Thread.sleep(1000);
                    ro = HTTPRegistry.getInstance().lookup(url);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                max_retry--;
            }

            Object o = ro.receiveMessage(this.request);

            return o;
        } catch (Exception e) {
            return e;
        }
    }
}

@@ -1333,22 +1333,116 @@
 .io.
-Serializable;%0A
+IOException;%0Aimport java.io.Serializable;%0A%0Aimport org.objectweb.proactive.core.body.future.MethodCallResult;
 %0Aimp
@@ -1573,16 +1573,87 @@
 Object;%0A
+import org.objectweb.proactive.core.remoteobject.SynchronousReplyImpl;%0A
 import o
@@ -1789,16 +1789,66 @@
 essage;%0A
+import org.objectweb.proactive.core.util.Sleeper;%0A
 %0A%0Apublic
@@ -2122,176 +2122,8 @@
 () %7B
- //throws Exception %7B%0A        //        if (this.returnedObject instanceof Exception) %7B%0A        //            throw (Exception) this.returnedObject;%0A        //        %7D
 %0A   
@@ -2459,11 +2459,11 @@
 y = 
-10;
+5;%0A
 %0A   
@@ -2475,15 +2475,11 @@
     
-while (
+if 
 (ro 
@@ -2491,89 +2491,378 @@
 ll) 
-&& (max_retry %3E 0)) %7B%0A                try %7B%0A                    Thread.sleep(1000
+%7B%0A                // this case happens when a method call has been performed while the%0A                // registration in the registry has not yet been completed.%0A                // this mostly appears in multithreaded code.%0A                Sleeper sleeper = new Sleeper(1000);%0A                while ((ro == null) && (max_retry %3E 0)) %7B%0A                    sleeper.sleep(
 );%0A 
@@ -2945,43 +2945,42 @@
     
-%7D catch (InterruptedException e1) %7B
+    max_retry--;%0A                %7D
 %0A   
@@ -2976,32 +2976,39 @@
       %7D%0A        
+    %7D%0A%0A
             e1.p
@@ -3007,30 +3007,53 @@
     
-e1.printStackTrace(
+Object o = ro.receiveMessage(this.request
 );%0A
+%0A
     
@@ -3064,189 +3064,595 @@
     
-    %7D%0A                max_retry--;%0A            %7D%0A%0A            Object o = ro.receiveMessage(this.request);%0A%0A            return o;%0A        %7D catch (Exception e) %7B%0A            return e
+return o;%0A        %7D catch (Throwable e) %7B%0A            // this point is mostly reached when the remote object is unknown by the registry.%0A            // functional exceptions have already been caught deeper in the remote object code.%0A            // Due to the current implementation, the only way to return the exception is by wrapping%0A            // it into a methodcallresult inside a synchronousreply.%0A            // this solves PROACTIVE-717%0A            return new SynchronousReplyImpl(new MethodCallResult(null, new IOException(%22remote object %22 +%0A                url + %22not found%22)))
 ;%0A  

resolve PROACTIVE-717:http does not return an io exception when the remote object is not found on the registry