001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Randomizer;
020    
021    import java.util.Map;
022    
023    /**
024     * <p>
025     * A separate instance of this class is created every time
026     * <code>renderRequest.getAttribute(PortletRequest.USER_INFO)</code> is called.
027     * It is safe to cache attributes in this instance because you can assume that
028     * all calls to this instance belong to the same user.
029     * </p>
030     *
031     * @author Brian Wing Shun Chan
032     */
033    public class DefaultCustomUserAttributes implements CustomUserAttributes {
034    
035            public String getValue(String name, Map<String, String> userInfo) {
036                    if (name == null) {
037                            return null;
038                    }
039    
040                    if (_log.isDebugEnabled()) {
041                            String companyId = userInfo.get(UserAttributes.LIFERAY_COMPANY_ID);
042                            String userId = userInfo.get(UserAttributes.LIFERAY_USER_ID);
043    
044                            _log.debug("Company id " + companyId);
045                            _log.debug("User id " + userId);
046                    }
047    
048                    if (name.equals("user.name.random")) {
049                            String[] names = new String[] {"Aaa", "Bbb", "Ccc"};
050    
051                            return names[Randomizer.getInstance().nextInt(3)];
052                    }
053                    else {
054                            return null;
055                    }
056            }
057    
058            public Object clone() {
059                    return new DefaultCustomUserAttributes();
060            }
061    
062            private static Log _log = LogFactoryUtil.getLog(
063                    DefaultCustomUserAttributes.class);
064    
065    }