001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file ../GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.dbschema;
016:
017: import java.sql.*;
018: import java.util.List;
019: import java.util.ArrayList;
020: import java.util.Properties;
021: import java.io.*;
022: import java.lang.reflect.*;
023: import java.net.InetAddress;
024:
025: import org.xmldb.api.base.*;
026: import org.xmldb.api.modules.*;
027: import org.xmldb.api.*;
028: import javax.xml.transform.OutputKeys;
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.parsers.ParserConfigurationException;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.DOMException;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.SAXParseException;
038: import org.xml.sax.InputSource;
039:
040: import org.griphyn.vdl.util.ChimeraProperties;
041: import org.griphyn.vdl.util.Logging;
042: import org.griphyn.vdl.parser.*;
043: import org.griphyn.vdl.invocation.*;
044: import org.griphyn.common.util.Separator;
045: import org.xml.sax.InputSource;
046:
047: /**
048: * This class provides basic functionalities to interact with the
049: * backend database for invocation records, such as insertion, deletion,
050: * and search.
051: *
052: * @author Jens-S. Vöckler
053: * @author Yong Zhao
054: * @version $Revision: 50 $
055: */
056: public class NXDInvSchema extends DatabaseSchema implements PTC {
057: private DocumentBuilderFactory m_factory;
058:
059: private DocumentBuilder m_builder;
060:
061: protected Collection m_db;
062:
063: protected Collection m_ptc;
064:
065: protected CollectionManagementService m_dbColService;
066:
067: protected CollectionManagementService m_ptcColService;
068:
069: protected XPathQueryService m_dbQrySvc;
070:
071: protected XPathQueryService m_ptcQrySvc;
072:
073: /**
074: * Default constructor for the provenance tracking.
075: *
076: * @param dbDriverName is the database driver name
077: */
078: public NXDInvSchema(String dbDriverName)
079: throws ClassNotFoundException, NoSuchMethodException,
080: InstantiationException, IllegalAccessException,
081: InvocationTargetException, SQLException, IOException,
082: ParserConfigurationException {
083: // load the driver from the properties
084: super (); // call minimalistic c'tor, no driver loading!
085: ChimeraProperties props = ChimeraProperties.instance();
086:
087: m_dbschemaprops = props
088: .getDatabaseSchemaProperties(PROPERTY_PREFIX);
089:
090: // extract those properties specific to the database driver.
091: // use default settings.
092: String driverPrefix = null;
093: String driverName = props.getDatabaseDriverName(driverPrefix);
094: Properties driverprops = props
095: .getDatabaseDriverProperties(driverPrefix);
096: String url = props.getDatabaseURL(driverPrefix);
097:
098: try {
099: m_factory = DocumentBuilderFactory.newInstance();
100: m_builder = m_factory.newDocumentBuilder();
101:
102: Class cl = Class.forName(driverName);
103: Database database = (Database) cl.newInstance();
104: DatabaseManager.registerDatabase(database);
105:
106: // get the collection
107: m_db = DatabaseManager.getCollection(url + "/db");
108: m_dbColService = (CollectionManagementService) m_db
109: .getService("CollectionManagementService", "1.0");
110:
111: m_ptc = m_db.getChildCollection("ptc");
112:
113: if (m_ptc == null) {
114: // collection does not exist, create
115: m_ptc = m_dbColService.createCollection("ptc");
116: }
117: m_ptc.setProperty(OutputKeys.INDENT, "no");
118:
119: m_ptcColService = (CollectionManagementService) m_ptc
120: .getService("CollectionManagementService", "1.0");
121:
122: m_dbQrySvc = (XPathQueryService) m_db.getService(
123: "XPathQueryService", "1.0");
124:
125: m_ptcQrySvc = (XPathQueryService) m_ptc.getService(
126: "XPathQueryService", "1.0");
127:
128: m_dbQrySvc.setProperty("indent", "no");
129:
130: m_ptcQrySvc.setProperty("indent", "no");
131: } catch (XMLDBException e) {
132: throw new SQLException(e.getMessage());
133: }
134: }
135:
136: /**
137: * Checks the existence of an invocation record in the database.
138: * The information is based on the (start,host,pid) tuple, although
139: * with private networks, cases may arise that have this tuple
140: * identical, yet are different.
141: *
142: * @param start is the start time of the grid launcher
143: * @param host is the address of the host it ran upon
144: * @param pid is the process id of the grid launcher itself.
145: * @return the id of the existing record, or -1
146: */
147: public long getInvocationID(java.util.Date start, InetAddress host,
148: int pid) throws SQLException {
149: long result = -1;
150: Logging.instance().log("xaction", 1,
151: "START select invocation id");
152:
153: String xquery = "/invocation[@start='" + start + "']";
154: xquery += "[@host='" + host.getHostAddress() + "']";
155: xquery += "[@pid=" + pid + "]";
156:
157: try {
158: Logging.instance().log("nxd", 2, xquery);
159: ResourceSet rs = m_dbQrySvc.query(xquery);
160: ResourceIterator i = rs.getIterator();
161: if (i.hasMoreResources()) {
162: result = 1;
163: } else {
164: result = -1;
165: }
166: } catch (XMLDBException e) {
167: throw new SQLException(e.getMessage());
168: }
169:
170: Logging.instance().log("xaction", 1,
171: "FINAL select invocation id");
172: return result;
173: }
174:
175: /**
176: * Inserts an invocation record into the database.
177: *
178: * @param ivr is the invocation record to store.
179: * @return true, if insertion was successful, false otherwise.
180: */
181: public boolean saveInvocation(InvocationRecord ivr)
182: throws SQLException {
183: try {
184: StringWriter sw = new StringWriter();
185:
186: ivr.toXML(sw, "", null);
187: // create new XMLResource; an id will be assigned to the new resource
188: XMLResource document = (XMLResource) m_ptc.createResource(
189: null, "XMLResource");
190: document.setContent(sw.toString());
191: System.out.println(sw.toString());
192: m_ptc.storeResource(document);
193: return true;
194: } catch (Exception e) {
195: throw new SQLException(e.getMessage());
196: }
197: }
198: }
|