Project

General

Profile

« Previous | Next » 

Revision 343

Added by Matt Jones over 24 years ago

Changed exception handling mechanisms for DBReader

View differences:

src/edu/ucsb/nceas/metacat/DBReader.java
14 14
package edu.ucsb.nceas.metacat;
15 15

  
16 16
import java.io.*;
17
import java.net.URL;
18
import java.net.MalformedURLException;
19 17
import java.sql.*;
20
import java.util.Stack;
21 18

  
22 19
/** 
23 20
 * A Class that creates an XML text document
......
54 51
          String xml = rd.readXMLDocument(docid);
55 52
          System.out.println(xml);
56 53

  
54
        } catch (McdbException me) {
55
          me.toXml(new PrintWriter(System.err));
57 56
        } catch (Exception e) {
58 57
          System.err.println("EXCEPTION HANDLING REQUIRED");
59 58
          System.err.println(e.getMessage());
......
70 69
   * @param conn the database connection from which to read the document
71 70
   */
72 71
  public DBReader( Connection conn ) 
73
                  throws IOException, 
74
                         SQLException, 
75
                         ClassNotFoundException
76 72
  {
77 73
    this.conn = conn;
78 74
  }
......
83 79
   * @param docid the document node contains the root of the document
84 80
   * @returns long the nodeid of the root node for this document
85 81
   */
