Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
This file is part of Niowire. Niowire is free software: you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Niowire 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 Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with Niowire. If not, see <http://www.gnu.org/licenses/>.
 
 package io.niowire.serversource;
 
 import java.io.*;
 import java.util.Map;
This class acts as a server source by watching the configured directory and parsing the files within it as JSON configuration files. On the first run it will return all the files in this directory as being added. And from then on it will return either added modified or deleted for each of the entries.

Author(s):
Trent Houliston
Configparam:
directory java.lang.String - the directory that the server configuration files are located
 
 public class DirectoryServerSource implements NioServerSource
 {
 
 	private static final Logger LOG = LoggerFactory.getLogger(DirectoryServerSource.class);
Our Directory
 
 	private File dir;
A GSON instance to parse the json files. has a type adapter that uses the io.niowire.entities.NioObjectFactory as the concrete type of the NioObjectFactory
 
 	private static Gson gson = new GsonBuilder().serializeNulls().create();
Our current state
 
 	private HashMap<FileLongservers = new HashMap<FileLong>(1);
 	private Map<String, ? extends Objectconfiguration;

 
 	public void configure(Map<String, ? extends Objectconfigurationthrows Exception
 	{
 		this. = configuration;
 
 		//Get our directory from the configuration
 		String directory = (Stringconfiguration.get("directory");
 
 		//Get the path
 		 = new File(directory);
 	}

 
 	public void close() throws IOException
 	{
 		 = null;
 	}

Gets the changes to the servers since this method was last run. It will on first run get all the servers in the directory, and then on subsequent runs it will get the changes (servers that have been added modified or deleted).

Returns:
a map of NioServerConfiguration's to the event that occurred on them (added deleted or modified)
Throws:
java.io.IOException
 
	{
		//Where we put the changes
		//Get our files
		for (File file : .listFiles())
		{
			//Check if we already had this server
			if (!.containsKey(file))
			{
				//Try to parse the message into an object
				InputStreamReader fr = null;
				try
				{
					//Read the file and make a server definition from it using the platforms default encoding
					fr = new InputStreamReader(new FileInputStream(file), Charset.defaultCharset());
					srv.setId(file.getName());
					//Add in that this server has been added
					changes.put(srv.);
					//Add it to the map
					.put(filefile.lastModified());
					//Log that it was added
					.info("Server definition {} was added"file.getName());
				}
				{
					//Put it in the map so we don't keep looking at it
					.put(filefile.lastModified());
					//If there was an exception ignore it
					.info("Server definition {} was invalid, Ignoring"file.getName());
				}
				finally
				{
					fr.close();
				}
			}
			//If we have the server but it's date modified has changed
			else if (.get(file) != file.lastModified())
			{
				//Try to build a server defintion
				InputStreamReader fr = null;
				try
				{
					//Read the file and make a server definition from it
					fr = new InputStreamReader(new FileInputStream(file), Charset.defaultCharset());
					srv.setId(file.getName());
					//Put it in the servers list
					.put(filefile.lastModified());
					//Add that we are updating the server
					changes.put(srv.);
					//Output that we are updating it
					.info("Server definition {} was updated"file.getName());
				}
				{
					//Put it in the servers list so we don't look at it again
					.put(filefile.lastModified());
					//Output that we are still using the old definition
					.info("Server definition {} was invalid, Using Previous Definition"file.getName());
				}
				finally
				{
					fr.close();
				}
			}
		}
		//Get a list of all the servers we currently know about
		HashSet<Filefiles = new HashSet<File>(.size());
		//Remove the ones that still exist
		files.removeAll(Arrays.asList(.listFiles()));
		//What remains now is all the server which have been deleted
		for (File f : files)
		{
			//Make a new server definition with the appropriate id (there is no more information)
			srv.setId(f.getName());
			changes.put(srv.);
			//Remove it from the servers list
			//Log that we removed it
			.info("Server definition {} was removed"f.getName());
		}
		//Return our result
		return changes;
	}

	public Map<String, ? extends ObjectgetConfiguration()
	{
	}

Returns false as this source is currently not a blocking source

Returns:
false
	public boolean isBlocking()
	{
		return false;
	}
New to GrepCode? Check out our FAQ X