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.sail.config;
007:
008: import org.openrdf.repository.Repository;
009: import org.openrdf.repository.config.RepositoryConfigException;
010: import org.openrdf.repository.config.RepositoryFactory;
011: import org.openrdf.repository.config.RepositoryImplConfig;
012: import org.openrdf.repository.sail.SailRepository;
013: import org.openrdf.sail.Sail;
014: import org.openrdf.sail.StackableSail;
015: import org.openrdf.sail.config.DelegatingSailImplConfig;
016: import org.openrdf.sail.config.SailConfigException;
017: import org.openrdf.sail.config.SailFactory;
018: import org.openrdf.sail.config.SailImplConfig;
019: import org.openrdf.sail.config.SailRegistry;
020:
021: /**
022: * A {@link RepositoryFactory} that creates {@link SailRepository}s based on
023: * RDF configuration data.
024: *
025: * @author Arjohn Kampman
026: */
027: public class SailRepositoryFactory implements RepositoryFactory {
028:
029: /*-----------*
030: * Constants *
031: *-----------*/
032:
033: /**
034: * The type of repositories that are created by this factory.
035: *
036: * @see RepositoryFactory#getRepositoryType()
037: */
038: public static final String REPOSITORY_TYPE = "openrdf:SailRepository";
039:
040: /*---------*
041: * Methods *
042: *---------*/
043:
044: /**
045: * Returns the repository's type: <tt>openrdf:SailRepository</tt>.
046: */
047: public String getRepositoryType() {
048: return REPOSITORY_TYPE;
049: }
050:
051: public RepositoryImplConfig getConfig() {
052: return new SailRepositoryConfig();
053: }
054:
055: public Repository getRepository(RepositoryImplConfig config)
056: throws RepositoryConfigException {
057: if (config instanceof SailRepositoryConfig) {
058: SailRepositoryConfig sailRepConfig = (SailRepositoryConfig) config;
059:
060: try {
061: Sail sail = createSailStack(sailRepConfig
062: .getSailImplConfig());
063: return new SailRepository(sail);
064: } catch (SailConfigException e) {
065: throw new RepositoryConfigException(e.getMessage(), e);
066: }
067: }
068:
069: throw new RepositoryConfigException(
070: "Invalid configuration class: " + config.getClass());
071: }
072:
073: private Sail createSailStack(SailImplConfig config)
074: throws RepositoryConfigException, SailConfigException {
075: Sail sail = createSail(config);
076:
077: if (config instanceof DelegatingSailImplConfig) {
078: SailImplConfig delegateConfig = ((DelegatingSailImplConfig) config)
079: .getDelegate();
080: if (delegateConfig != null) {
081: addDelegate(delegateConfig, sail);
082: }
083: }
084:
085: return sail;
086: }
087:
088: private Sail createSail(SailImplConfig config)
089: throws RepositoryConfigException, SailConfigException {
090: SailFactory sailFactory = SailRegistry.getInstance().get(
091: config.getType());
092:
093: if (sailFactory != null) {
094: return sailFactory.getSail(config);
095: }
096:
097: throw new RepositoryConfigException("Unsupported Sail type: "
098: + config.getType());
099: }
100:
101: private void addDelegate(SailImplConfig config, Sail sail)
102: throws RepositoryConfigException, SailConfigException {
103: Sail delegateSail = createSailStack(config);
104:
105: try {
106: ((StackableSail) sail).setBaseSail(delegateSail);
107: } catch (ClassCastException e) {
108: throw new RepositoryConfigException(
109: "Delegate configured but " + sail.getClass()
110: + " is not a StackableSail");
111: }
112: }
113: }
|