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.REPOSITORY;
009: import static org.openrdf.repository.config.RepositoryConfigSchema.REPOSITORYID;
010: import static org.openrdf.repository.config.RepositoryConfigSchema.REPOSITORYIMPL;
011:
012: import org.openrdf.model.BNode;
013: import org.openrdf.model.Graph;
014: import org.openrdf.model.Literal;
015: import org.openrdf.model.Resource;
016: import org.openrdf.model.ValueFactory;
017: import org.openrdf.model.util.GraphUtil;
018: import org.openrdf.model.util.GraphUtilException;
019: import org.openrdf.model.vocabulary.RDF;
020: import org.openrdf.model.vocabulary.RDFS;
021:
022: /**
023: * @author Arjohn Kampman
024: */
025: public class RepositoryConfig {
026:
027: private String id;
028:
029: private String title;
030:
031: private RepositoryImplConfig implConfig;
032:
033: /**
034: * Create a new RepositoryConfig.
035: */
036: public RepositoryConfig() {
037: }
038:
039: /**
040: * Create a new RepositoryConfigImpl.
041: */
042: public RepositoryConfig(String id) {
043: this ();
044: setID(id);
045: }
046:
047: /**
048: * Create a new RepositoryConfigImpl.
049: */
050: public RepositoryConfig(String id, RepositoryImplConfig implConfig) {
051: this (id);
052: setRepositoryImplConfig(implConfig);
053: }
054:
055: /**
056: * Create a new RepositoryConfigImpl.
057: */
058: public RepositoryConfig(String id, String title) {
059: this (id);
060: setTitle(title);
061: }
062:
063: /**
064: * Create a new RepositoryConfigImpl.
065: */
066: public RepositoryConfig(String id, String title,
067: RepositoryImplConfig implConfig) {
068: this (id, title);
069: setRepositoryImplConfig(implConfig);
070: }
071:
072: public String getID() {
073: return id;
074: }
075:
076: public void setID(String id) {
077: this .id = id;
078: }
079:
080: public String getTitle() {
081: return title;
082: }
083:
084: public void setTitle(String title) {
085: this .title = title;
086: }
087:
088: public RepositoryImplConfig getRepositoryImplConfig() {
089: return implConfig;
090: }
091:
092: public void setRepositoryImplConfig(RepositoryImplConfig implConfig) {
093: this .implConfig = implConfig;
094: }
095:
096: /**
097: * Validates this configuration. A {@link RepositoryConfigException} is
098: * thrown when the configuration is invalid. The exception should contain an
099: * error message that indicates why the configuration is invalid.
100: *
101: * @throws RepositoryConfigException
102: * If the configuration is invalid.
103: */
104: public void validate() throws RepositoryConfigException {
105: if (id == null) {
106: throw new RepositoryConfigException("Repository ID missing");
107: }
108: if (implConfig == null) {
109: throw new RepositoryConfigException(
110: "Repository implementation for repository missing");
111: }
112: implConfig.validate();
113: }
114:
115: public void export(Graph graph) {
116: ValueFactory vf = graph.getValueFactory();
117:
118: BNode repositoryNode = vf.createBNode();
119:
120: graph.add(repositoryNode, RDF.TYPE, REPOSITORY);
121:
122: if (id != null) {
123: graph.add(repositoryNode, REPOSITORYID, vf
124: .createLiteral(id));
125: }
126: if (title != null) {
127: graph.add(repositoryNode, RDFS.LABEL, vf
128: .createLiteral(title));
129: }
130: if (implConfig != null) {
131: Resource implNode = implConfig.export(graph);
132: graph.add(repositoryNode, REPOSITORYIMPL, implNode);
133: }
134: }
135:
136: public void parse(Graph graph, Resource repositoryNode)
137: throws RepositoryConfigException {
138: try {
139: Literal idLit = GraphUtil.getOptionalObjectLiteral(graph,
140: repositoryNode, REPOSITORYID);
141: if (idLit != null) {
142: setID(idLit.getLabel());
143: }
144:
145: Literal titleLit = GraphUtil.getOptionalObjectLiteral(
146: graph, repositoryNode, RDFS.LABEL);
147: if (titleLit != null) {
148: setTitle(titleLit.getLabel());
149: }
150:
151: Resource implNode = GraphUtil.getOptionalObjectResource(
152: graph, repositoryNode, REPOSITORYIMPL);
153: if (implNode != null) {
154: setRepositoryImplConfig(RepositoryImplConfigBase
155: .create(graph, implNode));
156: }
157: } catch (GraphUtilException e) {
158: throw new RepositoryConfigException(e.getMessage(), e);
159: }
160: }
161:
162: /**
163: * Creates a new <tt>RepositoryConfig</tt> object and initializes it by
164: * supplying the <tt>graph</tt> and <tt>repositoryNode</tt> to its
165: * {@link #parse(Graph, Resource) parse} method.
166: *
167: * @param graph
168: * @param repositoryNode
169: * @return
170: * @throws RepositoryConfigException
171: */
172: public static RepositoryConfig create(Graph graph,
173: Resource repositoryNode) throws RepositoryConfigException {
174: RepositoryConfig config = new RepositoryConfig();
175: config.parse(graph, repositoryNode);
176: return config;
177: }
178: }
|