All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsimpledb.spring.SpringXMLKVDatabase Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.spring;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.dellroad.stuff.io.StreamRepository;
import org.jsimpledb.kv.simple.XMLKVDatabase;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

/**
 * {@link XMLKVDatabase} that adds support for loading the default initial content from any Spring {@link Resource}.
 * For example, this allows initial content to be loaded from the classpath.
 *
 * @see XMLKVDatabase
 */
public class SpringXMLKVDatabase extends XMLKVDatabase {

    private Resource initialContentResource;

// Constructors

    /**
     * Normal constructor. Uses a {@link org.dellroad.stuff.io.FileStreamRepository} backed by the specified file.
     *
     * @param file persistent XML file
     * @throws IllegalArgumentException if {@code file} is null
     */
    public SpringXMLKVDatabase(File file) {
        super(file);
    }

    /**
     * Constructor allowing storage in any user-supplied {@link StreamRepository}.
     *
     * @param repository XML file storage
     * @throws IllegalArgumentException if {@code file} is null
     */
    public SpringXMLKVDatabase(StreamRepository repository) {
        super(repository);
    }

    /**
     * Configure the {@link Resource} containing default initial content for an uninitialized database. This method is invoked
     * by {@link #getInitialContent} when, on the first load, the backing XML file is not found.
     *
     * @param initialContentResource resource containing default initial XML database content, or null for none
     */
    public void setInitialContentResource(Resource initialContentResource) {
        this.initialContentResource = initialContentResource;
    }

    /**
     * Configure the {@link Resource} containing default initial content for an uninitialized database from the specified file.
     *
     * @param initialContentFile file containing default initial XML database content, or null for none
     */
    @Override
    public void setInitialContentFile(File initialContentFile) {
        this.setInitialContentResource(initialContentFile != null ? new FileSystemResource(initialContentFile) : null);
    }

    /**
     * Get the initial content for an uninitialized database. This method is invoked when, on the first load,
     * the backing XML file is not found. It should return a stream that reads initial content for the database,
     * if any, otherwise null.
     *
     * 

* The implementation in {@link SpringXMLKVDatabase} returns an {@link InputStream} acquired from the {@link Resource} * configured by {@link #setInitialContentResource setInitialContentResource()}, if any, otherwise null. * * @return default initial XML database content, or null for none */ @Override protected InputStream getInitialContent() throws IOException { return this.initialContentResource != null ? this.initialContentResource.getInputStream() : null; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy