JLDAP接続プールの作成およびテストプログラム.


Webベースの開発なので、tomcat管理データベース接続プールのようなものを自分で書きたいです.
基本的には、LDAPConectionDataFactoryによってLDAPConectionDataクラスが作成され、作成したインスタンスを管理クラスLDAPConectionDataPool(プール)に格納し、ユーザーが最も多くのLDAPConectionリンクの数を定義できるようになります.
1、LDAPConectionDataクラス
public class LDAPConectionData {
	
	private boolean conetIsFree=true;//          (      ,         )
	
	private boolean isClosed=false;//        ,             。
	
	private LDAPConnection conn=null;//LDAP  。
	
	public LDAPConectionData(String ldapURl,int port,String user,String passwd) throws LDAPException{//   LDAP  
		conn=new LDAPConnection(); 
		conn.connect(ldapURl, port);
		conn.bind(LDAPConnection.LDAP_V3, user, passwd);
		System.out.println("------LDAPConnection create successfully!-----");
	}


	
	public boolean getConetIsFree() {
		return conetIsFree;
	}



	public void setConetIsFree(boolean conetIsFree) {
		this.conetIsFree = conetIsFree;
	}



	public boolean getClosed() {
		return isClosed;
	}



	public void setClosed(boolean isClosed) {
		this.isClosed = isClosed;
	}



	public LDAPConnection getConn() {
		return this.conn;
	}



