1 26 27 package org.objectweb.jonas.security.realm.factory; 28 29 import java.security.NoSuchAlgorithmException ; 30 import java.text.MessageFormat ; 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 import java.util.Iterator ; 35 import java.util.Vector ; 36 37 import javax.naming.AuthenticationException ; 38 import javax.naming.Context ; 39 import javax.naming.Name ; 40 import javax.naming.NameParser ; 41 import javax.naming.NamingEnumeration ; 42 import javax.naming.NamingException ; 43 import javax.naming.Reference ; 44 import javax.naming.StringRefAddr ; 45 import javax.naming.directory.Attribute ; 46 import javax.naming.directory.Attributes ; 47 import javax.naming.directory.DirContext ; 48 import javax.naming.directory.InitialDirContext ; 49 import javax.naming.directory.SearchControls ; 50 import javax.naming.directory.SearchResult ; 51 52 import org.objectweb.util.monolog.api.BasicLevel; 53 54 import org.objectweb.jonas.security.realm.lib.HashHelper; 55 import org.objectweb.jonas.security.realm.principals.LDAPUser; 56 import org.objectweb.jonas.security.realm.principals.User; 57 58 63 public class JResourceLDAP extends JResource implements JResourceLDAPMBean { 64 65 68 private static final String FACTORY_TYPE = "org.objectweb.jonas.security.realm.factory.JResourceLDAP"; 69 70 73 private static final String FACTORY_NAME = "org.objectweb.jonas.security.realm.factory.JResourceLDAPFactory"; 74 75 79 private static final String BIND_AUTHENTICATION_MODE = "bind"; 80 81 86 private static final String COMPARE_AUTHENTICATION_MODE = "compare"; 87 88 91 private String initialContextFactory = null; 92 93 96 private String providerUrl = null; 97 98 102 private String securityAuthentication = null; 103 104 108 private String securityPrincipal = null; 109 110 113 private String securityCredentials = null; 114 115 119 private String securityProtocol = null; 120 121 125 private String language = null; 126 127 131 private String referral = null; 132 133 137 private String stateFactories = null; 138 139 143 private String authenticationMode = null; 144 145 148 private String userPasswordAttribute = null; 149 150 153 private String userRolesAttribute = null; 154 155 158 private String roleNameAttribute = null; 159 160 163 private String baseDN = null; 164 165 168 private String userDN = null; 169 170 173 private String userSearchFilter = null; 174 175 178 private String roleDN = null; 179 180 183 private String roleSearchFilter = null; 184 185 188 private String algorithm = null; 189 190 194 public JResourceLDAP() throws Exception { 195 super(); 196 } 197 198 204 public User findUser(String username) throws JResourceException { 205 206 if (username == null) { 208 return null; 209 } 210 211 LDAPUser user = new LDAPUser(); 213 user.setName(username); 214 try { 215 216 DirContext dirContext = getDirContext(); 218 SearchControls sctls = new SearchControls (); 219 220 String [] attributes = null; 221 if (authenticationMode.equals(COMPARE_AUTHENTICATION_MODE)) { 222 attributes = new String [] {userPasswordAttribute, userRolesAttribute}; 223 } else { 224 attributes = new String [0]; 225 } 226 sctls.setReturningAttributes(attributes); 227 sctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 228 229 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 230 getLogger().log(BasicLevel.DEBUG, "userDN = '" + userDN + "'"); 231 getLogger().log(BasicLevel.DEBUG, "baseDN = '" + baseDN + "'"); 232 } 233 234 String lookupUserDN = null; 235 if (userDN != null && !userDN.equals("")) { 237 lookupUserDN = userDN.concat(",").concat(baseDN); 238 } else { 239 lookupUserDN = baseDN; 240 } 241 Object [] arguments = {username}; 242 lookupUserDN = MessageFormat.format(lookupUserDN, arguments); 243 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 244 getLogger().log(BasicLevel.DEBUG, "lookupUserDN = '" + lookupUserDN + "'"); 245 } 246 247 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 250 getLogger().log(BasicLevel.DEBUG, "search : lookupUserDN = '" + lookupUserDN + "', searchFilter = '" 251 + userSearchFilter + "', username = '" + username + "'"); 252 } 253 NamingEnumeration answer = dirContext 254 .search(lookupUserDN, userSearchFilter, new Object [] {username}, sctls); 255 if (answer == null || !answer.hasMore()) { 257 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 258 if (answer == null) { 259 getLogger().log(BasicLevel.DEBUG, "answer is null"); 260 } else { 261 getLogger().log(BasicLevel.DEBUG, "no anwser"); 262 } 263 } 264 return null; 265 } 266 267 SearchResult firstAnswer = (SearchResult ) answer.next(); 269 if (answer.hasMore()) { 271 return null; 272 } 273 274 NameParser nameParser = dirContext.getNameParser(""); 276 Name contextName = nameParser.parse(dirContext.getNameInNamespace()); 277 278 Name baseDNName = contextName.addAll(nameParser.parse(lookupUserDN)); 280 281 Name userFullDN = baseDNName.addAll(nameParser.parse(firstAnswer.getName())); 283 user.setDN(userFullDN.toString()); 284 285 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 286 getLogger().log(BasicLevel.DEBUG, "DN found : '" + userFullDN + "'"); 287 } 288 289 Attributes userAttributes = firstAnswer.getAttributes(); 291 if (userAttributes == null) { 292 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 293 getLogger().log(BasicLevel.DEBUG, "No user attributes found"); 294 } 295 return null; 296 } 297 298 String password = null; 299 if (authenticationMode.equals(COMPARE_AUTHENTICATION_MODE)) { 301 password = readValueFromAttribute(userPasswordAttribute, userAttributes); 302 if (password != null) { 303 user.setPassword(password); 304 } 305 } 306 307 String roles = readValuesFromAttribute(userRolesAttribute, userAttributes); 309 user.setRoles(roles); 310 311 } catch (javax.naming.NamingException ne) { 312 throw new JResourceException("Could not find user :" + ne.getMessage()); 313 } 314 return user; 315 } 316 317 323 public boolean isValidUser(User user, String credentials) { 324 325 if (credentials == null || user == null) { 327 return false; 328 } 329 330 if (authenticationMode.equals(COMPARE_AUTHENTICATION_MODE)) { 332 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 333 getLogger().log(BasicLevel.DEBUG, "Compare mode"); 334 } 335 return isValidUserCompare(user, credentials); 336 } else if (authenticationMode.equals(BIND_AUTHENTICATION_MODE)) { 337 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 338 getLogger().log(BasicLevel.DEBUG, "Bind mode"); 339 } 340 return isValidUserBind(user, credentials); 341 } else { 342 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 343 getLogger().log(BasicLevel.DEBUG, "No authentication mode found, return false"); 344 } 345 return false; 346 } 347 348 } 349 350 358 public boolean isValidUserBind(User user, String credentials) { 359 360 if (!(user instanceof LDAPUser)) { 362 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 363 getLogger().log(BasicLevel.DEBUG, "Not instance of LDAPUser"); 364 } 365 return false; 366 } 367 368 String dn = ((LDAPUser) user).getDN(); 369 if (dn == null) { 370 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 371 getLogger().log(BasicLevel.DEBUG, "No DN found in User"); 372 } 373 return false; 374 } 375 376 Hashtable env = getEnvInitialDirContext(); 378 env.put(Context.SECURITY_PRINCIPAL, dn); 379 env.put(Context.SECURITY_CREDENTIALS, credentials); 380 381 boolean validated = false; 382 try { 383 DirContext dirContext = new InitialDirContext (env); 384 validated = true; 385 dirContext.close(); 386 } catch (AuthenticationException e) { 387 getLogger().log(BasicLevel.ERROR, "Can't make an initial dir context : " + e.getMessage()); 389 } catch (NamingException ne) { 390 getLogger().log(BasicLevel.ERROR, "Naming exception " + ne.getMessage()); 392 } 393 return validated; 394 } 395 396 404 public boolean isValidUserCompare(User user, String credentials) { 405 406 boolean validated = false; 407 408 if (user != null && user.getHashPassword() == null) { 409 String errMsg = "No password for the user so it cannot perform a check."; 411 errMsg += " Check that you are using the correct mode ('compare' or 'bind')."; 412 errMsg += " By using compare mode, the anonymous user cannot retrieved password in many cases."; 413 getLogger().log(BasicLevel.ERROR, errMsg); 414 return validated; 415 } 416 417 String pass = user.getHashPassword().getPassword(); 419 String algo = user.getHashPassword().getAlgorithm(); 420 421 if (algo != null && pass != null) { 423 try { 424 validated = HashHelper.hashPassword(credentials, algo).equalsIgnoreCase(pass); 425 } catch (NoSuchAlgorithmException nsae) { 426 getLogger().log(BasicLevel.ERROR, "Can't make a password with the algorithm " + algo + ". " 427 + nsae.getMessage()); 428 } 429 } else if ((algorithm != null) && (!algorithm.equals(""))) { 430 try { 432 validated = HashHelper.hashPassword(credentials, algorithm).equalsIgnoreCase(pass); 433 } catch (NoSuchAlgorithmException nsae) { 434 getLogger().log(BasicLevel.ERROR, "Can't make a password with the algorithm " + algorithm + ". " 435 + nsae.getMessage()); 436 } 437 } else { 438 validated = credentials.equals(pass); 440 } 441 return validated; 442 } 443 444 450 public ArrayList getArrayListCombinedRoles(User user) throws JResourceException { 451 452 ArrayList allCombinedRoles = new ArrayList (); 453 if (user == null) { 455 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 456 getLogger().log(BasicLevel.DEBUG, "User is empty, return empty array of roles"); 457 } 458 return allCombinedRoles; 459 } 460 461 String [] userRoles = user.getArrayRoles(); 463 for (int r = 0; r < userRoles.length; r++) { 464 String roleName = userRoles[r]; 465 if (!allCombinedRoles.contains(roleName)) { 466 allCombinedRoles.add(roleName); 467 } 468 } 469 470 if (!(user instanceof LDAPUser)) { 472 return allCombinedRoles; 473 } 474 String dn = ((LDAPUser) user).getDN(); 475 476 if (dn == null) { 478 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 479 getLogger().log(BasicLevel.DEBUG, "DN of user is empty, return empty array of roles"); 480 } 481 return allCombinedRoles; 482 } 483 484 try { 485 DirContext dirContext = getDirContext(); 487 SearchControls sctls = new SearchControls (); 488 489 String [] attributes = new String [] {roleNameAttribute}; 490 sctls.setReturningAttributes(attributes); 491 sctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 492 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 493 getLogger().log(BasicLevel.DEBUG, "roleDN = '" + roleDN + "'"); 494 getLogger().log(BasicLevel.DEBUG, "baseDN = '" + baseDN + "'"); 495 } 496 497 String lookupRoleDN = null; 499 if ((roleDN != null) && (!roleDN.equals(""))) { 500 lookupRoleDN = roleDN.concat(",").concat(baseDN); 501 } else { 502 lookupRoleDN = baseDN; 503 } 504 505 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 508 getLogger().log(BasicLevel.DEBUG, "search with lookupRoleDN = '" + lookupRoleDN + "', rolesearchFilter = '" 509 + roleSearchFilter + "', dn = '" + dn + "'"); 510 } 511 512 NamingEnumeration answers = dirContext.search(lookupRoleDN, roleSearchFilter, new Object [] {dn}, sctls); 513 514 if (answers == null) { 515 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 516 getLogger().log(BasicLevel.DEBUG, "answer is null"); 517 } 518 return allCombinedRoles; 519 } 520 521 Vector vRoles = new Vector (); 522 while (answers.hasMore()) { 523 SearchResult answer = (SearchResult ) answers.next(); 524 Attributes roleAttributes = answer.getAttributes(); 525 if (roleAttributes == null) { 526 continue; 527 } 528 addValueFromAttributeToVector(roleNameAttribute, roleAttributes, vRoles); 529 } 530 531 for (Enumeration e = vRoles.elements(); e.hasMoreElements();) { 532 String roleName = (String ) e.nextElement(); 533 if (!allCombinedRoles.contains(roleName)) { 534 allCombinedRoles.add(roleName); 535 } 536 } 537 } catch (javax.naming.NamingException ne) { 538 throw new JResourceException("Could not find roles from the user :" + ne.getMessage()); 539 } 540 user.setCombinedRoles(allCombinedRoles); 541 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 542 StringBuffer rolesStr = new StringBuffer (); 543 for (Iterator it = allCombinedRoles.iterator(); it.hasNext();) { 544 rolesStr.append((String ) it.next()); 545 rolesStr.append(","); 546 } 547 getLogger().log(BasicLevel.DEBUG, "Roles are : " + rolesStr + " for user '" + user.getName() + "'"); 548 } 549 550 return allCombinedRoles; 551 } 552 553 557 public String toXML() { 558 559 561 StringBuffer xml = new StringBuffer (" <ldaprealm name=\""); 562 xml.append(getName()); 563 564 xml.append("\"\n baseDN=\""); 565 if (baseDN != null) { 566 xml.append(baseDN); 567 } 568 569 if ((initialContextFactory != null) && (!initialContextFactory.equals(""))) { 570 xml.append("\"\n initialContextFactory=\""); 571 xml.append(initialContextFactory); 572 } 573 574 if ((providerUrl != null) && (!providerUrl.equals(""))) { 575 xml.append("\"\n providerUrl=\""); 576 xml.append(providerUrl); 577 } 578 579 if ((securityAuthentication != null) && (!securityAuthentication.equals(""))) { 580 xml.append("\"\n securityAuthentication=\""); 581 xml.append(securityAuthentication); 582 } 583 584 if ((securityPrincipal != null) && (!securityPrincipal.equals(""))) { 585 xml.append("\"\n securityPrincipal=\""); 586 xml.append(securityPrincipal); 587 } 588 589 if ((securityCredentials != null) && (!securityCredentials.equals(""))) { 590 xml.append("\"\n securityCredentials=\""); 591 xml.append(securityCredentials); 592 } 593 594 if ((authenticationMode != null) && (!authenticationMode.equals(""))) { 595 xml.append("\"\n authenticationMode=\""); 596 xml.append(authenticationMode); 597 } 598 599 if ((userPasswordAttribute != null) && (!userPasswordAttribute.equals(""))) { 600 xml.append("\"\n userPasswordAttribute=\""); 601 xml.append(userPasswordAttribute); 602 } 603 604 if ((userRolesAttribute != null) && (!userRolesAttribute.equals(""))) { 605 xml.append("\"\n userRolesAttribute=\""); 606 xml.append(userRolesAttribute); 607 } 608 609 if ((roleNameAttribute != null) && (!roleNameAttribute.equals(""))) { 610 xml.append("\"\n roleNameAttribute=\""); 611 xml.append(roleNameAttribute); 612 } 613 614 if ((userDN != null) && (!userDN.equals(""))) { 615 xml.append("\"\n userDN=\""); 616 xml.append(userDN); 617 } 618 619 if ((userSearchFilter != null) && (!userSearchFilter.equals(""))) { 620 xml.append("\"\n userSearchFilter=\""); 621 xml.append(userSearchFilter); 622 } 623 624 if ((roleDN != null) && (!roleDN.equals(""))) { 625 xml.append("\"\n roleDN=\""); 626 xml.append(roleDN); 627 } 628 629 if ((roleSearchFilter != null) && (!roleSearchFilter.equals(""))) { 630 xml.append("\"\n roleSearchFilter=\""); 631 xml.append(roleSearchFilter); 632 } 633 634 if ((securityProtocol != null) && (!securityProtocol.equals(""))) { 636 xml.append("\"\n securityProtocol=\""); 637 xml.append(securityProtocol); 638 } 639 640 if ((language != null) && (!language.equals(""))) { 641 xml.append("\"\n language=\""); 642 xml.append(language); 643 } 644 645 if ((referral != null) && (!referral.equals(""))) { 646 xml.append("\"\n referral=\""); 647 xml.append(referral); 648 } 649 650 if ((stateFactories != null) && (!stateFactories.equals(""))) { 651 xml.append("\"\n stateFactories=\""); 652 xml.append(stateFactories); 653 } 654 655 if ((algorithm != null) && (!algorithm.equals(""))) { 656 xml.append("\"\n algorithm=\""); 657 xml.append(algorithm); 658 } 659 660 xml.append("\" />"); 661 662 return xml.toString(); 663 } 664 665 669 public String toString() { 670 return this.toXML(); 671 } 672 673 681 public Reference getReference() throws NamingException { 682 683 Reference reference = new Reference (FACTORY_TYPE, FACTORY_NAME, null); 685 686 reference.add(new StringRefAddr ("name", getName())); 688 reference.add(new StringRefAddr ("initialContextFactory", getInitialContextFactory())); 689 reference.add(new StringRefAddr ("providerUrl", getProviderUrl())); 690 reference.add(new StringRefAddr ("securityAuthentication", getSecurityAuthentication())); 691 reference.add(new StringRefAddr ("securityPrincipal", getSecurityPrincipal())); 692 reference.add(new StringRefAddr ("securityCredentials", getSecurityCredentials())); 693 reference.add(new StringRefAddr ("securityProtocol", getSecurityProtocol())); 694 reference.add(new StringRefAddr ("language", getLanguage())); 695 reference.add(new StringRefAddr ("referral", getReferral())); 696 reference.add(new StringRefAddr ("stateFactories", getStateFactories())); 697 reference.add(new StringRefAddr ("authenticationMode", getAuthenticationMode())); 698 reference.add(new StringRefAddr ("userPasswordAttribute", getUserPasswordAttribute())); 699 reference.add(new StringRefAddr ("userRolesAttribute", getUserRolesAttribute())); 700 reference.add(new StringRefAddr ("roleNameAttribute", getRoleNameAttribute())); 701 reference.add(new StringRefAddr ("baseDN", getBaseDN())); 702 reference.add(new StringRefAddr ("userDN", getUserDN())); 703 reference.add(new StringRefAddr ("userSearchFilter", getUserSearchFilter())); 704 reference.add(new StringRefAddr ("roleDN", getRoleDN())); 705 reference.add(new StringRefAddr ("roleSearchFilter", getRoleSearchFilter())); 706 reference.add(new StringRefAddr ("algorithm", algorithm)); 707 708 return reference; 709 } 710 711 712 716 public void setInitialContextFactory(String initialContextFactory) { 717 this.initialContextFactory = initialContextFactory; 718 } 719 720 724 public void setProviderUrl(String providerUrl) { 725 this.providerUrl = providerUrl; 726 } 727 728 734 public void setSecurityAuthentication(String securityAuthentication) { 735 this.securityAuthentication = securityAuthentication; 736 } 737 738 742 public void setSecurityPrincipal(String securityPrincipal) { 743 this.securityPrincipal = securityPrincipal; 744 } 745 746 750 public void setSecurityCredentials(String securityCredentials) { 751 this.securityCredentials = securityCredentials; 752 } 753 754 758 public void setSecurityProtocol(String securityProtocol) { 759 this.securityProtocol = securityProtocol; 760 } 761 762 766 public void setLanguage(String language) { 767 this.language = language; 768 } 769 770 775 public void setReferral(String referral) { 776 this.referral = referral; 777 } 778 779 783 public void setStateFactories(String stateFactories) { 784 this.stateFactories = stateFactories; 785 } 786 787 792 public void setAuthenticationMode(String authenticationMode) { 793 this.authenticationMode = authenticationMode; 794 } 795 796 801 public void setUserPasswordAttribute(String userPasswordAttribute) { 802 this.userPasswordAttribute = userPasswordAttribute; 803 } 804 805 810 public void setUserRolesAttribute(String userRolesAttribute) { 811 this.userRolesAttribute = userRolesAttribute; 812 } 813 814 818 public void setRoleNameAttribute(String roleNameAttribute) { 819 this.roleNameAttribute = roleNameAttribute; 820 } 821 822 826 public void setBaseDN(String baseDN) { 827 if ((baseDN != null) && (!baseDN.equals(""))) { 829 this.baseDN = baseDN; 830 } 831 } 832 833 838 public void setUserDN(String userDN) { 839 if ((userDN != null) && (!userDN.equals(""))) { 840 this.userDN = userDN; 841 } 842 } 843 844 848 public void setUserSearchFilter(String userSearchFilter) { 849 this.userSearchFilter = userSearchFilter; 850 } 851 852 858 public void setRoleDN(String roleDN) { 859 this.roleDN = roleDN; 860 } 861 862 866 public void setRoleSearchFilter(String roleSearchFilter) { 867 this.roleSearchFilter = roleSearchFilter; 868 } 869 870 874 public void setAlgorithm(String algorithm) { 875 this.algorithm = algorithm; 876 } 877 878 882 public String getInitialContextFactory() { 883 return initialContextFactory; 884 } 885 886 890 public String getProviderUrl() { 891 return providerUrl; 892 } 893 894 899 public String getSecurityAuthentication() { 900 return securityAuthentication; 901 } 902 903 907 public String getSecurityPrincipal() { 908 return securityPrincipal; 909 } 910 911 915 public String getSecurityCredentials() { 916 return securityCredentials; 917 } 918 919 923 public String getSecurityProtocol() { 924 return securityProtocol; 925 } 926 927 931 public String getLanguage() { 932 return language; 933 } 934 935 940 public String getReferral() { 941 return referral; 942 } 943 944 948 public String getStateFactories() { 949 return stateFactories; 950 } 951 952 956 public String getAuthenticationMode() { 957 return authenticationMode; 958 } 959 960 964 public String getUserPasswordAttribute() { 965 return userPasswordAttribute; 966 } 967 968 972 public String getUserRolesAttribute() { 973 return userRolesAttribute; 974 } 975 976 980 public String getRoleNameAttribute() { 981 return roleNameAttribute; 982 } 983 984 988 public String getBaseDN() { 989 return baseDN; 990 } 991 992 997 public String getUserDN() { 998 return userDN; 999 } 1000 1001 1005 public String getUserSearchFilter() { 1006 return userSearchFilter; 1007 } 1008 1009 1015 public String getRoleDN() { 1016 return roleDN; 1017 } 1018 1019 1023 public String getRoleSearchFilter() { 1024 return roleSearchFilter; 1025 } 1026 1027 1031 public String getAlgorithm() { 1032 return algorithm; 1033 } 1034 1035 1040 protected DirContext getDirContext() throws NamingException { 1041 DirContext dirContext = null; 1042 dirContext = new InitialDirContext (getEnvInitialDirContext()); 1044 1045 return dirContext; 1046 } 1047 1048 1052 private Hashtable getEnvInitialDirContext() { 1053 1054 Hashtable env = new Hashtable (); 1056 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 1057 env.put(Context.PROVIDER_URL, providerUrl); 1058 env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication); 1059 if ((securityPrincipal != null) && (!securityPrincipal.equals(""))) { 1060 env.put(Context.SECURITY_PRINCIPAL, securityPrincipal); 1061 } 1062 if ((securityCredentials != null) && (!securityCredentials.equals(""))) { 1063 env.put(Context.SECURITY_CREDENTIALS, securityCredentials); 1064 } 1065 if ((language != null) && (!language.equals(""))) { 1066 env.put(Context.LANGUAGE, language); 1067 } 1068 if ((referral != null) && (!referral.equals(""))) { 1069 env.put(Context.REFERRAL, referral); 1070 } 1071 if ((securityProtocol != null) && (!securityProtocol.equals(""))) { 1072 env.put(Context.SECURITY_PROTOCOL, securityProtocol); 1073 } 1074 if ((stateFactories != null) && (!stateFactories.equals(""))) { 1075 env.put(Context.STATE_FACTORIES, stateFactories); 1076 } 1077 return env; 1078 } 1079 1080 1089 private String readValueFromAttribute(String attrID, Attributes attributes) throws NamingException { 1090 1091 if (attributes == null || attrID == null) { 1093 return null; 1094 } 1095 1096 Attribute attribute = attributes.get(attrID); 1097 1098 if (attribute == null) { 1100 return null; 1101 } 1102 1103 Object o = attribute.get(); 1104 String value = null; 1105 1106 if (o instanceof byte[]) { 1107 value = new String ((byte[]) o); 1108 } else { 1109 value = o.toString(); 1110 } 1111 1112 return value; 1113 } 1114 1115 1124 private String readValuesFromAttribute(String attrID, Attributes attributes) throws NamingException { 1125 1126 if (attributes == null || attrID == null) { 1128 return null; 1129 } 1130 1131 Attribute attribute = attributes.get(attrID); 1132 1133 if (attribute == null) { 1135 return null; 1136 } 1137 1138 String value = null; 1139 NamingEnumeration e = attribute.getAll(); 1140 while (e.hasMore()) { 1141 String s = (String ) e.next(); 1142 if (value == null) { 1143 value = s; 1144 } else { 1145 value = value + "," + s; 1146 } 1147 } 1148 return value; 1149 } 1150 1151 1159 private void addValueFromAttributeToVector(String attrID, Attributes attributes, Vector v) throws NamingException { 1160 1161 if (attributes == null || attrID == null) { 1163 return; 1164 } 1165 1166 Attribute attribute = attributes.get(attrID); 1167 1168 if (attribute == null) { 1170 return; 1171 } 1172 1173 NamingEnumeration e = attribute.getAll(); 1174 while (e.hasMore()) { 1175 v.add(e.next()); 1176 } 1177 1178 } 1179 1180 1184 public void removeMBeans() throws JResourceException { 1185 } 1187 1188} | Popular Tags |