2004/02
まずLDAPってなんやねん?ということですが、簡単にいうと階層型データベースです。
ディレクトリ構造でデータを管理するソフトウェアなわけですが、よくある
使われ方はシステムを使用するユーザの情報の管理です。ユーザIDやパスワード
なんかね。今回はLDAPからディレクトリ属性を取得する処理を作ります。
LDAPからディレクトリ属性を取得するには、
1.DirContextを取得。
2. 取得したDirContextのgetAttributes(キーとなる文字列)を呼び出す。
をおこないます。
で1を行うDirContextFactoryクラスと2を行うGetAttributesCommandクラス
を作成しました。
んじゃ、ソースいってみよう!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
package jp.gr.java_conf.yamarou.ldap; import java.util.Properties; import java.io.InputStream; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.Context; /** * DirContextを生成するクラス。 * 一度生成したらキャッシュし、使いまわす。 * @author yamarou */ public class DirContextFactory { /** キャッシュするDirContext */ private static DirContext context = createDirContext(); /** * ldap.propertiesを読み込み、読み込んだ情報を使用して * LDAPからユーザ情報ディレクトリのコンテキストを取得する。 * コンテキストを一度取得したら保持し、以降はそれを返す。 * @return ディレクトリコンテキスト */ public static DirContext getDirContext() { return context; } private static DirContext createDirContext() { try { Properties env = new Properties(); InputStream in = env.getClass().getResourceAsStream( "/ldap.properties"); env.load(in); return new InitialDirContext(env); } catch (Exception ex) { throw new RuntimeException(ex); } } } package jp.gr.java_conf.yamarou.ldap; import javax.naming.directory.Attributes; import javax.naming.directory.Attribute; import javax.naming.directory.DirContext; import javax.naming.NamingException; import javax.naming.NamingEnumeration; /** * LDAPの属性を取得するクラス。 * @author yamarou */ public class GetAttributesCommand { private Attributes attrs; public GetAttributesCommand() { } /** * nameのAttributesを取得し保持する。 * @param name ディレクトリ名 */ public void execute(String name) { try { DirContext context = DirContextFactory.getDirContext(); attrs = context.getAttributes(name); for (NamingEnumeration e = attrs.getAll();e.hasMore();) { System.out.println("enum="+e.next()); } } catch (Exception ex) { throw new RuntimeException(ex); } } /** * 属性値を返す。 * @param attrID 属性ID * @return 属性値 */ public Object getObject(String attrID) { try { Attribute attr = this.attrs.get(attrID); if (attr == null) { return null; } return attr.get(); } catch (NamingException ex) { throw new RuntimeException(ex); } } /** * 属性値を返す。 * @param attrID 属性ID * @return 属性値 */ public String getString(String attrID) { return (String)getObject(attrID); } /** * 属性値を返す。 * @param attrID 属性ID * @return 属性値 */ public byte[] getBytes(String attrID) { return (byte[])getObject(attrID); } } package jp.gr.java_conf.yamarou.ldap; import junit.framework.*; public class GetAttributesCommandTest extends TestCase { public GetAttributesCommandTest(String name) { super(name); } public void testGetString() { GetAttributesCommand get = new GetAttributesCommand(); get.execute("uid=USER001"); String uid = get.getString("uid"); assertEquals("USER001", uid); String cn = get.getString("cn"); assertEquals("cn00001", cn); } } |
実行には以下のファイル(ldap.properties)をクラスパス上に
配置しておくことが必要です。実行する環境に合わせて変更して下さい。
[ldap.properties]
java.naming.provider.url=ldap://localhost/ou=user,o=yamarou.co.jp
java.naming.security.authentication=simple
java.naming.security.principal=cn=Directory Manager
java.naming.security.credentials=password
GetAttributesCommandの使い方は
GetAttributesCommandTestクラスを見ていただくとわかるのですが、
GetAttributesCommandを
newして、
executeして
各属性をgetする
というものになっています。簡単でしょ!
DirContext#getAttributes(String name)を隠蔽してるってわけです。
Command自体にAttributesを持たすか、それとも
executeでAttributesをラップしたクラスを返すか迷ったのですが
とりあえず一つのクラスでカプセル化することにしました。
次回はLDAP更新系ライブラリを作ってみたいと思います。お楽しみに!
やまろう