Skip to content

Commit

Permalink
corrections context
Browse files Browse the repository at this point in the history
  • Loading branch information
khergalant committed Jul 18, 2024
1 parent 2ab996d commit 7183b93
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 55 deletions.
19 changes: 14 additions & 5 deletions src/main/java/fr/univlorraine/ecandidat/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
Expand All @@ -40,6 +41,7 @@
import ch.qos.logback.core.joran.spi.JoranException;
import fr.univlorraine.ecandidat.config.SpringConfig;
import fr.univlorraine.ecandidat.utils.ConstanteUtils;
import fr.univlorraine.ecandidat.utils.MethodUtils;
import fr.univlorraine.tools.logback.UserMdcServletFilter;

/**
Expand All @@ -48,6 +50,8 @@
*/
public class Initializer implements WebApplicationInitializer {

public final static String PROPERTY_FILE_PATH = "config.location";

/**
* Profil Spring de debug
*/
Expand Down Expand Up @@ -100,6 +104,10 @@ private void addContextParametersToSystemProperties(final ServletContext servlet
*/
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
/* Si un fichier de properties est fourni, on charge toutes les propriétés dans le servletContext pour alimenter logback par la suite */
final Properties properties = MethodUtils.loadPropertieFile();
properties.forEach((k, v) -> servletContext.setInitParameter((String) k, (String) v));

addContextParametersToSystemProperties(servletContext);
addContextParametersToLogbackConfig(servletContext);

Expand Down Expand Up @@ -127,11 +135,12 @@ public void sessionDestroyed(final HttpSessionEvent httpSessionEvent) {
springContext.register(SpringConfig.class);
servletContext.addListener(new ContextLoaderListener(springContext));

final String refreshRate = servletContext.getInitParameter("load.balancing.refresh.fixedRate");
if (refreshRate == null) {
//on place par défaut le refresh à 10min
servletContext.setInitParameter("load.balancing.refresh.fixedRate", "600000");
}
// final String refreshRate = servletContext.getInitParameter("load.balancing.refresh.fixedRate");
// System.out.println("refreshRate "+refreshRate);
// if (refreshRate == null) {
// //on place par défaut le refresh à 10min
// servletContext.setInitParameter("load.balancing.refresh.fixedRate", "600000");
// }

/* String refreshRateFichier = servletContext.getInitParameter("fiabilisation.fichier.refresh.fixedRate");
* if (refreshRateFichier==null){
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/fr/univlorraine/ecandidat/config/StopAppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* ESUP-Portail eCandidat - Copyright (c) 2016 ESUP-Portail consortium
*
*
* 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 fr.univlorraine.ecandidat.config;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

import fr.univlorraine.ecandidat.controllers.LockCandidatController;

/**
* Configuration du lancement de l'appli
* @author Kevin Hergalant
*/
@Component
public class StopAppConfig implements ApplicationListener<ContextStoppedEvent> {

private final Logger logger = LoggerFactory.getLogger(StopAppConfig.class);

@Resource
private transient LockCandidatController lockCandidatController;

@Override
public void onApplicationEvent(final ContextStoppedEvent event) {
preprocessCleanLock();
}

/** Au stop de l'appli, on supprime tout les locks */
private void preprocessCleanLock() {
logger.info("Nettoyage des locks");
lockCandidatController.cleanAllLockCandidatForInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,10 @@ public void reloadData(final String code, final Boolean needToPushToCandidat) {
reloadListeTypeAvis();
break;
case ConstanteUtils.CACHE_SPRING_CONF:
self.invalidConfCacheWithoutAskToReloadData();
self.invalidConfCache(false);
break;
case ConstanteUtils.CACHE_SPRING_RESSOURCES:
self.invalidRessourcesCacheWithoutAskToReloadData();
self.invalidRessourcesCache(false);
break;
default:
break;
Expand Down Expand Up @@ -1131,30 +1131,16 @@ public void askToReloadData(final String code) {
/**
* Invalide le cache de la config etablissement (url pegase, ..)
*/
public void invalidConfCacheWithoutAskToReloadData() {
public void invalidConfCache(final Boolean needToPushToCandidat) {
cacheManager.getCache(CacheConfig.CACHE_CONF_ETAB).clear();
loadBalancingController.askToReloadData(ConstanteUtils.CACHE_SPRING_CONF, needToPushToCandidat);
}

/**
* Invalide le cache de la config des ressrouces (images, fichiers siscol, templates)
*/
public void invalidRessourcesCacheWithoutAskToReloadData() {
public void invalidRessourcesCache(final Boolean needToPushToCandidat) {
cacheManager.getCache(CacheConfig.CACHE_CONF_RESSOURCE).clear();
}

/**
* Invalide le cache de la config (config etab, url pegase, ..)
*/
public void invalidConfCache() {
cacheManager.getCache(CacheConfig.CACHE_CONF_ETAB).clear();
loadBalancingController.askToReloadData(ConstanteUtils.CACHE_SPRING_CONF, true);
}

/**
* Invalide le cache de la config des ressrouces (images, fichiers siscol, templates)
*/
public void invalidRessourcesCache() {
cacheManager.getCache(CacheConfig.CACHE_CONF_RESSOURCE).clear();
loadBalancingController.askToReloadData(ConstanteUtils.CACHE_SPRING_RESSOURCES, true);
loadBalancingController.askToReloadData(ConstanteUtils.CACHE_SPRING_RESSOURCES, needToPushToCandidat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public void saveConfigEtab(final ConfigEtab config) {
saveConfigEtabItem(config.getAssistContactUrl(), Configuration.COD_CONFIG_ETAB_ASSIST_CONTACT_URL);

Notification.show(applicationContext.getMessage("config.save", null, UI.getCurrent().getLocale()), Type.TRAY_NOTIFICATION);
cacheController.invalidConfCache();
cacheController.invalidConfCache(true);
}

/**
Expand Down Expand Up @@ -508,7 +508,7 @@ public void saveConfigPegaseAuth(final ConfigPegaseAuthEtab configPegaseAuthEtab
configurationRepository.saveAndFlush(new Configuration(Configuration.COD_CONFIG_PEGASE_AUTH_PWD, CryptoUtils.encrypt(configPegaseAuthEtab.getPwd(), cryptoSecret, cryptoSalt)));
configurationRepository.saveAndFlush(new Configuration(Configuration.COD_CONFIG_PEGASE_AUTH_ETAB, configPegaseAuthEtab.getEtab()));
Notification.show(applicationContext.getMessage("config.save", null, UI.getCurrent().getLocale()), Type.TRAY_NOTIFICATION);
cacheController.invalidConfCache();
cacheController.invalidConfCache(true);
}

/**
Expand Down Expand Up @@ -595,7 +595,7 @@ public void saveConfigPegaseUrl(final ConfigPegaseUrl configPegaseUrl) {
configurationRepository.saveAndFlush(new Configuration(Configuration.COD_CONFIG_PEGASE_URL_PARAM_TEST_COD_ETU, configPegaseUrl.getParamTestCodEtu()));
configurationRepository.saveAndFlush(new Configuration(Configuration.COD_CONFIG_PEGASE_URL_PARAM_TEST_COD_FORMATION, configPegaseUrl.getParamTestCodFormation()));
Notification.show(applicationContext.getMessage("config.save", null, UI.getCurrent().getLocale()), Type.TRAY_NOTIFICATION);
cacheController.invalidConfCache();
cacheController.invalidConfCache(true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public List<LockCandidat> getListLockMore24Heure() {
*/
public void deleteLock(final LockCandidat lock, final LockCandidatListener listener) {
final ConfirmWindow confirmWindow = new ConfirmWindow(
applicationContext.getMessage("lock.candidat.window.confirmDelete", new Object[]
{ lock.getId().getRessourceLock(), lock.getId().getNumDossierOpiCptMin() }, UI.getCurrent().getLocale()),
applicationContext.getMessage("lock.candidat.window.confirmDelete", new Object[] { lock.getId().getRessourceLock(), lock.getId().getNumDossierOpiCptMin() }, UI.getCurrent().getLocale()),
applicationContext.getMessage("lock.candidat.window.confirmDeleteTitle", null, UI.getCurrent().getLocale()));
confirmWindow.addBtnOuiListener(e -> {
if (!lockCandidatRepository.exists(lock.getId())) {
Expand Down Expand Up @@ -136,6 +135,7 @@ private String getUIId() {
* Supprime tout les locks
*/
public void cleanAllLockCandidatForInstance() {
logger.info("Nettoyage des locks pour l'instance " + loadBalancingController.getIdInstance());
lockCandidatRepository.deleteInBatch(lockCandidatRepository.findByInstanceIdLock(loadBalancingController.getIdInstance()));
}

Expand Down Expand Up @@ -255,16 +255,15 @@ public boolean getLockOrNotifyCandidature(final Candidature candidature) {
}
return getLockOrNotify(candidature.getCandidat().getCompteMinima(),
ConstanteUtils.LOCK_CAND + "_" + candidature.getIdCand(),
applicationContext.getMessage("lock.message.candidature", new Object[]
{ candidature.getCandidat().getCompteMinima().getNumDossierOpiCptMin() }, UI.getCurrent().getLocale()));
applicationContext.getMessage("lock.message.candidature", new Object[] { candidature.getCandidat().getCompteMinima().getNumDossierOpiCptMin() }, UI.getCurrent().getLocale()));
}

/**
* Verrouille une ressource pour l'UI courante
* @param cptMin
* @param ressource
* @param msgIfAlreadyLocked message affiché si la ressource est déjà verrouillée pour une autre UI. Si cette propriété vaut null, un message par défaut est
* affiché.
* affiché.
* @return true si la ressource est bien verrouillée pour l'UI courante, false sinon
*/
private boolean getLockOrNotify(final CompteMinima cptMin, final String ressource, String msgIfAlreadyLocked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class ConstanteUtils {
public static final String SISCOL_TYP_PEGASE = "P";
public static final String SISCOL_TYP_DEFAULT = "D";

/* Fichier de properties */
public final static String PROPERTY_FILE_PATH = "config.location";

/* Parametres Servlet */
public static final String SERVLET_ALL_MATCH = "/*";
public static final String SERVLET_NO_MATCH = "/nomatchingpossible";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@
*/
package fr.univlorraine.ecandidat.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import ch.qos.logback.classic.jul.LevelChangePropagator;
import ch.qos.logback.core.Context;

public class LoggerStartupListener extends LevelChangePropagator {

String[] LOG_MODE_POSSIBLE = new String[] { "traceFull", "trace", "debug", "info" };

public final static String PROPERTY_FILE_PATH = "config.location";
private static final String PROPERTY_LOG_MODE = "logMode";
private static final String DEFAULT_LOG_MODE = "info";

Expand All @@ -55,23 +50,10 @@ public void start() {
}

/* Si un fichier de properties est fourni, on récupère la valeur de logMode */
final String systemFilePropertiesPath = System.getProperty(PROPERTY_FILE_PATH);
if (StringUtils.isNotBlank(systemFilePropertiesPath)) {
try {
final Properties properties = new Properties();
final File fileConfig = new File(systemFilePropertiesPath);
if (fileConfig.exists() && fileConfig.isFile()) {
try (FileInputStream file = new FileInputStream(fileConfig)) {
properties.load(file);
final String logModeProp = properties.getProperty(PROPERTY_LOG_MODE);
if (isValidLogMode(logModeProp)) {
logMode = logModeProp;
}
}
}
} catch (final Exception e) {

}
final Properties properties = MethodUtils.loadPropertieFile();
final String logModeProp = properties.getProperty(PROPERTY_LOG_MODE);
if (isValidLogMode(logModeProp)) {
logMode = logModeProp;
}
context.putProperty("LOG_MODE", logMode);

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/fr/univlorraine/ecandidat/utils/MethodUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
Expand All @@ -44,6 +45,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.StringTokenizer;
Expand Down Expand Up @@ -1327,4 +1329,25 @@ public static File getExternalResource(final String externalRessourceFolder, fin
}
return null;
}

/**
* Charge le fichier de properties
* @return les properties
*/
public static Properties loadPropertieFile() {
final Properties properties = new Properties();
try {
final String systemFilePropertiesPath = System.getProperty(ConstanteUtils.PROPERTY_FILE_PATH);
if (StringUtils.isNotBlank(systemFilePropertiesPath)) {
final File fileConfig = new File(systemFilePropertiesPath);
if (fileConfig.exists() && fileConfig.isFile()) {
try (FileInputStream file = new FileInputStream(fileConfig)) {
properties.load(file);
}
}
}
} catch (final Exception e) {
}
return properties;
}
}

0 comments on commit 7183b93

Please sign in to comment.