Subversion Repositories XServices

Rev

Rev 198 | Rev 201 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 brianR 1
/*
114 brianR 2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
16 brianR 3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
46 brianR 17
package net.brutex.xservices.ws.impl;
16 brianR 18
 
199 brianR 19
import net.brutex.xservices.types.*;
20
import net.brutex.xservices.types.alfevent.ALFEventResponseType;
21
import net.brutex.xservices.types.alfevent.ALFEventType;
22
import net.brutex.xservices.types.alfevent.ObjectFactory;
114 brianR 23
import net.brutex.xservices.types.ant.FileSetResource;
199 brianR 24
import net.brutex.xservices.util.EventEmitter;
17 brianR 25
import net.brutex.xservices.util.RunTask;
46 brianR 26
import net.brutex.xservices.ws.MiscService;
199 brianR 27
import net.brutex.xservices.ws.XServicesFault;
17 brianR 28
import org.apache.tools.ant.taskdefs.HostInfo;
20 brianR 29
import org.apache.tools.ant.taskdefs.Sleep;
18 brianR 30
import org.apache.tools.ant.taskdefs.email.EmailTask;
199 brianR 31
import org.h2.jdbcx.JdbcConnectionPool;
32
import org.quartz.*;
33
import org.quartz.impl.StdSchedulerFactory;
16 brianR 34
 
199 brianR 35
import javax.annotation.Resource;
36
import javax.jws.WebService;
37
import javax.servlet.ServletContext;
38
import javax.xml.bind.JAXBContext;
39
import javax.xml.bind.JAXBElement;
40
import javax.xml.bind.JAXBException;
41
import javax.xml.bind.Marshaller;
42
import javax.xml.ws.WebServiceContext;
43
import javax.xml.ws.handler.MessageContext;
44
import java.io.StringReader;
45
import java.io.StringWriter;
46
import java.math.BigInteger;
47
import java.sql.*;
48
import java.time.Instant;
49
import java.time.temporal.ChronoUnit;
50
import java.util.Date;
51
import java.util.Enumeration;
52
import java.util.Properties;
53
import java.util.UUID;
54
 
55
import static org.quartz.TriggerBuilder.newTrigger;
56
 
57
 
16 brianR 58
/**
114 brianR 59
 * Implements the web service
46 brianR 60
 *
16 brianR 61
 * @author Brian Rosenberger, bru@brutex.de
62
 */
114 brianR 63
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.MiscService", serviceName="MiscService")
64
public class MiscServiceImpl
199 brianR 65
  implements MiscService {
66
 
67
  @Resource
68
  private WebServiceContext context;
69
 
70
  // Grab the Scheduler instance from the Factory
71
  private final Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
72
  private final static String conStr = "jdbc:h2:mem:lockdb;DB_CLOSE_DELAY=-1;";
73
 
74
  private final static String dbinit = "" +
75
          "CREATE SCHEMA IF NOT EXISTS brutex;" +
76
          "CREATE TABLE IF NOT EXISTS brutex.tbl_events (" +
77
          "btx_event_type VARCHAR(128) NOT NULL," +
78
          "btx_id VARCHAR(32) NOT NULL, " +
79
          "btx_obj_type VARCHAR(32) NOT NULL, " +
80
          "btx_obj_id VARCHAR(32) NOT NULL, " +
81
          "btx_timestamp BIGINT NOT NULL," +
82
          "btx_event CLOB" +
83
          ");" +
84
          "CREATE INDEX IF NOT EXISTS brutex.btx_idx ON brutex.tbl_events (btx_obj_id, btx_obj_type, btx_event_type);" +
85
          "CREATE INDEX IF NOT EXISTS brutex.IDX_TO_DESC ON brutex.tbl_events (btx_timestamp ASC);";
86
 
87
    public MiscServiceImpl() throws SchedulerException {
88
    }
89
 
90
    public HostinfoType getHostinfo(String hostname) {
114 brianR 91
    HostInfo info = new HostInfo();
92
    info.setTaskName("HostInfo");
93
    RunTask runner = new RunTask(info);
94
    info.setHost(hostname);
16 brianR 95
 
114 brianR 96
    ReturnCode ret = runner.postTask();
97
    HostinfoType infotype = new HostinfoType(
199 brianR 98
            ret.getProperty("NAME"),
99
            ret.getProperty("DOMAIN"),
100
            ret.getProperty("ADDR4"),
101
            ret.getProperty("ADDR6"));
114 brianR 102
    return infotype;
103
  }
16 brianR 104
 
114 brianR 105
  public ReturnCode getInfo() {
106
    ReturnCode r = new ReturnCode();
107
    r.returnCode = 0;
54 brianR 108
 
114 brianR 109
    Properties props = System.getProperties();
54 brianR 110
 
114 brianR 111
    Enumeration e = props.propertyNames();
199 brianR 112
    while (e.hasMoreElements()) {
113
      String propName = (String) e.nextElement();
54 brianR 114
 
199 brianR 115
      String propValue = (String) props.get(propName);
114 brianR 116
      r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
117
    }
97 brianR 118
 
114 brianR 119
    return r;
120
  }
18 brianR 121
 
199 brianR 122
  public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message) {
123
    return sendMail(from, from, tolist, "", "", subject, message,
124
            "text/plain", null, mailhost.hostname, mailhost.port,
125
            mailhost.user, mailhost.password, "utf-8", false, false);
114 brianR 126
  }
