View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
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   * HiveMind aware Repository, the repository is closed when HiveMind shutdowns.
59   * 
60   * @author James Leigh
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 }