View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.service.impl;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.annotation.PostConstruct;
29  
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.transaction.PlatformTransactionManager;
32  import org.springframework.transaction.TransactionDefinition;
33  import org.springframework.transaction.TransactionStatus;
34  import org.springframework.transaction.support.DefaultTransactionDefinition;
35  
36  import com.gisgraphy.domain.repository.IStatsUsageDao;
37  import com.gisgraphy.service.IStatsUsageService;
38  import com.gisgraphy.stats.StatsUsage;
39  import com.gisgraphy.stats.StatsUsageType;
40  
41  public class StatsUsageServiceImpl implements IStatsUsageService {
42  
43  
44      private static final Long RESET_COUNTER_VALUE = Long.valueOf(0L);
45  
46      private Map<String, Long> counterMap = new HashMap<String, Long>();
47      
48      @Autowired
49      IStatsUsageDao statsUsageDao;
50  
51      @Autowired
52      private PlatformTransactionManager transactionManager;
53      
54      public static boolean disabled = false;
55  
56  
57      @PostConstruct
58      protected void init() {
59  	for (StatsUsageType statsUsageType : StatsUsageType.values()) {
60  	    initCounter(statsUsageType);
61  	}
62  
63      }
64  
65      private StatsUsage initCounter(StatsUsageType statsUsageType) {
66  	StatsUsage statsUsage = statsUsageDao
67  	    .getByUsageType(statsUsageType);
68  	if (statsUsage == null) {
69  	statsUsage = new StatsUsage(statsUsageType);
70  	statsUsageDao.save(statsUsage);
71  	}
72  	this.counterMap.put(statsUsage.getStatsUsageType().toString(),
73  	    statsUsage.getUsage());
74  	return statsUsage;
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see com.gisgraphy.service.IStatsUsageService#GetNumberOfCounter()
81       */
82      public int getNumberOfCounter() {
83      	return counterMap.size();
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see com.gisgraphy.service.IStatsUsageService#GetUsage(com.gisgraphy.stats.StatsUsageType)
90       */
91      public Long getUsage(StatsUsageType statsUsageType) {
92      	return counterMap.get(statsUsageType.toString());
93      }
94  
95      /*
96       * (non-Javadoc)
97       * 
98       * @see com.gisgraphy.service.IStatsUsageService#increaseUsage(com.gisgraphy.stats.StatsUsageType)
99       */
100     public void increaseUsage(StatsUsageType statsUsageType) {
101     	if (!disabled){
102     		long newValue = counterMap.get(statsUsageType.toString()) + 1;
103     		counterMap.put(statsUsageType.toString(), newValue);
104     		if (newValue % IStatsUsageService.FLUSH_THRESHOLD == 0) {
105     			flush(statsUsageType);
106     		}
107     	}
108     }
109 
110     public void resetUsage(StatsUsageType statsUsageType) {
111     	if (!disabled){
112     		counterMap.put(statsUsageType.toString(), RESET_COUNTER_VALUE);
113     		flush(statsUsageType);
114     	}
115     }
116 
117     public void flush(StatsUsageType statsUsageType) {
118     	if (!disabled){
119     		statsUsageDao.flushAndClear();
120     		StatsUsage statsUsage = statsUsageDao.getByUsageType(statsUsageType);
121     		if (statsUsage==null){
122     			statsUsage= initCounter(statsUsageType);
123     		}
124     		TransactionStatus txStatus = null;
125     		DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
126     		txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
127     		txDefinition.setReadOnly(false);
128 
129     		txStatus = transactionManager.getTransaction(txDefinition);
130     		statsUsage.setUsage(counterMap.get(statsUsageType.toString()));
131     		statsUsageDao.save(statsUsage);
132     		transactionManager.commit(txStatus);
133     	}
134     }
135 
136 
137 }