View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.security;
17  
18  import java.util.Hashtable;
19  
20  import javax.naming.Context;
21  import javax.naming.NamingException;
22  
23  import org.seasar.tuigwaa.util.TgwResource;
24  
25  import com.isenshi.util.extlib.ApacheDSUtil;
26  
27  /***
28   * @author someda
29   */
30  public class DirectoryUtils {
31  
32  	public static final String getDN(String name,String prefix,String suffix,String searchBase, boolean absolute){
33  		StringBuffer buf = new StringBuffer();
34  		buf.append(prefix + "=" + name);
35  		buf.append(buildSuffix(suffix,searchBase,true,absolute));	
36  		return buf.toString();
37  	}
38  	
39  	
40  	/***
41  	 * Returns suffix for given suffix string and searchBase.
42  	 * 
43  	 * @param name represents key value of the DN
44  	 * @param suffix represents attributes between searchBase and absolute DN, like ou=users
45  	 * @param searchBase represents search base, like dc=example,dc=com.
46  	 * @param isCommaStart if true, appends comma at the start.
47  	 * @param full if true and searchBase is not null, appends searchBase at the end.
48  	 * @return suffix of given searchBase and search filter
49  	 */	
50  	public static final String getAbsoluteDN(String name,String prefix,String suffix,String searchBase){
51  		return getDN(name,prefix,suffix,searchBase,true);
52  	}	
53  	
54  	/***
55  	 * Returns suffix for given suffix string and searchBase.
56  	 * 
57  	 * @param suffix represents attributes between searchBase and absolute DN, like ou=users
58  	 * @param searchBase represents search base, like dc=example,dc=com.
59  	 * @param isCommaStart if true, appends comma at the start.
60  	 * @param full if true and searchBase is not null, appends searchBase at the end.
61  	 * @return suffix of given searchBase and search filter
62  	 */
63  	public static final String buildSuffix(String suffix,String searchBase,boolean isCommaStart,boolean full){
64  		StringBuffer buf = new StringBuffer();
65  		if(suffix != null && !suffix.equals("")){
66  			if(isCommaStart) buf.append(",");
67  			buf.append(suffix);
68  		}
69  		if(full){
70  			if(searchBase != null && !searchBase.equals("")){
71  				buf.append("," + searchBase);
72  			}
73  		}		
74  		return buf.toString();
75  	}
76  			
77  	/***
78  	 * Returns RDN from search base.
79  	 * For example, if dn is "uid=user,ou=users,dc=example,dc=com" and searchBase
80  	 * is "dc=example,dc=com", returns "uid=user,ou=users".
81  	 * 
82  	 * @param dn represents absolute DN, like uid=user,ou=users,dc=example,dc=com
83  	 * @param searchBase represents search base, like dc=example,dc=com
84  	 * @return RDN
85  	 */
86  	public static final String getRDN(String dn, String searchBase){
87  		String ret = dn;
88  		int idx = 0;
89  		if(searchBase != null && !searchBase.equals("") && (idx =dn.indexOf(searchBase)) != -1){
90  			ret = dn.substring(0,idx-1);
91  		}
92  		return ret;		
93  	}
94  	
95  	
96  	public static final void shutdown(Hashtable env){		
97  		if(isEmbeded()){
98  			try{
99  				ApacheDSUtil.shutdown(env);
100 			}catch(NamingException ne){// quietly
101 			}
102 		}
103 	}
104 	
105 	public static final void closeQuietly(Context ctx){
106 		
107 		if(ctx != null){
108 			try{
109 				ctx.close();
110 			}catch(NamingException ne){// do-nothing
111 				ne.printStackTrace();
112 			}			
113 		}		
114 	}	
115 	
116 	private static final boolean isEmbeded(){
117 		return "true".equalsIgnoreCase(TgwResource.getProperty("ldap.service.embeded"));
118 	}
119 
120 }