Skip to content

Commit

Permalink
DUBBO-518 字节流泛化调用使用native java序列化
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi committed Jul 25, 2012
1 parent 4e7c8d3 commit 1f7a4cd
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,45 @@ public void testGenericSerializationJava() throws Exception {
String name = "kimi";
ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension("java").serialize(null, bos).writeObject(name);
.getExtension("nativejava").serialize(null, bos).writeObject(name);
byte[] arg = bos.toByteArray();
Object obj = genericService.$invoke("sayName", new String[]{String.class.getName()}, new Object[]{arg});
Assert.assertTrue(obj instanceof byte[]);
byte[] result = (byte[]) obj;
Assert.assertEquals(ref.sayName(name), ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension("java").deserialize(null, new ByteArrayInputStream(result)).readObject().toString());
.getExtension("nativejava").deserialize(null, new ByteArrayInputStream(result)).readObject().toString());

// getUsers
List<User> users = new ArrayList<User>();
User user = new User();
user.setName(name);
users.add(user);
bos = new ByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension("nativejava").serialize(null, bos).writeObject(users);
obj = genericService.$invoke("getUsers",
new String[]{List.class.getName()},
new Object[]{bos.toByteArray()});
Assert.assertTrue(obj instanceof byte[]);
result = (byte[]) obj;
Assert.assertEquals(users,
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension("nativejava")
.deserialize(null, new ByteArrayInputStream(result))
.readObject());

// echo(int)
bos = new ByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava")
.serialize(null, bos).writeObject(Integer.MAX_VALUE);
obj = genericService.$invoke("echo", new String[]{int.class.getName()}, new Object[]{bos.toByteArray()});
Assert.assertTrue(obj instanceof byte[]);
Assert.assertEquals(Integer.MAX_VALUE,
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension("nativejava")
.deserialize(null, new ByteArrayInputStream((byte[]) obj))
.readObject());

} finally {
reference.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.api;
package com.alibaba.dubbo.config.api;

import java.util.List;


/**
* DemoService
*
* @author william.liangf
*/


/**
* DemoService
*
* @author william.liangf
*/
public interface DemoService {


String sayName(String name);

Box getBox();

void throwDemoException() throws DemoException;

List<User> getUsers(List<User> users);


int echo(int i);

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,24 @@ public void setName(String name) {
this.name = name;
}

@Override
public int hashCode() {
return name == null ? -1 : name.hashCode();
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof User)) {
return false;
}
User other = (User) obj;
if (this == other) {
return true;
}
if (name != null && other.name != null) {
return name.equals(other.name);
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.provider.impl;

package com.alibaba.dubbo.config.provider.impl;

import java.util.List;

import com.alibaba.dubbo.config.api.Box;
import com.alibaba.dubbo.config.api.DemoException;
import com.alibaba.dubbo.config.api.DemoService;
import com.alibaba.dubbo.config.api.User;

/**
* DemoServiceImpl
*
* @author william.liangf
*/

/**
* DemoServiceImpl
*
* @author william.liangf
*/
public class DemoServiceImpl implements DemoService {

public String sayName(String name) {
return "say:" + name;

public String sayName(String name) {
return "say:" + name;
}

public Box getBox() {
Expand All @@ -44,5 +44,9 @@ public void throwDemoException() throws DemoException {
public List<User> getUsers(List<User> users) {
return users;
}


public int echo(int i) {
return i;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ public void throwDemoException() throws DemoException {
public List<User> getUsers(List<User> users) {
return users;
}

public int echo(int i) {
return i;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ public void throwDemoException() throws DemoException {
public List<User> getUsers(List<User> users) {
return users;
}

public int echo(int i) {
return i;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
try {
UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[])args[i]);
args[i] = ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension(Constants.GENERIC_SERIALIZATION_JAVA).deserialize(null, is).readObject();
.getExtension(getSerializationExtension(Constants.GENERIC_SERIALIZATION_JAVA))
.deserialize(null, is).readObject();
} catch (Exception e) {
throw new RpcException("Deserialize argument [" + (i + 1) + "] failed.", e);
}
Expand All @@ -93,7 +94,8 @@ public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension(Constants.GENERIC_SERIALIZATION_JAVA).serialize(null, os).writeObject(result.getValue());
.getExtension(getSerializationExtension(Constants.GENERIC_SERIALIZATION_JAVA))
.serialize(null, os).writeObject(result.getValue());
return new RpcResult(os.toByteArray());
} catch (IOException e) {
throw new RpcException("Serialize result failed.", e);
Expand All @@ -110,4 +112,7 @@ public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
return invoker.invoke(inv);
}

private String getSerializationExtension(String generic) {
return "nativejava";
}
}

0 comments on commit 1f7a4cd

Please sign in to comment.