geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject incubator-geode git commit: Fix up serialization of exceptions
Date Wed, 02 Mar 2016 00:08:46 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-949-2 6ac8fb492 -> f9b621dc0


Fix up serialization of exceptions


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/f9b621dc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/f9b621dc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/f9b621dc

Branch: refs/heads/feature/GEODE-949-2
Commit: f9b621dc06d9e2233e4a59bc488829373a632dac
Parents: 6ac8fb4
Author: Kirk Lund <klund@pdx2-office-dhcp32.eng.vmware.com>
Authored: Tue Mar 1 16:06:43 2016 -0800
Committer: Kirk Lund <klund@pdx2-office-dhcp32.eng.vmware.com>
Committed: Tue Mar 1 16:06:43 2016 -0800

----------------------------------------------------------------------
 .../security/GemFireSecurityException.java      | 33 +++++++++++++++++++-
 .../security/NotAuthorizedException.java        | 27 +++++++++++++++-
 .../security/NotAuthorizedExceptionTest.java    | 21 +++++++++++--
 3 files changed, 77 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f9b621dc/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
b/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
index 1f97420..441f66d 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/security/GemFireSecurityException.java
@@ -19,6 +19,9 @@ package com.gemstone.gemfire.security;
 
 import com.gemstone.gemfire.GemFireException;
 
+import javax.naming.NamingException;
+import java.io.*;
+
 /**
  * The base class for all com.gemstone.gemfire.security package related
  * exceptions.
@@ -27,7 +30,7 @@ import com.gemstone.gemfire.GemFireException;
  * @since 5.5
  */
 public class GemFireSecurityException extends GemFireException {
-private static final long serialVersionUID = 3814254578203076926L;
+  private static final long serialVersionUID = 3814254578203076926L;
 
   /**
    * Constructs instance of <code>SecurityException</code> with error message.
@@ -52,4 +55,32 @@ private static final long serialVersionUID = 3814254578203076926L;
     super(message, cause);
   }
 
+  protected boolean isSerializable(final Object object) {
+    if (object == null) {
+      return true;
+    }
+    return object.getClass().isInstance(Serializable.class);
+  }
+
+  protected Object getResolvedObj() {
+    if (getCause() != null && getCause().getClass().isInstance(NamingException.class))
{
+      return ((javax.naming.NamingException) getCause()).getResolvedObj();
+    }
+    return null;
+  }
+
+  private void writeObject(final ObjectOutputStream stream) throws IOException {
+    final Object resolvedObj = getResolvedObj();
+    if (isSerializable(resolvedObj)) {
+      stream.defaultWriteObject();
+    } else {
+      final NamingException namingException = (NamingException) getCause();
+      namingException.setResolvedObj(null);
+      try {
+        stream.defaultWriteObject();
+      } finally {
+        namingException.setResolvedObj(resolvedObj);
+      }
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f9b621dc/geode-core/src/main/java/com/gemstone/gemfire/security/NotAuthorizedException.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/security/NotAuthorizedException.java
b/geode-core/src/main/java/com/gemstone/gemfire/security/NotAuthorizedException.java
index 84869bc..4108742 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/security/NotAuthorizedException.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/security/NotAuthorizedException.java
@@ -17,6 +17,9 @@
 
 package com.gemstone.gemfire.security;
 
+import javax.naming.NamingException;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
 import java.security.Principal;
 
 /**
@@ -28,7 +31,7 @@ import java.security.Principal;
 public class NotAuthorizedException extends GemFireSecurityException {
   private static final long serialVersionUID = 419215768216387745L;
 
-  private transient Principal principal = null;
+  private Principal principal = null;
 
   /**
    * Constructs instance of <code>NotAuthorizedException</code> with error
@@ -63,4 +66,26 @@ public class NotAuthorizedException extends GemFireSecurityException {
     super(message, cause);
   }
 
+  private void writeObject(final ObjectOutputStream stream) throws IOException {
+    Principal myPrincipal = this.principal;
+    if (!isSerializable(myPrincipal)) {
+      this.principal = null;
+    }
+
+    Object resolvedObj = getResolvedObj();
+    NamingException namingException = null;
+    if (!isSerializable(resolvedObj)) {
+      namingException = (NamingException) getCause();
+      namingException.setResolvedObj(null);
+    }
+
+    try {
+      stream.defaultWriteObject();
+    } finally {
+      this.principal = myPrincipal;
+      if (namingException != null) {
+        namingException.setResolvedObj(resolvedObj);
+      }
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f9b621dc/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
b/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
index 27dab70..9391102 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/security/NotAuthorizedExceptionTest.java
@@ -1,3 +1,19 @@
+/*
+ * 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 com.gemstone.gemfire.security;
 
 import com.gemstone.gemfire.test.junit.categories.UnitTest;
@@ -45,7 +61,7 @@ public class NotAuthorizedExceptionTest {
   }
 
   @Test
-  public void canBeSerializedWithPrincipal() throws Exception {
+  public void canBeSerializedWithNonSerializablePrincipal() throws Exception {
     String message = "my message";
     Principal mockPrincipal = mock(Principal.class);
     NotAuthorizedException instance = new NotAuthorizedException(message, mockPrincipal);
@@ -53,6 +69,7 @@ public class NotAuthorizedExceptionTest {
     NotAuthorizedException cloned = (NotAuthorizedException) SerializationUtils.clone(instance);
 
     assertThat(cloned).hasMessage(message);
-    assertThat(cloned.getPrincipal()).isEqualTo(mockPrincipal);
+    //assertThat(cloned.getPrincipal()).isEqualTo(mockPrincipal);
+    assertThat(cloned.getPrincipal()).isNull();
   }
 }


Mime
View raw message