src/main/java/au/id/djc/jena/util/SdbTemplate.java (2342B) - raw
1 package au.id.djc.jena.util; 2 3 import java.sql.SQLException; 4 import java.util.Map; 5 6 import javax.sql.DataSource; 7 8 import org.springframework.jmx.export.annotation.ManagedOperation; 9 import org.springframework.jmx.export.annotation.ManagedResource; 10 11 import com.hp.hpl.jena.rdf.model.Model; 12 import com.hp.hpl.jena.sdb.SDBFactory; 13 import com.hp.hpl.jena.sdb.Store; 14 import com.hp.hpl.jena.sdb.StoreDesc; 15 import com.hp.hpl.jena.sdb.graph.PrefixMappingSDB; 16 import com.hp.hpl.jena.sdb.sql.SDBConnection; 17 18 @ManagedResource 19 public class SdbTemplate implements ModelOperations { 20 21 private final DataSource dataSource; 22 private final StoreDesc storeDesc; 23 24 public SdbTemplate(DataSource dataSource, StoreDesc storeDesc) { 25 this.dataSource = dataSource; 26 this.storeDesc = storeDesc; 27 } 28 29 private Store create() { 30 try { 31 return SDBFactory.connectStore(new SDBConnection(dataSource), storeDesc); 32 } catch (SQLException e) { 33 throw new RuntimeException(e); 34 } 35 } 36 37 @Override 38 public <T> T withModel(ModelExecutionCallback<T> callback) { 39 Store store = create(); 40 store.getLoader().setUseThreading(false); 41 try { 42 Model model = SDBFactory.connectDefaultModel(store); 43 try { 44 return callback.execute(model); 45 } finally { 46 model.close(); 47 } 48 } finally { 49 store.getConnection().close(); 50 store.close(); 51 } 52 } 53 54 @ManagedOperation(description = "Creates the tables and indices necessary for SDB") 55 public void format() { 56 Store store = create(); 57 store.getLoader().setUseThreading(false); 58 try { 59 store.getTableFormatter().create(); 60 } finally { 61 store.close(); 62 } 63 } 64 65 @ManagedOperation 66 public Map<String, String> getPrefixMappings() throws SQLException { 67 PrefixMappingSDB mapping = new PrefixMappingSDB(null, new SDBConnection(dataSource)); 68 return mapping.getNsPrefixMap(); 69 } 70 71 @ManagedOperation 72 public void addPrefixMapping(String prefix, String uri) throws SQLException { 73 PrefixMappingSDB mapping = new PrefixMappingSDB(null, new SDBConnection(dataSource)); 74 mapping.setNsPrefix(prefix, uri); 75 } 76 77 }