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.devutils.pagestore.browser;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.wicket.model.IModel;
024import org.apache.wicket.model.LoadableDetachableModel;
025import org.apache.wicket.pageStore.DefaultPageContext;
026import org.apache.wicket.pageStore.DiskPageStore;
027import org.apache.wicket.pageStore.IPageContext;
028import org.apache.wicket.pageStore.IPersistentPageStore;
029
030/**
031 * A model that collects the keys from the {@link DiskPageStore} folder
032 */
033public class SessionIdentifiersModel extends LoadableDetachableModel<List<String>>
034{
035        private final IModel<IPersistentPageStore> store;
036        
037        public SessionIdentifiersModel(IModel<IPersistentPageStore> store)
038        {
039                this.store = store;
040        }
041        
042        @Override
043        protected List<String> load()
044        {
045                IPersistentPageStore store = this.store.getObject();
046                if (store == null)
047                {
048                        return Collections.emptyList();
049                }
050
051                ArrayList<String> identifiers = new ArrayList<>(store.getSessionIdentifiers());
052
053                IPageContext context = new DefaultPageContext();
054                String current = store.getSessionIdentifier(context);
055                if (identifiers.contains(current) == false)
056                {
057                        // identifiers of the store seem no to match their sessions ids,
058                        // thus add the default identifier so the select works properly  
059                        identifiers.add(current);
060                }
061
062                return identifiers;
063        }
064        
065        @Override
066        public void detach()
067        {
068                super.detach();
069                
070                store.detach();
071        }
072}