FileDocCategorySizeDatePackage
DB4oController.javaAPI DocApache Lucene 2.1.013227Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.storage.db4o

DB4oController

public class DB4oController extends Object implements org.apache.lucene.gdata.server.registry.ScopeVisitor, org.apache.lucene.gdata.storage.StorageController
The DB4o StorageContorller can be used as a persitence component for the gdata-server. To use DB4o a third party jar needs to added to the lib directory of the project. If the jar is not available in the lib directory all db4o dependent class won't be included in the build.

If the jar is present in the lib directory this class can be configured as a {@link org.apache.lucene.gdata.server.registry.ComponentType#STORAGECONTROLLER} via the gdata-config.xml file. For detailed config documentation see the wiki page.

The DB4oController can run as a client or as a server to serve other running db4o clients in the network. To achive the best performance out of the db4o caching layer connections to the server will be reused in a connection pool. A connection will not be shared withing more than one thread. The controller release one connection per request and returns the connection when the request has been destroyed.

see
db4o website
see
org.apache.lucene.gdata.utils.Pool
author
Simon Willnauer

Fields Summary
private static final Log
LOG
private final ThreadLocal
threadLocalStorage
private org.apache.lucene.gdata.utils.Pool
containerPool
private com.db4o.ObjectServer
server
private final org.apache.lucene.gdata.storage.IDGenerator
idGenerator
private boolean
weakReferences
private boolean
runAsServer
private int
port
private String
filePath
private String
user
private String
password
private String
host
private int
containerPoolSize
Constructors Summary
public DB4oController()

throws
NoSuchAlgorithmException


            
        

        this.idGenerator = new IDGenerator(15);

    
Methods Summary
private voidcreateAdminAccount()

        GDataAccount adminAccount = GDataAccount.createAdminAccount();
        visiteInitialize();
        Storage sto = this.getStorage();
        try {
            sto.getAccount(adminAccount.getName());
        } catch (Exception e) {
            this.getStorage().storeAccount(adminAccount);
        } finally {
            visiteDestroy();
        }

    
public voiddestroy()

see
org.apache.lucene.gdata.storage.StorageController#destroy()

        this.containerPool.destroy();
        this.idGenerator.stopIDGenerator();
        this.server.close();
    
public intgetContainerPoolSize()

return
Returns the containerPoolSize.

        return this.containerPoolSize;
    
public java.lang.StringgetFilePath()

return
Returns the filePath.

        return this.filePath;
    
public java.lang.StringgetHost()

return
Returns the host.

        return this.host;
    
public java.lang.StringgetPassword()

return
Returns the password.

        return this.password;
    
public intgetPort()

return
Returns the port.

        return this.port;
    
public org.apache.lucene.gdata.storage.StoragegetStorage()

see
org.apache.lucene.gdata.storage.StorageController#getStorage()

       Storage retVal = this.threadLocalStorage.get();
        if (retVal != null)
            return retVal;

        retVal = new DB4oStorage(this.containerPool.aquire(), this);

        this.threadLocalStorage.set(retVal);
        return retVal;
    
public java.lang.StringgetUser()

return
Returns the user.

        return this.user;
    
public voidinitialize()

see
org.apache.lucene.gdata.server.registry.ServerComponent#initialize()

        if (LOG.isInfoEnabled())
            LOG.info("Initialize " + this.toString());

        Db4o.configure().objectClass(DB4oEntry.class).objectField("updated")
                .indexed(true);
        Db4o.configure().objectClass(BaseEntry.class).objectField("id")
                .indexed(true);
        Db4o.configure().objectClass(BaseFeed.class).objectField("id").indexed(
                true);
        Db4o.configure().objectClass(GDataAccount.class).objectField("name")
                .indexed(true);
        Db4o.configure().objectClass(ServerBaseFeed.class).cascadeOnDelete(
                false);
        Db4o.configure().objectClass(ServerBaseFeed.class)
                .maximumActivationDepth(0);
        Db4o.configure().objectClass(BaseFeed.class).minimumActivationDepth(1);
        Db4o.configure().objectClass(BaseEntry.class)
                .minimumActivationDepth(1);
        Db4o.configure().objectClass(BaseFeed.class).cascadeOnDelete(true);
        Db4o.configure().objectClass(DB4oEntry.class).cascadeOnDelete(true);
        Db4o.configure().objectClass(GDataAccount.class).cascadeOnDelete(true);
        Db4o.configure().weakReferences(this.weakReferences);
        Db4o.configure().optimizeNativeQueries(false);
        if (this.runAsServer) {
            this.server = Db4o.openServer(this.filePath, this.port);
            if(this.server == null)
                throw new RuntimeException("Can't create server at confiugred destination -- "+this.filePath);
            this.server.grantAccess(this.user, this.password);
        } else {
            InvocationHandler handler = new ObjectServerDecorator(this.user,
                    this.password, this.host, this.port);
            this.server = (ObjectServer) Proxy.newProxyInstance(this.getClass()
                    .getClassLoader(), new Class[] { ObjectServer.class },
                    handler);
        }

        PoolObjectFactory<ObjectContainer> factory = new ObjectContinerFactory(
                this.server);
        this.containerPool = new SimpleObjectPool<ObjectContainer>(
                this.containerPoolSize, factory);
        try {
            createAdminAccount();
        } catch (StorageException e) {
            LOG.error("Can not create admin account -- ",e);
        }
    
public booleanisRunAsServer()

return
Returns the runAsServer.

        return this.runAsServer;
    
public booleanisUseWeakReferences()

return
Returns the weakReferences.

        return this.weakReferences;
    
com.db4o.ObjectContainerreleaseContainer()

        return this.server.openClient();

    
public java.lang.StringreleaseId()

see
org.apache.lucene.gdata.storage.StorageController#releaseId()

        try{
        return this.idGenerator.getUID();
        }catch (InterruptedException e) {
            throw new StorageException("ID producer has been interrupted",e);
        }
    
public voidsetContainerPoolSize(int containerPoolSize)

param
containerPoolSize The containerPoolSize to set.

        this.containerPoolSize = containerPoolSize < 1 ? 1 : containerPoolSize;
    
public voidsetFilePath(java.lang.String filePath)

param
filePath The filePath to set.

        this.filePath = filePath;
    
public voidsetHost(java.lang.String host)

param
host The host to set.

        this.host = host;
    
public voidsetPassword(java.lang.String password)

param
password The password to set.

        this.password = password;
    
public voidsetPort(int port)

param
port The port to set.

        this.port = port;
    
public voidsetRunAsServer(boolean runAsServer)

param
runAsServer The runAsServer to set.

        this.runAsServer = runAsServer;
    
public voidsetUseWeakReferences(boolean weakReferences)

param
weakReferences The weakReferences to set.

        this.weakReferences = weakReferences;
    
public voidsetUser(java.lang.String user)

param
user The user to set.

        this.user = user;
    
public java.lang.StringtoString()

see
java.lang.Object#toString()

        StringBuilder builder = new StringBuilder(this.getClass().getName())
                .append(" ");
        builder.append("host: ").append(this.host).append(" ");
        builder.append("port: ").append(this.port).append(" ");
        builder.append("pool size: ").append(this.containerPoolSize)
                .append(" ");
        builder.append("runs as server: ").append(this.runAsServer).append(" ");
        builder.append("use weak references: ").append(this.weakReferences)
                .append(" ");
        builder.append("user: ").append(this.user).append(" ");
        builder.append("password length: ").append(
                this.password == null ? "no password" : this.password.length())
                .append(" ");

        return builder.toString();
    
public voidvisiteDestroy()

see
org.apache.lucene.gdata.server.registry.ScopeVisitor#visiteDestroy()

        Storage storage = this.threadLocalStorage.get();
        if (storage == null) {
            LOG.warn("no Storage opened -- threadlocal returned null");
            return;
        }
        this.containerPool.release(((DB4oStorage)storage).getContainer());
        this.threadLocalStorage.remove();
        if (LOG.isInfoEnabled())
            LOG.info("Closed Storage -- request destroyed");
    
public voidvisiteInitialize()

see
org.apache.lucene.gdata.server.registry.ScopeVisitor#visiteInitialize()

        if (LOG.isInfoEnabled())
            LOG.info("Opened Storage -- request initialized");
        Storage storage = this.threadLocalStorage.get();
        if (storage != null) {
            LOG.warn("Storage already opened");
            return;
        }

        storage = new DB4oStorage(this.containerPool.aquire(), this);

        this.threadLocalStorage.set(storage);