In order to accomplish frame communication I've tryed an interesting
approach that I can share with the community.

The goal is to serialize an object into an econded String directly on
client side, pass through this data to an internal frame that expose a
javascript method to do it.
The frame that is a GWT application call a service that decode the
encoded String into the correct object.

You can use this method to restore an history status with contents
stored in a separated internal frame,  to pass contents between frames
with only one server call.

Example code:

Class SharedObject:

package frame.communication.client;

import com.google.gwt.user.client.rpc.IsSerializable;

public class SharedObject implements IsSerializable {

        private String name;

        private String surname;

        SharedObject() {
        }

        public SharedObject(String name, String surname) {
                setName(name);
                setSurname(surname);
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getSurname() {
                return surname;
        }

        public void setSurname(String surname) {
                this.surname = surname;
        }

        @Override
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((name == null) ? 0 : 
name.hashCode());
                result = prime * result + ((surname == null) ? 0 : 
surname.hashCode
());
                return result;
        }

        @Override
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                SharedObject other = (SharedObject) obj;
                if (name == null) {
                        if (other.name != null)
                                return false;
                } else if (!name.equals(other.name))
                        return false;
                if (surname == null) {
                        if (other.surname != null)
                                return false;
                } else if (!surname.equals(other.surname))
                        return false;
                return true;
        }

}


Class FrameCommunication:

package frame.communication.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamFactory;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;

public class FrameCommunication implements EntryPoint {

        public void onModuleLoad() {
                SharedObject sharedObject = new SharedObject("John", "Doe");

                try {
                        String encodedRequest = 
clientSerialization(sharedObject);
                        decodeClientSerialization(sharedObject, encodedRequest);
                } catch (SerializationException e) {
                        Window.alert(e.getMessage());
                }
        }

        public String clientSerialization(SharedObject sharedObject)
                        throws SerializationException {
                SerializationStreamFactory factory = GWT
                                .create(SharedObjectService.class);
                SerializationStreamWriter writer = factory.createStreamWriter();
                writer.writeString(SharedObject.class.getName());
                writer.writeObject(sharedObject);
                return writer.toString();
        }

        public void decodeClientSerialization(final SharedObject
sharedObject,
                        String encodedRequest) {
                SharedObjectService.Util.getInstance().decode(encodedRequest,
                                new AsyncCallback<SharedObject>() {
                                        public void onFailure(Throwable caught) 
{
                                                
Window.alert(caught.getMessage());
                                        }

                                        public void onSuccess(SharedObject 
result) {
                                                boolean sameObject = 
sharedObject.equals(result);
                                                Window.alert("Successful: " + 
sameObject);
                                        }
                                });
        }

}


Class SharedService:

package frame.communication.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public interface SharedObjectService extends RemoteService {

        public static final String SERVICE_URI = "SharedObjectService";

        public static class Util {

                public static SharedObjectServiceAsync getInstance() {

                        SharedObjectServiceAsync instance = 
(SharedObjectServiceAsync) GWT
                                        .create(SharedObjectService.class);
                        ServiceDefTarget target = (ServiceDefTarget) instance;
                        target.setServiceEntryPoint(GWT.getModuleBaseURL() + 
SERVICE_URI);
                        return instance;
                }
        }

        public SharedObject decode(String encoded)
                        throws SerializationDecodeException;
}

Class SharedServiceImpl:

package frame.communication.server;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import
com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader;

import frame.communication.client.SerializationDecodeException;
import frame.communication.client.SharedObject;
import frame.communication.client.SharedObjectService;

public class SharedObjectServiceImpl extends RemoteServiceServlet
implements
                SharedObjectService {

        public SharedObject decode(String encodedRequest)
                        throws SerializationDecodeException {
                ClassLoader classLoader = Thread.currentThread()
                                .getContextClassLoader();

                ServerSerializationStreamReader streamReader = new
ServerSerializationStreamReader(
                                classLoader, null);

                try {
                        streamReader.prepareToRead(encodedRequest);
                        String className = (String) streamReader
                                        .deserializeValue(String.class);
                        return (SharedObject) 
streamReader.deserializeValue(Class
                                        .forName(className));
                } catch (SerializationException e) {
                        throw new SerializationDecodeException(e.getMessage());
                } catch (ClassCastException ccExc) {
                        throw new 
SerializationDecodeException(ccExc.getMessage());
                } catch (ClassNotFoundException e) {
                        throw new SerializationDecodeException(e.getMessage());
                }
        }

}




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to