Mercurial > hg > release > thermostat-0.11
changeset 1114:9c1ebac8d0b9
Refactor UsernameUtilImpl so as to not require LoggingUtils.
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-May/006750.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Thu, 23 May 2013 18:36:15 +0200 |
parents | 58d1c5979ddd |
children | d628eebb5eea |
files | agent/core/src/main/java/com/redhat/thermostat/utils/username/UserNameLookupException.java agent/core/src/main/java/com/redhat/thermostat/utils/username/UserNameUtil.java agent/core/src/main/java/com/redhat/thermostat/utils/username/internal/UserNameUtilImpl.java system-backend/src/main/java/com/redhat/thermostat/backend/system/ProcessUserInfoBuilder.java system-backend/src/test/java/com/redhat/thermostat/backend/system/ProcessUserInfoBuilderTest.java |
diffstat | 5 files changed, 81 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/core/src/main/java/com/redhat/thermostat/utils/username/UserNameLookupException.java Thu May 23 18:36:15 2013 +0200 @@ -0,0 +1,51 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.utils.username; + +/** + * + * Thrown on user name lookup error. + * + * @see UserNameUtil + */ +@SuppressWarnings("serial") +public class UserNameLookupException extends Exception { + + public UserNameLookupException(Throwable cause) { + super(cause); + } +}
--- a/agent/core/src/main/java/com/redhat/thermostat/utils/username/UserNameUtil.java Mon May 27 13:22:58 2013 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/utils/username/UserNameUtil.java Thu May 23 18:36:15 2013 +0200 @@ -36,17 +36,20 @@ package com.redhat.thermostat.utils.username; - /** * Utility to query the operating system for a user name. */ public interface UserNameUtil { - + /** - * Returns the user name corresponding to the provided UID. - * @param uid - the UID whose name to return - * @return the user name if found, or null otherwise + * Gets the user name corresponding to the provided UID. + * + * @param uid + * The UID whose name to return. + * @throws UserNameLookupException + * If an error occurred during retrieval of the user name. + * @return The user name if found. null if no such UID exists. */ - String getUserName(long uid); + String getUserName(long uid) throws UserNameLookupException; }
--- a/agent/core/src/main/java/com/redhat/thermostat/utils/username/internal/UserNameUtilImpl.java Mon May 27 13:22:58 2013 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/utils/username/internal/UserNameUtilImpl.java Thu May 23 18:36:15 2013 +0200 @@ -37,14 +37,11 @@ package com.redhat.thermostat.utils.username.internal; import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; -import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.utils.username.UserNameLookupException; import com.redhat.thermostat.utils.username.UserNameUtil; public class UserNameUtilImpl implements UserNameUtil { - private static final Logger logger = LoggingUtils.getLogger(UserNameUtilImpl.class); static { /* @@ -54,16 +51,21 @@ System.loadLibrary("UserNameUtilWrapper"); } - public String getUserName(long uid) { - String result = null; + public String getUserName(long uid) throws UserNameLookupException { + String username = null; if (uid >= 0) { try { - result = getUserName0(uid); + // getUserName0 may throw IOException. We catch it here and + // throw a more specific exception in order to avoid API + // leakage. This exception is then dealt with in the caller of + // this method rather than here. This free's us from using the + // logger in a JNI class. + username = getUserName0(uid); } catch (IOException e) { - logger.log(Level.WARNING, "Unable to retrieve username for uid: " + uid, e); + throw new UserNameLookupException(e); } } - return result; + return username; } private native String getUserName0(long uid) throws IOException;
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/ProcessUserInfoBuilder.java Mon May 27 13:22:58 2013 -0400 +++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/ProcessUserInfoBuilder.java Thu May 23 18:36:15 2013 +0200 @@ -44,6 +44,7 @@ import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.utils.ProcDataSource; +import com.redhat.thermostat.utils.username.UserNameLookupException; import com.redhat.thermostat.utils.username.UserNameUtil; class ProcessUserInfoBuilder { @@ -88,7 +89,12 @@ try { Reader reader = source.getStatusReader(pid); long uid = getUidFromProcfs(new BufferedReader(reader)); - String name = userNameUtil.getUserName(uid); + String name = null; + try { + name = userNameUtil.getUserName(uid); + } catch (UserNameLookupException e) { + logger.log(Level.WARNING, "Unable to retrieve username for uid: " + uid, e); + } info = new ProcessUserInfo(uid, name); } catch (IOException e) { logger.log(Level.WARNING, "Unable to read user info for " + pid, e);
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/ProcessUserInfoBuilderTest.java Mon May 27 13:22:58 2013 -0400 +++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/ProcessUserInfoBuilderTest.java Thu May 23 18:36:15 2013 +0200 @@ -49,12 +49,13 @@ import com.redhat.thermostat.backend.system.ProcessUserInfoBuilder.ProcessUserInfo; import com.redhat.thermostat.common.tools.ApplicationException; import com.redhat.thermostat.utils.ProcDataSource; +import com.redhat.thermostat.utils.username.UserNameLookupException; import com.redhat.thermostat.utils.username.UserNameUtil; public class ProcessUserInfoBuilderTest { @Test - public void testBuild() throws IOException, ApplicationException { + public void testBuild() throws UserNameLookupException, IOException, ApplicationException { StringReader reader = new StringReader("Uid: 2000 2000 2000 2000"); ProcDataSource source = mock(ProcDataSource.class); UserNameUtil util = mock(UserNameUtil.class); @@ -81,7 +82,7 @@ } @Test - public void testBuildErrorUsername() throws IOException, ApplicationException { + public void testBuildErrorUsername() throws IOException, UserNameLookupException, ApplicationException { StringReader reader = new StringReader("Uid: 2000 2000 2000 2000"); ProcDataSource source = mock(ProcDataSource.class); UserNameUtil util = mock(UserNameUtil.class);