@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		log.i("Initializing Context");
		
		// Load properties
		try {
			// Load the file in the usual path of the file in the project (case of the manager)
			File serverProperties = new File(PROPERTIES_FILE_PATH);
			InputStream is = new FileInputStream(serverProperties);
			
			Properties properties = new Properties();
			properties.load(is);
			
			role = properties.getProperty("role");
			name = properties.getProperty("name");
			securityGroup = properties.getProperty("securityGroup");
			keypair = properties.getProperty("keypair");
			repoURL = properties.getProperty("repoURL");
			ami = properties.getProperty("ami");
			instanceType = properties.getProperty("instanceType");
			localIp = properties.getProperty("localIp");
			publicIp = properties.getProperty("publicIp");
			instanceId = properties.getProperty("instanceId");
			
			if ( role.equals("manager") ) {
				
				log.i("Performing Manager setup");
				
				// Reset the database
				//new WorkerDAO().deleteAll();
				//new ActivityDAO().deleteAll();
				//new PolicyDAO().deleteAll();
				
				// If this is the manager, we put ourself in the workers list
				Worker worker = new Worker();
				worker.setLocalIp(localIp);
				worker.setPublicIp(publicIp);
				worker.setInstanceId(instanceId);
				worker.setManager(true);
				worker.setStatus("ready");
				
				// Verify if there is a manager already in the database
				WorkerDAO wdao = new WorkerDAO();
				Worker [] workers = wdao.list();
				boolean thisIsARedeploy = false;
				for ( Worker w : workers )
					if ( w.isManager() ) {
						thisIsARedeploy = true;
						workerId = w.getId();
						break;
					}
				
				if ( !thisIsARedeploy ) {

					workerId = wdao.insert(worker);
					
					Policy defaultPolicy = new Policy("default");
					defaultPolicy.setRule("fixedWorkers", "1");
					defaultPolicy.setActive(true);
					
					PolicyDAO pdao = new PolicyDAO();
					pdao.insert(defaultPolicy);
					pdao.setActive(defaultPolicy);
					
					// delete contents that might have been left there (this is not a redeploy preserrving database)
					if ( new File("/home/pod/app").exists() )
						deleteContents(new File("/home/pod/app"));
					
				}
				// We should as the workers what is their state, but for now we're going to set them to ready
				else {
					for ( Worker w : workers ) {
						if ( !w.getStatus().equals("ready") ) {
							w.setStatus("ready");
							wdao.update(w);
						}
					}
				}
				
				managerLocalIp = localIp;
				
				// Set up the timer for periodic tasks
				Timer time = new Timer();
				SystemSchedulerTask sct = new SystemSchedulerTask();
				time.schedule(sct, PERIODIC_CHECKS_INTERVAL, PERIODIC_CHECKS_INTERVAL);

				
			}
			// If this is a worker, read the property from the properties file
			else {
				
				log.i("Performing Worker setup");
				
				managerLocalIp = properties.getProperty("managerLocalIp");
				workerId = Integer.parseInt( properties.getProperty("workerId") );
			
				// We must contact the master here, so they know we've launched
				HttpSender sender = new HttpSender();
				
				// The workerId is automatically added to the message
				JsonObject message = new JsonObject();
				message.add("action", Action.WORKER_DEPLOYED.getId() );
				message.add("localIp", localIp);
				message.add("publicIp", publicIp);
				message.add("instanceId", instanceId);
				

				sender.setMessage(message);
				sender.setDestinationIP( ServerProperties.getManagerLocalIp() );
				sender.setDestinationRole("manager");
				String response = "";
				try {
					response = sender.send();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				JsonObject jsonResponse = JsonObject.readFrom(response);
				
				// Set up the timer for periodic tasks
				Timer time = new Timer();
				WorkerUpdaterTask sct = new WorkerUpdaterTask();
				time.schedule(sct, WORKER_PERIODIC_UPDATES_INTERVAL, WORKER_PERIODIC_UPDATES_INTERVAL);
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		log.i("Context initialized. Name="+name +" Role="+role);
		
	}