001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: GraphRDBMaker.java,v 1.26 2008/01/02 12:08:23 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import com.hp.hpl.jena.db.GraphRDB;
010: import com.hp.hpl.jena.db.IDBConnection;
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.graph.impl.*;
013: import com.hp.hpl.jena.shared.*;
014: import com.hp.hpl.jena.util.CollectionFactory;
015: import com.hp.hpl.jena.util.iterator.*;
016: import com.hp.hpl.jena.vocabulary.*;
017:
018: import java.rmi.server.UID;
019: import java.util.*;
020:
021: /**
022: A GraphFactory that produces Graphs from database connections.
023: The connection is supplied when the factory is constructed. All the
024: created graphs are tracked so that we can supply a removeAll call
025: to dispose of them.
026:
027: @author kers
028: */
029:
030: public class GraphRDBMaker extends BaseGraphMaker {
031: protected IDBConnection c;
032: protected Set created = CollectionFactory.createHashedSet();
033: int reificationStyle;
034:
035: /**
036: Construct a new GraphRDB factory based on the supplied DB connection.
037: @param c the database connection
038: */
039: public GraphRDBMaker(IDBConnection c, ReificationStyle style) {
040: super (style);
041: this .c = c;
042: this .reificationStyle = GraphRDB.styleRDB(style);
043: }
044:
045: /**
046: Answer the RDFS class of this RDB GraphMaker
047: @return JenaModelSpec.RDBMakerClass [as node]
048: */
049: public Node getMakerClass() {
050: return JenaModelSpec.RDBMakerSpec.asNode();
051: }
052:
053: /**
054: Augment the maker description of this maker with RDB-specific properties.
055: TODO do this
056: */
057: protected void augmentDescription(Graph g, Node self) {
058: }
059:
060: /**
061: Answer the default graph of this Maker; make it if necessary.
062: */
063: public Graph getGraph() {
064: if (defaultGraph == null)
065: defaultGraph = consGraph(null, !c.containsDefaultModel());
066: return defaultGraph;
067: }
068:
069: /**
070: The default graph for this maker, or null if there isn't one.
071: */
072: protected Graph defaultGraph = null;
073:
074: public Graph openGraph() {
075: if (defaultGraph != null)
076: return defaultGraph;
077: if (c.containsDefaultModel())
078: return defaultGraph = consGraph(null, false);
079: throw new DoesNotExistException(
080: "no default graph in this GraphMaker");
081: }
082:
083: /**
084: Answer an "anonymous", freshly-created graph. We fake this by creating
085: a graph with the name "anon_"+UID().toString. This may lead to problems
086: later; eg such a graph may need to be deleted when the connection is
087: closed.
088:
089: TODO resolve this issue.
090: */
091: public Graph createGraph() {
092: return createGraph(freshGraphName(), false);
093: }
094:
095: /**
096: Answer a freshly-synthesised "anonymous" name.
097: */
098: public String freshGraphName() {
099: return "anon_" + new UID().toString();
100: }
101:
102: /**
103: Create an RDB graph and remember its name.
104: */
105: public Graph createGraph(String name, boolean strict) {
106: created.add(name);
107: boolean fresh = strict || !hasGraph(name);
108: return consGraph(name, fresh);
109: }
110:
111: /**
112: Open an existing graph; if there's no such graph, but failIfAbsent is
113: false, create a new one. In any case, return that graph.
114: */
115: public Graph openGraph(String name, boolean strict) {
116: boolean fresh = hasGraph(name) == false && strict == false;
117: if (fresh)
118: created.add(name);
119: return consGraph(name, fresh);
120: }
121:
122: protected Graph consGraph(String name, boolean fresh) {
123: Graph p = c.getDefaultModelProperties().getGraph();
124: return new GraphRDB(c, name, (fresh ? p : null),
125: reificationStyle, fresh);
126: }
127:
128: /**
129: Remove a graph from the database - at present, this has to be done by
130: opening it first.
131:
132: */
133: public void removeGraph(String name) {
134: GraphRDB toDelete = (GraphRDB) openGraph(name, true);
135: toDelete.remove();
136: toDelete.close();
137: created.remove(name);
138: }
139:
140: /**
141: Return true iff there's a graph with the given name.
142: */
143: public boolean hasGraph(String name) {
144: return c.containsModel(name);
145: }
146:
147: /**
148: Remove all the graphs that have been created by this factory.
149: */
150: public void removeAll() {
151: Iterator it = CollectionFactory.createHashedSet(created)
152: .iterator();
153: while (it.hasNext())
154: removeGraph((String) it.next());
155: }
156:
157: public void close() { /* should consider - do we close the connection or not? */
158: }
159:
160: public ExtendedIterator listGraphs() {
161: return c.getAllModelNames().filterDrop(filterDEFAULT);
162: }
163:
164: private Filter filterDEFAULT = new Filter() {
165: public boolean accept(Object x) {
166: return "DEFAULT".equals(x);
167: }
168: };
169: }
170:
171: /*
172: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
173: All rights reserved.
174:
175: Redistribution and use in source and binary forms, with or without
176: modification, are permitted provided that the following conditions
177: are met:
178:
179: 1. Redistributions of source code must retain the above copyright
180: notice, this list of conditions and the following disclaimer.
181:
182: 2. Redistributions in binary form must reproduce the above copyright
183: notice, this list of conditions and the following disclaimer in the
184: documentation and/or other materials provided with the distribution.
185:
186: 3. The name of the author may not be used to endorse or promote products
187: derived from this software without specific prior written permission.
188:
189: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
190: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
193: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
194: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
195: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
196: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
197: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
198: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199: */
|