All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.druid.support.http.stat.WebAppStatManager Maven / Gradle / Ivy

/*
 * Copyright 1999-2011 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.druid.support.http.stat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.util.StringUtils;

public class WebAppStatManager {

    public final static String             SYS_PROP_INSTANCES = "druid.web.webAppStat";

    private final static WebAppStatManager instance           = new WebAppStatManager();

    private Set                    webAppStatSet      = null;

    public static WebAppStatManager getInstance() {
        return instance;
    }
    
    public synchronized WebAppStat getWebAppStat(String contextPath) {
        Set stats = getWebAppStatSet();
        for (Object item : stats) {
            if (item instanceof WebAppStat) {
                WebAppStat stat = (WebAppStat) item;
                if (StringUtils.equals(stat.getContextPath(), contextPath)) {
                    return stat;
                }
            }
        }
        
        WebAppStat stat = new WebAppStat(contextPath);
        this.addWebAppStatSet(stat);
        return stat;
    }

    public Set getWebAppStatSet() {
        if (webAppStatSet == null) {
            if (DruidDataSourceStatManager.isRegisterToSystemProperty()) {
                webAppStatSet = getWebAppStatSet0();
            } else {
                webAppStatSet = new CopyOnWriteArraySet();                
            }
        }

        return webAppStatSet;
    }

    public List> getWebAppStatData() {
        Set stats = getWebAppStatSet();

        List> statDataList = new ArrayList>(stats.size());

        for (Object stat : stats) {
            Map statData = WebAppStatUtils.getStatData(stat);
            statDataList.add(statData);
        }

        return statDataList;
    }

    public List> getURIStatData() {
        Set stats = getWebAppStatSet();

        List> allAppUriStatDataList = new ArrayList>();

        for (Object stat : stats) {
            List> uriStatDataList = WebAppStatUtils.getURIStatDataList(stat);
            allAppUriStatDataList.addAll(uriStatDataList);
        }

        return allAppUriStatDataList;
    }

    public List> getSessionStatData() {
        Set stats = getWebAppStatSet();

        List> allAppUriStatDataList = new ArrayList>();

        for (Object stat : stats) {
            List> uriStatDataList = WebAppStatUtils.getSessionStatDataList(stat);
            allAppUriStatDataList.addAll(uriStatDataList);
        }

        return allAppUriStatDataList;
    }
    
    public Map getSessionStat(String sessionId) {
        Set stats = getWebAppStatSet();

        for (Object stat : stats) {
            Map statData = WebAppStatUtils.getSessionStatData(stat, sessionId);
            if (statData != null) {
                return statData;
            }
        }
        
        return null;
    }
    
    public Map getURIStatData(String uri) {
        Set stats = getWebAppStatSet();
        
        for (Object stat : stats) {
            Map statData = WebAppStatUtils.getURIStatData(stat, uri);
            if (statData != null) {
                return statData;
            }
        }
        
        return null;
    }

    public void addWebAppStatSet(Object webAppStat) {
        getWebAppStatSet().add(webAppStat);
    }

    public boolean remove(Object webAppStat) {
        return getWebAppStatSet().remove(webAppStat);
    }

    @SuppressWarnings("unchecked")
    static Set getWebAppStatSet0() {
        Properties properties = System.getProperties();
        Set webAppStats = (Set) properties.get(SYS_PROP_INSTANCES);

        if (webAppStats == null) {
            synchronized (properties) {
                webAppStats = (Set) properties.get(SYS_PROP_INSTANCES);

                if (webAppStats == null) {
                    webAppStats = new CopyOnWriteArraySet();
                    properties.put(SYS_PROP_INSTANCES, webAppStats);
                }
            }
        }

        return webAppStats;
    }

    public void resetStat() {
        Set stats = getWebAppStatSet();

        for (Object stat : stats) {
            WebAppStatUtils.reset(stat);
        }
    }

}