cara, continuo na mesma.
Posso usar a classe que vc indicou (ReloadableResourceBundleMessageSource) pra carregar minhas constantes numa boa mas e depois que elas forem carregadas? Lembre que estou usando membros estáticos (isso pra poder identificar qq erro de digitação em tempo de compilação e não ter de procurar nada em cache - fica tudo em mémória).
Essa abordagem precisa ser usada com cautela - eu sei. Carregar muita coisa em memória em vez de só carregar qdo necessário é sempre perigoso. Tento balancear o uso desse artifício limitando-o a alguns casos bem específicos.
Vamos aos exemplos, acho que vai ficar mais fácil de me fazer entender:
ServiceConstants.properties [code]
user.adm = adm
user.pwd = adm
SLGerenteFilmes.local = com.gec.ejb.service.SLGerenteFilmesLocalHome
SLGerenteFilmes.remote = com.gec.ejb.service.SLGerenteFilmesHome
SLGerenteFilmes.use = remote
SLGerenteLocacoes.local = com.gec.ejb.service.SLGerenteLocacoesLocalHome
SLGerenteLocacoes.remote = com.gec.ejb.service.SLGerenteLocacoesHome
SLGerenteLocacoes.use = remote
SLGerenteUsuarios.local = com.gec.ejb.service.SLGerenteUsuariosLocalHome
SLGerenteUsuarios.remote = com.gec.ejb.service.SLGerenteUsuariosHome
SLGerenteUsuarios.use = remote
SLSequence.local = com.gec.ejb.util.SLSequenceLocalHome
Categoria.local = com.gec.ejb.model.CategoriaLocalHome
Filme.local = com.gec.ejb.model.FilmeLocalHome
Copia.local = com.gec.ejb.model.CopiaLocalHome
Locacao.local = com.gec.ejb.model.LocacaoLocalHome
Usuario.local = com.gec.ejb.model.UsuarioLocalHome
[/code]
[code]package com.gec.ejb.service;
import com.gec.util.ConstantBundle;
/**
- Constantes para identificação e localização.
-
- Carregado apenas na primeira utilização.
-
-
@author Anaximandro de Godinho
-
@version $Revision: 1.0 $
-
- Be carefully: keys in resource bundle are case sensitive!
*/
final class ServicesConstants {
// INTERNAL BUNDLE ------------------------------------------------------- //
private static ConstantBundle cb = new ConstantBundle( Constants.class.getName() );
// PUBLIC STATIC CONSTANTS - value defined inside resource bundle. ------- //
public static final String sUSER_ADM = cb.getString( “user.adm” ); //$NON-NLS-1$
public static final String sUSER_PWD = cb.getString( “user.pwd” ); //$NON-NLS-1$
public static final String SLGerenteFilmes_LOCALHOME = cb.getString( “SLGerenteFilmes.local” ); //$NON-NLS-1$
public static final String SLGerenteFilmes_HOME = cb.getString( “SLGerenteFilmes.remote” ); //$NON-NLS-1$
public static final String SLGerenteFilmes_USE = cb.getString( “SLGerenteFilmes.use” ); //$NON-NLS-1$
public static final String SLGerenteLocacoes_LOCALHOME = cb.getString( “SLGerenteLocacoes.local” ); //$NON-NLS-1$
public static final String SLGerenteLocacoes_HOME = cb.getString( “SLGerenteLocacoes.remote” ); //$NON-NLS-1$
public static final String SLGerenteLocacoes_USE = cb.getString( “SLGerenteLocacoes.use” ); //$NON-NLS-1$
public static final String SLGerenteUsuarios_LOCALHOME = cb.getString( “SLGerenteUsuarios.local” ); //$NON-NLS-1$
public static final String SLGerenteUsuarios_HOME = cb.getString( “SLGerenteUsuarios.remote” ); //$NON-NLS-1$
public static final String SLGerenteUsuarios_USE = cb.getString( “SLGerenteUsuarios.use” ); //$NON-NLS-1$
public static final String SLSequence_LOCALHOME = cb.getString( “SLSequence.local” ); //$NON-NLS-1$
public static final String CATEGORIA_LOCALHOME = cb.getString( “Categoria.local” ); //$NON-NLS-1$
public static final String COPIA_LOCALHOME = cb.getString( “Copia.local” ); //$NON-NLS-1$
public static final String FILME_LOCALHOME = cb.getString( “Filme.local” ); //$NON-NLS-1$
public static final String LOCACAO_LOCALHOME = cb.getString( “Locacao.local” ); //$NON-NLS-1$
public static final String USUARIO_LOCALHOME = cb.getString( “Usuario.local” ); //$NON-NLS-1$
// Load resource bundle data, RUN ONLY ONCE and free used resources. ----- //
static {
cb = null;
}
// ONLY can be used as singleton ----------------------------------------- //
private ServicesConstants() {
}
}
[/code]
[code]package com.gec.util;
import java.io.Serializable;
import org.apache.log4j.Logger;
public abstract class LogAbstract implements Serializable {
private static final long serialVersionUID = -1461088044990375670L;
public final Logger _log;
public LogAbstract( final String className ) {
_log = Logger.getLogger( className );
}
/////////////////////////////////////////
// debug < info < warn < error < fatal //
/////////////////////////////////////////
/**
- Delegar as chamadas do log aqui não será uma boa opção pois perderemos as informações de log (linha, classe, método, etc …) das chamadas originais.
*/
}
[/code]
[code]package com.gec.util;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public final class ConstantBundle extends LogAbstract {
// Constante interna para controle de serialização //
private static final long serialVersionUID = -3804649032724591482L;
// ResourceBundle local //
private final ResourceBundle _rb;
/**
*
* @param className
*/
public ConstantBundle( final String className ) {
super( className );
_log.debug( IM1 + className + IM2 );
_rb = ResourceBundle.getBundle( className );
}
/**
*
*/
protected void finalize() throws Throwable {
_log.debug( IM3 );
super.finalize();
}
/**
*
* @param key
* @return
*/
public String getString( final String key ) {
try {
final String s = _rb.getString( key ).trim();
_log.debug( IE11 + key + IE12 + s + IE13 );
return s;
} catch( final MissingResourceException e ) {
_log.error( IE11 + key + IE14 );
return null;
}
}
/**
*
* @param key
* @return
*/
public int getInt( final String key ) {
try {
final int i = new Integer( _rb.getString( key ).trim() ).intValue();
_log.debug( IE11 + key + IE12 + i + IE13 );
return i;
} catch( final MissingResourceException e ) {
_log.error( IE11 + key + IE14 );
return -1;
}
}
// INTERNAL CONSTANTS //
private static final String IM1 = “# ConstantBundle Loading[”; //$NON-NLS-1$
private static final String IM2 = “] #”; //$NON-NLS-1$
private static final String IM3 = “# ConstantBundle Finalized #”; //$NON-NLS-1$
private static final String IE11 = “”; //$NON-NLS-1$
private static final String IE12 = " = ‘"; //$NON-NLS-1$
private static final String IE13 = "’."; //$NON-NLS-1$
private static final String IE14 = “’ not found.”; //$NON-NLS-1$
}
[/code]
E o problema persiste: como recarregar as constantes definidas na classe ServiceConstants ?
Woody