Author: cutting
Date: Sat Oct  4 00:01:02 2014
New Revision: 1629342

URL: http://svn.apache.org/r1629342
Log:
AVRO-1591. Java: Fix specific RPC so that proxies implement hashCode(), 
equals() and toString(). Contributed by Mark Spadoni.

Added:
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/
    
avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/TestSpecificRequestor.java
   (with props)
Modified:
    avro/trunk/CHANGES.txt
    
avro/trunk/lang/java/ipc/src/main/java/org/apache/avro/ipc/specific/SpecificRequestor.java

Modified: avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1629342&r1=1629341&r2=1629342&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Sat Oct  4 00:01:02 2014
@@ -46,6 +46,9 @@ Trunk (not yet released)
     AVRO-1589. Java: Fix ReflectData.AllowNulls to not create unions
     for primitive types. (Ryan Blue via cutting)
 
+    AVRO-1591. Java: Fix specific RPC so that proxies implement hashCode(),
+    equals() and toString(). (Mark Spadoni via cutting)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

Modified: 
avro/trunk/lang/java/ipc/src/main/java/org/apache/avro/ipc/specific/SpecificRequestor.java
URL: 
http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/main/java/org/apache/avro/ipc/specific/SpecificRequestor.java?rev=1629342&r1=1629341&r2=1629342&view=diff
==============================================================================
--- 
avro/trunk/lang/java/ipc/src/main/java/org/apache/avro/ipc/specific/SpecificRequestor.java
 (original)
+++ 
avro/trunk/lang/java/ipc/src/main/java/org/apache/avro/ipc/specific/SpecificRequestor.java
 Sat Oct  4 00:01:02 2014
@@ -72,36 +72,67 @@ public class SpecificRequestor extends R
   @Override
   public Object invoke(Object proxy, Method method, Object[] args)
     throws Throwable {
-    try {
-      // Check if this is a callback-based RPC:
-      Type[] parameterTypes = method.getParameterTypes();
-      if ((parameterTypes.length > 0) &&
-          (parameterTypes[parameterTypes.length - 1] instanceof Class) &&
-          
Callback.class.isAssignableFrom(((Class<?>)parameterTypes[parameterTypes.length 
- 1]))) {
-        // Extract the Callback from the end of of the argument list
-        Object[] finalArgs = Arrays.copyOf(args, args.length - 1);
-        Callback<?> callback = (Callback<?>)args[args.length - 1];
-        request(method.getName(), finalArgs, callback);
-        return null;
-      }
-      else {
-        return request(method.getName(), args);
+    String name = method.getName();
+    if (name.equals("hashCode")) {
+      return hashCode();
+    }
+    else if (name.equals("equals")) {
+      Object obj = args[0];
+      return (proxy == obj) || (obj != null && 
Proxy.isProxyClass(obj.getClass())
+                                && 
this.equals(Proxy.getInvocationHandler(obj)));
+    }
+    else if (name.equals("toString")) {
+      String protocol = "unknown";
+      String remote = "unknown";
+      Class<?>[] interfaces = proxy.getClass().getInterfaces();
+      if (interfaces.length > 0) {
+        try {
+          protocol = Class.forName(interfaces[0].getName()).getSimpleName();
+        } catch (ClassNotFoundException e) {
+        }
+
+        InvocationHandler handler = Proxy.getInvocationHandler(proxy);
+        if (handler instanceof Requestor) {
+          try {
+            remote = ((Requestor) handler).getTransceiver().getRemoteName();
+          } catch (IOException e) {
+          }
+        }
       }
-    } catch (Exception e) {
-      // Check if this is a declared Exception:
-      for (Class<?> exceptionClass : method.getExceptionTypes()) {
-        if (exceptionClass.isAssignableFrom(e.getClass())) {
+      return "Proxy[" + protocol + "," + remote + "]";
+    }
+    else {
+      try {
+        // Check if this is a callback-based RPC:
+        Type[] parameterTypes = method.getParameterTypes();
+        if ((parameterTypes.length > 0) &&
+            (parameterTypes[parameterTypes.length - 1] instanceof Class) &&
+            
Callback.class.isAssignableFrom(((Class<?>)parameterTypes[parameterTypes.length 
- 1]))) {
+          // Extract the Callback from the end of of the argument list
+          Object[] finalArgs = Arrays.copyOf(args, args.length - 1);
+          Callback<?> callback = (Callback<?>)args[args.length - 1];
+          request(method.getName(), finalArgs, callback);
+          return null;
+        }
+        else {
+          return request(method.getName(), args);
+        }
+      } catch (Exception e) {
+        // Check if this is a declared Exception:
+        for (Class<?> exceptionClass : method.getExceptionTypes()) {
+          if (exceptionClass.isAssignableFrom(e.getClass())) {
+            throw e;
+          }
+        }
+      
+        // Next, check for RuntimeExceptions:
+        if (e instanceof RuntimeException) {
           throw e;
         }
-      }
       
-      // Next, check for RuntimeExceptions:
-      if (e instanceof RuntimeException) {
-        throw e;
+        // Not an expected Exception, so wrap it in AvroRemoteException:
+        throw new AvroRemoteException(e);
       }
-      
-      // Not an expected Exception, so wrap it in AvroRemoteException:
-      throw new AvroRemoteException(e);
     }
   }
 

Added: 
avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/TestSpecificRequestor.java
URL: 
http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/TestSpecificRequestor.java?rev=1629342&view=auto
==============================================================================
--- 
avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/TestSpecificRequestor.java
 (added)
+++ 
avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/TestSpecificRequestor.java
 Sat Oct  4 00:01:02 2014
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.ipc.specific;
+
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.avro.AvroRuntimeException;
+import org.apache.avro.Protocol;
+import org.apache.avro.ipc.HttpTransceiver;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestSpecificRequestor {
+  public interface SampleSpecificProtocol {
+    public static final Protocol PROTOCOL = 
Protocol.parse("{\"protocol\":\"SampleSpecificProtocol\",\"namespace\":\"org.apache.avro.ipc.specific\",\"types\":[],\"messages\":{}}");
+  }
+
+  static Object proxy;
+
+  @BeforeClass
+  public static void initializeProxy() throws Exception {
+    HttpTransceiver client = new HttpTransceiver(new URL("http://localhost";));
+    SpecificRequestor requestor = new 
SpecificRequestor(SampleSpecificProtocol.class, client);
+    proxy = SpecificRequestor.getClient(SampleSpecificProtocol.class, 
requestor);
+  }
+
+  @Test
+  public void testHashCode() throws IOException {
+    try {
+      proxy.hashCode();
+    } catch (AvroRuntimeException e) {
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testEquals() throws IOException {
+    try {
+      proxy.equals(proxy);
+    } catch (AvroRuntimeException e) {
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testToString() throws IOException {
+    try {
+      proxy.toString();
+    } catch (AvroRuntimeException e) {
+      fail(e.getMessage());
+    }
+  }
+
+}

Propchange: 
avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/ipc/specific/TestSpecificRequestor.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to