001    /**
002     * Copyright (c) 2000-2013 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            @Override
036            public Object clone() {
037                    return new DefaultCustomUserAttributes();
038            }
039    
040            @Override
041            public String getValue(String name, Map<String, String> userInfo) {
042                    if (name == null) {
043                            return null;
044                    }
045    
046                    if (_log.isDebugEnabled()) {
047                            String companyId = userInfo.get(UserAttributes.LIFERAY_COMPANY_ID);
048                            String userId = userInfo.get(UserAttributes.LIFERAY_USER_ID);
049    
050                            _log.debug("Company id " + companyId);
051                            _log.debug("User id " + userId);
052                    }
053    
054                    if (name.equals("user.name.random")) {
055                            String[] names = new String[] {"Aaa", "Bbb", "Ccc"};
056    
057                            return names[Randomizer.getInstance().nextInt(3)];
058                    }
059                    else {
060                            return null;
061                    }
062            }
063    
064            private static Log _log = LogFactoryUtil.getLog(
065                    DefaultCustomUserAttributes.class);
066    
067    }