    001    // Copyright 2009, 2011 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
    015    package org.apache.tapestry5.hibernate.pages;
    016
    017    import java.util.Collection;
    018
    019    import org.apache.tapestry5.SymbolConstants;
    020    import org.apache.tapestry5.annotations.ContentType;
    021    import org.apache.tapestry5.annotations.Property;
    022    import org.apache.tapestry5.annotations.WhitelistAccessOnly;
    023    import org.apache.tapestry5.ioc.annotations.Inject;
    024    import org.apache.tapestry5.ioc.annotations.Symbol;
    025    import org.hibernate.Session;
    026    import org.hibernate.metadata.ClassMetadata;
    027    import org.hibernate.stat.CollectionStatistics;
    028    import org.hibernate.stat.EntityStatistics;
    029    import org.hibernate.stat.QueryStatistics;
    030    import org.hibernate.stat.SecondLevelCacheStatistics;
    031
    032    /**
    033     * Page used to see the Hibernate statistics.
    034     *
    035     * @since 5.1.0.2
    036     *
    037     */
    038    @ContentType("text/html")
    039    @WhitelistAccessOnly
    040    public class Statistics {
    041       @Inject
    042       private Session session;
    043
    044       @Property
    045       @Inject
    046       @Symbol(SymbolConstants.PRODUCTION_MODE)
    047       private boolean productionMode;
    048
    049       @Property
    050       private String currentEntityName;
    051
    052       @Property
    053       private String currentCollectionRoleName;
    054
    055       @Property
    056       private String currentQuery;
    057
    058       @Property
    059       private String currentSecondLevelCacheRegionName;
    060
    061       @Property
    062       private org.hibernate.stat.Statistics statistics;
    063
    064       void onActivate() {
    065         this.statistics = this.session.getSessionFactory().getStatistics();
    066       }
    067
    068       @SuppressWarnings("unchecked")
    069       public Collection<ClassMetadata> getAllClassMetadata() {
    070         return this.session.getSessionFactory().getAllClassMetadata().values
    071    ();
    072       }
    073
    074       public EntityStatistics getEntityStatistics() {
    075         return this.statistics.getEntityStatistics(this.currentEntityName);
    076       }
    077
    078       public CollectionStatistics getCollectionStatistics() {
    079         return this.statistics
    080               .getCollectionStatistics(this.currentCollectionRoleName);
    081       }
    082
    083       public QueryStatistics getQueryStatistics() {
    084         return this.statistics.getQueryStatistics(this.currentQuery);
    085       }
    086
    087       public SecondLevelCacheStatistics getSecondLevelCacheStatistics() {
    088         return this.statistics
    089               .getSecondLevelCacheStatistics
    090    (this.currentSecondLevelCacheRegionName);
    091       }
    092    }