1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.Randomizer;
20  
21  import java.util.Map;
22  
23  /**
24   * <a href="CustomUserAttributes.java.html"><b><i>View Source</i></b></a>
25   *
26   * <p>
27   * A separate instance of this class is created every time
28   * <code>renderRequest.getAttribute(PortletRequest.USER_INFO)</code> is called.
29   * It is safe to cache attributes in this instance because you can assume that
30   * all calls to this instance belong to the same user.
31   * </p>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class CustomUserAttributes implements Cloneable {
36  
37      public String getValue(String name, Map<String, String> userInfo) {
38          if (name == null) {
39              return null;
40          }
41  
42          if (_log.isDebugEnabled()) {
43              String companyId = userInfo.get(UserAttributes.LIFERAY_COMPANY_ID);
44              String userId = userInfo.get(UserAttributes.LIFERAY_USER_ID);
45  
46              _log.debug("Company id " + companyId);
47              _log.debug("User id " + userId);
48          }
49  
50          if (name.equals("user.name.random")) {
51              String[] names = new String[] {"Aaa", "Bbb", "Ccc"};
52  
53              return names[Randomizer.getInstance().nextInt(3)];
54          }
55          else {
56              return null;
57          }
58      }
59  
60      public Object clone() {
61          return new CustomUserAttributes();
62      }
63  
64      private static Log _log = LogFactoryUtil.getLog(CustomUserAttributes.class);
65  
66  }