Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Add Store Update #14

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions LICENSE.EDL
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Eclipse Distribution License - v 1.0

Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Eclipse Foundation, Inc. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Now, add the following dependency:
<dependency>
<groupId>org.eclipse.lyo.store</groupId>
<artifactId>store-core</artifactId>
<version>2.4.0</version>
<version>%version%</version>
</dependency>
```
Now you are all set to start using the library.
Expand Down
14 changes: 13 additions & 1 deletion src/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>store-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Lyo Store Parent</name>
<name>Lyo Store :: Parent</name>

<properties>
<maven.deploy.skip>false</maven.deploy.skip>
Expand All @@ -22,6 +22,8 @@

<modules>
<module>store-core</module>
<module>store-sync</module>
<!-- <module>store-sync-trs</module>-->
</modules>

<dependencyManagement>
Expand Down Expand Up @@ -61,6 +63,12 @@
<artifactId>xml-apis</artifactId>
<version>1.3.04</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
Expand Down Expand Up @@ -134,5 +142,9 @@
<name>Eclipse Public License 1.0</name>
<url>https://www.eclipse.org/legal/epl-v10.html</url>
</license>
<license>
<name>Eclipse Distribution License 1.0</name>
<url>https://www.eclipse.org/org/documents/edl-v10.html</url>
</license>
</licenses>
</project>
2 changes: 2 additions & 0 deletions src/store-core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
hs_err_pid*

# Eclipse
**/bin/
**/tmp/
.project
.classpath
.metadata
Expand Down
5 changes: 5 additions & 0 deletions src/store-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</parent>

<artifactId>store-core</artifactId>
<name>Lyo Store :: Core</name>

<properties>
<lyo.version>${project.parent.version}</lyo.version>
Expand Down Expand Up @@ -247,5 +248,9 @@
<name>Eclipse Public License 1.0</name>
<url>https://www.eclipse.org/legal/epl-v10.html</url>
</license>
<license>
<name>Eclipse Distribution License 1.0</name>
<url>https://www.eclipse.org/org/documents/edl-v10.html</url>
</license>
</licenses>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
* @since 0.14.0
*/
public class ModelUnmarshallingException extends Exception {
/**
*
*/
private static final long serialVersionUID = 7760792549346636219L;

public ModelUnmarshallingException() {
super();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
* @since 0.13
*/
public class StoreAccessException extends Exception {
/**
*
*/
private static final long serialVersionUID = -7535919324037847033L;

public StoreAccessException() {
super();
}
Expand Down
30 changes: 30 additions & 0 deletions src/store-sync-trs/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>store-parent</artifactId>
<groupId>org.eclipse.lyo.store</groupId>
<version>4.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>store-sync-trs</artifactId>


<dependencies>
<dependency>
<groupId>org.eclipse.lyo.store</groupId>
<artifactId>store-sync</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-1.0 OR BSD-3-Clause
*/
package org.eclipse.lyo.store.sync.handlers;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.lyo.oslc4j.trs.server.HistoryData;
import org.eclipse.lyo.oslc4j.trs.server.TrsEventHandler;
import org.eclipse.lyo.store.Store;
import org.eclipse.lyo.store.sync.Handler;
import org.eclipse.lyo.store.sync.change.Change;
import org.eclipse.lyo.store.sync.change.ChangeKind;
import org.eclipse.lyo.store.sync.change.HistoryResource;

public class TrsHandler<M> implements Handler<M> {

private TrsEventHandler eventHandler;

public TrsHandler(TrsEventHandler trsEventHandler) {
this.eventHandler = trsEventHandler;
}

@Override
public Collection<Change<M>> handle(Store store, Collection<Change<M>> changes) {
updateChangelogHistory(changes);
return changes;
}

protected List<HistoryData> updateChangelogHistory(Collection<Change<M>> changes) {
List<HistoryData> changesHistory = changesHistory(changes);
for (HistoryData element: changesHistory
) {
if(HistoryData.CREATED.equals(element.getType()) {
// FIXME: 2020-11-09 Either pass the resource or add "light" onCreatedMeta() resources
eventHandler.onCreated();
}
}
return changesHistory;
}

private List<HistoryData> changesHistory(Collection<Change<M>> changes) {
return changes.stream().map(this::historyElementFromChange).collect(Collectors.toList());
}

private HistoryData historyElementFromChange(Change<M> change) {
HistoryResource h = change.getHistoryResource();
HistoryData historyData = HistoryData.getInstance(h.getTimestamp(), h.getResourceURI(),
historyDataType(h.getChangeKindEnum()));
return historyData;
}

private String historyDataType(ChangeKind changeKind) {
if (changeKind.equals(ChangeKind.CREATION)) {
return HistoryData.CREATED;
} else if (changeKind.equals(ChangeKind.MODIFICATION)) {
return HistoryData.MODIFIED;
} else if (changeKind.equals(ChangeKind.DELETION)) {
return HistoryData.DELETED;
} else {
throw new IllegalArgumentException("Illegal ChangeKind value");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-1.0 OR BSD-3-Clause
*/
package org.eclipse.lyo.store.sync.handlers;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.TimeZone;

import javax.xml.datatype.DatatypeConfigurationException;

import org.eclipse.lyo.core.trs.ChangeEvent;
import org.eclipse.lyo.core.trs.Creation;
import org.eclipse.lyo.core.trs.Deletion;
import org.eclipse.lyo.core.trs.Modification;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreApplicationException;
import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper;
import org.eclipse.lyo.oslc4j.trs.server.ChangeHistories;
import org.eclipse.lyo.oslc4j.trs.server.HistoryData;
import org.eclipse.lyo.store.sync.change.Change;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.jena.rdf.model.Model;

public class TrsMqttChangeLogHandler<T> extends TrsHandler<T> {
private final Logger log = LoggerFactory.getLogger(TrsMqttChangeLogHandler.class);

private final MqttClient mqttClient;
private final String topic;
private int order;

public TrsMqttChangeLogHandler(final ChangeHistories changeLog, final MqttClient mqttClient, final String topic) {
super(changeLog);
this.mqttClient = mqttClient;
this.topic = topic;
// FIXME Andrew@2018-03-11: may and should blow up, but bringing back
this.order = changeLog.getHistory(null, null).length;
}

@Override
protected List<HistoryData> updateChangelogHistory(Collection<Change<T>> changes) {
List<HistoryData> updateChangelogHistory = super.updateChangelogHistory(changes);
for (HistoryData historyData : updateChangelogHistory) {
AbstractResource res = trsChangeResourceFrom(historyData);
MqttMessage message = buildMqttMessage(res);
try {
mqttClient.publish(topic, message);
} catch (MqttException e) {
log.error("Can't publish the message to the MQTT channel", e);
}
}
return updateChangelogHistory;
}

private MqttMessage buildMqttMessage(AbstractResource res) {
try {
Model changeEventJenaModel = JenaModelHelper.createJenaModel(new Object[] { res });
MqttMessage message = new MqttMessage();
message.setPayload(changeEventJenaModel.toString().getBytes());
return message;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| DatatypeConfigurationException | OslcCoreApplicationException e) {
throw new IllegalArgumentException(e);
}
}

private AbstractResource trsChangeResourceFrom(HistoryData historyData) {
// FIXME Andrew@2018-03-11: not thread-safe
this.order += 1;
String histDataType = historyData.getType();
URI uri = historyData.getUri();
URI changedUri;
try {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
df.setTimeZone(tz);
String nowAsISO = df.format(historyData.getTimestamp());
changedUri = new URI("urn:x-trs:" + nowAsISO + ":" + this.order);
ChangeEvent ce;
if (histDataType == HistoryData.CREATED) {
ce = new Creation(changedUri, uri, this.order);
} else if (histDataType == HistoryData.MODIFIED) {
ce = new Modification(changedUri, uri, this.order);
} else {
ce = new Deletion(changedUri, uri, this.order);
}
return ce;
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
}

}
Loading