1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.openrdf.elmo.tapestry;
30
31 import info.aduna.platform.Platform;
32 import info.aduna.platform.PlatformFactory;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.net.URL;
37
38 import org.apache.hivemind.ApplicationRuntimeException;
39 import org.apache.hivemind.Resource;
40 import org.apache.hivemind.events.RegistryShutdownListener;
41 import org.openrdf.model.ValueFactory;
42 import org.openrdf.repository.Repository;
43 import org.openrdf.repository.RepositoryConnection;
44 import org.openrdf.repository.RepositoryException;
45 import org.openrdf.repository.config.RepositoryConfig;
46 import org.openrdf.repository.config.RepositoryConfigException;
47 import org.openrdf.repository.config.RepositoryConfigUtil;
48 import org.openrdf.repository.manager.LocalRepositoryManager;
49 import org.openrdf.repository.sail.config.SailRepositoryConfig;
50 import org.openrdf.repository.threadproxy.ThreadProxyRepository;
51 import org.openrdf.rio.RDFFormat;
52 import org.openrdf.rio.RDFParseException;
53 import org.openrdf.sail.memory.config.MemoryStoreConfig;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63 public class HiveMindSesameRepository implements Repository,
64 RegistryShutdownListener {
65
66 private final Logger logger = LoggerFactory
67 .getLogger(HiveMindSesameRepository.class);
68
69 private String applicationId = "elmo-tapestry";
70
71 private File dataDir;
72
73 private String repositoryId = "default";
74
75 private Resource initData;
76
77 private Repository repository;
78
79 public HiveMindSesameRepository(Repository repository) {
80 this.repository = repository;
81 }
82
83 public void setApplicationId(String applicationId) {
84 this.applicationId = applicationId;
85 }
86
87 public File getDataDir() {
88 if (repository == null)
89 return dataDir;
90 return repository.getDataDir();
91 }
92
93 public void setDataDir(File dataDir) {
94 this.dataDir = dataDir;
95 if (repository != null) {
96 repository.setDataDir(dataDir);
97 }
98 }
99
100 public void setInitData(Resource initData) {
101 this.initData = initData;
102 }
103
104 public void setRepositoryId(String repositoryId) {
105 this.repositoryId = repositoryId;
106 }
107
108 public void initialize() {
109 Platform platform = PlatformFactory.getPlatform();
110 File dir = platform.getApplicationDataDir(applicationId);
111 if (dataDir != null) {
112 dir = dataDir;
113 }
114 logger.info("Using data dir: {}", dir);
115 try {
116 repository = getRepository(dir, repositoryId);
117 if (initData != null) {
118 RepositoryConnection conn = repository.getConnection();
119 try {
120 if (conn.isEmpty()) {
121 URL data = initData.getResourceURL();
122 conn.add(data, "", RDFFormat
123 .forFileName(data.getFile()));
124 }
125 } finally {
126 conn.close();
127 }
128 }
129 } catch (IOException e) {
130 throw new ApplicationRuntimeException(e);
131 } catch (RDFParseException e) {
132 throw new ApplicationRuntimeException(e);
133 } catch (RepositoryException e) {
134 throw new ApplicationRuntimeException(e);
135 } catch (RepositoryConfigException e) {
136 throw new ApplicationRuntimeException(e);
137 }
138 }
139
140 protected Repository getRepository(File dir, String id)
141 throws RepositoryException, RepositoryConfigException {
142 LocalRepositoryManager manager = new LocalRepositoryManager(dir);
143 manager.initialize();
144 Repository repository = manager.getRepository(id);
145 if (repository == null) {
146 logger.warn("Creating repository configuration for: {}", id);
147 MemoryStoreConfig mc = new MemoryStoreConfig();
148 mc.setPersist(true);
149 SailRepositoryConfig sc = new SailRepositoryConfig(mc);
150 RepositoryConfig config = new RepositoryConfig(id, sc);
151 Repository system = manager.getSystemRepository();
152 RepositoryConfigUtil.updateRepositoryConfigs(system, config);
153 repository = manager.getRepository(id);
154 }
155 repository = new ThreadProxyRepository(repository);
156 return repository;
157 }
158
159 public ValueFactory getValueFactory() {
160 return repository.getValueFactory();
161 }
162
163 public boolean isWritable() throws RepositoryException {
164 return repository.isWritable();
165 }
166
167 public RepositoryConnection getConnection() throws RepositoryException {
168 return repository.getConnection();
169 }
170
171 public void registryDidShutdown() {
172 try {
173 repository.shutDown();
174 } catch (RepositoryException e) {
175 throw new ApplicationRuntimeException(e);
176 }
177 }
178
179 public void shutDown() throws RepositoryException {
180 repository.shutDown();
181 }
182
183 }