1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.ldap;
16  
17  import com.liferay.portal.kernel.util.Validator;
18  import com.liferay.util.ldap.DummyDirContext;
19  
20  import javax.naming.Name;
21  import javax.naming.NameNotFoundException;
22  import javax.naming.NamingException;
23  import javax.naming.directory.Attribute;
24  import javax.naming.directory.Attributes;
25  import javax.naming.directory.BasicAttributes;
26  
27  /**
28   * <a href="PortalLDAPContext.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Edward Han
31   */
32  public class PortalLDAPContext extends DummyDirContext {
33  
34      public PortalLDAPContext(Attributes attributes) {
35          _attributes = attributes;
36      }
37  
38      public PortalLDAPContext(boolean ignoreCase) {
39          _attributes = new BasicAttributes(ignoreCase);
40      }
41  
42      public void addAttribute(String name, Object object) {
43          _attributes.put(name, object);
44      }
45  
46      public Attributes getAttributes() {
47          return _attributes;
48      }
49  
50      public Attributes getAttributes(Name name) throws NamingException {
51          return getAttributes(name.toString());
52      }
53  
54      public Attributes getAttributes(Name name, String[] ids)
55          throws NamingException {
56  
57          return getAttributes(name.toString(), ids);
58      }
59  
60      public Attributes getAttributes(String name) throws NamingException {
61          if (Validator.isNotNull(name)) {
62              throw new NameNotFoundException();
63          }
64  
65          return (Attributes)_attributes.clone();
66      }
67  
68      public Attributes getAttributes(String name, String[] ids)
69          throws NamingException {
70  
71          if (Validator.isNotNull(name)) {
72              throw new NameNotFoundException();
73          }
74  
75          Attributes attributes = new BasicAttributes(true);
76  
77          for (int i = 0; i < ids.length; i++) {
78              Attribute attribute = _attributes.get(ids[i]);
79  
80              if (attribute != null) {
81                  attributes.put(attribute);
82              }
83          }
84  
85          return attributes;
86      }
87  
88      public void setAttributes(Attributes attributes) {
89          _attributes = attributes;
90      }
91  
92      private Attributes _attributes;
93  
94  }