marshal/unmarshal a java method - java

I have a java class that contain a method with some logic control . This class is avaible as output via a webservice method to a java swing client .
My question sounds crazy but is it possible to marshal/unmarshal the method to make it available to the client when he generate the class from the wsdl file ?
Thanks.
ps:I'm using Metro/jax-ws and glassfish 3.1.1.

You mean so you can send some logic down to the client to be executed there?
I'm afraid the answer is no - this is not how web services work.
All the logic is performed on the server side.
Why do you wish to do this?

Why not provide this class (your VO) to the client so both the server and client are marshalling/unmarshalling the same class? Then both can use the your method...

Marshalling" refers to the process of converting the data or the objects into a byte-stream, and "unmarshalling" is the reverse process of converting the byte-stream back into their original data or object.
The conversion is achieved through "serialization".
The purpose of the "marshalling/unmarshalling" process is to transfer data between the RMI systems.
For more http://ws.apache.org/old/jaxme/manual/ch02s02.html

Related

How to send a "command" Object through Sockets to be excecuted on the server? Java

i am working on a chat program.
[JAVA] [Without RMI, just Sockets] [Command example: 'sentToMike', 'Disconnect', 'Login', etc]
How do i send a "command" Object through Sockets to be excecuted on the server directly?
I want to send all kind of messages(Strings, Audio, Video), and all kind of Command objects to many clients, any of them. I know there exist ObjectInput/Output objects and all of that. My problem is trying to get a polymorphic solution.
For example i want to create a IMessage interface with a method signature "execute()". Then i would create a AudioMessage, TextMessage, etc that implements the IMessage. The problem is that at some point i need to share the server code with the client and viceversa in order Server and client know all the objects involved in every excecute method. And worst of all is that if i send an IMessage, the server would't know what specific type the message is, so i dont know to what kind cast the Object. The same would happen when i send the Command back to the client.
I can work a solution with simple text strings "commands" and a big and ugly switch in the server(and in the client by the way), but i believe that is not elegant, i would need to create a wrapper class with the string command plus the object of the kind i want to send plus the string with the type of object been sent(Message[String type; String command; IMessage->AudioMessage ]), this wont be polymorphic since i will need to use the switch to ask the type of the object and then cast it to AudioMessage for example. Furthermore i would need to share a lot of code between server and client and i dont know if that would be ok.
Any advice will be very very welcome, maybe i need a design pattern, an architecture pattern, i have no clue.
There are security reason to not allow just any code to run on server!
But if you are willing to expose your server (and client) to unknown code, then you need to also serve classes bytecode, and have classloaders to enable instanciating classes' types you expect the other end to accept. Your protocol would have to send the full classname and locations (if not inlining the bytecode) of the alien class (and all its dependencies not found in parent classloader), for the purpose of hoping to call any method of such object.
(FYI, that just reinventing RMI).
If you don't have to call anything on this object (it's not your case, I know, but I musy say it), then it is passive and there is really no point in transporting it as an object instance.

How can I send an instance over a socket connection?

I'm attempting to write two Java programs.
One to simulate a server, and one to simulate a client.
How could I go about sending an instance of a Response class over a socket?
The Response class represents status codes of the server connection. e.g. 404 Not Found etc
I'm not allowed to use Serialisation unfortunately.
Any advice would be greatly appreciated.
At some level Serialization must occur in order to send an object across a connection. I can only assume your comment about being not allowed to use serialization refers to not being able to use Serializable instead of a blanket prohibition of serialization(which makes no sense). A very simple method to accomplish this would be the use of a external serialization library such as gson. Gson serializes an object into a JSON string that you can transmit over your socket and then using the same library deserialize it back into an object on the other side. You can of course use any of your preferred serialization libraries with your favorite format eg. XML, json, YAML,...
You wouldn't be sending an instance of the Response class itself. When sending things over a network, client and server machines understand bytes. Your application can understand more than bytes, it can understand specific representations. For example, your server might send a JSON representation of your Response class like:
{
"response" : {
"code":404
}
}
Then your client must be able to understand what this sequence of bytes means. That's basically what a protocol is: how two machines can communicate.
Regardless of what language the server or clients are written in, the Response is an Entity. In Java you might use a Class to represent it, in C++ you might use a struct. However, both would need to know that when you are communicating with an external application.system, they would have to put it in a format that everyone understands, be it json, xml, or any other.
As for sending this through sockets, Oracle has a nice tutorial here. You get the OutputStream
from the socket and start writing your representation.

send object using HTTPservletResponse

I have a client-server program that are using servlets to communicate with each other, each of them is sending an object (of a class that I defined) to the other.
Can I send it directly using the setContentType(myClass) in the servlet?
Or do I need to something completely different? I couldn't find a way to do it.
You need to serialize the object at server side, send it as an array of bytes, or encoded as a text (using base64 for example), and than deserialize it at the client side.
There is one more alternative approach: use WebServices. You can get to know more about them at Apache AXIS website.

Sending object via socket, but different language client, how to make the object serialize?

I have a Java Server that sending the java serializable object to my client, and receive java serializable object for execution. If my client is also java written, which is nice, that allow me to do communication within any problems.
But now, I would like to extend my programme to not only java client, the client may be written in C, objective C, python or php. So, I would like to do something to "convent" to client request to a java object, and send back to Server. The convent process, I can use the JSON to receive, and construct a Java object to the Server, but I also need a layer that convert back the Java object to JSON to the client.
My Question is except make a JSON-Java Translation layer, is there any other ways to do so? Also, we can afford to change some code in server side, but we must use Java as our primary language for that. Any suggestions? Thanks.
I use Netty API for designing my protocol and it is quite quick to do so if you can understand a NIO-like Byte and Buffer API.
It is design to work with a concept of Encoder and Decoder that could fit your need, there are a lot of default implementation of Encoder and Decoder for zipping, using ssl...
The problem you have seems to looks like this one:
JBoss Netty with JSON
I don't know JSON very well but most of the time is could also be quick and easy to design your own protocol.
Do you need a generic Serialization process for any kind of Object or do you simply need to serialize some String and primitive types (Integer, Short, Float..etc)?
In the case of simple objects it is easy and a lot faster to do the wrapper by yourself.
If objects are quite simple, and I would guess this is the case, your need it to design your own "protocol" specification meaning how to turn each Object into a sequence of primitive types, String and arrays. Than it should be quite easy to write both the Encoder and the Decoder in each language.
Good luck
There are other libraries designed for this, like protocol buffers and thrift.
http://thrift.apache.org/
http://code.google.com/p/protobuf/

How to send class over TCP connection?

I am trying to send a class over a TCP connection using java. When the client receives the class it should invoke the methods of that class and return the result to the server.
I have serialized the class to a byte array to send to the client, but I don't know with it once the client receives it.
Thank you.
Your question is a bit ambiguous. Are you sending a *.class file or an instance of the class? I'll bet that you actually mean an instance of the class since you literally said that you want to send it back. This makes only sense if it were an instance. The other side should then have the class file in its classpath as well. Then you can just import the class and cast to the desired class on readObject(). Finally you'll be able to invoke methods on it according the class' contract.
See also:
Basic Serialization tutorial
Advanced Serialization tutorial
If you're actually sending a *.class file, using a ClassLoader to load it would indeed be the answer.
You probably need to use a ClassLoader.
If possible for you, you may want to look at RMI where clients can provide classes for the server to invoke.

Categories