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;
|
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 javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.zkoss.util.resource.Labels;
|
|
||||||
import org.zkoss.zk.ui.WebApp;
|
|
||||||
import org.quartz.JobDetail;
|
import org.quartz.JobDetail;
|
||||||
import org.quartz.Scheduler;
|
import org.quartz.Scheduler;
|
||||||
import org.quartz.SchedulerException;
|
import org.quartz.SchedulerException;
|
||||||
|
@ -21,6 +16,13 @@ import org.quartz.TriggerUtils;
|
||||||
import org.quartz.impl.StdSchedulerFactory;
|
import org.quartz.impl.StdSchedulerFactory;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
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 {
|
public class MyAppInit implements org.zkoss.zk.ui.util.WebAppInit {
|
||||||
|
|
||||||
private static Logger log = Logger.getLogger(MyAppInit.class);
|
private static Logger log = Logger.getLogger(MyAppInit.class);
|
||||||
SchedulerFactory schedFact = new StdSchedulerFactory();
|
SchedulerFactory schedFact = new StdSchedulerFactory();
|
||||||
Scheduler sched;
|
Scheduler sched;
|
||||||
|
|
||||||
public Scheduler getSched() {
|
public Scheduler getSched() {
|
||||||
return sched;
|
return sched;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSched(Scheduler sched) {
|
public void setSched(Scheduler sched) {
|
||||||
this.sched = sched;
|
this.sched = sched;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SchedulerFactory getSchedFact() {
|
public SchedulerFactory getSchedFact() {
|
||||||
return schedFact;
|
return schedFact;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSchedFact(SchedulerFactory schedFact) {
|
public void setSchedFact(SchedulerFactory schedFact) {
|
||||||
this.schedFact = schedFact;
|
this.schedFact = schedFact;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(WebApp wapp) throws Exception {
|
public void init(WebApp wapp) throws Exception {
|
||||||
Labels.register(new MyLabelLocatorGeneral((ServletContext) wapp.getNativeContext()));
|
Labels.register(new MyLabelLocatorGeneral((ServletContext) wapp.getNativeContext()));
|
||||||
Labels.register(new MyLabelLocatorCliente((ServletContext) wapp.getNativeContext()));
|
Labels.register(new MyLabelLocatorCliente((ServletContext) wapp.getNativeContext()));
|
||||||
|
|
||||||
//Generacion Automatica de Corridas
|
Constante constanteServidorException = getConstanteService().buscarPorNomeConstante("SERVIDOR_EXCEPCION_GENERACION_CORRIDA");
|
||||||
jobGeneracionCorridas();
|
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;
|
Integer[] arrayHoraMinuto = new Integer[2];
|
||||||
ConstanteService cs = (ConstanteService) factory.getBean("constanteService");
|
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];
|
int hora = 1;
|
||||||
arrayHoraMinuto[0] = 1;
|
int minuto = 0;
|
||||||
arrayHoraMinuto[1] = 0;
|
try {
|
||||||
|
String[] vlrContante = constante.getValorconstante().split(":");
|
||||||
|
|
||||||
if ( (constante == null) || (constante.getValorconstante() == null)) {
|
hora = Integer.parseInt(vlrContante[0]);
|
||||||
return null;
|
minuto = Integer.parseInt(vlrContante[1]);
|
||||||
}
|
if (hora < 0 || hora > 23) {
|
||||||
|
hora = 1;
|
||||||
|
}
|
||||||
|
if (minuto < 0 || minuto > 59) {
|
||||||
|
minuto = 0;
|
||||||
|
}
|
||||||
|
|
||||||
int hora = 1;
|
arrayHoraMinuto[0] = hora;
|
||||||
int minuto = 0;
|
arrayHoraMinuto[1] = minuto;
|
||||||
try {
|
|
||||||
String[] vlrContante = constante.getValorconstante().split(":");
|
|
||||||
|
|
||||||
hora = Integer.parseInt(vlrContante[0]);
|
} catch (Exception e) {
|
||||||
minuto = Integer.parseInt(vlrContante[1]);
|
log.error("Param hora geracion corrida invalido : " + constante.getValorconstante());
|
||||||
if (hora < 0 || hora > 23) {
|
arrayHoraMinuto[0] = 1;
|
||||||
hora = 1;
|
arrayHoraMinuto[1] = 0;
|
||||||
}
|
}
|
||||||
if (minuto < 0 || minuto > 59) {
|
|
||||||
minuto = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
arrayHoraMinuto[0] = hora;
|
return arrayHoraMinuto;
|
||||||
arrayHoraMinuto[1] = minuto;
|
|
||||||
|
|
||||||
} 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() {
|
Integer[] hora = horaExecucion();
|
||||||
try {
|
|
||||||
sched = schedFact.getScheduler();
|
|
||||||
|
|
||||||
JobDetail jobDetail = new JobDetail("GeneracionCorridas", null, GeneracionCorridaJob.class);
|
if (hora == null) {
|
||||||
|
log.debug("constante HORA_GENERACION_CORRIDA não habilitada.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Integer[] hora = horaExecucion();
|
log.debug("Horario de geracao de corrida : " + hora[0] + ":" + hora[1]);
|
||||||
|
|
||||||
if(hora == null){
|
Trigger trigger = TriggerUtils.makeDailyTrigger("generacionCorridasTrigger", hora[0], hora[1]);
|
||||||
log.debug("constante HORA_GENERACION_CORRIDA não habilitada.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("Horario de geracao de corrida : " + hora[0] + ":" + hora[1]);
|
// TESTE TRIGGER
|
||||||
|
// Trigger trigger = TriggerUtils.makeImmediateTrigger("generacionCorridasTrigger", 1, 1000);
|
||||||
|
|
||||||
Trigger trigger = TriggerUtils.makeDailyTrigger("generacionCorridasTrigger", hora[0], hora[1]);
|
trigger.setName("generacionCorridasTrigger");
|
||||||
|
|
||||||
//TESTE TRIGGER
|
JobDetail job = sched.getJobDetail(jobDetail.getName(), jobDetail.getGroup());
|
||||||
//Trigger trigger = TriggerUtils.makeImmediateTrigger("generacionCorridasTrigger", 1, 1000);
|
|
||||||
|
|
||||||
trigger.setName("generacionCorridasTrigger");
|
log.info("Job=" + jobDetail.getName() + "." + jobDetail.getGroup());
|
||||||
|
|
||||||
JobDetail job = sched.getJobDetail(jobDetail.getName(), jobDetail.getGroup());
|
|
||||||
|
|
||||||
log.info("Job="+jobDetail.getName() +"." +jobDetail.getGroup());
|
|
||||||
|
|
||||||
if (job != null) {
|
if (job != null) {
|
||||||
log.info("Job ya existia");
|
log.info("Job ya existia");
|
||||||
boolean deleted= sched.deleteJob(job.getName(), Scheduler.DEFAULT_GROUP);
|
boolean deleted = sched.deleteJob(job.getName(), Scheduler.DEFAULT_GROUP);
|
||||||
log.info("Deleted="+deleted);
|
log.info("Deleted=" + deleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
sched.scheduleJob(jobDetail, trigger);
|
sched.scheduleJob(jobDetail, trigger);
|
||||||
|
|
||||||
sched.start();
|
sched.start();
|
||||||
|
|
||||||
} catch (SchedulerException ex) {
|
} catch (SchedulerException ex) {
|
||||||
log.error(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