package pt.ist.fenixframework.pstm; import java.util.Enumeration; import java.util.HashSet; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Set; import com.linkare.commons.metainfo.Linkare; public class ServiceInfo { final String username; final String serviceName; final Object[] arguments; ServiceInfo(String username, String serviceName, Object[] arguments) { this.username = username; this.serviceName = serviceName; this.arguments = arguments; } public boolean shouldLog() { return ((username != null) && USERS_TO_MONITOR.contains(username.toUpperCase())) || SERVICES_TO_MONITOR.contains(serviceName.toUpperCase()); } public String getArgumentsAsString() { StringBuilder argumentsInString = new StringBuilder(); for (Object argument : arguments) { if (argument != null) { try { argumentsInString.append(argument.toString()); } catch (NullPointerException e) { argumentsInString.append(argument.getClass().getName()); } argumentsInString.append("; "); } } return argumentsInString.toString(); } /** * @return the username */ @Linkare(author = "Paulo Zenida") public String getUsername() { return username; } /** * @return the serviceName */ @Linkare(author = "Paulo Zenida") public String getServiceName() { return serviceName; } // STATICs start here private static final ThreadLocal CURRENT_SERVICE = new ThreadLocal(); private static Set SERVICES_TO_MONITOR; private static Set USERS_TO_MONITOR; static { initServicesAndUsers(); } public static void setCurrentServiceInfo(String username, String serviceName, Object[] args) { CURRENT_SERVICE.set(new ServiceInfo(username, serviceName, args)); } public static ServiceInfo getCurrentServiceInfo() { return CURRENT_SERVICE.get(); } private static void initServicesAndUsers() { SERVICES_TO_MONITOR = readSubjectsToMonitor("servicesToMonitor"); USERS_TO_MONITOR = readSubjectsToMonitor("usersToMonitor"); } private static Set readSubjectsToMonitor(String fileName) { Set valuesSet = new HashSet(); try { ResourceBundle rb = ResourceBundle.getBundle(fileName); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements(); ) { valuesSet.add(keys.nextElement().toUpperCase()); } } catch (MissingResourceException mre) { // if the resource does not exist, that's ok: it means that no monitoring will be performed // so, ignore the exception and return an empty set } return valuesSet; } }