/**
 * ﻿Copyright (C) 2013
 * by 52 North Initiative for Geospatial Open Source Software GmbH
 *
 * Contact: Andreas Wytzisk
 * 52 North Initiative for Geospatial Open Source Software GmbH
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * This program is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed WITHOUT ANY WARRANTY; even without the implied
 * WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program (see gnu-gpl v2.txt). If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or
 * visit the Free Software Foundation web page, http://www.fsf.org.
 */

package org.n52.sensorweb.wdc;

import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataCollectionTask extends TimerTask {

    private static Logger LOG = LoggerFactory.getLogger(DataCollectionTask.class);

    private final DataCollector dataCollector;

    public DataCollectionTask(final DataCollector checkerP) {
        dataCollector = checkerP;
    }

    @Override
    public void run() {
        LOG.info("*** Run dataCollector {}", dataCollector);

        // TODO here we should handle the file writing and appending on a global level
        
        dataCollector.collectWeatherData();

        LOG.info("*** Ran dataCollector. Next run in '{}' minutes.",dataCollector.getParseIntervalMillis()/60000);
    }

    @Override
    public boolean cancel() {
        LOG.info("Cancelling {}",this);
        return super.cancel();
    }

    @Override
    protected void finalize() throws Throwable {
    	LOG.debug("Finalizing {}",this);
        super.finalize();
    }

	@Override
	public String toString()
	{
		return String.format("DataCollectionTask [dataCollector=%s]", dataCollector);
	}

}

@@ -1018,16 +1018,65 @@
 merTask;
+%0Aimport java.util.concurrent.locks.ReentrantLock;
 %0A%0Aimport
@@ -1310,16 +1310,104 @@
 llector;
+%0A    %0A    private static final ReentrantLock oneCollectorLock = new ReentrantLock(true);
 %0A%0A    pu
@@ -1601,16 +1601,146 @@
 lector);
+%0A%09%09 // used to sync access to lastUsedDateFile and to not have more than one collector at a time.%0A        oneCollectorLock.lock();
 %0A%0A      
@@ -1824,24 +1824,29 @@
 vel%0A        
+try %7B
 %0A        dat
@@ -1834,32 +1834,33 @@
   try %7B%0A        
+%09
 dataCollector.co
@@ -1879,25 +1879,24 @@
 Data();%0A
-%0A
         
 LOG.info
@@ -1879,32 +1879,33 @@
 Data();%0A        
+%09
 LOG.info(%22*** Ra
@@ -1995,16 +1995,91 @@
 60000);%0A
+        %7D %0A        finally %7B%0A        %09oneCollectorLock.unlock();%0A        %7D%0A
     %7D%0A%0A 

add one collector at a time lock