86
  private long getRootNode(String docid) {
82
  private long getRootNode(String docid) 
83
          throws McdbDocNotFoundException
84
  {
87 85
    // Now look up the root node id
88 86
    long rootnodeid = 0;
89 87

  
......
96 94
      pstmt.setString(1, docid);
97 95

  
98 96
      pstmt.execute();
99
      try {
100 97
        ResultSet rs = pstmt.getResultSet();
101
        try {
102 98
          boolean tableHasRows = rs.next();
103 99
          if (tableHasRows) {
104
            try {
105 100
              rootnodeid = rs.getLong(1);
106 101

  
107
            } catch (SQLException e) {
108
              System.out.println("Error with getLong: " + e.getMessage());
109
            }
110 102
          }
111
        } catch (SQLException e) {
112
          System.out.println("Error with next: " + e.getMessage());
113
        }
114
      } catch (SQLException e) {
115
        System.out.println("Error with getrset: " + e.getMessage());
116
      }
117 103
      pstmt.close();
118 104
    } catch (SQLException e) {
119
      System.out.println("Error getting id: " + e.getMessage());
105
      throw new McdbDocNotFoundException(
106
                "Root node not found for: " + docid, e);
120 107
    }
121 108

  
122 109
    return rootnodeid;
......
128 115
   *
129 116
   * @param docid the document that we want retrieved
130 117
   */
131
  public String readXMLDocument(String docid) {
132
    StringBuffer doc = new StringBuffer();
133
    DoctypeInfo dti = getDoctypeInfo(docid);
118
  public String readXMLDocument(String docid) 
119
         throws McdbException
120
  {
121
    try {
122
      StringBuffer doc = new StringBuffer();
123
      DoctypeInfo dti = getDoctypeInfo(docid);
134 124

  
135
    if (dti != null) {
136 125
      String docname = dti.getDocname();
137 126
      String doctype = dti.getDoctype();
138 127
      String sysid = dti.getSystemID();
139
  
128
    
140 129
      ElementNode element = new ElementNode(conn, getRootNode(docid));
141 130
      doc.append("<?xml version=\"1.0\"?>\n");
142
      
131
        
143 132
      if (docname != null) {
144 133
        if ((doctype != null) && (sysid != null)) {
145 134
          doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype + 
......
149 138
        }
150 139
      }
151 140
      doc.append(element.toString());
152
    } else {
153
      doc.append("<error>Document " + docid + " not found.</error>");
141
  
142
      return (doc.toString());
143

  
144
    } catch (McdbException ex) {
145
      throw ex;
146
    } catch (Throwable t) {
147
      throw new McdbException("Error reading document " + docid + ".");
154 148
    }
155

  
156
    return (doc.toString());
157 149
  }
158 150

  
159 151
  /**
......
161 153
   *
162 154
   * @param docid the id of the document to look up
163 155
   */
164
  public DoctypeInfo getDoctypeInfo(String docid) {
156
  public DoctypeInfo getDoctypeInfo(String docid) 
157
    throws McdbException 
158
  {
165 159
    PreparedStatement pstmt;
166 160
    String doctype = null;
167 161
    String docname = null;
......
202 196
        pstmt.close();
203 197
      }
204 198
    } catch (SQLException e) {
205
      System.out.println("Error getting id: " + e.getMessage());
199
      throw new McdbException("Error accessing database connection.", e);
206 200
    }
207 201

  
208 202
    if (docname != null) {
209 203
      dti = new DoctypeInfo(docname, doctype, sysid);
204
      return dti;
210 205
    } else {
211
      dti = null;
206
      throw new McdbDocNotFoundException("Document not found: " + docid);
212 207
    }
213

  
214
    return dti;
215 208
  }
216 209

  
217 210
  /**
......
257 250

  
258 251
/**
259 252
 * '$Log$
253
 * 'Revision 1.18  2000/08/10 22:39:04  jones
254
 * 'changed getRootNode method from public to private
255
 * '
260 256
 * 'Revision 1.17  2000/06/26 10:35:05  jones
261 257
 * 'Merged in substantial changes to DBWriter and associated classes and to
262 258
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
228 228
      if (sess != null) { sess.invalidate();  }    
229 229
      response.sendRedirect("/xmltodb/lib/login.html"); 
230 230
    // aware of session expiration on every request  
231
    } else {    
231
    } else {   
232 232
      HttpSession sess = request.getSession(true);
233 233
      if (sess.isNew()) { 
234 234
        // session expired or has not been stored b/w user requests
......
546 546
        // Transform the document to the new doctype
547 547
        dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
548 548
      }
549
    } catch (NullPointerException npe) {
549
    //} catch (NullPointerException npe) {
550
      //response.setContentType("text/html");
551
      //out.println("Error getting document ID: " + docidstr +" (" + docid + ")");
552
    } catch (McdbException e) {
553
      response.setContentType("text/xml");
554
      e.toXml(out);
555
    } catch (Throwable t) {
550 556
      response.setContentType("text/html");
551
      out.println("Error getting document ID: " + docidstr +" (" + docid + ")");
552
    } catch (Exception e) {
553
      response.setContentType("text/html");
554
      out.println(e.getMessage());
557
      out.println(t.getMessage());
555 558
    } finally {
556 559
      util.returnConnection(conn);
557 560
    }    
......
867 870

  
868 871
/**
869 872
 * '$Log$
873
 * 'Revision 1.63  2000/08/11 18:25:26  berkley
874
 * 'broke up handleQueryAction into handleQuery, handleSQuery, runQuery and transformDocument
875
 * '
870 876
 * 'Revision 1.61  2000/08/10 18:56:48  bojilova
871 877
 * 'added "anonymous" user connection
872 878
 * '
src/edu/ucsb/nceas/metacat/McdbException.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An Exception thrown when an error occurs because a
4
 *             problem occurred in the metacat database
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 */
13

  
14
package edu.ucsb.nceas.metacat;
15

  
16
import java.io.PrintWriter;
17

  
18
/**
19
 * Exception thrown when an error occurs because a problem occurred in
20
 * the metacat database.  This is the general type of Exception that is
21
 * thrown whenever the server encounters an Error or Exception that is
22
 * outside of the scope of normal operation.  This class may be 
23
 * subclassed to provide more detailed informatin about the error.
24
 */
25
public class McdbException extends Exception {
26

  
27
  /**
28
   * @serial The embedded exception if wrapping an Exception
29
   */
30
  private Exception exception;
31

  
32
  /**
33
   * Create a new McdbException.
34
   */
35
  public McdbException() {
36
    super();
37
    this.exception = null;
38
  }
39

  
40
  /**
41
   * Create a new McdbException.
42
   *
43
   * @param message The error or warning message.
44
   */
45
  public McdbException(String message) {
46
    super(message);
47
    this.exception = null;
48
  }
49

  
50
  /**
51
   * Create a new McdbException.
52
   *
53
   * @param e The exception to tunnel inside this exception
54
   */
55
  public McdbException(Exception e) {
56
    super();
57
    this.exception = e;
58
  }
59

  
60
  /**
61
   * Create a new McdbException.
62
   *
63
   * @param message The error or warning message.
64
   * @param e The exception to tunnel inside this exception
65
   */
66
  public McdbException(String message, Exception e) {
67
    super(message);
68
    this.exception = e;
69
  }
70

  
71
  /**
72
   * Get the tunneled Exception
73
   */
74
  public Exception getException() {
75
    return exception;
76
  }
77

  
78
  /**
79
   * Get the message from this exception.
80
   *
81
   * <p>This returns the message from this exception, but if it
82
   * is null, and if the tunnelled exception is not null, then
83
   * it returns the message fromthe tunnelled exception.
84
   */
85
  public String getMessage() {
86
    String msg = super.getMessage();
87

  
88
    if (msg == null && exception != null) {
89
      return exception.getMessage();
90
    }
91

  
92
    return msg;
93
  }
94

  
95
  /**
96
   * Print the message from this exception in XML format.
97
   *
98
   * <p>This returns the message from this exception, but if it
99
   * is null, and if the tunnelled exception is not null, then
100
   * it returns the message from the tunnelled exception.
101
   */
102
  public void toXml(PrintWriter pw) {
103
    String msg = super.getMessage();
104

  
105
    if (msg == null && exception != null) {
106
      msg = exception.getMessage();
107
    }
108

  
109
    pw.println("<?xml version=\"1.0\"?>");
110
    pw.println("<error>" + msg + "</error>");
111
    pw.flush();
112
  }
113
}
0 114

  
src/edu/ucsb/nceas/metacat/McdbDocNotFoundException.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An Exception thrown when an error occurs because a
4
 *             document with a given ID could not be found in the 
5
 *             metacat database.
6
 *  Copyright: 2000 Regents of the University of California and the
7
 *             National Center for Ecological Analysis and Synthesis
8
 *    Authors: Matt Jones
9
 *
10
 *   '$Author$'
11
 *     '$Date$'
12
 * '$Revision$'
13
 */
14

  
15
package edu.ucsb.nceas.metacat;
16

  
17
/**
18
 * Exception thrown when an error occurs because a document with a
19
 * given ID could not be found in the metacat database.  
20
 */
21
public class McdbDocNotFoundException extends McdbException {
22

  
23
  /**
24
   * Create a new McdbDocNotFoundException.
25
   */
26
  public McdbDocNotFoundException() {
27
    super();
28
  }
29

  
30
  /**
31
   * Create a new McdbDocNotFoundException.
32
   *
33
   * @param message The error or warning message.
34
   */
35
  public McdbDocNotFoundException(String message) {
36
    super(message);
37
  }
38

  
39
  /**
40
   * Create a new McdbDocNotFoundException.
41
   *
42
   * @param e The exception to tunnel inside this exception
43
   */
44
  public McdbDocNotFoundException(Exception e) {
45
    super(e);
46
  }
47

  
48
  /**
49
   * Create a new McdbDocNotFoundException.
50
   *
51
   * @param message The error or warning message.
52
   * @param e The exception to tunnel inside this exception
53
   */
54
  public McdbDocNotFoundException(String message, Exception e) {
55
    super(message, e);
56
  }
57
}
0 58

  

Also available in: Unified diff