package jsmith.causalmatrix6;
//package exercise;
import java.net.*;
import java.io.*;
import java.util.*;
/**
* DatabaseBroker is the link between the evaluators for the Applets,
* and CMU-Online.
*
* @author David Danks
* @version 1.0; Mar 10, 1998
*/
public class DatabaseBroker {
private static final String URLBASE =
"http://online.web.cmu.edu/";
/* **************** Variables ************ */
protected String module;
protected String page;
private boolean online = true;
private String codebase = "";
/* **************** Constructor ********** */
/**
* This constructor instantiates a DatabaseBroker for an evaluator
* to use to communicate with CMU-Online.
*
* @param module The module that the evaluator is located in
* @param page The page the evaluator is on
*/
public DatabaseBroker(String module, String page) {
this(module, page, false); }
/**
* This constructor allows you to also set whether the DatabaseBroker
* will be run locally or remotely.
*
* @param module The module that the evaluator is located in
* @param page The page the evaluator is on
* @param local Whether the Applet is being run online
*/
public DatabaseBroker(String module, String page, boolean online) {
this.online = online;
this.module = module;
this.page = page;
}
/**
* This constructor allows you to pass the codebase of the Applet when
* we're running locally, so that we can avoid SecurityExceptions
*
* @param module The module that the evaluator is located in
* @param page The page the evaluator is on
* @param codebase The codebase of the Applet
*/
public DatabaseBroker(String module, String page, String codebase) {
online = false;
this.module = module;
this.page = page;
this.codebase = codebase;
}
/**
* Submits the student's grade to CMU-Online.
*
* @param question The index of the question being answered
* @param grade The student's grade (T or F, for now)
* @return true, if the grade was submitted
* false, if it didn't work
*/
public boolean submitGrade(String question, String grade) {
if (!(online)) {
System.out.println("Grade "+grade+" given for question "+question);
return true;
}
String req = "grader/plsql/submitgrade?component="+
module+"&module="+page+"&task="+question+"&grade="+grade;
InputStream is = openInputStream(req);
if (is == null) return false;
BufferedReader in = new BufferedReader(new InputStreamReader(is));
// check to make sure the connection worked
try {
String val = in.readLine();
if (!(val.equals("true"))) return false; }
catch (IOException i) {
System.err.println("Trouble reading data: "+i.toString());
return false; }
// close everything up
try { in.close(); }
catch (IOException i) {
System.err.println("Trouble closing connection: "+i.toString());
return false; }
return true;
}
/**
* Logs the student out of the CMU-Online system
*
* @return null, if the connection didn't work;
* the URL of the new page to display, if it did work.
*/
public String logout() {
if (!(online)) {
System.out.println("logged out");
return null;
}
BufferedReader in = getTextFile("student/plsql/logout");
if (in == null) return null;
// check to make sure the connection worked
try {
String val = in.readLine();
if (!(val.equals("true"))) return null; }
catch (IOException i) {
System.err.println("Trouble reading data: "+i.toString());
return null; }
// return them to the front door
return URLBASE;
}
/**
* getTextFile returns a BufferedReader for filename
.
*
* @param filename The filename of the file to retrieve. You may need to
* include directory structure.
* @return A BufferedReader to the given file
*/
public BufferedReader getTextFile(String filename) {
InputStream is = openInputStream(filename);
if (is == null) return null;
BufferedReader in = new BufferedReader(new InputStreamReader(is));
return null;
}
/**
* Returns the ObjectInputStream to a binary file so that we can read
* in Java objects from CMU-Online
*
* @param filename The filename of the object we want to read in. Note:
* you may need to include directory structure.
* @return An ObjectInputStream to the binary file
*/
public ObjectInputStream getBinaryFile(String filename) {
ObjectInputStream o;
try {
InputStream is = openInputStream(filename);
if (is == null) return null;
o = new ObjectInputStream(is); }
catch (IOException i) {
System.err.println("Problem with object stream: "+i.toString());
return null; }
return o;
}
/**
* Allows the user to write a text file to CMU-Online.
*
* @param filename The filename to use for writing. Note: you can
* include directory structure
* @return A PrintWriter to write out the text file. null
* is also returned if filename is the same as a currently
* existing file.
*/
public PrintWriter writeTextFile(String filename) {
OutputStream o = openOutputStream(filename);
if (o == null) return null;
else return new PrintWriter(o);
}
/**
* Allows the user to write a Java Object to CMU-Online.
*
* @param filename The filename to use for writing. Note: you can
* include directory structure
* @return An ObjecOutputStream to write out the Object.
* null
is returned if filename is the same
* as a currently existing file.
*/
public ObjectOutputStream writeBinaryFile(String filename) {
OutputStream o = openOutputStream(filename);
if (o == null) return null;
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(o);
}
catch (IOException i) {
System.err.println("Problem creating object connection: "+
i.toString());
return null;
}
return oos;
}
/**
* Variably opens a FileInputStream or an InputStream (from a URL)
* depending on whether we're working locally or remotely
*
* @param name The actual filename of the file to open
* @return An InputStream to that file
*/
private InputStream openInputStream(String name) {
if (!(online)) {
if (!(name.startsWith(codebase))) {
System.err.println(name+" must be below the Applet code!");
return null; }
FileInputStream f;
try { f = new FileInputStream(name); }
catch (FileNotFoundException e) {
System.err.println("File not found: "+e.toString());
return null; }
return f;
}
else {
URL str;
InputStream i;
try { str = new URL(URLBASE+name);
i = str.openStream(); }
catch (MalformedURLException m) {
System.err.println("Bad URL: "+m.toString());
return null; }
catch (IOException e) {
System.err.println("IO Problem: "+e.toString());
return null; }
return i;
}
}
/**
* Variably opens a FileOutputStream or an OutputStream (from a URL)
* depending on whether we're working locally or remotely
*
* @param name The actual filename of the file to write to
* @return An OutputStream to that file
*/
private OutputStream openOutputStream(String name) {
if (!(online)) {
if (!(name.startsWith(codebase))) {
System.err.println(name+" must be below the Applet code!");
return null; }
File file = new File(name);
if (file.exists()) return null;
FileOutputStream f;
try { f = new FileOutputStream(file); }
catch (IOException e) {
System.err.println("Problem opening connection: "+e.toString());
return null; }
return f;
}
else {
URL str;
OutputStream o;
try { str = new URL(URLBASE+name);
URLConnection conn = str.openConnection();
o = conn.getOutputStream(); }
catch (MalformedURLException m) {
System.err.println("Bad URL: "+m.toString());
return null; }
catch (IOException e) {
System.err.println("IO Problem: "+e.toString());
return null; }
return o;
}
}
/**
* Sets whether we're online
*
* @param b true
, if we're online;
* false
, if we're offline.
*/
public void setOnline(boolean b) { online = b; }
/**
* Returns whether we're online
*
* @return true
, if we're online;
* false
, if we're offline.
*/
public boolean getOnline() { return online; }
/**
* Tells the DBB the codebase of the calling Applet
*
* @param base The Applet's codebase
*/
public void setCodebase(String base) { codebase = base; }
}