Subversion Repositories XServices

Compare Revisions

Ignore whitespace Rev 201 → Rev 203

//xservices/trunk/src/main/java/net/brutex/xservices/ws/impl/MiscServiceImpl.java
41,6 → 41,7
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import java.io.StringReader;
59,258 → 60,288
 
/**
* Implements the web service
*
*
* @author Brian Rosenberger, bru@brutex.de
*/
@Slf4j
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.MiscService", serviceName="MiscService")
@WebService(targetNamespace = "http://ws.xservices.brutex.net", endpointInterface = "net.brutex.xservices.ws.MiscService", serviceName = "MiscService")
public class MiscServiceImpl
implements MiscService {
implements MiscService {
 
@Resource
private WebServiceContext context;
@Resource
private WebServiceContext context;
 
public MiscServiceImpl() throws SchedulerException {
}
 
public HostinfoType getHostinfo(String hostname) {
HostInfo info = new HostInfo();
info.setTaskName("HostInfo");
RunTask runner = new RunTask(info);
info.setHost(hostname);
HostInfo info = new HostInfo();
info.setTaskName("HostInfo");
RunTask runner = new RunTask(info);
info.setHost(hostname);
 
ReturnCode ret = runner.postTask();
HostinfoType infotype = new HostinfoType(
ret.getProperty("NAME"),
ret.getProperty("DOMAIN"),
ret.getProperty("ADDR4"),
ret.getProperty("ADDR6"));
return infotype;
}
ReturnCode ret = runner.postTask();
HostinfoType infotype = new HostinfoType(
ret.getProperty("NAME"),
ret.getProperty("DOMAIN"),
ret.getProperty("ADDR4"),
ret.getProperty("ADDR6"));
return infotype;
}
 
public ReturnCode getInfo() {
ReturnCode r = new ReturnCode();
r.returnCode = 0;
public ReturnCode getInfo() {
ReturnCode r = new ReturnCode();
r.returnCode = 0;
 
Properties props = System.getProperties();
Properties props = System.getProperties();
 
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String propName = (String) e.nextElement();
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String propName = (String) e.nextElement();
 
String propValue = (String) props.get(propName);
r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
String propValue = (String) props.get(propName);
r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
}
 
return r;
}
 
return r;
}
public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message) {
return sendMail(from, from, tolist, "", "", subject, message,
"text/plain", null, mailhost.hostname, mailhost.port,
mailhost.user, mailhost.password, "utf-8", false, false);
}
 
public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message) {
return sendMail(from, from, tolist, "", "", subject, message,
"text/plain", null, mailhost.hostname, mailhost.port,
mailhost.user, mailhost.password, "utf-8", false, false);
}
public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res) {
return sendMail(from, from, tolist, "", "", subject, message,
"text/plain", res, mailhost.hostname, mailhost.port,
mailhost.user, mailhost.password, "utf-8", false, false);
}
 
public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res) {
return sendMail(from, from, tolist, "", "", subject, message,
"text/plain", res, mailhost.hostname, mailhost.port,
mailhost.user, mailhost.password, "utf-8", false, false);
}
public ReturnCode sendMail(HostConnection mailhost, String from, String tolist, String cclist, String bcclist, String subject, MailMimeType mimetype, String charset, String message, FileSetResource res, boolean ssl, boolean tls) {
return sendMail(from, from, tolist, cclist, bcclist, subject, message,
mimetype.value(), res, mailhost.hostname, mailhost.port,
mailhost.user, mailhost.password, charset, tls, ssl);
}
 
public ReturnCode sendMail(HostConnection mailhost, String from, String tolist, String cclist, String bcclist, String subject, MailMimeType mimetype, String charset, String message, FileSetResource res, boolean ssl, boolean tls) {
return sendMail(from, from, tolist, cclist, bcclist, subject, message,
mimetype.value(), res, mailhost.hostname, mailhost.port,
mailhost.user, mailhost.password, charset, tls, ssl);
}
public ReturnCode sleep(int minutes, int seconds) {
return sleep(0, minutes, seconds, 0);
}
 
