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.util.List;
020
021import org.apache.wicket.Component;
022import org.apache.wicket.Page;
023
024/**
025 * A checker that doesn't allow the serialization of {@link Component component}s
026 * which are not a {@link Page page} and have no parent component.
027 *
028 * <p>
029 *     Note: The Wizard component from wicket-extensions use such kind of orphaned components
030 *     and will fail this check unless the step classes are specified as exclusions.
031 * </p>
032 */
033public class OrphanComponentChecker extends AbstractObjectChecker
034{
035        /**
036         * Constructor.
037         *
038         * Checks all passed objects.
039         */
040        public OrphanComponentChecker()
041        {
042                super();
043        }
044
045        /**
046         * Constructor.
047         *
048         * Checks objects which types are not excluded.
049         *
050         * @param exclusions
051         *      a list of types which should not be checked
052         */
053        public OrphanComponentChecker(List<Class<?>> exclusions)
054        {
055                super(exclusions);
056        }
057
058        @Override
059        public Result doCheck(Object object)
060        {
061                Result result = Result.SUCCESS;
062
063                if (object instanceof Component)
064                {
065                        Component component = (Component) object;
066                        if (component instanceof Page == false && component.getParent() == null)
067                        {
068                                result = new Result(Result.Status.FAILURE, "A component without a parent is detected.");
069                        }
070                }
071
072                return result;
073        }
074}