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

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


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

package org.jsimpledb.spring;

import com.google.common.base.Preconditions;

import org.jsimpledb.DefaultStorageIdGenerator;
import org.jsimpledb.JSimpleDB;
import org.jsimpledb.JSimpleDBFactory;
import org.jsimpledb.StorageIdGenerator;
import org.jsimpledb.core.Database;
import org.jsimpledb.core.FieldType;
import org.jsimpledb.kv.KVDatabase;
import org.jsimpledb.kv.simple.SimpleKVDatabase;
import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * Factory bean created by <jsimpledb:jsimpledb> tags.
 */
class JSimpleDBFactoryBean extends AbstractFactoryBean {

    private KVDatabase kvstore;
    private int schemaVersion;
    private StorageIdGenerator storageIdGenerator = new DefaultStorageIdGenerator();
    private Iterable> modelClasses;
    private Iterable>> fieldTypeClasses;

// Properties

    public void setKVStore(KVDatabase kvstore) {
        this.kvstore = kvstore;
    }

    public void setSchemaVersion(int schemaVersion) {
        this.schemaVersion = schemaVersion;
    }

    public void setStorageIdGenerator(StorageIdGenerator storageIdGenerator) {
        this.storageIdGenerator = storageIdGenerator;
    }

    public void setModelClasses(Iterable> modelClasses) {
        this.modelClasses = modelClasses;
    }

    public void setFieldTypeClasses(Iterable>> fieldTypeClasses) {
        this.fieldTypeClasses = fieldTypeClasses;
    }

// InitializingBean

    @Override
    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        Preconditions.checkState(this.modelClasses != null, "no modelClasss configured");
    }

// AbstractFactoryBean

    @Override
    public Class getObjectType() {
        return JSimpleDB.class;
    }

    @Override
    protected JSimpleDB createInstance() {

        // Apply defaults
        KVDatabase kvstore1 = this.kvstore;
        int schemaVersion1 = this.schemaVersion;
        if (kvstore1 == null) {
            kvstore1 = new SimpleKVDatabase();
            if (schemaVersion1 == 0)
                schemaVersion1 = 1;
        }
        final Database db = new Database(kvstore1);

        // Add custom field types
        if (this.fieldTypeClasses != null)
            db.getFieldTypeRegistry().addClasses(this.fieldTypeClasses);

        // Build JSimpleDB
        return new JSimpleDBFactory()
          .setDatabase(db)
          .setSchemaVersion(schemaVersion1)
          .setStorageIdGenerator(this.storageIdGenerator)
          .setModelClasses(this.modelClasses)
          .newJSimpleDB();
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy