Java tutorial
/* * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.scoppelletti.programmerpower.security; import java.io.*; import java.net.*; import java.util.*; import org.slf4j.*; import org.springframework.beans.factory.*; import it.scoppelletti.programmerpower.*; import it.scoppelletti.programmerpower.io.*; import it.scoppelletti.programmerpower.reflect.*; import it.scoppelletti.programmerpower.types.*; /** * Implementazione di default dell’interfaccia {@code RoleManager}. * * <P>Questa implementazione dell’interfaccia {@code RoleManager} * acquisce i ruoli e le relative descrizioni dal file di proprietà * {@code roles.properties} e dalle sue localizzazioni inseriti nel * sotto-direttorio * {@code it.scoppelletti.programmerpower.security.DefaultRoleManager} del * direttorio dei dati condivisi tra tutte le applicazioni * {@code SharedDataDirectory}.</P> * * @see it.scoppelletti.programmerpower.io.SharedDataDirectory * @since 1.0.0 */ @Final public class DefaultRoleManager implements RoleManager, InitializingBean { /** * Base del nome del contenitore di risorse per la localizzazione dei ruoli. * Il valore della costante è <CODE>{@value}</CODE>. */ public static final String BASENAME = "roles"; private static final Logger myLogger = LoggerFactory.getLogger(DefaultRoleManager.class); private ClassLoader myLoader; /** * Costruttore. */ public DefaultRoleManager() { } /** * Inizializzazione. */ public void afterPropertiesSet() throws Exception { File dir; URL[] urls = new URL[1]; dir = SharedDataDirectory.getInstance().initResourceDirectory(DefaultRoleManager.class); urls[0] = dir.toURI().toURL(); myLoader = new URLClassLoader(urls); } public Map<String, String> loadRoles(Locale locale) { String desc, role; ResourceBundle res; Map<String, String> map; Enumeration<String> keys; if (locale == null) { throw new ArgumentNullException("locale"); } map = new TreeMap<String, String>(); try { res = ResourceBundle.getBundle(DefaultRoleManager.BASENAME, locale, myLoader); } catch (MissingResourceException ex) { myLogger.warn(String.format("Resource bundle %1$s not found.", DefaultRoleManager.BASENAME), ex); return map; } keys = res.getKeys(); while (keys.hasMoreElements()) { role = keys.nextElement(); try { desc = res.getString(role); } catch (MissingResourceException ex) { desc = null; } if (Strings.isNullOrEmpty(desc)) { desc = role; } map.put(role, desc); } return map; } }