summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/CalendarDatabase.java271
-rw-r--r--server/CalendarFormatParser.java269
-rw-r--r--server/NetCalendarServer.java160
3 files changed, 700 insertions, 0 deletions
diff --git a/server/CalendarDatabase.java b/server/CalendarDatabase.java
new file mode 100644
index 0000000..50caecb
--- /dev/null
+++ b/server/CalendarDatabase.java
@@ -0,0 +1,271 @@
+package server;
+
+import java.util.*;
+
+import shared.*;
+import shared.remotecall.*;
+
+/**
+ * A object of this class encapsulates the whole calendar database on the server side.
+ * This class is used for getting/modifying and adding/removing of calendar events.
+ * @author buetow
+ *
+ */
+public final class CalendarDatabase {
+ private Vector vecCategories;
+ private HashMap mapEvents;
+ private HashMap mapCategories;
+
+ /**
+ * Simple constructur. Creates a calendar database.
+ * @param vecCategories Specifies the vector of CalendarCategory objects for initial database content.
+ */
+ public CalendarDatabase(Vector vecCategories) {
+ this.vecCategories = vecCategories;
+ initializeMaps();
+ }
+
+ /**
+ * Initializes a private hash map so that the server can access events by their id numbers very fast.
+ */
+ private void initializeMaps() {
+ this.mapEvents = new HashMap();
+ this.mapCategories = new HashMap();
+
+ Enumeration enumCategory = vecCategories.elements();
+ while (enumCategory.hasMoreElements()) {
+ CalendarCategory category = (CalendarCategory) enumCategory.nextElement();
+ mapCategories.put(category.getName(), category);
+
+ Vector vecEvents = category.getEvents();
+ Enumeration enumEvents = vecEvents.elements();
+ while (enumEvents.hasMoreElements()) {
+ CalendarEvent event = (CalendarEvent) enumEvents.nextElement();
+ mapEvents.put(new Integer(event.getEventID()), event);
+ }
+ }
+ }
+
+ /**
+ * Returns a specific calendar event using fast hash lookup.
+ * @param iEventID Specifies the ID of the calendar event to lookup.
+ * @return Returns the requested calendar event.
+ */
+ public CalendarEvent getEvent(int iEventID) {
+ return (CalendarEvent) mapEvents.get(new Integer(iEventID));
+ }
+
+ /**
+ * Returns a specific calendar category using fast hash lookup.
+ * @param sCategoryName Specifies the name of the calendar category to lookup.
+ * @return Returns the requested calendar category.
+ */
+ public CalendarCategory getCategory(String sCategoryName) {
+ return (CalendarCategory) mapCategories.get(sCategoryName);
+ }
+
+ /**
+ * Gets all calendar categories.
+ * @return Returns a Vector of all available CalendarCategory objects.
+ */
+ public Vector getCategories() {
+ return vecCategories;
+ }
+
+ /**
+ * This method returns a Vector of all available calendar events.
+ * @return Returns all available CalendarEvent objects of the database.
+ */
+ public Vector getAllEvents() {
+ MyVector vecAllEvents = new MyVector();
+ Enumeration enumCategories = vecCategories.elements();
+
+ while (enumCategories.hasMoreElements()) {
+ CalendarCategory category = (CalendarCategory) enumCategories.nextElement();
+ vecAllEvents.appendVector(category.getEvents());
+ }
+
+ return vecAllEvents;
+ }
+
+ /**
+ * This method looks for all calendar events which match the a given client request.
+ * @param clientRequest Specifies the client request sent by the calendar client.
+ * @return Returns a Vector of all matching CalendarEvent objects.
+ */
+ public Vector getMatchingEvents(ClientRequest clientRequest) {
+ MyVector vecEvents = new MyVector();
+ int iNumEventsToRequest = clientRequest.getNumEventsToRequest();
+
+ Enumeration enumCategories = vecCategories.elements();
+ while (enumCategories.hasMoreElements()) {
+ CalendarCategory category = (CalendarCategory) enumCategories.nextElement();
+ Vector vecNewEvents = category.getMatchingEvents(clientRequest);
+ vecEvents.appendVector(vecNewEvents);
+
+ if (iNumEventsToRequest > -1) {
+ if (iNumEventsToRequest == vecEvents.size())
+ break;
+
+ } else {
+ clientRequest.setNumEventsToRequest(iNumEventsToRequest - vecNewEvents.size());
+ }
+
+ }
+
+ return vecEvents;
+ }
+
+ /**
+ * This method adds a calendar event to the calendar database.
+ * @param clientRequest Specifies the client request sent by the netcalendar client. Its containing the new event to add.
+ */
+ public void addEvent(ClientRequest clientRequest) {
+ CalendarEvent event = clientRequest.getEvent();
+ String sCategoryName = event.getCategoryName();
+ CalendarCategory category = getCategory(sCategoryName);
+
+ // Check if the have to create a new category
+ if (category == null) {
+ category = new CalendarCategory(sCategoryName);
+ mapCategories.put(sCategoryName, category);
+ vecCategories.add(category);
+ }
+
+ category.addEvent(event);
+ category.setHasChanged(true);
+ event.setCategory(category);
+ mapEvents.put(new Integer(event.getEventID()), event);
+ }
+
+ /**
+ * This method modifies an calendar event of the calendar database.
+ * @param clientRequest Specifies the client request sent by the calendar client.
+ */
+ public void modifyEvent(ClientRequest clientRequest) {
+ CalendarEvent newEvent = clientRequest.getEvent();
+ CalendarEvent orgEvent = getEvent(newEvent.getEventID());
+
+ // If the event doesnt exist any more, recreate it
+ if (orgEvent == null) {
+ clientRequest.setEvent(newEvent);
+ addEvent(clientRequest);
+ return;
+ }
+
+ orgEvent.setDate(newEvent.getDate());
+ orgEvent.setYearly(newEvent.isYearly());
+ orgEvent.setDescription(newEvent.getDescription());
+ orgEvent.setPlace(newEvent.getPlace());
+
+ // Modify the events category
+ if (!orgEvent.getCategoryName().equals(newEvent.getCategoryName())) {
+ String sNewCategoryName = newEvent.getCategoryName();
+
+ // First remove the event from its current category
+ orgEvent.removeFromCurrentCategory();
+ CalendarCategory category = getCategory(sNewCategoryName);
+
+ if (category == null) {
+ category = new CalendarCategory(sNewCategoryName);
+ mapCategories.put(sNewCategoryName, category);
+ vecCategories.add(category);
+ }
+
+ category.addEvent(orgEvent);
+ category.setHasChanged(true);
+ orgEvent.setCategory(category);
+
+ } else {
+ orgEvent.getCategory().setHasChanged(true);
+ }
+ }
+
+ /**
+ * This method deletes a calendar event of the calendar database. If the specified calendar event doesnt
+ * exist eny more, nothing will be changed.
+ * @param clientRequest Specifies the client request sent by the netcalendar client.
+ */
+ public void deleteEvent(ClientRequest clientRequest) {
+ // Get the server side reference of the calendar event
+ CalendarEvent event = getEvent(clientRequest.getEvent().getEventID());
+
+ if (event != null) {
+ event.getCategory().setHasChanged(true);
+ event.removeFromCurrentCategory();
+ mapEvents.remove(new Integer(event.getEventID()));
+ }
+ }
+
+ /**
+ * This method renames a calendar category.
+ * @param clientRequest Specifies the client request sent by the netcalendar client.
+ */
+ public void renameCategory(ClientRequest clientRequest) {
+ String sOldCategoryName = clientRequest.getEvent().getCategoryName();
+ CalendarCategory category = getCategory(sOldCategoryName);
+
+ if (category == null)
+ return;
+
+ String sNewCategoryName = clientRequest.getString();
+
+ if (sOldCategoryName.equals(sNewCategoryName))
+ return;
+
+ mapCategories.remove(sOldCategoryName);
+
+ category.setName(sNewCategoryName);
+
+ Vector vecEvents = category.getEvents();
+ Enumeration enumEvents = vecEvents.elements();
+ while (enumEvents.hasMoreElements()) {
+ CalendarEvent event = (CalendarEvent) enumEvents.nextElement();
+ event.setCategoryName(sNewCategoryName);
+ }
+
+ // Check if the category exists already
+ CalendarCategory categoryExists = getCategory(sNewCategoryName);
+
+ if (categoryExists == null) {
+ mapCategories.put(category.getName(), category);
+ } else {
+ vecCategories.remove(category);
+ categoryExists.merge(category);
+ }
+
+ }
+
+ /**
+ * This method daletes a calendar category.
+ * @param clientRequest Specifies the client request sent by the netcalendar client.
+ */
+ public void deleteCategory(ClientRequest clientRequest) {
+ String sCategoryName = clientRequest.getEvent().getCategoryName();
+ CalendarCategory category = getCategory(sCategoryName);
+
+ if (category == null)
+ return;
+
+ mapCategories.remove(sCategoryName);
+ vecCategories.remove(category);
+ category.deleteDatabaseFile();
+ }
+
+ /**
+ * This method flushes all the changed calendar categories of the calendar database to the filesystem.
+ */
+ public void flush() {
+ Enumeration enumCategories = vecCategories.elements();
+
+ while (enumCategories.hasMoreElements()) {
+ CalendarCategory category = (CalendarCategory) enumCategories.nextElement();
+ if (category.hasChanged())
+ category.flush();
+ }
+
+ Main.execExternalCommand(Config.getStringValue("server_updatedb_command"));
+ }
+
+}
+
diff --git a/server/CalendarFormatParser.java b/server/CalendarFormatParser.java
new file mode 100644
index 0000000..4ef6fe5
--- /dev/null
+++ b/server/CalendarFormatParser.java
@@ -0,0 +1,269 @@
+/**
+ *
+ */
+package server;
+
+import java.util.*;
+import java.util.regex.*;
+import java.text.*;
+import java.io.*;
+
+import shared.*;
+
+/**
+ * This class is needed for parsing the original UNIX calendar database format.
+ * Its parsing the calendar database from the file-system and its caching it into the
+ * memory. Then, the CalendarDatabase class can be used to access the database.
+ * Each calendar category has its own file. In each file all the calendar events of the
+ * specific categories are stored.
+ * @author buetow
+ *
+ */
+public final class CalendarFormatParser {
+ private int iCurrentYear;
+ private Date dateCurrent;
+ private String sWorkdir;
+ private Vector vecCategories;
+ private static final Pattern datePattern;
+ private static final Pattern dateYearlyPattern;
+ private static final Pattern dateEndPattern;
+ // private static final Pattern emptyLine;
+ private static final String DATE_FORMAT = "M/d-y";
+ private static final String EXTENDED_DATE_FORMAT = "M/d-y-H:m";
+
+ // Those pattern will be used later, precompile here once to use often
+ static {
+ // Matches calendar date format like: "01/25 2006-14:23"
+ datePattern = Pattern.compile("\\d{2}/\\d{2}\t\\d{4}-\\d{2}:\\d{2}");
+
+ // Matches calendar date format like: "01/25 yearly-14:23"
+ dateYearlyPattern = Pattern.compile("\\d{2}/\\d{2}\tyearly-\\d{2}:\\d{2}");
+
+ // Matches the end of the date string like: "arly-14:23 " or "2006-14:23"
+ dateEndPattern = Pattern.compile(".{4}-\\d{2}:\\d{2}");
+ }
+
+ /**
+ * Simple constructor. Creates a calendar format parser object and initializes some private members.
+ */
+ public CalendarFormatParser() {
+ // Use current dir as workdir by default
+ this.sWorkdir = ".";
+ this.vecCategories = new Vector();
+
+ // Events without a year specified will use the current year!
+ GregorianCalendar cal = new GregorianCalendar();
+ iCurrentYear = cal.get(Calendar.YEAR);
+
+ dateCurrent = new Date();
+ }
+
+ /**
+ * This method returns a vector of all found calendar categories after parsing.
+ * @return Returns a Vector of all available CalendarCategory objects.
+ */
+ public Vector getCategories() {
+ return vecCategories;
+ }
+
+ /**
+ * This method sets the working directory. Its the "server_database_dir" variable defined in the current configuration by default.
+ * @param sWorkdir Specifies the working directory.
+ */
+ public void setWorkdir(String sWorkdir) {
+ this.sWorkdir = sWorkdir;
+ }
+
+ /**
+ * Starts the parsing work of the calendar database files.
+ */
+ public void start() {
+ lookForCategories();
+ parseAllCategories();
+ Main.execExternalCommand(Config.getStringValue("server_startup_command"));
+ }
+
+ /**
+ * Parses for the available calendar categories.
+ */
+ private void lookForCategories() {
+ File dir = new File(sWorkdir);
+ File[] dirContent = dir.listFiles();
+
+ for (int i = 0; i < dirContent.length; ++i)
+ if (dirContent[i].isFile())
+ // Ignore the 'calendar' file, only read the 'calendar.*' files
+ if (!dirContent[i].getName().equals("calendar"))
+ vecCategories.add(new CalendarCategory(dirContent[i]));
+ }
+
+ /**
+ * Parses all events of all available categories.
+ */
+ private void parseAllCategories() {
+ Enumeration enumCategories = vecCategories.elements();
+ while (enumCategories.hasMoreElements())
+ parseCategory((CalendarCategory) enumCategories.nextElement());
+ }
+
+ /**
+ * Parses all events of a category file.
+ * @param category Specifies the calendar category to be parsed.
+ */
+ private void parseCategory(CalendarCategory category) {
+ Vector vecEvents = new Vector();
+ File file = category.getFile();
+
+ try {
+ BufferedReader in = new BufferedReader(new FileReader(file));
+ String sLine;
+
+ while ((sLine = in.readLine()) != null) {
+ CalendarEvent event = new CalendarEvent(category);
+
+ // Ignore empty lines!
+ if (sLine.equals(""))
+ continue;
+
+ setEventsDate(event, sLine);
+ setEventsPlace(event, sLine);
+ setEventsDescription(event, sLine);
+
+ vecEvents.add(event);
+ }
+
+ } catch (Exception e) {
+ Main.infoMessage("Error: " + e.toString());
+ }
+
+ category.setEvents(vecEvents);
+ category.unsetFile();
+ }
+
+ /**
+ * This method parses all known informations from a given calendar format line and saves them
+ * into the given calendar event object.
+ * @param event Specifies the calendar event to be modified.
+ * @param sLine Specifies the single line of the category file to be parsed.
+ */
+ private void setEventsDate(CalendarEvent event, String sLine) {
+ // Create a local copy because the string may be modified
+ String sMyLine = new String(sLine).replaceAll(" ", "");
+ String sDateFormat;
+ boolean bValidDateFormat = false;
+
+ // Check if its a yearly event
+ Matcher matcher = dateYearlyPattern.matcher(sMyLine);
+ if (matcher.find()) {
+ sMyLine = sMyLine.replaceFirst("\tyearly", "-" + iCurrentYear);
+ sDateFormat = EXTENDED_DATE_FORMAT;
+ event.setYearly(true);
+ bValidDateFormat = true;
+
+ } else {
+ // Event is not yearly, but check if its still using NetCalendars
+ // extended format which contains the event's time (hours:minutes)
+ matcher = datePattern.matcher(sMyLine);
+ if (matcher.find()) {
+ sMyLine = sMyLine.replaceFirst("\t", "-");
+ sDateFormat = EXTENDED_DATE_FORMAT;
+ event.setYearly(false);
+ bValidDateFormat = true;
+
+ } else {
+ // Just use original Calendar format without any year and time
+ // informations
+ // Assume yearly
+ sMyLine = sMyLine.replaceFirst("\t", "-" + iCurrentYear);
+ sDateFormat = DATE_FORMAT;
+ event.setYearly(true);
+ bValidDateFormat = true;
+ }
+ }
+
+ // Create a new date object containing the events time informations
+ SimpleDateFormat formatter = new SimpleDateFormat(sDateFormat);
+ Date date = null;
+
+ if (bValidDateFormat) {
+ try {
+ date = formatter.parse(sMyLine);
+
+ } catch (ParseException e) {
+ Main.infoMessage("Error: Calendar format parser error at category "
+ + event.getCategoryName() + ": " + e.getMessage());
+ }
+ }
+
+ // The event is yearly, but occured already this year, so increment the events
+ // year by one!
+ if (event.isYearly() && date.getTime() < dateCurrent.getTime()) {
+ Calendar calendar = new GregorianCalendar();
+ calendar.setTime(date);
+ calendar.set(Calendar.YEAR, iCurrentYear + 1);
+ date = calendar.getTime();
+ }
+
+ event.setDate(date);
+ }
+
+ /**
+ * Parses a single calendar line for the place information.
+ * @param event Specifies the calendar event to be modified.
+ * @param sLine Specifies the single line of the category file to be parsed.
+ */
+ private void setEventsPlace(CalendarEvent event, String sLine) {
+ int iPos = sLine.indexOf(";;");
+
+ // No event! Return empty string!
+ if (iPos < 0) {
+ event.setPlace("");
+
+ } else {
+ event.setPlace(trim(sLine.substring(iPos + 2)));
+ }
+ }
+
+ /**
+ * Parses a single calendar line for the description information.
+ * @param event Specifies the calendar event to be modified.
+ * @param sLine Specifies the single line of the category file to be parsed.
+ */
+ private void setEventsDescription(CalendarEvent event, String sLine) {
+ // We need a local copy because we may modify the string
+ String sTmp = new String(sLine);
+
+ // Check if there is a place string...
+ int iPos = sLine.indexOf(";;");
+
+ // ... if yes, dont include it!
+ if (iPos >= 0)
+ sTmp = sTmp.substring(0, iPos);
+
+ // Remove the events date from the string
+ Matcher matcher = dateEndPattern.matcher(sTmp);
+
+ if (matcher.find()) {
+ sTmp = sTmp.substring(matcher.start()+11);
+
+ } else {
+ sTmp = sTmp.substring(sTmp.indexOf("\t")+1);
+ }
+
+ event.setDescription(trim(sTmp));
+ }
+
+ /**
+ * Its like String.trim() but also removes a ending newline.
+ * @param sTrimString Specifies the String to be trimmed.
+ * @return Returns a copy of the string, with leading and trailing whitespace omitted, also a trailing newline will be omitted.
+ */
+ private String trim(String sTrimString) {
+ int iPos = sTrimString.indexOf("\n");
+
+ if (iPos >= 0)
+ return sTrimString.substring(0, iPos).trim();
+
+ return sTrimString.trim();
+ }
+}
diff --git a/server/NetCalendarServer.java b/server/NetCalendarServer.java
new file mode 100644
index 0000000..10704c0
--- /dev/null
+++ b/server/NetCalendarServer.java
@@ -0,0 +1,160 @@
+/**
+ *
+ */
+package server;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import javax.net.*;
+import javax.net.ssl.*;
+
+import shared.*;
+import shared.remotecall.*;
+
+/**
+ * This is the main class of the server part of the netcalendar suite.
+ * It contains the server socket part and hold a references to the calendar database.
+ * @author buetow
+ */
+public class NetCalendarServer extends Thread {
+ private int iPort;
+ private String sWorkdir;
+
+ /**
+ * Creates a NetCalendarServer instance and runs it as a thread.
+ * @param iPort Specifies the server port of the server to be used.
+ * @param sWorkdir Specifies the working directory of the server.
+ */
+ public NetCalendarServer(int iPort, String sWorkdir) {
+ this.iPort = iPort;
+ this.sWorkdir = sWorkdir;
+
+ start();
+ }
+
+ /**
+ * This method initializes a new server socket.
+ * @return Returns the server socket object.
+ */
+ private ServerSocket makeServerSocket() throws IOException {
+ if (!Config.getBooleanValue("use_ssl"))
+ return new ServerSocket(iPort);
+
+ ServerSocketFactory sslSocketFactory = SSLServerSocketFactory.getDefault();
+ return sslSocketFactory.createServerSocket(iPort);
+ }
+
+ /**
+ * This method specifies the start method of the Thread. Its setting up the server port.
+ */
+ public void run() {
+ ServerSocket serverSocket = null;
+ Socket socket = null;
+
+ // Read and parse the whole calendar database from file!
+ CalendarFormatParser parser = new CalendarFormatParser();
+ parser.setWorkdir(sWorkdir);
+ parser.start();
+
+ CalendarDatabase calendarDatabase = new CalendarDatabase(parser.getCategories());
+
+ try {
+ serverSocket = makeServerSocket();
+
+ while (true) {
+ // This will wait for a connection to be made to this socket.
+ socket = serverSocket.accept();
+ // serverSocket.accept();
+ socket.setKeepAlive(true);
+
+ // Recieve the client's request object
+ InputStream inputStream = socket.getInputStream();
+ ObjectInput objectInput = new ObjectInputStream(inputStream);
+ ClientRequest clientRequest = (ClientRequest) objectInput.readObject();
+
+ if (!clientRequest.checkPassphrase(Config.getStringValue("passphrase", false))) {
+ Main.infoMessage("Server: Client refused, wrong passphrase!");
+
+ // Check if a event has been modified!
+ } else if (clientRequest.requestsNewEvents()) {
+ Main.infoMessage("Server: Client requests new events");
+
+ // Get all calendar events which match the request
+ Vector vecEvents = calendarDatabase.getMatchingEvents(clientRequest);
+ ServerResponse serverResponse = new ServerResponse(vecEvents);
+
+ OutputStream outputStream = socket.getOutputStream();
+ ObjectOutput objectOutput = new ObjectOutputStream(outputStream);
+ objectOutput.writeObject(serverResponse);
+ objectOutput.flush();
+ objectOutput.close();
+
+ } else if (clientRequest.actionIs(ClientRequest.MODIFY_EVENT)) {
+ Main.infoMessage("Server: Client wants to modify an event");
+ calendarDatabase.modifyEvent(clientRequest);
+ // if (Config.getBooleanValue("client_run"))
+ calendarDatabase.flush();
+
+ } else if (clientRequest.actionIs(ClientRequest.DELETE_EVENT)) {
+ Main.infoMessage("Server: Client wants to delete an event");
+ calendarDatabase.deleteEvent(clientRequest);
+ // if (Config.getBooleanValue("client_run"))
+ calendarDatabase.flush();
+
+ } else if (clientRequest.actionIs(ClientRequest.ADD_EVENT)) {
+ Main.infoMessage("Server: Client wants to add an event");
+ calendarDatabase.addEvent(clientRequest);
+ // if (Config.getBooleanValue("client_run"))
+ calendarDatabase.flush();
+
+ } else if (clientRequest.actionIs(ClientRequest.RENAME_CATEGORY)) {
+ Main.infoMessage("Server: Client wants to rename a category");
+ calendarDatabase.renameCategory(clientRequest);
+ // if (Config.getBooleanValue("client_run"))
+ calendarDatabase.flush();
+
+ } else if (clientRequest.actionIs(ClientRequest.DELETE_CATEGORY)) {
+ Main.infoMessage("Server: Client wants to delete a category (NYI)");
+ calendarDatabase.deleteCategory(clientRequest);
+ // if (Config.getBooleanValue("client_run"))
+ calendarDatabase.flush();
+
+ } else if (clientRequest.actionIs(ClientRequest.RELOAD_DATABASE)) {
+ // Read and parse the whole calendar database from file!
+ parser = new CalendarFormatParser();
+ parser.setWorkdir(sWorkdir);
+ parser.start();
+
+ calendarDatabase = new CalendarDatabase(parser.getCategories());
+
+ } else if (clientRequest.actionIs(ClientRequest.FLUSH_DATABASE)) {
+ Main.infoMessage("Server: Client wants to flush the database");
+ calendarDatabase.flush();
+
+ } else if (clientRequest.actionIs(ClientRequest.SHUTDOWN_SERVER)) {
+ Main.infoMessage("Server: Client wants the server to shut down");
+ calendarDatabase.flush();
+ socket.close();
+
+ if (Config.getBooleanValue("client_run")) {
+ Main.infoMessage("Server: Shutting down the server thread");
+ socket.close();
+ serverSocket.close();
+ break;
+ } else {
+ Main.exit(0);
+ }
+ }
+
+ socket.close();
+ }
+
+ } catch (ClassNotFoundException e) {
+ Main.infoMessage("Error: Server error during serialization: " + e.getMessage());
+
+ } catch (IOException e) {
+ Main.infoMessage("Error: Server error during serialization: " + e.getMessage());
+ }
+ }
+}