001// Copyright 2009, 2011, 2013 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.corelib.pages;
016
017import org.apache.tapestry5.SymbolConstants;
018import org.apache.tapestry5.annotations.Property;
019import org.apache.tapestry5.annotations.UnknownActivationContextCheck;
020import org.apache.tapestry5.annotations.WhitelistAccessOnly;
021import org.apache.tapestry5.ioc.annotations.Inject;
022import org.apache.tapestry5.ioc.annotations.Symbol;
023import org.hibernate.Session;
024import org.hibernate.metadata.ClassMetadata;
025import org.hibernate.stat.*;
026
027import java.util.Collection;
028
029/**
030 * Page used to see the Hibernate statistics.
031 *
032 * @since 5.1.0.2
033 */
034@UnknownActivationContextCheck(false)
035@WhitelistAccessOnly
036public class HibernateStatistics
037{
038    @Inject
039    private Session session;
040
041    @Property
042    @Inject
043    @Symbol(SymbolConstants.PRODUCTION_MODE)
044    private boolean productionMode;
045
046    @Property
047    private String currentEntityName;
048
049    @Property
050    private String currentCollectionRoleName;
051
052    @Property
053    private String currentQuery;
054
055    @Property
056    private String currentSecondLevelCacheRegionName;
057
058    public Statistics getStatistics()
059    {
060        return session.getSessionFactory().getStatistics();
061    }
062
063
064    @SuppressWarnings("unchecked")
065    public Collection<ClassMetadata> getAllClassMetadata()
066    {
067        return session.getSessionFactory().getAllClassMetadata().values();
068    }
069
070    public EntityStatistics getEntityStatistics()
071    {
072        return getStatistics().getEntityStatistics(currentEntityName);
073    }
074
075    public CollectionStatistics getCollectionStatistics()
076    {
077        return getStatistics().getCollectionStatistics(currentCollectionRoleName);
078    }
079
080    public QueryStatistics getQueryStatistics()
081    {
082        return getStatistics().getQueryStatistics(currentQuery);
083    }
084
085    public SecondLevelCacheStatistics getSecondLevelCacheStatistics()
086    {
087        return getStatistics().getSecondLevelCacheStatistics(currentSecondLevelCacheRegionName);
088    }
089}