diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-02 22:21:03 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-02 22:21:03 +0000 |
commit | f3e3191c911992ae4b55cf1619206ad7dd2b7c9f (patch) | |
tree | 59522e24f2dcb85cf55f7b4eb9e161382059fe45 /das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java | |
parent | 1a07cc91f40d1a8307189f746f6f55e3be7bb6d5 (diff) |
Moving LDAP DAS to contrib folder at new SVN structure
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@832142 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java')
-rw-r--r-- | das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java new file mode 100644 index 0000000000..be19f0b6c4 --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/util/JNDIUtil.java @@ -0,0 +1,168 @@ + +package org.apache.tuscany.das.ldap.util; + +import javax.naming.NamingException; +import javax.naming.directory.DirContext; + +/** + * The Class JNDIUtil. + */ +public class JNDIUtil +{ + /** + * Calculate DN components. + * + * @param context the context + * + * @return the string[] containing the components of the DN + * + * @throws NamingException the naming exception + */ + public static String[] calculateDNComponents( + DirContext context) + throws NamingException + { + String contextDN = + context.getNameInNamespace(); + + String[] initialContextComponents = + contextDN.split( "[,]" ); + + return initialContextComponents; + } + + + /** + * Calculate DN components. + * + * @param contextDN the context DN + * + * @return the string[] containing the components of the DN + * + * @throws NamingException the naming exception + */ + public static String[] calculateDNComponents( + String contextDN) + throws NamingException + { + String[] initialContextComponents = + contextDN.split( "[,]" ); + + return initialContextComponents; + } + + /** + * Gets the parent context. + * + * @param partitionContext the partition context + * @param childContext the child context + * + * @return the parent context (Null if the child context is the root) + * + * @throws NamingException the naming exception + */ + public static DirContext getParentContext( + DirContext childContext, + DirContext partitionContext) + throws NamingException + { + String[] childContextDNComponents = + calculateDNComponents( childContext ); + + String[] partitionDNComponents = + calculateDNComponents( partitionContext ); + + DirContext parentContext = null; + + if (childContextDNComponents.length == partitionDNComponents.length) + { + return null; + } + else if ( (childContextDNComponents.length - 1) == partitionDNComponents.length) + { + return partitionContext; + } + else + { + String parentLookupDN = + calculateParentRelativeDN( + childContext, partitionContext.getNameInNamespace() ); + + + + parentContext = + ( DirContext ) partitionContext.lookup( parentLookupDN ); + } + return parentContext; + } + + + /** + * Calculate parent relative DN. + * The parent relative DN is the + * DN of the parent context, relative + * to the partition context. + * + * So if the partition context is + * <i>ou=system</i> and + * the child context has DN + * <i>cn=accounts, cn=users, cn=example, ou=system</i> + * + * then the parent relative DN is + * <i>cn=users, cn=example</i> + * + * @param childContext the child context + * @param partitionDN the partition DN + * + * @return the string + * + * @throws NamingException the naming exception + */ + public static String calculateParentRelativeDN( + DirContext childContext, + String partitionDN) + throws NamingException + { + String parentRelativeDN = null; + + String[] childContextDNComponents = + calculateDNComponents( childContext ); + + String[] partitionDNComponents = + calculateDNComponents( partitionDN ); + + if (childContextDNComponents.length == + partitionDNComponents.length) + { + return null; + } + else if (childContextDNComponents.length == + partitionDNComponents.length + 1) + { + String childRDN = childContextDNComponents[0]; + String childContextDN = childContext.getNameInNamespace(); + + int beginIndex = childRDN.length() + 1; + int endIndex = childContextDN.length(); + + parentRelativeDN = childContextDN.substring( + beginIndex, endIndex ); + + return parentRelativeDN; + } + else + { + String childRDN = childContextDNComponents[0]; + String childContextDN = childContext.getNameInNamespace(); + + int beginIndex = childRDN.length() + 1; + int endIndex = + childContextDN.length() - + ( partitionDN.length() +1); + + parentRelativeDN = childContextDN.substring( + beginIndex, endIndex ); + } + return parentRelativeDN; + } +} |