aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/backend/server/Server.java
blob: 57a00300c12db8abe8255c664da0a1eddfa04fca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Server.java
 * -----------
 * 
 * Part of the URY Server Platform
 * 
 * V0.00  2011/03/20
 * 
 * (C) 2011 URY Computing
 */

package uk.org.ury.backend.server;

import java.io.IOException;

import uk.org.ury.backend.config.ConfigReader;
import uk.org.ury.backend.database.DatabaseDriver;
import uk.org.ury.backend.database.UserClass;
import uk.org.ury.backend.database.exceptions.ConnectionFailureException;
import uk.org.ury.backend.database.exceptions.MissingCredentialsException;

/**
 * The unified URY server, accepting requests over HTTP.
 * 
 * @author Matt Windsor
 * @version 2011.0320
 */
public class Server {
    public static final String SERVER_VERSION = "SLUT 0.0";
    public static final String DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
	    + "\"http://www.w3.org/TR/html4/strict.dtd\">";
    public static final String INDEX_HTML = "\n<html>" + "\n  <head>"
	    + "\n    <title>" + SERVER_VERSION + "</title>" + "\n  </head>"
	    + "\n  <body>" + "\n    <h1>Welcome to the " + SERVER_VERSION
	    + " server</h1>"
	    + "\n    <p>This server exposes a class-based API for accessing"
	    + "\n    the internals of the " + SERVER_VERSION + " system.</p>"
	    + "\n    <p>See the documentation for details.</p>" + "\n  </body>"
	    + "\n</html>";

    /**
     * The main method, which serves to create a server.
     * 
     * @param args
     *            The argument vector.
     */
    public static void main(String[] args) {
	Server srv = new Server();
	srv.run();
    }

    /**
     * Run the server.
     */
    private void run() {
	Thread thread = null;

	try {
	    thread = new HttpListenerThread(8000, this);
	} catch (IOException e) {
	    e.printStackTrace();
	    System.exit(-1);
	}

	thread.setDaemon(false);
	thread.run();
    }

    /**
     * Gets a database connection using the given user class.
     * 
     * @param userClass
     *            The user class to get a connection for.
     * 
     * @return a database connection, which may or may not have been created on
     *         this call.
     * 
     * @throws MissingCredentialsException
     *             if the credentials for the given userclass are missing.
     * 
     * @throws ConnectionFailureException
     *             if the connection failed.
     */
    public DatabaseDriver getDatabaseConnection(UserClass userClass)
	    throws MissingCredentialsException, ConnectionFailureException {
	// TODO: Singleton

	ConfigReader config = new ConfigReader("res/conf.xml");

	return new DatabaseDriver(config, UserClass.READ_ONLY);
    }

    /**
     * @return the version string of the server.
     */
    public String getVersion() {
	return SERVER_VERSION;
    }
}