001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business.runnable;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.RollerException;
028: import org.apache.roller.business.HitCountQueue;
029: import org.apache.roller.business.RollerFactory;
030: import org.apache.roller.business.UserManager;
031: import org.apache.roller.business.WeblogManager;
032: import org.apache.roller.pojos.WebsiteData;
033:
034: /**
035: * A job which gathers the currently queued hits from the HitCountQueue and
036: * stores them in the database.
037: */
038: public class HitCountProcessingJob implements Job {
039:
040: private static Log log = LogFactory
041: .getLog(HitCountProcessingJob.class);
042:
043: public HitCountProcessingJob() {
044: }
045:
046: /**
047: * Execute the job.
048: *
049: * We want to extract the currently queued hits from the HitCounter and
050: * then propogate them to the db for persistent storage.
051: */
052: public void execute() {
053:
054: UserManager umgr = null;
055: WeblogManager wmgr = null;
056: try {
057: umgr = RollerFactory.getRoller().getUserManager();
058: wmgr = RollerFactory.getRoller().getWeblogManager();
059: } catch (RollerException ex) {
060: // if we can't even get the manager instances then bail now
061: log.error("Error getting managers", ex);
062: return;
063: }
064:
065: HitCountQueue hitCounter = HitCountQueue.getInstance();
066:
067: // first get the current set of hits
068: List currentHits = hitCounter.getHits();
069:
070: // now reset the queued hits
071: hitCounter.resetHits();
072:
073: // tally the counts, grouped by weblog handle
074: Map hitsTally = new HashMap();
075: String weblogHandle = null;
076: for (int i = 0; i < currentHits.size(); i++) {
077: weblogHandle = (String) currentHits.get(i);
078:
079: Long count = (Long) hitsTally.get(weblogHandle);
080: if (count == null) {
081: count = new Long(1);
082: } else {
083: count = new Long(count.longValue() + 1);
084: }
085: hitsTally.put(weblogHandle, count);
086: }
087:
088: // iterate over the tallied hits and store them in the db
089: try {
090: long startTime = System.currentTimeMillis();
091:
092: WebsiteData weblog = null;
093: String key = null;
094: Iterator it = hitsTally.keySet().iterator();
095: while (it.hasNext()) {
096: key = (String) it.next();
097:
098: try {
099: weblog = umgr.getWebsiteByHandle(key);
100: wmgr.incrementHitCount(weblog, ((Long) hitsTally
101: .get(key)).intValue());
102: } catch (RollerException ex) {
103: log.error(ex);
104: }
105: }
106:
107: // flush the results to the db
108: RollerFactory.getRoller().flush();
109:
110: long endTime = System.currentTimeMillis();
111:
112: log.debug("Completed: " + (endTime - startTime) / 1000
113: + " secs");
114:
115: } catch (RollerException ex) {
116: log.error("Error persisting updated hit counts", ex);
117: } finally {
118: // release session
119: RollerFactory.getRoller().release();
120: }
121: }
122:
123: public void input(Map input) {
124: // no-op
125: }
126:
127: public Map output() {
128: return null;
129: }
130:
131: }
|