21 brianR 127
 
199 brianR 128
  public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res) {
129
    return sendMail(from, from, tolist, "", "", subject, message,
130
            "text/plain", res, mailhost.hostname, mailhost.port,
131
            mailhost.user, mailhost.password, "utf-8", false, false);
114 brianR 132
  }
21 brianR 133
 
199 brianR 134
  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) {
135
    return sendMail(from, from, tolist, cclist, bcclist, subject, message,
136
            mimetype.value(), res, mailhost.hostname, mailhost.port,
137
            mailhost.user, mailhost.password, charset, tls, ssl);
114 brianR 138
  }
21 brianR 139
 
114 brianR 140
  public ReturnCode sleep(int minutes, int seconds) {
141
    return sleep(0, minutes, seconds, 0);
142
  }
22 brianR 143
 
114 brianR 144
  public String generateUUID() {
145
    return UUID.randomUUID().toString();
146
  }
22 brianR 147
 
199 brianR 148
  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) {
114 brianR 149
    EmailTask mail = new EmailTask();
150
    mail.setTaskName("Mail");
151
    RunTask runner = new RunTask(mail);
152
    mail.setFrom(from);
153
    mail.setReplyTo(replyto);
154
    mail.setToList(tolist);
155
    mail.setCcList(cclist);
156
    mail.setBccList(bcclist);
157
    mail.setSubject(subject);
158
    mail.setMessage(message);
159
    mail.setMessageMimeType(messagemimetype);
160
    if (attachments != null) {
161
      mail.addFileset(attachments.getAntResource(mail.getProject()));
162
    }
163
    mail.setMailhost(mailhost);
164
    mail.setMailport(mailport);
165
    mail.setUser(user);
166
    mail.setPassword(password);
167
    mail.setCharset(charset);
168
    mail.setSSL(ssl);
169
    mail.setEnableStartTLS(tls);
170
    return runner.postTask();
171
  }
172
 
199 brianR 173
  private ReturnCode sleep(int hours, int minutes, int seconds, int milliseconds) {
114 brianR 174
    Sleep sleep = new Sleep();
175
    sleep.setTaskName("Sleep");
176
    RunTask runner = new RunTask(sleep);
177
    sleep.setHours(hours);
178
    sleep.setMinutes(minutes);
179
    sleep.setSeconds(seconds);
180
    sleep.setMilliseconds(milliseconds);
181
    return runner.postTask();
182
  }
183
 
184
  public RuntimeInfoType getMemory() {
185
    return new RuntimeInfoType();
186
  }
199 brianR 187
 
188
  @Override
189
  public BigInteger lock(String id, String objectId) throws XServicesFault {
190
 
191
 
192
 
193
    final String conString = "jdbc:h2:mem:lockdb;DB_CLOSE_DELAY=10;" +
194
            "INIT=CREATE SCHEMA IF NOT EXISTS brutex\\;" +
195
            // "SET SCHEMA brutex\\;" +
196
            "CREATE SEQUENCE IF NOT EXISTS brutex.btx_sequence1\\;" +
197
            "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);";
198
 
199
    //JdbcConnectionPool cp = JdbcConnectionPool.create(conString, "sa", "");
200
    //cp.setMaxConnections(1);
201
 
202
    Connection con = null;
203
    long rows = 0L;
204
    final long ts = new Date().getTime();
205
    try {
206
      Class.forName("org.h2.Driver"); //Java 1.8
207
      con = DriverManager.getConnection(conString);
208
      PreparedStatement prep = con.prepareStatement(
209
              "SELECT btx_id from brutex.tbl_lock where btx_obj_id=? ORDER BY btx_seq DESC");
210
      prep.setString(1, objectId);
211
 
212
      ResultSet rs = prep.executeQuery();
213
      StringBuffer bf = new StringBuffer();
214
      while (rs.next()) {
215
        //bf.append(rs.getString(1));
216
        rows++;
217
      }
218
      rs.close();
219
 
220
      prep = con.prepareStatement("INSERT INTO brutex.tbl_lock values (NEXT VALUE FOR brutex.btx_sequence1, ?, ?, ?)");
221
      prep.setString(1, id);
222
      prep.setString(2, objectId);
223
      prep.setLong(3, ts);
224
      prep.execute();
225
 
226
      prep = con.prepareStatement("DELETE from brutex.tbl_lock WHERE btx_timestamp < ?");
227
      prep.setLong(1, ts - 10000);
228
      prep.execute();
229
      prep.close();
230
 
231
      con.close();
232
      //System.out.println(bf);
233
    } catch (SQLException | ClassNotFoundException e) {
234
      throw new XServicesFault(e);
235
    }
236
 
237
    return BigInteger.valueOf(rows);
238
  }
