001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.config;
007:
008: import static org.openrdf.repository.config.RepositoryConfigSchema.REPOSITORYTYPE;
009:
010: import org.openrdf.model.BNode;
011: import org.openrdf.model.Graph;
012: import org.openrdf.model.Literal;
013: import org.openrdf.model.Resource;
014: import org.openrdf.model.util.GraphUtil;
015: import org.openrdf.model.util.GraphUtilException;
016:
017: /**
018: * @author Herko ter Horst
019: */
020: public class RepositoryImplConfigBase implements RepositoryImplConfig {
021:
022: private String type;
023:
024: /**
025: * Create a new RepositoryConfigImpl.
026: */
027: public RepositoryImplConfigBase() {
028: }
029:
030: /**
031: * Create a new RepositoryConfigImpl.
032: */
033: public RepositoryImplConfigBase(String type) {
034: this ();
035: setType(type);
036: }
037:
038: public String getType() {
039: return type;
040: }
041:
042: public void setType(String type) {
043: this .type = type;
044: }
045:
046: public void validate() throws RepositoryConfigException {
047: if (type == null) {
048: throw new RepositoryConfigException(
049: "No type specified for repository implementation");
050: }
051: }
052:
053: public Resource export(Graph graph) {
054: BNode implNode = graph.getValueFactory().createBNode();
055:
056: if (type != null) {
057: graph.add(implNode, REPOSITORYTYPE, graph.getValueFactory()
058: .createLiteral(type));
059: }
060:
061: return implNode;
062: }
063:
064: public void parse(Graph graph, Resource implNode)
065: throws RepositoryConfigException {
066: try {
067: Literal typeLit = GraphUtil.getOptionalObjectLiteral(graph,
068: implNode, REPOSITORYTYPE);
069: if (typeLit != null) {
070: setType(typeLit.getLabel());
071: }
072: } catch (GraphUtilException e) {
073: throw new RepositoryConfigException(e.getMessage(), e);
074: }
075: }
076:
077: public static RepositoryImplConfig create(Graph graph,
078: Resource implNode) throws RepositoryConfigException {
079: try {
080: Literal typeLit = GraphUtil.getOptionalObjectLiteral(graph,
081: implNode, REPOSITORYTYPE);
082:
083: if (typeLit != null) {
084: RepositoryFactory factory = RepositoryRegistry
085: .getInstance().get(typeLit.getLabel());
086:
087: if (factory == null) {
088: throw new RepositoryConfigException(
089: "Unsupported repository type: "
090: + typeLit.getLabel());
091: }
092:
093: RepositoryImplConfig implConfig = factory.getConfig();
094: implConfig.parse(graph, implNode);
095: return implConfig;
096: }
097:
098: return null;
099: } catch (GraphUtilException e) {
100: throw new RepositoryConfigException(e.getMessage(), e);
101: }
102: }
103: }
|