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.pageStore;
018
019import org.apache.wicket.page.IManageablePage;
020import org.apache.wicket.serialize.ISerializer;
021import org.apache.wicket.util.lang.Args;
022import org.apache.wicket.util.lang.Classes;
023
024/**
025 * A store that serializes all pages before delegating and vice versa.
026 */
027public class SerializingPageStore extends DelegatingPageStore
028{
029
030        private final ISerializer serializer;
031
032        /**
033         * @param delegate
034         *            store to delegate to
035         * @param serializer
036         *            serializer to use if session gets persisted
037         */
038        public SerializingPageStore(IPageStore delegate, ISerializer serializer)
039        {
040                super(delegate);
041
042                this.serializer = Args.notNull(serializer, "serializer");
043        }
044
045        /**
046         * Versioning is supported, since pages are always serialized.
047         */
048        @Override
049        public boolean supportsVersioning()
050        {
051                return true;
052        }
053
054        /**
055         * Supports asynchronous add if the delegate supports it.
056         */
057        @Override
058        public boolean canBeAsynchronous(IPageContext context)
059        {
060                return getDelegate().canBeAsynchronous(context);
061        }
062        
063        @Override
064        public IManageablePage getPage(IPageContext context, int id)
065        {
066                IManageablePage page = getDelegate().getPage(context, id);
067
068                if (page instanceof SerializedPage)
069                {
070                        page = (IManageablePage)serializer.deserialize(((SerializedPage)page).getData());
071                }
072                return page;
073        }
074
075        @Override
076        public void addPage(IPageContext context, IManageablePage page)
077        {
078                if (page instanceof SerializedPage == false)
079                {
080                        page = new SerializedPage(page.getPageId(), Classes.name(page.getClass()), serializer.serialize(page));
081                }
082                getDelegate().addPage(context, page);
083        }
084}