KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > test > jnditester > JndiChecker


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.test.jnditester;
21
22 import java.util.Hashtable JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.directory.*;
26
27
28 public class JndiChecker {
29     public static void main(String JavaDoc[] args) {
30         JndiProperties properties = JndiProperties.getInstance();
31         Hashtable JavaDoc env = new Hashtable JavaDoc();
32         System.out.println("Initializing conext environment for :"
33             + properties.getConnectionURL());
34         env.put(Context.INITIAL_CONTEXT_FACTORY, properties.getContextFactory());
35         env.put(Context.PROVIDER_URL, properties.getConnectionURL());
36
37         if (properties.getConnectionName() != null) {
38             System.out.println("Setting up connection User '"
39                 + properties.getConnectionName()
40                 + "' and connection Password '"
41                 + properties.getConnectionPassword() + "'");
42             env.put(Context.SECURITY_PRINCIPAL, properties.getConnectionName());
43             env.put(Context.SECURITY_CREDENTIALS,
44                 properties.getConnectionPassword());
45         }
46
47         if (properties.getReferal() != null) {
48             System.out.println("Setting up REFERRAL :'"
49                 + properties.getReferal() + "'");
50             env.put(Context.REFERRAL, properties.getReferal());
51         }
52
53         try {
54             DirContext ctx = new InitialDirContext(env);
55             System.out.println("context initialization done..");
56             System.out.println("initializing search control");
57
58             SearchControls constraints = new SearchControls();
59
60             if (properties.getSearchScope()) {
61                 System.out.println("setting up search scope to sub tree level");
62                 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
63             } else {
64                 System.out.println("setting up search scope to one level");
65                 constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
66             }
67
68             if (properties.getReturnAttibutes() != null) {
69                 System.out.println("setting up returning attributes :"
70                     + properties.getReturnAttibutes());
71                 constraints.setReturningAttributes(new String JavaDoc[] {
72                         properties.getReturnAttibutes()
73                     });
74             } else {
75                 //constraints.setReturningAttributes(new String[0]);
76
}
77
78             System.out.println("initializing search control done..");
79             System.out.println("searching with filter '"
80                 + properties.getSearchFilter() + "' in context base '"
81                 + properties.getSearchBase() + "' for attribute '"
82                 + properties.getReturnAttibutes() + "'");
83
84             NamingEnumeration JavaDoc results = ctx.search(properties.getSearchBase(),
85                     properties.getSearchFilter(), constraints);
86             System.out.println("search executed succesfully");
87             System.out.println("Looping search results :");
88
89             while (results.hasMoreElements()) { //) {
90

91                 SearchResult sr = (SearchResult) results.next();
92
93                 if (properties.getReturnAttibutes() != null) {
94                     System.out.println("Found :"
95                         + sr.getAttributes().get(properties.getReturnAttibutes())
96                             .get().toString());
97                 } else {
98                     System.out.println("Found :"
99                         + sr.getAttributes().toString());
100                 }
101             }
102         } catch (Exception JavaDoc ex) {
103             System.out.println("Error :" + ex.toString());
104         }
105     }
106 }
107
Popular Tags