fixes bug #7405
git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Web/trunk/ventaboletos@55415 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
273baed541
commit
4c428f5dee
|
@ -0,0 +1,91 @@
|
|||
package com.rjconsultores.ventaboletos.web.utilerias;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class IPValidatorUtileria {
|
||||
|
||||
private static Logger log = Logger.getLogger(IPValidatorUtileria.class);
|
||||
|
||||
private Pattern pattern;
|
||||
private Matcher matcher;
|
||||
|
||||
private static final String IPADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
|
||||
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
|
||||
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
|
||||
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
|
||||
|
||||
public IPValidatorUtileria() {
|
||||
pattern = Pattern.compile(IPADDRESS_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate ip address with regular expression
|
||||
*
|
||||
* @param ip
|
||||
* ip address for validation
|
||||
* @return true valid ip address, false invalid ip address
|
||||
*/
|
||||
public boolean validate(final String ip) {
|
||||
matcher = pattern.matcher(ip);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
public static String getIp() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getIpExternalAWS() {
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
URL whatismyip = new URL("http://checkip.amazonaws.com");
|
||||
in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
|
||||
String ip = in.readLine();
|
||||
return ip;
|
||||
} catch (MalformedURLException e1) {
|
||||
log.error("", e1);
|
||||
} catch (IOException e) {
|
||||
log.error("", e);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean ipExternoException(String ipServidor) {
|
||||
if (ipServidor.equals(getIpExternalAWS())) {
|
||||
log.info(" IP capturado: " + getIpExternalAWS());
|
||||
log.info(" IP: " + ipServidor);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Current IP address : " + IPValidatorUtileria.getIp());
|
||||
System.out.println("Current IP address AWS: " + IPValidatorUtileria.getIpExternalAWS());
|
||||
}
|
||||
}
|
|
@ -4,14 +4,9 @@
|
|||
*/
|
||||
package com.rjconsultores.ventaboletos.web.utilerias;
|
||||
|
||||
import com.rjconsultores.ventaboletos.entidad.Constante;
|
||||
import com.rjconsultores.ventaboletos.service.ConstanteService;
|
||||
import com.rjconsultores.ventaboletos.web.gui.controladores.job.GeneracionCorridaJob;
|
||||
import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.zkoss.util.resource.Labels;
|
||||
import org.zkoss.zk.ui.WebApp;
|
||||
import org.quartz.JobDetail;
|
||||
import org.quartz.Scheduler;
|
||||
import org.quartz.SchedulerException;
|
||||
|
@ -21,6 +16,13 @@ import org.quartz.TriggerUtils;
|
|||
import org.quartz.impl.StdSchedulerFactory;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.zkoss.util.resource.Labels;
|
||||
import org.zkoss.zk.ui.WebApp;
|
||||
|
||||
import com.rjconsultores.ventaboletos.entidad.Constante;
|
||||
import com.rjconsultores.ventaboletos.service.ConstanteService;
|
||||
import com.rjconsultores.ventaboletos.web.gui.controladores.job.GeneracionCorridaJob;
|
||||
import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -28,117 +30,123 @@ import org.springframework.context.ApplicationContext;
|
|||
*/
|
||||
public class MyAppInit implements org.zkoss.zk.ui.util.WebAppInit {
|
||||
|
||||
private static Logger log = Logger.getLogger(MyAppInit.class);
|
||||
SchedulerFactory schedFact = new StdSchedulerFactory();
|
||||
Scheduler sched;
|
||||
private static Logger log = Logger.getLogger(MyAppInit.class);
|
||||
SchedulerFactory schedFact = new StdSchedulerFactory();
|
||||
Scheduler sched;
|
||||
|
||||
public Scheduler getSched() {
|
||||
return sched;
|
||||
}
|
||||
public Scheduler getSched() {
|
||||
return sched;
|
||||
}
|
||||
|
||||
public void setSched(Scheduler sched) {
|
||||
this.sched = sched;
|
||||
}
|
||||
public void setSched(Scheduler sched) {
|
||||
this.sched = sched;
|
||||
}
|
||||
|
||||
public SchedulerFactory getSchedFact() {
|
||||
return schedFact;
|
||||
}
|
||||
public SchedulerFactory getSchedFact() {
|
||||
return schedFact;
|
||||
}
|
||||
|
||||
public void setSchedFact(SchedulerFactory schedFact) {
|
||||
this.schedFact = schedFact;
|
||||
}
|
||||
public void setSchedFact(SchedulerFactory schedFact) {
|
||||
this.schedFact = schedFact;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(WebApp wapp) throws Exception {
|
||||
Labels.register(new MyLabelLocatorGeneral((ServletContext) wapp.getNativeContext()));
|
||||
Labels.register(new MyLabelLocatorCliente((ServletContext) wapp.getNativeContext()));
|
||||
@Override
|
||||
public void init(WebApp wapp) throws Exception {
|
||||
Labels.register(new MyLabelLocatorGeneral((ServletContext) wapp.getNativeContext()));
|
||||
Labels.register(new MyLabelLocatorCliente((ServletContext) wapp.getNativeContext()));
|
||||
|
||||
//Generacion Automatica de Corridas
|
||||
jobGeneracionCorridas();
|
||||
}
|
||||
Constante constanteServidorException = getConstanteService().buscarPorNomeConstante("SERVIDOR_EXCEPCION_GENERACION_CORRIDA");
|
||||
String ipConstante = constanteServidorException == null ? "" : constanteServidorException.getValorconstante();
|
||||
if (!IPValidatorUtileria.ipExternoException(ipConstante)) {
|
||||
// Generacion Automatica de Corridas
|
||||
jobGeneracionCorridas();
|
||||
}
|
||||
}
|
||||
|
||||
private Integer[] horaExecucion() {
|
||||
private Integer[] horaExecucion() {
|
||||
|
||||
ApplicationContext appContext = AppContext.getApplicationContext();
|
||||
Constante constante = getConstanteService().buscarPorNomeConstante("HORA_GENERACION_CORRIDA");
|
||||
|
||||
BeanFactory factory = (BeanFactory) appContext;
|
||||
ConstanteService cs = (ConstanteService) factory.getBean("constanteService");
|
||||
Integer[] arrayHoraMinuto = new Integer[2];
|
||||
arrayHoraMinuto[0] = 1;
|
||||
arrayHoraMinuto[1] = 0;
|
||||
|
||||
Constante constante = cs.buscarPorNomeConstante("HORA_GENERACION_CORRIDA");
|
||||
if ((constante == null) || (constante.getValorconstante() == null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Integer[] arrayHoraMinuto = new Integer[2];
|
||||
arrayHoraMinuto[0] = 1;
|
||||
arrayHoraMinuto[1] = 0;
|
||||
int hora = 1;
|
||||
int minuto = 0;
|
||||
try {
|
||||
String[] vlrContante = constante.getValorconstante().split(":");
|
||||
|
||||
if ( (constante == null) || (constante.getValorconstante() == null)) {
|
||||
return null;
|
||||
}
|
||||
hora = Integer.parseInt(vlrContante[0]);
|
||||
minuto = Integer.parseInt(vlrContante[1]);
|
||||
if (hora < 0 || hora > 23) {
|
||||
hora = 1;
|
||||
}
|
||||
if (minuto < 0 || minuto > 59) {
|
||||
minuto = 0;
|
||||
}
|
||||
|
||||
int hora = 1;
|
||||
int minuto = 0;
|
||||
try {
|
||||
String[] vlrContante = constante.getValorconstante().split(":");
|
||||
arrayHoraMinuto[0] = hora;
|
||||
arrayHoraMinuto[1] = minuto;
|
||||
|
||||
hora = Integer.parseInt(vlrContante[0]);
|
||||
minuto = Integer.parseInt(vlrContante[1]);
|
||||
if (hora < 0 || hora > 23) {
|
||||
hora = 1;
|
||||
}
|
||||
if (minuto < 0 || minuto > 59) {
|
||||
minuto = 0;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Param hora geracion corrida invalido : " + constante.getValorconstante());
|
||||
arrayHoraMinuto[0] = 1;
|
||||
arrayHoraMinuto[1] = 0;
|
||||
}
|
||||
|
||||
arrayHoraMinuto[0] = hora;
|
||||
arrayHoraMinuto[1] = minuto;
|
||||
return arrayHoraMinuto;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Param hora geracion corrida invalido : " + constante.getValorconstante());
|
||||
arrayHoraMinuto[0] = 1;
|
||||
arrayHoraMinuto[1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return arrayHoraMinuto;
|
||||
private void jobGeneracionCorridas() {
|
||||
try {
|
||||
sched = schedFact.getScheduler();
|
||||
|
||||
}
|
||||
JobDetail jobDetail = new JobDetail("GeneracionCorridas", null, GeneracionCorridaJob.class);
|
||||
|
||||
private void jobGeneracionCorridas() {
|
||||
try {
|
||||
sched = schedFact.getScheduler();
|
||||
Integer[] hora = horaExecucion();
|
||||
|
||||
JobDetail jobDetail = new JobDetail("GeneracionCorridas", null, GeneracionCorridaJob.class);
|
||||
if (hora == null) {
|
||||
log.debug("constante HORA_GENERACION_CORRIDA não habilitada.");
|
||||
return;
|
||||
}
|
||||
|
||||
Integer[] hora = horaExecucion();
|
||||
|
||||
if(hora == null){
|
||||
log.debug("constante HORA_GENERACION_CORRIDA não habilitada.");
|
||||
return;
|
||||
}
|
||||
log.debug("Horario de geracao de corrida : " + hora[0] + ":" + hora[1]);
|
||||
|
||||
log.debug("Horario de geracao de corrida : " + hora[0] + ":" + hora[1]);
|
||||
Trigger trigger = TriggerUtils.makeDailyTrigger("generacionCorridasTrigger", hora[0], hora[1]);
|
||||
|
||||
Trigger trigger = TriggerUtils.makeDailyTrigger("generacionCorridasTrigger", hora[0], hora[1]);
|
||||
// TESTE TRIGGER
|
||||
// Trigger trigger = TriggerUtils.makeImmediateTrigger("generacionCorridasTrigger", 1, 1000);
|
||||
|
||||
//TESTE TRIGGER
|
||||
//Trigger trigger = TriggerUtils.makeImmediateTrigger("generacionCorridasTrigger", 1, 1000);
|
||||
trigger.setName("generacionCorridasTrigger");
|
||||
|
||||
trigger.setName("generacionCorridasTrigger");
|
||||
|
||||
JobDetail job = sched.getJobDetail(jobDetail.getName(), jobDetail.getGroup());
|
||||
JobDetail job = sched.getJobDetail(jobDetail.getName(), jobDetail.getGroup());
|
||||
|
||||
log.info("Job=" + jobDetail.getName() + "." + jobDetail.getGroup());
|
||||
|
||||
log.info("Job="+jobDetail.getName() +"." +jobDetail.getGroup());
|
||||
|
||||
if (job != null) {
|
||||
log.info("Job ya existia");
|
||||
boolean deleted= sched.deleteJob(job.getName(), Scheduler.DEFAULT_GROUP);
|
||||
log.info("Deleted="+deleted);
|
||||
boolean deleted = sched.deleteJob(job.getName(), Scheduler.DEFAULT_GROUP);
|
||||
log.info("Deleted=" + deleted);
|
||||
}
|
||||
|
||||
sched.scheduleJob(jobDetail, trigger);
|
||||
|
||||
sched.start();
|
||||
sched.scheduleJob(jobDetail, trigger);
|
||||
|
||||
} catch (SchedulerException ex) {
|
||||
log.error(ex);
|
||||
}
|
||||
}
|
||||
sched.start();
|
||||
|
||||
} catch (SchedulerException ex) {
|
||||
log.error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private ConstanteService getConstanteService() {
|
||||
ApplicationContext appContext = AppContext.getApplicationContext();
|
||||
BeanFactory factory = (BeanFactory) appContext;
|
||||
ConstanteService cs = (ConstanteService) factory.getBean("constanteService");
|
||||
return cs;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue