001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.core.util.objects.checker;
018
019import java.io.NotSerializableException;
020import java.io.Serializable;
021import java.lang.reflect.Proxy;
022
023/**
024 * An implementation of IObjectChecker that checks whether the object
025 * implements {@link java.io.Serializable} interface
026 *
027 * <p>
028 * Note: There is a system property <em>-Dsun.io.serialization.extendedDebugInfo=true</em> that could give
029 * even more debug information.
030 * </p>
031 */
032public class ObjectSerializationChecker extends AbstractObjectChecker
033{
034        /** Exception that should be set as the cause when throwing a new exception. */
035        private final NotSerializableException cause;
036
037        /**
038         * A constructor to use when the checker is used before a previous attempt to
039         * serialize the object.
040         */
041        public ObjectSerializationChecker()
042        {
043                this(null);
044        }
045
046        /**
047         * A constructor to use when there was a previous attempt to serialize the
048         * object and it failed with the {@code cause}.
049         *
050         * @param cause
051         *      the cause of the serialization failure in a previous attempt.
052         */
053        public ObjectSerializationChecker(NotSerializableException cause)
054        {
055                this.cause = cause;
056        }
057
058        /**
059         * Makes the check for all objects. Exclusions by type is not supported.
060         * @param object
061         *      the object to check
062         * @return the {@link org.apache.wicket.core.util.objects.checker.IObjectChecker.Result#SUCCESS} if the object can be serialized.
063         */
064        @Override
065        public Result check(Object object)
066        {
067                Result result = Result.SUCCESS;
068                if (!(object instanceof Serializable) && (!Proxy.isProxyClass(object.getClass())))
069                {
070                        result = new Result(Result.Status.FAILURE, "The object type is not Serializable!", cause);
071                }
072
073                return result;
074        }
075}