//package spo.src.JDBC; import java.io.*; import java.net.*; import java.util.Vector; import java.util.StringTokenizer; /** * * Class XConnection is java class, which allows client to * connect to the SPDBMS data server. * *
* @author Wenzhong Zhao * @version 1.0 */ public class XConnection { /* The connection to the server */ private Socket sock = null; private PrintWriter out = null; private BufferedReader in = null; private static boolean success = false; //Indicate whether the latest command succeeds or not. //Default host and port final static String DEFAULT_HOST = "alphaville.csr.uky.edu"; final static int DEFAULT_PORT = 2000; /** * Default constructor * * Initialize the XConnection */ public XConnection() { } /** * Use default host and port to connect */ public void connect() { connect(DEFAULT_HOST, DEFAULT_PORT); } /** * Connect to the data server with (host, port) * * @param host data server name * @param port the prot number which the server is listening */ public void connect(String host, int port) { try { sock = new Socket(host, port); out = new PrintWriter(sock.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(sock.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " +host); success = false; //System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: " +host); success = false; //System.exit(1); } success = true; } /** * Close the connection to the data server */ public void close() { //Inform the server to close the thread out.println("exit"); //Close all the streams try { out.close(); in.close(); sock.close(); } catch (IOException ie) { System.out.println("Problem with closing socket."); success = false; } success = true; } //SPDB simple API functions /** * Get the names of relations in the data server */ public Vector getRelNames() throws IOException { //Execute it executeUpdate("GET RELATION NAMES"); String temp = in.readLine(); if (temp.startsWith("FAILED:")) { success = false; return null; } else { Vector names = new Vector(); StringTokenizer tokens = new StringTokenizer(temp, " "); while(tokens.hasMoreTokens()) { names.add(tokens.nextToken()); } if (names.size() == 0) { success = false; return null; } else { success = true; return names; } } } /** * Get the dependency config file of the relation */ public String getContext(String relName) throws IOException { if (relName == null) { success = false; return "FAILED: Please input a relation name."; } //Execute it String temp = executeQuery("GETXC " +relName); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Get the dependency config file of the relation */ public String getConfig(String relName) throws IOException { if (relName == null) { success = false; return "FAILED: Please input a relation name."; } //Execute it String temp = executeQuery("GETX " +relName); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Create the relation in the data server * * @param relName name of the relation * @param schemaURL URL string of the schema */ public String create(String relName, String schemaURL) throws IOException { if (relName == null) { success = false; return "FAILED: Please input a relation name."; } StringBuffer sb = new StringBuffer(); if (schemaURL == null) sb.append("CREATE " +relName); else sb.append("CREATE " +relName+ " " +schemaURL); //Execute it executeUpdate(sb.toString()); String temp = in.readLine(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Drop the relation * * @param relName name of the relation */ public String drop(String relName) throws IOException { if (relName == null) { success = false; return "FAILED: Please input a relation name."; } StringBuffer sb = new StringBuffer(); sb.append("DROP " +relName); //Execute it executeUpdate(sb.toString()); String temp = in.readLine(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Insert a dependency config file for the relation * * @param relName name of the relation * @param cfgName name the dependency config file */ public String setConfig(String relName, String cfgName) throws IOException { if (relName == null || cfgName == null) { success = false; return "FAILED: Please input both a relation name and a dependency file name."; } StringBuffer sb = new StringBuffer(); sb.append("INSERTX INTO " +relName); executeUpdate(sb.toString()); //Check if server is ready to receive the XML data String isReady = null; while (!in.ready()) {;} //Waits for the stream to be ready isReady = in.readLine(); if (!isReady.equals("Ready")) { return isReady; } //Get the XML stream and send to server BufferedReader br = new BufferedReader(new FileReader(cfgName)); String aLine = br.readLine(); while (aLine != null) { out.println(aLine); aLine = br.readLine(); } //Close br br.close(); //No need to set the end of stream //Expected 'EOF' from the parser if more char after the closing root tag!!! //Tell the server that the xml file ends //Important!!! out.println("null"); //Get response String temp = getRespond(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Insert a dependency config file for the relation * * @param relName name of the relation * @param cfgName name the dependency config file */ public String setContext(String relName, String cfgName) throws IOException { if (relName == null || cfgName == null) { success = false; return "FAILED: Please input both a relation name and a dependency file name."; } StringBuffer sb = new StringBuffer(); sb.append("INSERTXC INTO " +relName); executeUpdate(sb.toString()); //Check if server is ready to receive the XML data String isReady = null; while (!in.ready()) {;} //Waits for the stream to be ready isReady = in.readLine(); if (!isReady.equals("Ready")) { return isReady; } //Get the XML stream and send to server BufferedReader br = new BufferedReader(new FileReader(cfgName)); String aLine = br.readLine(); while (aLine != null) { out.println(aLine); aLine = br.readLine(); } //Close br br.close(); //No need to set the end of stream //Expected 'EOF' from the parser if more char after the closing root tag!!! //Tell the server that the xml file ends //Important!!! out.println("null"); //Get response String temp = getRespond(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Insert a xml file into the relation * * @param relName name of the relation * @param xmlFile name the xml file */ public String insert(String relName, String xmlFile) throws IOException { if (relName == null || xmlFile == null) { success = false; return "FAILED: Please input both a relation name and XML file name."; } StringBuffer sb = new StringBuffer(); sb.append("INSERT INTO " +relName); executeUpdate(sb.toString()); //Check if server is ready to receive the XML data String isReady = null; // while ((isReady = in.readLine()) == null) {;} // try { while (!in.ready()) {;} //Waits for the stream to be ready // } catch (InterruptedException ie) {;} isReady = in.readLine(); if (!isReady.equals("Ready")) { return isReady; } //Get the XML stream and send to server BufferedReader br = new BufferedReader(new FileReader(xmlFile)); String aLine = br.readLine(); while (aLine != null) { out.println(aLine); aLine = br.readLine(); } //Close br br.close(); //No need to set the end of stream //Expected 'EOF' from the parser if more char after the closing root tag!!! //Tell the server that the xml file ends //Important!!! out.println("null"); //Get response String temp = getRespond(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Insert a xml stream into the relation * * @param relName name of the relation * @param xmldata * author : Krol Mathias */ public String insertdata(String relName, String xmldata) throws IOException { if (relName == null || xmldata == null) { success = false; return "FAILED: Please input both a relation name and data."; } StringBuffer sb = new StringBuffer(); sb.append("INSERT INTO " +relName); executeUpdate(sb.toString()); //Check if server is ready to receive the XML data String isReady = null; // while ((isReady = in.readLine()) == null) {;} // try { while (!in.ready()) {;} //Waits for the stream to be ready // } catch (InterruptedException ie) {;} isReady = in.readLine(); if (!isReady.equals("Ready")) { return isReady; } // send the xmldata string //System.out.println(xmldata); out.println(xmldata); //No need to set the end of stream //Expected 'EOF' from the parser if more char after the closing root tag!!! //Tell the server that the xml file ends //Important!!! out.println("null"); //Get response String temp = getRespond(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Delete from the relation SPOs which satisfy the condition * * @param relName name of the relation * @param condition the condition string */ public String delete(String relName, String condition) throws IOException { if (relName == null || condition == null) { success = false; return "FAILED: Please input both a relation name and a condition."; } StringBuffer sb = new StringBuffer(); sb.append("DELETE FROM " +relName+ " WHERE " +condition); executeUpdate(sb.toString()); //Execute it String temp = in.readLine(); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Send a query to the data server * * @param queryStr the query string * @param isXML set the format of the query result. true will return * XML, otherwise HTML */ public String query(String queryStr, boolean isXML) throws IOException { if (queryStr == null) { success = false; return "FAILED: Please input a query string."; } StringBuffer sb = new StringBuffer(); if (isXML) sb.append("QUERY XML " +queryStr); else sb.append("QUERY HTML " +queryStr); //Execute it String temp = executeQuery(sb.toString()); if (temp.startsWith("FAILED:")) { success = false; } else { success = true; } return temp; } /** * Get the respond from the data server */ private String getRespond() throws IOException{ String inputLine = null; StringBuffer sb = new StringBuffer(); while ((inputLine = in.readLine()) != null && inputLine.length() > 0) { sb.append(inputLine+ "\n"); } return (sb.toString()).trim(); } /** * Execute the update command * * @param update the update string */ private void executeUpdate(String update) { out.println(update); } /** * Execute the query command * * @param query the query string */ private String executeQuery(String query) throws IOException { out.println(query); //Get the XML data from the server StringBuffer sb = new StringBuffer(); String aLine = null; int blankLineCount = 0; //Count of continuous blank lines while ((aLine = in.readLine()) != null) { if (aLine.equals("null")) break; if (aLine.length() == 0) { blankLineCount++; } else { blankLineCount = 0; } sb.append(aLine+ "\n"); if (blankLineCount == 2) break; //Check if the end of message } return (sb.toString()).trim(); } // Return the status public boolean getSuccess() { return success; } }