podd.triples.SesameRepositoryFactory.java Source code

Java tutorial

Introduction

Here is the source code for podd.triples.SesameRepositoryFactory.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PODD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.triples;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openrdf.repository.RepositoryException;
import org.springframework.beans.factory.DisposableBean;
import podd.triples.impl.SesameLocalRepoWrapperImpl;
import podd.triples.impl.SesameRemoteRepoWrapperImpl;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.Collections.synchronizedMap;
import static podd.triples.SesameRepoWrapper.DEFAULT_REPO_DIR_NAME;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */
public class SesameRepositoryFactory implements DisposableBean {
    private static final Logger LOGGER = LoggerFactory.getLogger(SesameRepositoryFactory.class);

    private static final SesameRepositoryFactory SINGLETON = new SesameRepositoryFactory();

    private Map<String, SesameRepoWrapper> map;

    public static SesameRepositoryFactory getSesameRepositoryFactory() {
        return SINGLETON;
    }

    private SesameRepositoryFactory() {
        map = synchronizedMap(new LinkedHashMap<String, SesameRepoWrapper>());
    }

    public SesameRepoWrapper createLocalSesameRepo(String repoPath, String indexOps)
            throws RepositoryException, IOException {
        final File file = new File(repoPath);
        final String path = file.getAbsolutePath();
        if (map.containsKey(path)) {
            return map.get(path);
        } else {
            final SesameRepoWrapper wrapper = createRepo(repoPath, indexOps);
            map.put(path, wrapper);
            return wrapper;
        }
    }

    public SesameRepoWrapper createRemoteSesameRepo(String serverAddress, String repoID)
            throws IOException, RepositoryException {
        try {
            URL repoURL = new URL(new URL(serverAddress), repoID);
            if (map.containsKey(repoURL.toString())) {
                return map.get(repoURL.toString());
            } else {
                SesameRepoWrapper wrapper = new SesameRemoteRepoWrapperImpl(serverAddress, repoID);
                map.put(repoURL.toString(), wrapper);
                return wrapper;
            }
        } catch (MalformedURLException e) {
            throw new IOException("Invalid Sesame remote repository address: <" + serverAddress + ", " + repoID, e);
        }
    }

    public SesameRepoWrapper createSesameRepo(String repoPath) throws RepositoryException, IOException {
        final File file = new File(repoPath);
        final String path = file.getAbsolutePath();
        if (map.containsKey(path)) {
            return map.get(path);
        } else {
            final SesameRepoWrapper wrapper = createRepo(repoPath);
            map.put(path, wrapper);
            return wrapper;
        }
    }

    public SesameRepoWrapper createSesameRepo() throws RepositoryException, IOException {
        final File file = new File(System.getProperty("java.io.tmpdir") + DEFAULT_REPO_DIR_NAME);
        final String path = file.getAbsolutePath();
        if (map.containsKey(path)) {
            return map.get(path);
        } else {
            final SesameRepoWrapper wrapper = createRepo();
            map.put(path, wrapper);
            return wrapper;
        }
    }

    private SesameRepoWrapper createRepo(String... opts) throws IOException, RepositoryException {
        SesameRepoWrapper wrapper;
        if (opts.length == 0) {
            wrapper = new SesameLocalRepoWrapperImpl();
        } else if (opts.length == 1) {
            wrapper = new SesameLocalRepoWrapperImpl(opts[0]);
        } else {
            wrapper = new SesameLocalRepoWrapperImpl(opts[0], opts[1]);
        }
        return wrapper;
    }

    @Override
    public void destroy() throws Exception {
        for (SesameRepoWrapper repo : map.values()) {
            try {
                repo.close();
            } catch (Exception e) {
                LOGGER.error("Error shutting down Sesame repo: " + repo.getRepoPath().toString(), e);
            }
        }
    }
}