Subversion Repositories XServices

Rev

Rev 198 | Rev 201 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 199
1
/*
1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with 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
6
 *   You may obtain a copy of the License at
7
 *
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
14
 *   limitations under the License.
15
 */
15
 */
16
 
16
 
17
package net.brutex.xservices.ws.impl;
17
package net.brutex.xservices.ws.impl;
18
 
-
 
19
import java.util.Enumeration;
-
 
20
import java.util.Properties;
-
 
21
import java.util.UUID;
-
 
22
 
-
 
23
import javax.jws.WebService;
18
 
24
import net.brutex.xservices.types.HostConnection;
19
import net.brutex.xservices.types.*;
25
import net.brutex.xservices.types.HostinfoType;
20
import net.brutex.xservices.types.alfevent.ALFEventResponseType;
26
import net.brutex.xservices.types.MailMimeType;
-
 
27
import net.brutex.xservices.types.ReturnCode;
21
import net.brutex.xservices.types.alfevent.ALFEventType;
28
import net.brutex.xservices.types.RuntimeInfoType;
22
import net.brutex.xservices.types.alfevent.ObjectFactory;
29
import net.brutex.xservices.types.ant.FileSetResource;
23
import net.brutex.xservices.types.ant.FileSetResource;
30
import net.brutex.xservices.util.BrutexNamespaces;
24
import net.brutex.xservices.util.EventEmitter;
31
import net.brutex.xservices.util.RunTask;
25
import net.brutex.xservices.util.RunTask;
32
import net.brutex.xservices.ws.MiscService;
-
 
33
 
26
import net.brutex.xservices.ws.MiscService;
34
import org.apache.cxf.annotations.WSDLDocumentation;
-
 
35
import org.apache.cxf.annotations.WSDLDocumentationCollection;
27
import net.brutex.xservices.ws.XServicesFault;
36
import org.apache.tools.ant.taskdefs.HostInfo;
28
import org.apache.tools.ant.taskdefs.HostInfo;
37
import org.apache.tools.ant.taskdefs.Sleep;
29
import org.apache.tools.ant.taskdefs.Sleep;
38
import org.apache.tools.ant.taskdefs.email.EmailTask;
30
import org.apache.tools.ant.taskdefs.email.EmailTask;
-
 
31
import org.h2.jdbcx.JdbcConnectionPool;
-
 
32
import org.quartz.*;
-
 
33
import org.quartz.impl.StdSchedulerFactory;
-
 
34
 
-
 
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
 
39
 
57
 
40
/**
58
/**
41
 * Implements the web service
59
 * Implements the web service
42
 * 
60
 * 
43
 * @author Brian Rosenberger, bru@brutex.de
61
 * @author Brian Rosenberger, bru@brutex.de
44
 */
62
 */
45
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.MiscService", serviceName="MiscService")
63
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.MiscService", serviceName="MiscService")
46
public class MiscServiceImpl
64
public class MiscServiceImpl
47
  implements MiscService
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;";
48
{
73
 
49
  public HostinfoType getHostinfo(String hostname)
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 {
50
  {
88
    }
-
 
89
 
-
 
90
    public HostinfoType getHostinfo(String hostname) {
51
    HostInfo info = new HostInfo();
91
    HostInfo info = new HostInfo();
52
    info.setTaskName("HostInfo");
92
    info.setTaskName("HostInfo");
53
    RunTask runner = new RunTask(info);
93
    RunTask runner = new RunTask(info);
54
    info.setHost(hostname);
94
    info.setHost(hostname);
55
 
95
 
56
    ReturnCode ret = runner.postTask();
96
    ReturnCode ret = runner.postTask();
57
    HostinfoType infotype = new HostinfoType(
97
    HostinfoType infotype = new HostinfoType(
58
      ret.getProperty("NAME"), 
98
            ret.getProperty("NAME"),
59
      ret.getProperty("DOMAIN"), 
99
            ret.getProperty("DOMAIN"),
60
      ret.getProperty("ADDR4"), 
100
            ret.getProperty("ADDR4"),
61
      ret.getProperty("ADDR6"));
101
            ret.getProperty("ADDR6"));
62
    return infotype;
102
    return infotype;
63
  }
103
  }
64
 
104
 
65
  public ReturnCode getInfo() {
105
  public ReturnCode getInfo() {
66
    ReturnCode r = new ReturnCode();
106
    ReturnCode r = new ReturnCode();
67
    r.returnCode = 0;
107
    r.returnCode = 0;
68
 
108
 
69
    Properties props = System.getProperties();
109
    Properties props = System.getProperties();
70
 
110
 
71
    Enumeration e = props.propertyNames();
111
    Enumeration e = props.propertyNames();
72
    while (e.hasMoreElements())
112
    while (e.hasMoreElements()) {
73
    {
-
 
74
      String propName = (String)e.nextElement();
113
      String propName = (String) e.nextElement();
75
 
114
 
76
      String propValue = (String)props.get(propName);
115
      String propValue = (String) props.get(propName);
77
      r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
116
      r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
78
    }
117
    }
79
 
118
 
80
    return r;
119
    return r;
81
  }
120
  }
82
 
121
 
83
  public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message)
-
 