239
 
240
  @Override
241
  public ALFEventResponseType mergeALFEvent(ALFEventType event) throws XServicesFault {
242
    final Instant d = Instant.now();
243
    final long ts = d.toEpochMilli();
244
    //System.out.println("Step 1: " + ChronoUnit.MILLIS.between(Instant.now(), d));
245
 
246
    final ServletContext servletContext =
247
            (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
248
    final JdbcConnectionPool pool = (JdbcConnectionPool) servletContext.getAttribute("dbConnection");
249
    //System.out.println("Step 2: " + ChronoUnit.MILLIS.between(Instant.now(), d));
250
    final ObjectFactory of = new ObjectFactory();
251
 
252
    final String objectId = event.getBase().getObjectId();
253
    final String eventId = event.getBase().getEventId();
254
    final String objectType = event.getBase().getObjectType();
255
    final String eventType = event.getBase().getEventType();
256
 
257
    final String mergeStatememt = "MERGE INTO brutex.tbl_events " +
258
            "KEY (btx_event_type, btx_obj_type, btx_obj_id) " +
259
            "VALUES (?,?,?,?,?,?) " +
260
            "";
261
 
262
    long rows = 0L;
263
    //System.out.println("Step 3: " + ChronoUnit.MILLIS.between(Instant.now(), d));
264
    try {
265
      //System.out.println("Step 4: " + ChronoUnit.MILLIS.between(Instant.now(), d));
266
      Marshaller m = JAXBContext.newInstance(ALFEventType.class).createMarshaller();
267
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
268
      m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
269
      JAXBElement<ALFEventType> e = of.createEventNotice(event);
270
      StringWriter sw = new StringWriter();
271
      m.marshal(e, sw);
272
      StringBuilder sb = new StringBuilder();
273
      sb.append(" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://www.eclipse.org/alf/schema/EventBase/1\">\n");
274
      sb.append("<soapenv:Body>\n");
275
      sb.append("<ns:EventNotice>\n");
276
      sb.append(sw);
277
      sb.append("</ns:EventNotice>\n");
278
      sb.append("</soapenv:Body>");
279
      sb.append("</soapenv:Envelope>");
280
      //System.out.println("Step 5: " + ChronoUnit.MILLIS.between(Instant.now(), d));
281
      Connection con = pool.getConnection();
282
      //System.out.println("Step 6: " + ChronoUnit.MILLIS.between(Instant.now(), d));
283
      PreparedStatement prep = con.prepareStatement(mergeStatememt);
284
      prep.setString(1, eventType);
285
      prep.setString(2, eventId);
286
      prep.setString(3, objectType);
287
      prep.setString(4, objectId);
288
      prep.setLong(5, ts);
289
      prep.setClob(6, new StringReader(sb.toString()));
290
      //prep.setLong(7, ts-20000);
291
      //System.out.println("Step 7 SQL START: " + ChronoUnit.MILLIS.between(Instant.now(), d));
292
      prep.execute();
293
      con.commit();
294
      con.close();
295
      //System.out.println("Step 8 SQL END: " + ChronoUnit.MILLIS.between(Instant.now(), d));
296
      //SimpleSoap ss = new SimpleSoap("http://localhost:8099/ALFEventManager/services/ALFEventManagerSOAP", sb.toString());
297
      //ss.sendSoap(false);
298
 
299
 
300
      // and start it off
301
 
302
      if (!scheduler.isStarted())
303
        scheduler.start();
304
      if (scheduler.isInStandbyMode())
305
        scheduler.resumeAll();
306
      //System.out.println("Step 9: " + ChronoUnit.MILLIS.between(Instant.now(), d));
307
      synchronized (scheduler) {
308
          if (!scheduler.checkExists(JobKey.jobKey("ALFEmitter"))) {
309
            JobDetail job2 = JobBuilder.newJob(EventEmitter.class)
310
                    .withIdentity("ALFEmitter").build();
311
            //job2.getJobDataMap().put("script", job.getScript());
312
            //job2.getJobDataMap().put("description", job.getDescription());
313
            //job2.getJobDataMap().put("date", job.getDate());
314
 
315
            SimpleTrigger t = (SimpleTrigger) newTrigger()
316
                    .withIdentity("ALFEmitter").startAt(Date.from(d.plusSeconds(20)))
317
                    .build();
318
 
319
            scheduler.scheduleJob(job2, t);
320
        }
321
      }
322
      //System.out.println("Step 10: " + ChronoUnit.MILLIS.between(Instant.now(), d));
323
    } catch (JAXBException | SQLException | SchedulerException e) {
324
      throw new XServicesFault(e);
325
    }
326
    return of.createALFEventResponseType();
327
  }
114 brianR 328
}