112 lines
3.6 KiB
Plaintext
Executable File
112 lines
3.6 KiB
Plaintext
Executable File
package com.fp.base.persistence.util.job;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.util.List;
|
|
|
|
import com.fp.common.helper.Constant;
|
|
import com.fp.common.logger.APPLogger;
|
|
import com.fp.dto.rules.Service;
|
|
import com.fp.persistence.commondb.PersistenceHelper;
|
|
import com.fp.persistence.pgeneral.gene.TgeneServices;
|
|
|
|
/**
|
|
* Clase que se encarga de manejar serviciso o demonios que estan activos en el servidor de aplicaciones.
|
|
*
|
|
* @author Jorge Vaca
|
|
* @version 2.1
|
|
*/
|
|
public class ServiceManager {
|
|
|
|
/**
|
|
* Metodo que inicia todos los servicios declarados como automaticos, el proceso lo ejecuta cuando sube el servidor
|
|
* de aplicaciones.
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public void autoamaticInit() throws Exception {
|
|
List<TgeneServices> ldata = TgeneServices.find(PersistenceHelper.getEntityManager());
|
|
for (TgeneServices obj : ldata) {
|
|
try {
|
|
if(Constant.ifYes(obj.getAutomatic()) ){
|
|
this.startByService(obj);
|
|
}
|
|
} catch (Exception e) {
|
|
APPLogger.getLogger().error("***********NO PUEDE INICIAR EL SERVICIO********** :"+obj.getPk()+" DEFINIDO EN TgeneServices ***********");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Metodo que inicia un servicio, dado un codigo de servicio.
|
|
*
|
|
* @param pService Codigo de servicio a iniciar
|
|
* @throws Exception
|
|
*/
|
|
public void startService(String pService) throws Exception {
|
|
TgeneServices tgeneServices = TgeneServices.find(PersistenceHelper.getEntityManager(), pService);
|
|
this.startByService(tgeneServices);
|
|
}
|
|
|
|
/**
|
|
* Metodo que detiene un servicio, dado un codigo de servicio.
|
|
*
|
|
* @param pService Codigo de servicio a detener.
|
|
* @throws Exception
|
|
*/
|
|
public void stopService(String pService) throws Exception {
|
|
TgeneServices tgeneServices = TgeneServices.find(PersistenceHelper.getEntityManager(), pService);
|
|
this.stopByService(tgeneServices);
|
|
}
|
|
|
|
/**
|
|
* Metodo que inicia un servicio.
|
|
*
|
|
* @param pTgeneServices Objeto que contiene la defincion de un servicio.
|
|
* @throws Exception
|
|
*/
|
|
private void startByService(TgeneServices pTgeneServices) throws Exception {
|
|
Service service = this.getService(pTgeneServices);
|
|
service.start();
|
|
}
|
|
|
|
/**
|
|
* Metodo que detiene un servicio.
|
|
*
|
|
* @param pTgeneServices Objeto que contiene la defincion de un servicio.
|
|
* @throws Exception
|
|
*/
|
|
private void stopByService(TgeneServices pTgeneServices) throws Exception {
|
|
Service service = this.getService(pTgeneServices);
|
|
service.stop();
|
|
}
|
|
|
|
/**
|
|
* Metodo que indica si el servisio esta en estado de ejecucion o no.
|
|
*
|
|
* @param pService Codigo de servicio.
|
|
* @return boolean
|
|
* @throws Exception
|
|
*/
|
|
public boolean isRunning(String pService) throws Exception {
|
|
TgeneServices tgeneServices = TgeneServices.find(PersistenceHelper.getEntityManager(), pService);
|
|
Service service = this.getService(tgeneServices);
|
|
return service.isrunning();
|
|
}
|
|
|
|
/**
|
|
* Metodo que obtiene una instancia del servicio, de manera dinamica.
|
|
*
|
|
* @param pTgeneServices Objeto que contiene la defincion de un servicio.
|
|
* @return Service
|
|
* @throws Exception
|
|
*/
|
|
private Service getService(TgeneServices pTgeneServices) throws Exception {
|
|
Service service = null;
|
|
Method m = Class.forName(pTgeneServices.getClassname()).getDeclaredMethod("getInstance");
|
|
m.setAccessible(true); // if security settings allow this
|
|
service = (Service) m.invoke(null); // use null if the method is static
|
|
return service;
|
|
}
|
|
|
|
}
|