[Logo] Mentawai Forum - Mentawai Web Framework
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 

Forum Read-Only! Check the new site and forum here!

Classes for NeoDatis Connection caching and Auto-Wiring  XML
Forum Index -> General Comments
Author Message
navigo



Joined: 17/06/2008 11:27:55
Messages: 7
Offline

org.mentawai.db.ODBConnectionHandler.java:
Code:
package org.mentawai.db;
 
 import java.util.ArrayList;
 import org.neodatis.odb.ODB;
 import org.neodatis.odb.ODBFactory;
 
 /**
  *
  * @author navigo
  */
 public class ODBConnectionHandler
 {
 
 	private String host;
 	private int port;
 	private String dbName;
 	private int poolSize;
 	ArrayList<ODB> availableODB;
 	ArrayList<ODB> usedODB;
 
 	public ODBConnectionHandler(String host, int port, String dbName, int poolSize)
 	{
 		this.host = host;
 		this.port = port;
 		this.dbName = dbName;
 		this.poolSize = poolSize;
 		this.availableODB = new ArrayList<ODB>();
 		this.usedODB = new ArrayList<ODB>();
 
 		ODB odb;
 		for(int i = 0; i < this.poolSize; i++)
 		{
 			odb = ODBFactory.openClient(host, port, dbName);
 			availableODB.add(odb);
 		}
 	}
 
 	public ODB getConnection()
 	{
 		ODB odb;
 		if (availableODB.size() > 0)
 		{
 			odb = availableODB.remove(0);
 			if (odb.isClosed())
 			{
 				odb = ODBFactory.openClient(host, port, dbName);
 			}
 			usedODB.add(odb);
 		}
 		else
 		{
 			System.out.println("ODB Client pool exhausted; adding another client.");
 			odb = ODBFactory.openClient(host, port, dbName);
 			usedODB.add(odb);
 		}
 		return odb;
 	}
 
 	public void release(ODB odb)
 	{
 		if(odb != null)
 		{
 			if(! odb.isClosed())
 			{
 				usedODB.remove(odb);
 				availableODB.add(odb);
 			}
 			else
 			{
 				System.out.println("Returned ODB Client is closed; dropping.");
 			}
 		}
 	}
 
 	public void destroy()
 	{
 		for(ODB myODB : availableODB)
 		{
 			myODB.close();
 		}
 		for(ODB myODB : usedODB)
 		{
 			myODB.close();
 		}
 	}
 }
 


org.mentawai.filter.NeoDatisFilter.java:
Code:
package org.mentawai.filter;
 
 import org.mentawai.core.Action;
 import org.mentawai.core.AfterConsequenceFilter;
 import org.mentawai.core.Consequence;
 import org.mentawai.core.InputWrapper;
 import org.mentawai.core.InvocationChain;
 import org.mentawai.db.ODBConnectionHandler;
 import org.neodatis.odb.ODB;
 
 /**
  *
  * @author navigo
  */
 public class NeoDatisFilter extends InputWrapper implements AfterConsequenceFilter
 {
 	public static final String KEY = "conn";
 	private String connKey;
 	private final ODBConnectionHandler odbConnectionHandler;
 	private ThreadLocal<ODB> odb = new ThreadLocal<ODB>();
 
 	public NeoDatisFilter(String connKey, ODBConnectionHandler odbConnectionHandler)
 	{
 		super();
 		this.connKey = connKey;
 		this.odbConnectionHandler = odbConnectionHandler;
 	}
 
 	public NeoDatisFilter(ODBConnectionHandler odbConnectionHandler)
 	{
 		this(KEY, odbConnectionHandler);
 	}
 
 	public NeoDatisFilter(ODBConnectionHandler odbConnectionHandler, String connKey)
 	{
 		this(connKey, odbConnectionHandler);
 	}
 
 	public String filter(InvocationChain chain) throws Exception
 	{
 		Action action = chain.getAction();
 		super.setInput(action.getInput());
 		action.setInput(this);
 		return chain.invoke();
 	}
 
 	public void destroy()
 	{
 		odbConnectionHandler.destroy();
 	}
 
 	public void afterConsequence(Action action, Consequence cnsqnc, boolean bln, boolean bln1, String string)
 	{
 		ODB myODB = this.odb.get();
 		if(myODB != null)
 		{
 			this.odb.set(null);
 			removeValue(connKey);
 			odbConnectionHandler.release(myODB);
 		}
 	}
 
 	@Override
 	public Object getValue(String key)
 	{
 		if(key.equals(connKey))
 		{
 			ODB myODB = odb.get();
 			if(myODB == null)
 			{
 				try
 				{
 					myODB = odbConnectionHandler.getConnection();
 					odb.set(myODB);
 					setValue(connKey, myODB);
 				}
 				catch(Exception e)
 				{
 					throw new RuntimeException("Error while getting the connection from the connection handler '" + odbConnectionHandler + "': " + e, e);
 				}
 			}
 			return myODB;
 		}
 		return super.getValue(key);
 	}
 }
 


These allow you to use AutoWiring in your application with a great object open source database http://neodatis.org

Use these in your Application Manager as such:
Code:
public class ApplicationManager extends org.mentawai.core.ApplicationManager
 {
 
 	private ODBConnectionHandler odbch = null;
 
 	@Override
 	public void init(Context application)
 	{
 		setDebugMode(true);
 		odbch = new ODBConnectionHandler("localhost", 9090, "main", 1);
 	}
 .............snip
 		//Connection Pooling
 		filter(new NeoDatisFilter("odb", odbch));
 .............snip
 		//IoC components (DAOs)
 		ioc("userAccountDAO", NeoDatisUserAccountDAO.class);
 
 		//Auto-wiring
 		filter(new AutoWiringFilter());
 
 		//Dependencies
 		aw("odb", ODB.class); //DAOs depend on ODB
 .............snip


That should get the general idea across. Feel free to comment or correct.
saoj



Joined: 01/07/2005 09:59:17
Messages: 2846
Location: Rio de Janeiro, RJ
Offline


Nice. Check Space4J as well: http://www.space4j.org

Sergio Oliveira

 
Forum Index -> General Comments
Go to:   
Powered by JForum 2.1.6 © JForum Team