commit bf7024b3f7cbc21b66ac531e633cfb9a7ac314a2 Author: Dan Callaghan <djc@djc.id.au> Date: Sun, 28 Mar 2010 21:46:17 +1000 initial revision (copied from books) Diffstat:
A | pom.xml | | | 123 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/main/java/org/deadlit/rdf/util/SdbTemplate.java | | | 89 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 212 insertions(+), 0 deletions(-) diff --git a/pom.xml b/pom.xml @@ -0,0 +1,123 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.deadlit</groupId> + <artifactId>jena-spring</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + + <name>Jena Spring integration</name> + <url>http://code.deadlit.org/</url> + <organization> + <name>deadlit.org</name> + <url>http://deadlit.org/</url> + </organization> + + <properties> + <spring.version>3.0.1.RELEASE</spring.version> + </properties> + + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.6</source> + <target>1.6</target> + <showDeprecation>true</showDeprecation> + <showWarnings>true</showWarnings> + <encoding>UTF-8</encoding> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-resources-plugin</artifactId> + <configuration> + <encoding>UTF-8</encoding> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <id>attach-sources</id> + <goals> + <goal>jar</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </pluginManagement> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.7</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>au.com.miskinhill.jena</groupId> + <artifactId>jena</artifactId> + <version>2.6.2.1</version> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + </exclusion> + <exclusion> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>com.hp.hpl.jena</groupId> + <artifactId>sdb</artifactId> + <version>1.3.1</version> + <exclusions> + <exclusion> + <groupId>com.hp.hpl.jena</groupId> + <artifactId>jena</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jdk14</artifactId> + <version>1.5.0</version> + <scope>runtime</scope> + </dependency> + </dependencies> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>com.hp.hpl.jena</groupId> + <artifactId>arq</artifactId> + <version>2.8.1</version> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + </exclusion> + <exclusion> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> + </dependencyManagement> + +</project> diff --git a/src/main/java/org/deadlit/rdf/util/SdbTemplate.java b/src/main/java/org/deadlit/rdf/util/SdbTemplate.java @@ -0,0 +1,89 @@ +package org.deadlit.rdf.util; + +import java.sql.SQLException; +import java.util.Map; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jmx.export.annotation.ManagedOperation; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.stereotype.Component; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.sdb.SDBFactory; +import com.hp.hpl.jena.sdb.Store; +import com.hp.hpl.jena.sdb.StoreDesc; +import com.hp.hpl.jena.sdb.graph.PrefixMappingSDB; +import com.hp.hpl.jena.sdb.sql.SDBConnection; + +@ManagedResource +public class SdbTemplate { + + public static interface ModelExecutionCallback<T> { + T execute(Model model); + } + public static abstract class ModelExecutionCallbackWithoutResult implements ModelExecutionCallback<Object> { + @Override + final public Object execute(Model model) { + executeWithoutResult(model); + return null; + } + protected abstract void executeWithoutResult(Model model); + } + + private final DataSource dataSource; + private final StoreDesc storeDesc; + + public SdbTemplate(DataSource dataSource, StoreDesc storeDesc) { + this.dataSource = dataSource; + this.storeDesc = storeDesc; + } + + private Store create() { + try { + return SDBFactory.connectStore(new SDBConnection(dataSource), storeDesc); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public <T> T withModel(ModelExecutionCallback<T> callback) { + Store store = create(); + store.getLoader().setUseThreading(false); + try { + Model model = SDBFactory.connectDefaultModel(store); + try { + return callback.execute(model); + } finally { + model.close(); + } + } finally { + store.close(); + } + } + + @ManagedOperation(description = "Creates the tables and indices necessary for SDB") + public void format() { + Store store = create(); + store.getLoader().setUseThreading(false); + try { + store.getTableFormatter().create(); + } finally { + store.close(); + } + } + + @ManagedOperation + public Map<String, String> getPrefixMappings() throws SQLException { + PrefixMappingSDB mapping = new PrefixMappingSDB(null, new SDBConnection(dataSource)); + return mapping.getNsPrefixMap(); + } + + @ManagedOperation + public void addPrefixMapping(String prefix, String uri) throws SQLException { + PrefixMappingSDB mapping = new PrefixMappingSDB(null, new SDBConnection(dataSource)); + mapping.setNsPrefix(prefix, uri); + } + +}