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.debugbar;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.Page;
021import org.apache.wicket.devutils.inspector.LiveSessionsPage;
022import org.apache.wicket.devutils.inspector.SessionSizeModel;
023import org.apache.wicket.model.IModel;
024import org.apache.wicket.request.resource.PackageResourceReference;
025import org.apache.wicket.request.resource.ResourceReference;
026import org.apache.wicket.util.lang.Bytes;
027
028/**
029 * A panel for the debug bar that shows the session size and links to the page that shows more
030 * information about sessions.
031 * 
032 * @author Jeremy Thomerson
033 */
034public class SessionSizeDebugPanel extends StandardDebugPanel
035{
036        private static final long serialVersionUID = 1L;
037
038        /** */
039        public static final IDebugBarContributor DEBUG_BAR_CONTRIB = new IDebugBarContributor()
040        {
041                private static final long serialVersionUID = 1L;
042
043                @Override
044                public Component createComponent(final String id, final DebugBar debugBar)
045                {
046                        return new SessionSizeDebugPanel(id);
047                }
048
049        };
050
051        /**
052         * Construct.
053         * 
054         * @param id
055         *      the component id
056         */
057        public SessionSizeDebugPanel(final String id)
058        {
059                super(id);
060        }
061
062        @Override
063        protected Class<? extends Page> getLinkPageClass()
064        {
065                return LiveSessionsPage.class;
066        }
067
068        @Override
069        protected ResourceReference getImageResourceReference()
070        {
071                // TODO: need better image for this:
072                return new PackageResourceReference(SessionSizeDebugPanel.class, "harddrive.png");
073        }
074
075        @Override
076        protected IModel<String> getDataModel()
077        {
078                return new IModel<String>()
079                {
080                        private static final long serialVersionUID = 1L;
081
082                        private final IModel<Bytes> size = new SessionSizeModel();
083
084                        @Override
085                        public String getObject()
086                        {
087                                Bytes sessionSizeInBytes = size.getObject();
088                                String sessionSizeAsString = sessionSizeInBytes != null
089                                        ? sessionSizeInBytes.toString() : "unknown";
090
091                                return "Session: " + sessionSizeAsString;
092                        }
093
094                        @Override
095                        public void detach()
096                        {
097                                size.detach();
098                        }
099                };
100        }
101
102}