/*******************************************************************************
 * DIANNE  - Framework for distributed artificial neural networks
 * Copyright (C) 2015  iMinds - IBCN - UGent
 *
 * This file is part of DIANNE.
 *
 * DIANNE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Tim Verbelen, Steven Bohez
 *******************************************************************************/
package be.iminds.iot.dianne.rl.exp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(immediate=true)
public class ExperiencePoolLoader {

	private ConfigurationAdmin configAdmin;
	
	private String dir = "exp";
	
	@Activate
	public void activate(BundleContext context) throws Exception {
		String expDir = context.getProperty("be.iminds.iot.dianne.pool");
		if(expDir!=null){
			dir = expDir;
		}
		
		String factoryPid = FileExperiencePool.class.getName();
		
		File d = new File(dir);
		if(d.isDirectory()){
			for(String s : d.list()){
				String poolDir = d.getAbsolutePath()+File.separator+s;
				String propertiesFileName = poolDir+File.separator+"config.properties";
			
				File propertiesFile = new File(propertiesFileName);
				if(!propertiesFile.exists()){
					System.out.println("No config.properties for experience pool "+s);
					continue;
				}
				
				Dictionary<String, Object> properties = new Hashtable<>();

				BufferedReader input = null;
				try {
					input = new BufferedReader(new InputStreamReader(new FileInputStream(propertiesFile)));
					String line = null;
					while((line = input.readLine()) != null){
						if(line.startsWith("#"))
							continue;
						
						if(!line.contains("="))
							continue;
						
						String[] pair = line.split("=");
						
						String key = pair[0].trim();
						properties.put(pair[0].trim(), pair[1].trim());
					}
					
					properties.put("dir", poolDir);

					String instancePid = configAdmin.createFactoryConfiguration(factoryPid, null).getPid();
					configAdmin.getConfiguration(instancePid).update(properties);
				
				} catch(IOException e){
					System.out.println("Error initializing experience pool "+s);
					e.printStackTrace();
				} finally {
					input.close();
				}
			}
		}
	}
	
	@Reference
	void setConfigurationAdmin(ConfigurationAdmin ca){
		this.configAdmin = ca;
	}
}

@@ -1852,66 +1852,187 @@
 %09%0A%09%09
-String factoryPid = FileExperiencePool.class.getName();%0A%09%09
+Thread t = new Thread(()-%3EcheckDir());%0A%09%09t.start();%0A%09%09%0A%09%7D%0A%09%0A%09@Reference%0A%09void setConfigurationAdmin(ConfigurationAdmin ca)%7B%0A%09%09this.configAdmin = ca;%0A%09%7D%0A%09%0A%09private void checkDir()%7B
 %0A%09%09F
@@ -2996,24 +2996,85 @@
  poolDir);%0A%0A
+%09%09%09%09%09String factoryPid = FileExperiencePool.class.getName();%0A
 %09%09%09%09%09String 
@@ -3358,24 +3358,36 @@
 %7D finally %7B%0A
+%09%09%09%09%09try %7B%0A%09
 %09%09%09%09%09input.c
@@ -3402,112 +3402,50 @@
 %09%09%09%09
-%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Reference%0A%09void setConfigurationAdmin(ConfigurationAdmin ca)%7B%0A%09%09this.configAdmin = ca;
+%09%7D catch (IOException e) %7B%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A

load experience pools in separate thread