Subversion Repositories XServices

Rev

Rev 198 | Rev 201 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 199
Line 14... Line 14...
14
 *   limitations under the License.
14
 *   limitations under the License.
15
 */
15
 */
Line 16... Line 16...
16
 
16
 
Line 17... Line -...
17
package net.brutex.xservices.ws.impl;
-
 
18
 
-
 
19
import java.util.Enumeration;
-
 
20
import java.util.Properties;
-
 
21
import java.util.UUID;
-
 
22
 
17
package net.brutex.xservices.ws.impl;
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;
-
 
26
import net.brutex.xservices.types.MailMimeType;
20
import net.brutex.xservices.types.alfevent.ALFEventResponseType;
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;
-
 
32
import net.brutex.xservices.ws.MiscService;
25
import net.brutex.xservices.util.RunTask;
33
 
-
 
34
import org.apache.cxf.annotations.WSDLDocumentation;
26
import net.brutex.xservices.ws.MiscService;
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;
-
 
29
import org.apache.tools.ant.taskdefs.Sleep;
-
 
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
 
Line 37... Line 55...
37
import org.apache.tools.ant.taskdefs.Sleep;
55
import static org.quartz.TriggerBuilder.newTrigger;
38
import org.apache.tools.ant.taskdefs.email.EmailTask;
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
 */
-
 
63
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.MiscService", serviceName="MiscService")
-
 
64
public class MiscServiceImpl
-
 
65
  implements MiscService {
-
 
66
 
-
 
67
  @Resource
-
 
68
  private WebServiceContext context;
-
 
69
 
45
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.MiscService", serviceName="MiscService")
70
  // Grab the Scheduler instance from the Factory
46
public class MiscServiceImpl
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);" +
47
  implements MiscService
85
          "CREATE INDEX IF NOT EXISTS brutex.IDX_TO_DESC ON brutex.tbl_events (btx_timestamp ASC);";
-
 
86
 
-
 
87
    public MiscServiceImpl() throws SchedulerException {
48
{
88
    }
49
  public HostinfoType getHostinfo(String hostname)
89
 
50
  {
90
    public HostinfoType getHostinfo(String hostname) {
51
    HostInfo info = new HostInfo();
91
    HostInfo info = new HostInfo();
Line 52... Line 92...
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"),
Line 60... Line 100...
60
      ret.getProperty("ADDR4"), 
100
            ret.getProperty("ADDR4"),
61
      ret.getProperty("ADDR6"));
101
            ret.getProperty("ADDR6"));
62
    return infotype;
102
    return infotype;
Line 63... Line 103...
63
  }
103
  }
Line 64... Line 104...
64
 
104
 
65
  public ReturnCode getInfo() {
105
  public ReturnCode getInfo() {
66
    ReturnCode r = new ReturnCode();
-
 
67
    r.returnCode = 0;
106
    ReturnCode r = new ReturnCode();
Line 68... Line 107...
68
 
107
    r.returnCode = 0;
69
    Properties props = System.getProperties();
108
 
70
 
109
    Properties props = System.getProperties();
Line 71... Line 110...
71
    Enumeration e = props.propertyNames();
110
 
72
    while (e.hasMoreElements())
111
    Enumeration e = props.propertyNames();
Line 73... Line 112...
73
    {
112
    while (e.hasMoreElements()) {
74
      String propName = (String)e.nextElement();
-
 
75
 
113
      String propName = (String) e.nextElement();
76
      String propValue = (String)props.get(propName);
114
 
77
      r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
115
      String propValue = (String) props.get(propName);
78
    }
116
      r.stdOut = (r.stdOut + propName + ": " + propValue + "\n");
79
 
117
    }
80
    return r;
118
 
81
  }
-
 
82
 
119
    return r;
83
  public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message)
120
  }
84
  {
121
 
85
    return sendMail(from, from, tolist, "", "", subject, message, 
122
  public ReturnCode sendMailSimple(HostConnection mailhost, String from, String tolist, String subject, String message) {
86
      "text/plain", null, mailhost.hostname, mailhost.port, 
123
    return sendMail(from, from, tolist, "", "", subject, message,
87
      mailhost.user, mailhost.password, "utf-8", false, false);
124
            "text/plain", null, mailhost.hostname, mailhost.port,
88
  }
-
 
89
 
125
            mailhost.user, mailhost.password, "utf-8", false, false);
90
  public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res)
126
  }
91
  {
127
 
92
    return sendMail(from, from, tolist, "", "", subject, message, 
128
  public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost, String from, String tolist, String subject, String message, FileSetResource res) {
Line 93... Line 129...
93
      "text/plain", res, mailhost.hostname, mailhost.port, 
129
    return sendMail(from, from, tolist, "", "", subject, message,
94
      mailhost.user, mailhost.password, "utf-8", false, false);
130
            "text/plain", res, mailhost.hostname, mailhost.port,
95
  }
131
            mailhost.user, mailhost.password, "utf-8", false, false);
Line 96... Line 132...
96
 
132
  }
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)
133
 
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) {
Line 99... Line 135...
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, 
-
 
101
      mailhost.user, mailhost.password, charset, tls, ssl);
136
            mimetype.value(), res, mailhost.hostname, mailhost.port,
102
  }
137
            mailhost.user, mailhost.password, charset, tls, ssl);
103
 
138
  }
104
  public ReturnCode sleep(int minutes, int seconds) {
139
 
105
    return sleep(0, minutes, seconds, 0);
140
  public ReturnCode sleep(int minutes, int seconds) {
Line 133... Line 168...
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
  }
Line 137... Line 172...
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);
Line 148... Line 182...
148
  }
182
  }
Line 149... Line 183...
149
 
183
 
150
  public RuntimeInfoType getMemory() {
184
  public RuntimeInfoType getMemory() {
151
    return new RuntimeInfoType();
185
    return new RuntimeInfoType();
-
 
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();
152
  }
327
  }
153
}
328
}