	public void closeLDAPConnection(){
		if(this.conn!=null){
			try {
				conn.disconnect();
			} catch (LDAPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

 
2.LDAPConectionDataPoolクラス.このクラスは接続を取得し、リンク(接続リソースの管理)を解放する.
import java.util.ArrayList;
import java.util.List;
import com.novell.ldap.LDAPException;

final class LDAPConectionDataPool implements java.io.Serializable {

	private List<LDAPConectionData> pool = new ArrayList<LDAPConectionData>();//   LDAPConectionData  ,       List ,    。

	private static LDAPConectionDataPool LDAPConnDataPool = null;//            。

	private LDAPConectionDataPool() {

	}

	public static synchronized LDAPConectionDataPool getInstance() {
		if (LDAPConnDataPool == null) {
			return new LDAPConectionDataPool();
		} else {
			return LDAPConnDataPool;
		}
	}

	public void add(LDAPConectionData aa) {
		aa.setConetIsFree(false);
		this.pool.add(aa);
	}

	public synchronized void setConectionDataFree(LDAPConectionData da) {//             ,        ,         。

		if (!da.getConetIsFree()) {
			da.setConetIsFree(true);
			this.notifyAll();
		}
	}

	public synchronized LDAPConectionData getLDAPConectionData()
			throws InterruptedException {//         LDAPConectionData
		LDAPConectionData datatemp = null;
		if (this.getSize() < LDAPConectionDataFactory.MAX_NUM_CONN) {//     LDAPConectionData          ,         。

			if (getSize() == 0 || datatemp == null) {
				try {
					datatemp = LDAPConectionDataFactory.createLDAPConection();
					System.out.println(getSize());
					this.add(datatemp);
				} catch (LDAPException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {//            。      ,       LDAPConectionData    。
			wait();
			for (LDAPConectionData data : pool) {
				if (data.getConetIsFree())
					datatemp = data;
				break;

			}

		}
		return datatemp;
	}

	public int getSize() {
		return pool.size();
	}

	public void removeAll() {
		closeAll();
		pool.clear();
	}

	public void closeAll() {
		for (LDAPConectionData data : pool) {
			data.closeLDAPConnection();
		}
	}

	public LDAPConectionData hasFree() {//         。
		if (getSize() < 1)
			return null;
		LDAPConectionData datatemp = null;
		for (LDAPConectionData data : pool) {
			if (data.getConetIsFree())
				datatemp = data;
			break;

		}
		return datatemp;
	}
	
	public void finalize()throws Throwable{
		this.removeAll();
	}
}

 
3.LDAPConectionDataFactoryファクトリクラス.接続を作成するクラス.
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import com.novell.ldap.LDAPException;

public class LDAPConectionDataFactory {

	private static final String LDAP_URL = "192.168.16.21";//     

	private static final int LDAP_PORT = 20000;//   。

	private static final String MANAGER_USR = "cn=admin";//    

	private static final String MANAGER_PASSWD = "12345678";//   

	public static final int MAX_NUM_CONN = 2;//                  。

	public static LDAPConectionData createLDAPConection() throws LDAPException {
		return new LDAPConectionData(LDAPConectionDataFactory.LDAP_URL,
				LDAPConectionDataFactory.LDAP_PORT,
				LDAPConectionDataFactory.MANAGER_USR,
				LDAPConectionDataFactory.MANAGER_PASSWD);

	}
}

 
 
以上が紹介したすべてです.
また、テストクラスと実行結果を貼り付けます.
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;

public class ThreadTest extends Thread {

	private int uid;

	public LDAPConectionDataPool pool = null;

	public ThreadTest(int uid, LDAPConectionDataPool pl) {
		this.uid = uid;
		this.pool = pl;
	}

	public void run() {
		System.out.println("Thread:" + uid + " is run!");
		LDAPAttributeSet attributeSet = new LDAPAttributeSet();
		attributeSet.add(new LDAPAttribute("objectclass", new String(
				"inetOrgPerson")));
		attributeSet.add(new LDAPAttribute("cn", new String[] { " ",
				"Jim Smith", "Jimmy Smith" }));
		attributeSet.add(new LDAPAttribute("givenname", new String[] { "  ",
				"Jim", "Jimmy" }));
		attributeSet.add(new LDAPAttribute("sn", new String("Smith")));
		attributeSet.add(new LDAPAttribute("telephonenumber", new String(
				"1 801 555 1212")));
		attributeSet.add(new LDAPAttribute("mail",
				new String("[email protected]")));
		attributeSet.add(new LDAPAttribute("userpassword", new String(
				"newpassword")));
		LDAPEntry entry = new LDAPEntry("uid=" + uid
				+ ",ou=Code,cn=Lizl,dc=4a,dc=4adomain", attributeSet);
		LDAPConectionData data = null;
		try {
			data = pool.getLDAPConectionData();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			LDAPConnection lc = data.getConn();
			lc.add(entry);
			System.out.println("POOL's maxsize="+pool.getSize());
		} catch (LDAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (data != null)
				pool.setConectionDataFree(data);

		}
	}

	public static void main(String args[]) {
		LDAPConectionDataPool pool = LDAPConectionDataPool.getInstance();
		ThreadTest a = new ThreadTest(431, pool);
		ThreadTest b = new ThreadTest(421, pool);
		ThreadTest c = new ThreadTest(422, pool);
		ThreadTest d = new ThreadTest(423, pool);
		ThreadTest e = new ThreadTest(424, pool);
		ThreadTest f = new ThreadTest(425, pool);
		ThreadTest g = new ThreadTest(426, pool);
		ThreadTest h = new ThreadTest(427, pool);
		ThreadTest i = new ThreadTest(428, pool);
		ThreadTest j = new ThreadTest(429, pool);
		ThreadTest k = new ThreadTest(430, pool);
		try {
			a.join();
			b.join();
			c.join();
			d.join();
			e.join();
			f.join();
			g.join();
			h.join();
			i.join();
			k.join();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		
		a.start();
		b.start();
		c.start();
		d.start();
		e.start();
		f.start();
		g.start();
		h.start();
		i.start();
		j.start();
		k.start();

	}
}

 
実行結果は次のとおりです.
 
Thread:400 is run!Thread:401 is run!Thread:403 is run!Thread:405 is run!Thread:402 is run!Thread:404 is run!Thread:406 is run!Thread:408 is run!Thread:407 is run!Thread:409 is run!Thread:410 is run!------LDAPConnection create successfully!-----0------LDAPConnection create successfully!-----1