public ReturnCode sleep(int minutes, int seconds) {
return sleep(0, minutes, seconds, 0);
}
public String generateUUID() {
return UUID.randomUUID().toString();
}
 
public String generateUUID() {
return UUID.randomUUID().toString();
}
private ReturnCode sendMail(String from, String replyto, String tolist, String cclist, String bcclist, String subject, String message, String messagemimetype, FileSetResource attachments, String mailhost, int mailport, String user, String password, String charset, boolean tls, boolean ssl) {
EmailTask mail = new EmailTask();
mail.setTaskName("Mail");
RunTask runner = new RunTask(mail);
mail.setFrom(from);
mail.setReplyTo(replyto);
mail.setToList(tolist);
mail.setCcList(cclist);
mail.setBccList(bcclist);
mail.setSubject(subject);
mail.setMessage(message);
mail.setMessageMimeType(messagemimetype);
if (attachments != null) {
mail.addFileset(attachments.getAntResource(mail.getProject()));
}
mail.setMailhost(mailhost);
mail.setMailport(mailport);
mail.setUser(user);
mail.setPassword(password);
mail.setCharset(charset);
mail.setSSL(ssl);
mail.setEnableStartTLS(tls);
return runner.postTask();
}
 
private ReturnCode sendMail(String from, String replyto, String tolist, String cclist, String bcclist, String subject, String message, String messagemimetype, FileSetResource attachments, String mailhost, int mailport, String user, String password, String charset, boolean tls, boolean ssl) {
EmailTask mail = new EmailTask();
mail.setTaskName("Mail");
RunTask runner = new RunTask(mail);
mail.setFrom(from);
mail.setReplyTo(replyto);
mail.setToList(tolist);
mail.setCcList(cclist);
mail.setBccList(bcclist);
mail.setSubject(subject);
mail.setMessage(message);
mail.setMessageMimeType(messagemimetype);
if (attachments != null) {
mail.addFileset(attachments.getAntResource(mail.getProject()));
private ReturnCode sleep(int hours, int minutes, int seconds, int milliseconds) {
Sleep sleep = new Sleep();
sleep.setTaskName("Sleep");
RunTask runner = new RunTask(sleep);
sleep.setHours(hours);
sleep.setMinutes(minutes);
sleep.setSeconds(seconds);
sleep.setMilliseconds(milliseconds);
return runner.postTask();
}
mail.setMailhost(mailhost);
mail.setMailport(mailport);
mail.setUser(user);
mail.setPassword(password);
mail.setCharset(charset);
mail.setSSL(ssl);
mail.setEnableStartTLS(tls);
return runner.postTask();
}
 
private ReturnCode sleep(int hours, int minutes, int seconds, int milliseconds) {
Sleep sleep = new Sleep();
sleep.setTaskName("Sleep");
RunTask runner = new RunTask(sleep);
sleep.setHours(hours);
sleep.setMinutes(minutes);
sleep.setSeconds(seconds);
sleep.setMilliseconds(milliseconds);
return runner.postTask();
}
public RuntimeInfoType getMemory() {
return new RuntimeInfoType();
}
 
public RuntimeInfoType getMemory() {
return new RuntimeInfoType();
}
@Override
public BigInteger lock(String id, String objectId) throws XServicesFault {
 
@Override
public BigInteger lock(String id, String objectId) throws XServicesFault {
 
final String conString = "jdbc:h2:mem:lockdb;DB_CLOSE_DELAY=10;" +
"INIT=CREATE SCHEMA IF NOT EXISTS brutex\\;" +
// "SET SCHEMA brutex\\;" +
"CREATE SEQUENCE IF NOT EXISTS brutex.btx_sequence1\\;" +
"CREATE TABLE IF NOT EXISTS brutex.tbl_lock (btx_seq BIGINT NOT NULL, btx_id VARCHAR(100) NOT NULL, btx_obj_id VARCHAR(100) NOT NULL, btx_timestamp BIGINT NOT NULL);";
 
//JdbcConnectionPool cp = JdbcConnectionPool.create(conString, "sa", "");
//cp.setMaxConnections(1);
 
final String conString = "jdbc:h2:mem:lockdb;DB_CLOSE_DELAY=10;" +
"INIT=CREATE SCHEMA IF NOT EXISTS brutex\\;" +
// "SET SCHEMA brutex\\;" +
"CREATE SEQUENCE IF NOT EXISTS brutex.btx_sequence1\\;" +
"CREATE TABLE IF NOT EXISTS brutex.tbl_lock (btx_seq BIGINT NOT NULL, btx_id VARCHAR(100) NOT NULL, btx_obj_id VARCHAR(100) NOT NULL, btx_timestamp BIGINT NOT NULL);";
Connection con = null;
long rows = 0L;
final long ts = new Date().getTime();
try {
Class.forName("org.h2.Driver"); //Java 1.8
con = DriverManager.getConnection(conString);
PreparedStatement prep = con.prepareStatement(
"SELECT btx_id from brutex.tbl_lock where btx_obj_id=? ORDER BY btx_seq DESC");
prep.setString(1, objectId);
 
//JdbcConnectionPool cp = JdbcConnectionPool.create(conString, "sa", "");
//cp.setMaxConnections(1);
ResultSet rs = prep.executeQuery();
StringBuffer bf = new StringBuffer();
while (rs.next()) {
//bf.append(rs.getString(1));
rows++;
}
rs.close();
 
Connection con = null;
long rows = 0L;
final long ts = new Date().getTime();
try {
Class.forName("org.h2.Driver"); //Java 1.8
con = DriverManager.getConnection(conString);
PreparedStatement prep = con.prepareStatement(
"SELECT btx_id from brutex.tbl_lock where btx_obj_id=? ORDER BY btx_seq DESC");
prep.setString(1, objectId);
prep = con.prepareStatement("INSERT INTO brutex.tbl_lock values (NEXT VALUE FOR brutex.btx_sequence1, ?, ?, ?)");
prep.setString(1, id);
prep.setString(2, objectId);
prep.setLong(3, ts);
prep.execute();
 
ResultSet rs = prep.executeQuery();
StringBuffer bf = new StringBuffer();
while (rs.next()) {
//bf.append(rs.getString(1));
rows++;
}
rs.close();
prep = con.prepareStatement("DELETE from brutex.tbl_lock WHERE btx_timestamp < ?");
prep.setLong(1, ts - 10000);
prep.execute();
prep.close();
 
prep = con.prepareStatement("INSERT INTO brutex.tbl_lock values (NEXT VALUE FOR brutex.btx_sequence1, ?, ?, ?)");
prep.setString(1, id);
prep.setString(2, objectId);
prep.setLong(3, ts);
prep.execute();
con.close();
//System.out.println(bf);
} catch (SQLException | ClassNotFoundException e) {
throw new XServicesFault(e);
}
 
prep = con.prepareStatement("DELETE from brutex.tbl_lock WHERE btx_timestamp < ?");
prep.setLong(1, ts - 10000);
prep.execute();
prep.close();
 
con.close();
//System.out.println(bf);
} catch (SQLException | ClassNotFoundException e) {
throw new XServicesFault(e);
return BigInteger.valueOf(rows);
}
 
return BigInteger.valueOf(rows);
}
@Override
public ALFEventResponseType mergeALFEvent(ALFEventType event) throws XServicesFault {
final Instant d = Instant.now();
final long ts = d.toEpochMilli();
 
@Override
public ALFEventResponseType mergeALFEvent(ALFEventType event) throws XServicesFault {
final Instant d = Instant.now();
final long ts = d.toEpochMilli();
MessageContext cont = context.getMessageContext();
 
//Get Parameters from the Servlet Context
final ServletContext servletContext =
(ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
final EventmanagerConfiguration conf = (EventmanagerConfiguration) servletContext
.getAttribute(EventmanagerConfiguration.KEY);
//Get Parameters from the Servlet Context
final ServletContext servletContext =
(ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
final EventmanagerConfiguration conf = (EventmanagerConfiguration) servletContext
.getAttribute(EventmanagerConfiguration.KEY);
 
final JdbcConnectionPool pool = (JdbcConnectionPool) servletContext.getAttribute("mdbConnection");
final JdbcConnectionPool fpool = (JdbcConnectionPool) servletContext.getAttribute("fdbConnection");
final AtomicLong egres_counter = (AtomicLong) servletContext.getAttribute("egres_counter");
final AtomicLong ingres_counter = (AtomicLong) servletContext.getAttribute("ingres_counter");
final Scheduler scheduler = (Scheduler) servletContext.getAttribute("scheduler");
final JdbcConnectionPool pool = (JdbcConnectionPool) servletContext.getAttribute("mdbConnection");
final JdbcConnectionPool fpool = (JdbcConnectionPool) servletContext.getAttribute("fdbConnection");
final AtomicLong egres_counter = (AtomicLong) servletContext.getAttribute("egres_counter");
final AtomicLong ingres_counter = (AtomicLong) servletContext.getAttribute("ingres_counter");
final Scheduler scheduler = (Scheduler) servletContext.getAttribute("scheduler");
 
log.trace("Read dbConnection from servlet context: {}", pool);
log.trace("Read dbConnection from servlet context: {}", pool);
 
//System.out.println("Step 2: " + ChronoUnit.MILLIS.between(Instant.now(), d));
final ObjectFactory of = new ObjectFactory();
//System.out.println("Step 2: " + ChronoUnit.MILLIS.between(Instant.now(), d));
final ObjectFactory of = new ObjectFactory();
 
final String objectId = event.getBase().getObjectId();
final String eventId = event.getBase().getEventId();
final String objectType = event.getBase().getObjectType();
final String eventType = event.getBase().getEventType();
log.debug("Event id '{}', type '{}' received for object '{}' with object_id '{}'.",
eventId, eventType, objectType, objectId);
final String objectId = event.getBase().getObjectId();
final String eventId = event.getBase().getEventId();
final String objectType = event.getBase().getObjectType();
final String eventType = event.getBase().getEventType();
log.debug("Event id '{}', type '{}' received for object '{}' with object_id '{}'.",
eventId, eventType, objectType, objectId);
 
final String mergeStatememt = "MERGE INTO brutex.tbl_events " +
"KEY (btx_event_type, btx_obj_type, btx_obj_id) " +
"VALUES (?,?,?,?,?,?);";
final String mergeStatememt = "SELECT btx_id FROM OLD TABLE (MERGE INTO brutex.tbl_events " +
"KEY (btx_event_type, btx_obj_type, btx_obj_id) " +
"VALUES (?,?,?,?,?,?));";
 
final String insertAll = "INSERT INTO brutex.tbl_events_all VALUES (?,?,?,?,?,?,?)";
 
try {
Marshaller m = JAXBContext.newInstance(ALFEventType.class).createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
JAXBElement<ALFEventType> e = of.createEventNotice(event);
StringWriter sw = new StringWriter();
m.marshal(e, sw);
StringBuilder sb = new StringBuilder();
sb.append(" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:ns=\"http://www.eclipse.org/alf/schema/EventBase/1\">\n");
sb.append("<soapenv:Body>\n");
sb.append("<ns:EventNotice>\n");
sb.append(sw);
sb.append("</ns:EventNotice>\n");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
 
Connection con = pool.getConnection();
PreparedStatement prep = con.prepareStatement(mergeStatememt);
prep.setString(1, eventType);
prep.setString(2, eventId);
prep.setString(3, objectType);
prep.setString(4, objectId);
prep.setLong(5, ts);
prep.setClob(6, new StringReader(sb.toString()));
prep.execute();
con.commit();
con.close();
try (Connection con = pool.getConnection()) {
Marshaller m = JAXBContext.newInstance(ALFEventType.class).createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
JAXBElement<ALFEventType> e = of.createALFEventNoticeDoc(event);
StringWriter sw = new StringWriter();
m.marshal(e, sw);
StringBuilder sb = new StringBuilder();
 
ingres_counter.incrementAndGet();
sb.append(" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:ns=\"http://www.eclipse.org/alf/schema/EventBase/1\">\n");
sb.append("<soapenv:Body>\n");
 
synchronized (scheduler) {
if (!scheduler.checkExists(JobKey.jobKey("ALFEmitter"))) {
JobDetail job2 = JobBuilder.newJob(EventEmitter.class)
.withIdentity("ALFEmitter")
.build();
job2.getJobDataMap().put("mdbConnection", pool);
job2.getJobDataMap().put("fdbConnection", fpool);
job2.getJobDataMap().put("run_key", ts);
job2.getJobDataMap().put("egres_counter", egres_counter);
job2.getJobDataMap().put("ingres_counter", ingres_counter);
sb.append(sw);
 
job2.getJobDataMap().put(EventmanagerConfiguration.KEY, conf);
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
 
SimpleTrigger t = (SimpleTrigger) newTrigger()
.withIdentity("ALFEmitter").startAt(Date.from(d.plusSeconds(conf.getInterval())))
.build();
 
scheduler.scheduleJob(job2, t);
}
}
} catch (JAXBException | SQLException | SchedulerException e) {
log.error(e.getMessage());
throw new XServicesFault(e);
PreparedStatement prep = con.prepareStatement(mergeStatememt);
prep.setString(1, eventType);
prep.setString(2, eventId);
prep.setString(3, objectType);
prep.setString(4, objectId);
prep.setLong(5, ts);
prep.setClob(6, new StringReader(sb.toString()));
ResultSet r = prep.executeQuery();
con.commit();
String supersed_id = null;
if (r.next()) {
supersed_id = r.getString(1);
log.trace("Event '{}' superseded event '{}'", eventId, supersed_id);
} else {
log.trace("Event '{}' inserted into outgoing queue.", eventId);
}
 
 
/* Write all into permanent storage */
//Connection fcon = fpool.getConnection();
prep = con.prepareStatement(insertAll);
prep.setString(1, eventType);
prep.setString(2, eventId);
prep.setString(3, objectType);
prep.setString(4, objectId);
prep.setLong(5, ts);
prep.setString(6, supersed_id);
prep.setClob(7, new StringReader(sb.toString()));
prep.execute();
 
con.commit();
con.close();
 
ingres_counter.incrementAndGet();
 
if (conf.isEmitterActive()) {
synchronized (scheduler) {
if (!scheduler.checkExists(JobKey.jobKey("ALFEmitter"))) {
JobDetail job2 = JobBuilder.newJob(EventEmitter.class)
.withIdentity("ALFEmitter")
.build();
job2.getJobDataMap().put("mdbConnection", pool);
job2.getJobDataMap().put("fdbConnection", fpool);
job2.getJobDataMap().put("run_key", ts);
job2.getJobDataMap().put("egres_counter", egres_counter);
job2.getJobDataMap().put("ingres_counter", ingres_counter);
 
job2.getJobDataMap().put(EventmanagerConfiguration.KEY, conf);
 
SimpleTrigger t = (SimpleTrigger) newTrigger()
.withIdentity("ALFEmitter").startAt(Date.from(d.plusSeconds(conf.getInterval())))
.build();
 
scheduler.scheduleJob(job2, t);
}
}
}
} catch (JAXBException | SQLException | SchedulerException e) {
log.error(e.getMessage());
throw new XServicesFault(e);
}
return of.createALFEventResponseType();
}
return of.createALFEventResponseType();
}
}