Author: aadamchik Date: Sun Jan 14 08:48:53 2007 New Revision: 496095 URL: http://svn.apache.org/viewvc?view=rev&rev=496095 Log: CAY-718: ROP: handle non-serializable server side exceptions
Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/BaseRemoteServiceTest.java incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/MockUnserializableException.java Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/remote/service/BaseRemoteService.java Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/remote/service/BaseRemoteService.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/remote/service/BaseRemoteService.java?view=diff&rev=496095&r1=496094&r2=496095 ============================================================================== --- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/remote/service/BaseRemoteService.java (original) +++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/remote/service/BaseRemoteService.java Sun Jan 14 08:48:53 2007 @@ -134,6 +134,10 @@ public Object processMessage(ClientMessage message) throws Throwable { + if (message == null) { + throw new IllegalArgumentException("Null client message."); + } + ServerSession handler = getServerSession(); if (handler == null) { @@ -149,7 +153,20 @@ catch (Throwable th) { th = Util.unwindException(th); logObj.info("error processing message", th); - throw th; + + // throw exception that will likely be propagated to the client... + // recast the exception to a guaranteed serializable form + + StringBuffer buffer = new StringBuffer(); + buffer.append("Exception processing message ").append( + message.getClass().getName()); + + String exceptionText = th.getLocalizedMessage(); + if (exceptionText != null) { + buffer.append(". Root cause: ").append(exceptionText); + } + + throw new CayenneRuntimeException(buffer.toString()); } } Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/BaseRemoteServiceTest.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/BaseRemoteServiceTest.java?view=auto&rev=496095 ============================================================================== --- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/BaseRemoteServiceTest.java (added) +++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/BaseRemoteServiceTest.java Sun Jan 14 08:48:53 2007 @@ -0,0 +1,78 @@ +/***************************************************************** + * 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.cayenne.remote.service; + +import junit.framework.TestCase; + +import org.apache.cayenne.CayenneRuntimeException; +import org.apache.cayenne.query.Query; +import org.apache.cayenne.remote.QueryMessage; +import org.apache.cayenne.remote.RemoteSession; +import org.apache.cayenne.util.Util; + +public class BaseRemoteServiceTest extends TestCase { + + public void testProcessMessageExceptionSerializability() throws Throwable { + + BaseRemoteService handler = new BaseRemoteService() { + + protected ServerSession createServerSession() { + return new ServerSession(new RemoteSession("a"), null); + } + + protected ServerSession createServerSession(String name) { + return createServerSession(); + } + + protected ServerSession getServerSession() { + return createServerSession(); + } + }; + + try { + handler.processMessage(new QueryMessage(null) { + + public Query getQuery() { + // serializable exception thrown + throw new CayenneRuntimeException(); + } + }); + + fail("Expected to throw"); + } + catch (Exception ex) { + Util.cloneViaSerialization(ex); + } + + try { + handler.processMessage(new QueryMessage(null) { + + public Query getQuery() { + // non-serializable exception thrown + throw new MockUnserializableException(); + } + }); + + fail("Expected to throw"); + } + catch (Exception ex) { + Util.cloneViaSerialization(ex); + } + } +} Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/MockUnserializableException.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/MockUnserializableException.java?view=auto&rev=496095 ============================================================================== --- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/MockUnserializableException.java (added) +++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/remote/service/MockUnserializableException.java Sun Jan 14 08:48:53 2007 @@ -0,0 +1,24 @@ +/***************************************************************** + * 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.cayenne.remote.service; + +class MockUnserializableException extends RuntimeException { + + protected Object notSerializableField = new Object(); +}