84
  {
122
  public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message) {
85
    return sendMail(from, from, tolist, "", "", subject, message, 
123
    return sendMail(from, from, tolist, "", "", subject, message,
86
      "text/plain", null, mailhost.hostname, mailhost.port, 
124
            "text/plain", null, mailhost.hostname, mailhost.port,
87
      mailhost.user, mailhost.password, "utf-8", false, false);
125
            mailhost.user, mailhost.password, "utf-8", false, false);
88
  }
126
  }
89
 
127
 
90
  public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res)
-
 
91
  {
128
  public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res) {
92
    return sendMail(from, from, tolist, "", "", subject, message, 
129
    return sendMail(from, from, tolist, "", "", subject, message,
93
      "text/plain", res, mailhost.hostname, mailhost.port, 
130
            "text/plain", res, mailhost.hostname, mailhost.port,
94
      mailhost.user, mailhost.password, "utf-8", false, false);
131
            mailhost.user, mailhost.password, "utf-8", false, false);
95
  }
132
  }
96
 
133
 
97
  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)
-
 
98
  {
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) {
99
    return sendMail(from, from, tolist, cclist, bcclist, subject, message, 
135
    return sendMail(from, from, tolist, cclist, bcclist, subject, message,
100
      mimetype.value(), res, mailhost.hostname, mailhost.port, 
136
            mimetype.value(), res, mailhost.hostname, mailhost.port,
101
      mailhost.user, mailhost.password, charset, tls, ssl);
137
            mailhost.user, mailhost.password, charset, tls, ssl);
102
  }
138
  }
103
 
139
 
104
  public ReturnCode sleep(int minutes, int seconds) {
140
  public ReturnCode sleep(int minutes, int seconds) {
105
    return sleep(0, minutes, seconds, 0);
141
    return sleep(0, minutes, seconds, 0);
106
  }
142
  }
107
 
143
 
108
  public String generateUUID() {
144
  public String generateUUID() {
109
    return UUID.randomUUID().toString();
145
    return UUID.randomUUID().toString();
110
  }
146
  }
111
 
147
 
112
  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)
-
 
113
  {
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
    EmailTask mail = new EmailTask();
149
    EmailTask mail = new EmailTask();
115
    mail.setTaskName("Mail");
150
    mail.setTaskName("Mail");
116
    RunTask runner = new RunTask(mail);
151
    RunTask runner = new RunTask(mail);
117
    mail.setFrom(from);
152
    mail.setFrom(from);
118
    mail.setReplyTo(replyto);
153
    mail.setReplyTo(replyto);
119
    mail.setToList(tolist);
154
    mail.setToList(tolist);
120
    mail.setCcList(cclist);
155
    mail.setCcList(cclist);
121
    mail.setBccList(bcclist);
156
    mail.setBccList(bcclist);
122
    mail.setSubject(subject);
157
    mail.setSubject(subject);
123
    mail.setMessage(message);
158
    mail.setMessage(message);
124
    mail.setMessageMimeType(messagemimetype);
159
    mail.setMessageMimeType(messagemimetype);
125
    if (attachments != null) {
160
    if (attachments != null) {
126
      mail.addFileset(attachments.getAntResource(mail.getProject()));
161
      mail.addFileset(attachments.getAntResource(mail.getProject()));
127
    }
162
    }
128
    mail.setMailhost(mailhost);
163
    mail.setMailhost(mailhost);
129
    mail.setMailport(mailport);
164
    mail.setMailport(mailport);
130
    mail.setUser(user);
165
    mail.setUser(user);
131
    mail.setPassword(password);
166
    mail.setPassword(password);
132
    mail.setCharset(charset);
167
    mail.setCharset(charset);
133
    mail.setSSL(ssl);
168
    mail.setSSL(ssl);
134
    mail.setEnableStartTLS(tls);
169
    mail.setEnableStartTLS(tls);
135
    return runner.postTask();
170
    return runner.postTask();
136
  }
171
  }
137
 
172
 
138
  private ReturnCode sleep(int hours, int minutes, int seconds, int milliseconds)
-
 
139
  {
173
  private ReturnCode sleep(int hours, int minutes, int seconds, int milliseconds) {
140
    Sleep sleep = new Sleep();
174
    Sleep sleep = new Sleep();
141
    sleep.setTaskName("Sleep");
175
    sleep.setTaskName("Sleep");
142
    RunTask runner = new RunTask(sleep);
176
    RunTask runner = new RunTask(sleep);
143
    sleep.setHours(hours);
177
    sleep.setHours(hours);
144
    sleep.setMinutes(minutes);
178
    sleep.setMinutes(minutes);
145
    sleep.setSeconds(seconds);
179
    sleep.setSeconds(seconds);
146
    sleep.setMilliseconds(milliseconds);
180
    sleep.setMilliseconds(milliseconds);
147
    return runner.postTask();
181
    return runner.postTask();
148
  }
182
  }
149
 
183
 
150
  public RuntimeInfoType getMemory() {
184
  public RuntimeInfoType getMemory() {
151
    return new RuntimeInfoType();
185
    return new RuntimeInfoType();
152
  }
186
  }
-
 
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
  }
153
}
328
}
154
 
329
 
155
Generated by GNU Enscript 1.6.5.90.
330
Generated by GNU Enscript 1.6.5.90.
156
 
331
 
157
 
332