diff --git a/LICENSE.EDL b/LICENSE.EDL new file mode 100644 index 0000000..8a8992b --- /dev/null +++ b/LICENSE.EDL @@ -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. diff --git a/README.md b/README.md index 3dd3e7a..5c9af44 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Now, add the following dependency: org.eclipse.lyo.store store-core - 2.4.0 + %version% ``` Now you are all set to start using the library. diff --git a/src/pom.xml b/src/pom.xml index 74d0063..b35385c 100644 --- a/src/pom.xml +++ b/src/pom.xml @@ -8,7 +8,7 @@ store-parent 4.0.0-SNAPSHOT pom - Lyo Store Parent + Lyo Store :: Parent false @@ -22,6 +22,8 @@ store-core + store-sync + @@ -61,6 +63,12 @@ xml-apis 1.3.04 + + javax.servlet + javax.servlet-api + 3.1.0 + provided + @@ -134,5 +142,9 @@ Eclipse Public License 1.0 https://www.eclipse.org/legal/epl-v10.html + + Eclipse Distribution License 1.0 + https://www.eclipse.org/org/documents/edl-v10.html + diff --git a/src/store-core/.gitignore b/src/store-core/.gitignore index afa7537..698f030 100644 --- a/src/store-core/.gitignore +++ b/src/store-core/.gitignore @@ -13,6 +13,8 @@ hs_err_pid* # Eclipse +**/bin/ +**/tmp/ .project .classpath .metadata diff --git a/src/store-core/pom.xml b/src/store-core/pom.xml index ff6d896..ea4e41c 100644 --- a/src/store-core/pom.xml +++ b/src/store-core/pom.xml @@ -12,6 +12,7 @@ store-core + Lyo Store :: Core ${project.parent.version} @@ -247,5 +248,9 @@ Eclipse Public License 1.0 https://www.eclipse.org/legal/epl-v10.html + + Eclipse Distribution License 1.0 + https://www.eclipse.org/org/documents/edl-v10.html + diff --git a/src/store-core/src/main/java/org/eclipse/lyo/store/ModelUnmarshallingException.java b/src/store-core/src/main/java/org/eclipse/lyo/store/ModelUnmarshallingException.java index 1e859b9..a5b10ad 100644 --- a/src/store-core/src/main/java/org/eclipse/lyo/store/ModelUnmarshallingException.java +++ b/src/store-core/src/main/java/org/eclipse/lyo/store/ModelUnmarshallingException.java @@ -24,6 +24,11 @@ * @since 0.14.0 */ public class ModelUnmarshallingException extends Exception { + /** + * + */ + private static final long serialVersionUID = 7760792549346636219L; + public ModelUnmarshallingException() { super(); } diff --git a/src/store-core/src/main/java/org/eclipse/lyo/store/StoreAccessException.java b/src/store-core/src/main/java/org/eclipse/lyo/store/StoreAccessException.java index b44e466..8deb6fe 100644 --- a/src/store-core/src/main/java/org/eclipse/lyo/store/StoreAccessException.java +++ b/src/store-core/src/main/java/org/eclipse/lyo/store/StoreAccessException.java @@ -23,6 +23,11 @@ * @since 0.13 */ public class StoreAccessException extends Exception { + /** + * + */ + private static final long serialVersionUID = -7535919324037847033L; + public StoreAccessException() { super(); } diff --git a/src/store-sync-trs/pom.xml b/src/store-sync-trs/pom.xml new file mode 100644 index 0000000..72d1a57 --- /dev/null +++ b/src/store-sync-trs/pom.xml @@ -0,0 +1,30 @@ + + + + store-parent + org.eclipse.lyo.store + 4.0.0-SNAPSHOT + + 4.0.0 + + store-sync-trs + + + + + org.eclipse.lyo.store + store-sync + ${project.version} + + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + 1.1.0 + + + + + \ No newline at end of file diff --git a/src/store-sync-trs/src/main/java/org/eclipse/lyo/store/sync/handlers/TrsHandler.java b/src/store-sync-trs/src/main/java/org/eclipse/lyo/store/sync/handlers/TrsHandler.java new file mode 100644 index 0000000..9ed50bf --- /dev/null +++ b/src/store-sync-trs/src/main/java/org/eclipse/lyo/store/sync/handlers/TrsHandler.java @@ -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 implements Handler { + + private TrsEventHandler eventHandler; + + public TrsHandler(TrsEventHandler trsEventHandler) { + this.eventHandler = trsEventHandler; + } + + @Override + public Collection> handle(Store store, Collection> changes) { + updateChangelogHistory(changes); + return changes; + } + + protected List updateChangelogHistory(Collection> changes) { + List 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 changesHistory(Collection> changes) { + return changes.stream().map(this::historyElementFromChange).collect(Collectors.toList()); + } + + private HistoryData historyElementFromChange(Change 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"); + } + } + +} diff --git a/src/store-sync-trs/src/main/java/org/eclipse/lyo/store/sync/handlers/TrsMqttChangeLogHandler.java b/src/store-sync-trs/src/main/java/org/eclipse/lyo/store/sync/handlers/TrsMqttChangeLogHandler.java new file mode 100644 index 0000000..fc5e31a --- /dev/null +++ b/src/store-sync-trs/src/main/java/org/eclipse/lyo/store/sync/handlers/TrsMqttChangeLogHandler.java @@ -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 extends TrsHandler { + 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 updateChangelogHistory(Collection> changes) { + List 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); + } + } + +} diff --git a/src/store-sync/.gitignore b/src/store-sync/.gitignore new file mode 100644 index 0000000..2b917aa --- /dev/null +++ b/src/store-sync/.gitignore @@ -0,0 +1,87 @@ +######################### +# PROJECT +######################### + +/*.iml +/.idea/ + +######################### +# JAVA +######################### + +# Mobile Tools for Java (J2ME) +**/.mtj.tmp/ + +# Package Files # +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +############################### +# Eclipse +# exceptions: .project, .launch +############################### + +.metadata +**/bin/ +**/tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +**/.settings/ +.loadpath +.recommenders + +# External tool builders +**/.externalToolBuilders/ + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +**/.recommenders/ + + +######################### +# MAVEN +######################### + +**/overlays/ +**/target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +**/.mvn/timing.properties diff --git a/src/store-sync/LICENSE.txt b/src/store-sync/LICENSE.txt new file mode 100644 index 0000000..f735bee --- /dev/null +++ b/src/store-sync/LICENSE.txt @@ -0,0 +1,203 @@ +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR +IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, +NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), 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 OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. diff --git a/src/store-sync/README.md b/src/store-sync/README.md new file mode 100644 index 0000000..2ab6f22 --- /dev/null +++ b/src/store-sync/README.md @@ -0,0 +1,74 @@ +# Lyo Store + +*Lyo Store* is a library that provides a simple interface for working with a triplestore via Java objects representing OSLC Resources. Lyo Store and Jena Models in the triple stores. + +## Prerequisites + +* For an in-memory option: JVM heap should be set to 1G or above (see [Jena documentation][1] for more information on this). +* For an on-disk option: an empty writable folder on disk, around 200MB of space is pre-allocated. +* For a SPARQL option: a pair of SPARQL Query and SPARQL Update URLs, optionally with basic authentication. + +## Maven dependency + + + org.eclipse.lyo.tools + lyo-store + %version% + + +More build systems are covered here: http://assume.gitlab.io/jena-cache/dependency-info.html + + +## Initialisation + +### SPARQL + + String sparqlQueryEndpoint = properties.getProperty("sparqlQueryEndpoint"); + String sparqlUpdateEndpoint = properties.getProperty("sparqlUpdateEndpoint"); + Store store = StoreFactory.sparql(sparqlQueryEndpoint, sparqlUpdateEndpoint); + +### On-disk + + String storeDirProp = properties.getProperty("storeDir"); + Path storeDir = Paths.get(storeDirProp); + Store store = StoreFactory.onDisk(storeDir); + +### In-memory + + Store store = StoreFactory.inMemory(); + +## Basic usage + +### Add the new resources and overwrite the existing ones + +```java +try { + store.updateResources(GRAPH_NAME, // URI of the named graph + resourceArray); // an array of OSLC Resources +} catch (StoreAccessException e) { + logger.error("Error executing a query on a triplestore"); +} +``` + +### Retrieving the resources from the triplestore + +```java +if (store.namedGraphExists(GRAPH_NAME)) { + try { + final int limit = 10; // fetch 10 resources + final int offset = 0; // start with the first page + // 'limit+1' is a technique that allows you to determine if there are + // more results on the next page + List requirements = store.getResources(GRAPH_NAME, + Requirement.class, // resources of this class will be fetched and unmarshalled + limit + 1, // resource limit + offset)); // how many resources to skip, use for paging + } catch (StoreAccessException e) { + logger.error("Error executing a query on a triplestore"); + } catch ( ModelUnmarshallingException e) { + logger.error("Error unmarshalling the RDF from triplestore into Requirement class instances"); + } +} +``` + +[1]: https://jena.apache.org/documentation/tdb/architecture.html#caching-on-32-and-64-bit-java-systems diff --git a/src/store-sync/doc/architecture.png b/src/store-sync/doc/architecture.png new file mode 100644 index 0000000..398cd67 Binary files /dev/null and b/src/store-sync/doc/architecture.png differ diff --git a/src/store-sync/pom.xml b/src/store-sync/pom.xml new file mode 100644 index 0000000..162e1ea --- /dev/null +++ b/src/store-sync/pom.xml @@ -0,0 +1,219 @@ + + + 4.0.0 + + + org.eclipse.lyo.store + store-parent + 4.0.0-SNAPSHOT + ../pom.xml + + + store-sync + Lyo Store :: Sync + + UTF-8 + UTF-8 + 1.8 + 1.8 + 4.0.0-SNAPSHOT + + + + + javax.servlet + javax.servlet-api + provided + + + org.eclipse.lyo.store + store-core + ${version.lyo} + + + junit + junit + test + + + org.assertj + assertj-core + test + + + org.eclipse.lyo.trs + trs-server + ${version.lyo} + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + + + integration-test + verify + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.7 + + + true + + + + + + org.codehaus.mojo + license-maven-plugin + 1.10 + + eplv1 + ${project.baseUri}src/license + + KTH Royal Institute of Technology + + 2016 + + ${basedir}/src/license/eplv1/description.txt.ftl + + true + + + + + com.qulice + qulice-maven-plugin + 0.17 + + file:${basedir}/LICENSE.txt + + checkstyle:.* + findbugs:~* + cobertura:.* + dependencies:net.sourceforge.cobertura:cobertura + duplicatefinder:.* + + + + + + org.jacoco + jacoco-maven-plugin + 0.7.7.201606060606 + + + jacoco-initialize + + prepare-agent + + + + jacoco-site + package + + report + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + + + + jar + + + + + + org.tendiwa + git-version-insert-maven-plugin + 0.1 + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + public + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.7 + + + + cobertura + + + + + + org.jacoco + jacoco-maven-plugin + 0.7.7.201606060606 + + + + + + + + Eclipse Public License 1.0 + https://www.eclipse.org/legal/epl-v10.html + + + Eclipse Distribution License 1.0 + https://www.eclipse.org/org/documents/edl-v10.html + + + + + + lyo-releases + lyo-releases repository + https://repo.eclipse.org/content/repositories/lyo-releases/ + + + lyo-snapshots + lyo-snapshots repository + https://repo.eclipse.org/content/repositories/lyo-snapshots/ + + + diff --git a/src/store-sync/src/license/eplv1/description.txt.ftl b/src/store-sync/src/license/eplv1/description.txt.ftl new file mode 100644 index 0000000..ab33804 --- /dev/null +++ b/src/store-sync/src/license/eplv1/description.txt.ftl @@ -0,0 +1,13 @@ +<#-- + #%L + Contributors: + %% + Copyright (C) 2016 KTH Royal Institute of Technology + %% + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License v1.0 + which accompanies this distribution, and is available at + http://www.eclipse.org/legal/epl-v10.html + #L% +--> +Contributors: diff --git a/src/store-sync/src/license/eplv1/header.txt.ftl b/src/store-sync/src/license/eplv1/header.txt.ftl new file mode 100644 index 0000000..6521ed8 --- /dev/null +++ b/src/store-sync/src/license/eplv1/header.txt.ftl @@ -0,0 +1,16 @@ +<#-- + #%L + Contributors: + %% + Copyright (C) 2016 KTH Royal Institute of Technology + %% + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License v1.0 + which accompanies this distribution, and is available at + http://www.eclipse.org/legal/epl-v10.html + #L% +--> +All rights reserved. This program and the accompanying materials +are made available under the terms of the Eclipse Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/epl-v10.html diff --git a/src/store-sync/src/license/eplv1/license.txt b/src/store-sync/src/license/eplv1/license.txt new file mode 100644 index 0000000..f735bee --- /dev/null +++ b/src/store-sync/src/license/eplv1/license.txt @@ -0,0 +1,203 @@ +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR +IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, +NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), 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 OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. diff --git a/src/store-sync/src/license/licenses.properties b/src/store-sync/src/license/licenses.properties new file mode 100644 index 0000000..23021d4 --- /dev/null +++ b/src/store-sync/src/license/licenses.properties @@ -0,0 +1,13 @@ +### +# #%L +# jena-cache +# %% +# Copyright (C) 2016 Andrew Berezovskyi +# %% +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# #L% +### +eplv1=Eclipse Public License 1.0 diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/Handler.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/Handler.java new file mode 100644 index 0000000..85ba20d --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/Handler.java @@ -0,0 +1,29 @@ +/* + * 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; + +import java.util.Collection; +import org.eclipse.lyo.store.Store; +import org.eclipse.lyo.store.sync.change.Change; + +/** + * Handler is . + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +public interface Handler { + Collection> handle(Store store, Collection> changes); +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/ServiceProviderMessage.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/ServiceProviderMessage.java new file mode 100644 index 0000000..fb95534 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/ServiceProviderMessage.java @@ -0,0 +1,27 @@ +/* + * 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; + +import java.net.URI; + +/** + * Created on 06.03.17 + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 0.0.1 + */ +public interface ServiceProviderMessage { + URI getServiceProviderUri(); +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/StoreUpdateManager.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/StoreUpdateManager.java new file mode 100644 index 0000000..358d219 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/StoreUpdateManager.java @@ -0,0 +1,115 @@ +/* + * 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; + +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import org.eclipse.lyo.store.Store; +import org.eclipse.lyo.store.sync.change.Change; +import org.eclipse.lyo.store.sync.change.ChangeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Schedules {@link StoreUpdateRunnable} via internal + * {@link ScheduledExecutorService} with a predefined delay or on-demand. + * Operates on a generic message that is passed to handlers. + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +public class StoreUpdateManager { + private final static Logger LOGGER = LoggerFactory.getLogger(StoreUpdateManager.class); + private static final int NODELAY = 0; + private static final int SINGLE_THREAD_POOL = 1; + private final ScheduledExecutorService executor; + private final ChangeProvider changeProvider; + private final Store store; + private final List> handlers = new ArrayList<>(); + + /** + * Schedules {@link StoreUpdateRunnable} via internal + * {@link ScheduledExecutorService} with a predefined delay or on-demand. + * + * @param store + * Instance of an initialised Store + * @param changeProvider + * Provider of the changes in the underlying tool. + */ + public StoreUpdateManager(final Store store, final ChangeProvider changeProvider) { + executor = Executors.newScheduledThreadPool(StoreUpdateManager.SINGLE_THREAD_POOL); + this.store = store; + this.changeProvider = changeProvider; + } + + /** + * Polling update on a given {@link ChangeProvider} followed by a notification + * to all previously registered {@link Handler} objects. + * + * @param lastUpdate + * Time of last store update, changes before this moment might be + * dropped. + * @param delaySeconds + * Seconds between polling checks. + */ + public void poll(final ZonedDateTime lastUpdate, final int delaySeconds) { + final StoreUpdateRunnable updateRunnable = buildRunnable(store, changeProvider, lastUpdate, null, handlers); + executor.scheduleWithFixedDelay(updateRunnable, StoreUpdateManager.NODELAY, delaySeconds, TimeUnit.SECONDS); + + StoreUpdateManager.LOGGER.trace("Poll request has been enqueued"); + } + + /** + * Submit a single update request. Typically done from the HTTP handler. + * + * @param lastUpdate + * @param message + * Specific details for the {@link ChangeProvider} + * @return {@link Future} that allows to block until the runnable is finished + * executing (strongly discouraged). + */ + public Future submit(final ZonedDateTime lastUpdate, final M message) { + final StoreUpdateRunnable updateRunnable = buildRunnable(store, changeProvider, lastUpdate, message, + handlers); + return executor.submit(updateRunnable); + } + + /** + * Add a {@link Handler} that will process the collection of {@link Change} + * generated by the {@link ChangeProvider}. Handler is not guaranteed to be + * called if added after the update has been scheduled. + * + * @param handler + * Handler that should be called whenever {@link ChangeProvider} + * returns any changes. + */ + public void addHandler(final Handler handler) { + handlers.add(handler); + } + + /** + * Method can be overridden in case a more sophisticated Runnable has to be + * constructed. + */ + protected StoreUpdateRunnable buildRunnable(final Store store, final ChangeProvider changeProvider, + final ZonedDateTime lastUpdate, final M message, final List> handlers) { + return new StoreUpdateRunnable<>(store, changeProvider, lastUpdate, message, handlers); + } +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/StoreUpdateRunnable.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/StoreUpdateRunnable.java new file mode 100644 index 0000000..c91e279 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/StoreUpdateRunnable.java @@ -0,0 +1,107 @@ +/* + * 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; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import org.eclipse.lyo.store.Store; +import org.eclipse.lyo.store.sync.change.Change; +import org.eclipse.lyo.store.sync.change.ChangeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Requests latest {@link Change} collection from {@link ChangeProvider} and triggers each + * handler afterwards. Stores the timestamp of the newest change to request new updates starting + * from that point in time. + *

+ *

Handlers will be notified with different message per service provider as accessible via + * {@link OSLCMessage#getServiceProviderId()}.

+ * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 0.18.0 + */ +class StoreUpdateRunnable implements Runnable { + + private static final ZonedDateTime LAST_UPDATE_DEFAULT = ZonedDateTime.of(1971, 1, 1, 1, 1, 1, + 1, ZoneId.of("UTC")); + private final Logger log = LoggerFactory.getLogger(StoreUpdateRunnable.class); + private final ChangeProvider changeProvider; + private final M message; + private final Store store; + private final List> handlers; + private ZonedDateTime lastUpdate; + + /** + * @param store Initialised Store + * @param changeProvider Change provider + * @param lastUpdate Initial timestamp. If null, will be set to {@link + * StoreUpdateRunnable#LAST_UPDATE_DEFAULT} + * @param message Initial message to the {@link ChangeProvider} + * @param handlers List of handlers to be notified. + */ + StoreUpdateRunnable(final Store store, final ChangeProvider changeProvider, + final ZonedDateTime lastUpdate, final M message, final List> handlers) { + this.store = store; + this.changeProvider = changeProvider; + this.message = message; + this.handlers = handlers; + if (lastUpdate != null) { + this.lastUpdate = lastUpdate; + } else { + this.lastUpdate = StoreUpdateRunnable.LAST_UPDATE_DEFAULT; + } + } + + @Override + public void run() { + try { + log.trace("Running background update"); + Collection> changes = null; + try { + changes = changeProvider.getChangesSince(lastUpdate, message); + } catch (final Exception e) { + log.error("ChangeProvider threw an exception", e); + } + if (changes != null && !changes.isEmpty()) { + for (final Handler handler : handlers) { + log.trace("Notifying {}", handler); + try { + handler.handle(store, changes); + } catch (final Exception e) { + log.warn("Handler {} threw an exception", handler, e); + } + } + for (final Change change : changes) { + if (change != null) { + final Date date = change.getHistoryResource().getTimestamp(); + final ZonedDateTime dateTime = date.toInstant() + .atZone(ZoneId.systemDefault()); + if (lastUpdate.isBefore(dateTime)) { + lastUpdate = dateTime; + } + } + } + log.trace("Setting previous revision to {}", lastUpdate); + } + } catch (final Exception e) { + // ExecutorService will terminate the whole schedule if a Runnable throws an exception + log.error("A handler threw an exception!", e); + } + } +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/Change.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/Change.java new file mode 100644 index 0000000..d26d93c --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/Change.java @@ -0,0 +1,49 @@ +/* + * 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.change; + +import org.eclipse.lyo.oslc4j.core.model.AbstractResource; + +/** + * Change is . + * + * @version $version-stub$ + * @since 4.0.0 + */ +public class Change { + /** + * Not IExtendedResource due to the use of AbstractResource throughout Lyo generated code. + */ + private final AbstractResource resource; + private final HistoryResource historyResource; + private final T message; + + public Change(AbstractResource resource, HistoryResource historyResource, T message) { + this.resource = resource; + this.historyResource = historyResource; + this.message = message; + } + + public AbstractResource getResource() { + return resource; + } + + public HistoryResource getHistoryResource() { + return historyResource; + } + + public T getMessage() { + return message; + } +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeHelper.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeHelper.java new file mode 100644 index 0000000..8f0738c --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeHelper.java @@ -0,0 +1,50 @@ +/* + * 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.change; + +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * ChangeHelper is . + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +public class ChangeHelper { + + public static List changesCreated(Collection allChanges) { + return allChanges.stream() + .filter(change -> Objects.equals(change.getHistoryResource().getChangeKindEnum(), ChangeKind.CREATION)) + .collect(Collectors.toList()); + } + + public static List changesModified(Collection allChanges) { + return allChanges.stream().filter( + change -> Objects.equals(change.getHistoryResource().getChangeKindEnum(), ChangeKind.MODIFICATION)) + .collect(Collectors.toList()); + } + + public static List historyFrom(Collection changes) { + return ChangeHelper.mapFn(changes, Change::getHistoryResource); + } + + public static List mapFn(Collection changes, Function mapper) { + return changes.stream().map(mapper).collect(Collectors.toList()); + } +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeKind.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeKind.java new file mode 100644 index 0000000..e3a251f --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeKind.java @@ -0,0 +1,58 @@ +/* + * 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.change; + +import org.eclipse.lyo.oslc4j.core.annotation.OslcName; +import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespace; +import org.eclipse.lyo.oslc4j.core.annotation.OslcResourceShape; + +/** + * ChangeKind is . + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +@OslcNamespace(HistoryResource.NS_TRS) +@OslcName(ChangeKind.NAME) +@OslcResourceShape(title = "Change Kind Resource Shape", describes = HistoryResource.NS_TRS + ChangeKind.NAME) +public enum ChangeKind { + // Strings taken from the TRS provider implementation + CREATION("Created"), + MODIFICATION("Modified"), + DELETION("Deleted"); + + public static final String NAME = "ChangeKind"; + private final String created; + + ChangeKind(String created) { + this.created = created; + } + + public static ChangeKind fromString(String text) { + if (text != null) { + for (ChangeKind kind : ChangeKind.values()) { + if (text.equalsIgnoreCase(kind.created)) { + return kind; + } + } + } + throw new IllegalArgumentException("No constant with text " + text + " found"); + } + + @Override + public String toString() { + return created; + } +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeProvider.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeProvider.java new file mode 100644 index 0000000..70bb0b4 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/ChangeProvider.java @@ -0,0 +1,28 @@ +/* + * 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.change; + +import java.time.ZonedDateTime; +import java.util.Collection; + +/** + * ChangeProvider is . + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +public interface ChangeProvider { + Collection> getChangesSince(ZonedDateTime lastUpdate, T message); +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/HistoryResource.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/HistoryResource.java new file mode 100644 index 0000000..d09b1f1 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/HistoryResource.java @@ -0,0 +1,100 @@ +/* + * 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.change; + +import org.eclipse.lyo.oslc4j.core.annotation.OslcName; +import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespace; +import org.eclipse.lyo.oslc4j.core.annotation.OslcPropertyDefinition; +import org.eclipse.lyo.oslc4j.core.annotation.OslcResourceShape; +import org.eclipse.lyo.oslc4j.core.model.AbstractResource; + +import java.net.URI; +import java.time.ZonedDateTime; +import java.util.Date; + +/** + * HistoryResource is a wrapper OSLC Resource around org.eclipse.lyo.oslc4j.trs.provider.HistoryData. + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +@OslcNamespace(HistoryResource.NS_TRS) +@OslcName(HistoryResource.NAME) +@OslcResourceShape(title = "TRS History Resource Shape", describes = HistoryResource.TYPE) +public class HistoryResource extends AbstractResource { + public static final String NS_TRS = "http://open-services.net/ns/core/trs#"; + public static final String NAME = "TrsHistoryResource"; + public static final String TYPE = NS_TRS + NAME; + private ChangeKind changeKind; + private Date timestamp; + private URI resourceURI; + + /** + * Shall be used only by the OSLC Jena Model Helper + */ + @Deprecated + public HistoryResource() { + } + + public HistoryResource(ChangeKind changeKind, Date timestamp, URI resourceURI) { + this.changeKind = changeKind; + this.timestamp = timestamp; + this.resourceURI = resourceURI; + } + + public HistoryResource(ChangeKind changeKind, ZonedDateTime timestamp, URI resourceURI) { + this.changeKind = changeKind; + this.timestamp = Date.from(timestamp.toInstant()); + this.resourceURI = resourceURI; + } + + @OslcName("change_kind") + @OslcPropertyDefinition(NS_TRS + "change_kind") + public String getChangeKind() { + return changeKind.toString(); + } + + public void setChangeKind(ChangeKind changeKind) { + this.changeKind = changeKind; + } + + public ChangeKind getChangeKindEnum() { + return changeKind; + } + + public void setChangeKind(String changeKind) { + this.changeKind = ChangeKind.fromString(changeKind); + } + + @OslcName("timestamp") + @OslcPropertyDefinition(NS_TRS + "timestamp") + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + @OslcName("uri") + @OslcPropertyDefinition(NS_TRS + "uri") + public URI getResourceURI() { + return resourceURI; + } + + public void setResourceURI(URI resourceURI) { + this.resourceURI = resourceURI; + } +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/package-info.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/package-info.java new file mode 100644 index 0000000..6241141 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/change/package-info.java @@ -0,0 +1,8 @@ +/** + * Classes for defining changes for handlers. + * {@link org.eclipse.lyo.store.sync.change.HistoryResource} can be persisted in a + * triplestore. + * @version $version-stub$ + * @since 4.0.0 + */ +package org.eclipse.lyo.store.sync.change; diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/handlers/SimpleStoreHandler.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/handlers/SimpleStoreHandler.java new file mode 100644 index 0000000..cde1051 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/handlers/SimpleStoreHandler.java @@ -0,0 +1,116 @@ +/* + * 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.net.URI; +import java.net.URISyntaxException; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.eclipse.lyo.oslc4j.core.model.AbstractResource; +import org.eclipse.lyo.oslc4j.core.model.ServiceProvider; +import org.eclipse.lyo.store.Store; +import org.eclipse.lyo.store.StoreAccessException; +import org.eclipse.lyo.store.sync.Handler; +import org.eclipse.lyo.store.sync.ServiceProviderMessage; +import org.eclipse.lyo.store.sync.change.Change; +import org.eclipse.lyo.store.sync.change.ChangeKind; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a simple handler that puts resources into named graphs per + * {@link ServiceProvider} if the message implements + * {@link ServiceProviderMessage}. + * + * @author Andrew Berezovskyi + * @version $version-stub$ + * @param + * @since 2.4.0 + * + */ +public class SimpleStoreHandler implements Handler { + + private final URI defaultGraph; + + private final Logger log = LoggerFactory.getLogger(SimpleStoreHandler.class); + + private final Store store; + + public SimpleStoreHandler(Store store) { + this.store = store; + try { + this.defaultGraph = new URI("urn:x-arq:DefaultGraph"); + } catch (URISyntaxException e) { + // this should never happen - we don't want the exception trail + // so we cheat with wrapping this in an unmanaged exception + // that will halt the execution. + IllegalStateException exception = new IllegalStateException(e); + log.error("Failed to generate default graph URI"); + throw exception; + } + } + + @Override + public Collection> handle(Store store, Collection> changes) { + if (changes == null || changes.isEmpty()) { + log.warn("Empty change list cannot be handled"); + return changes; + } + + Optional> firstChange = changes.stream().findFirst(); + if (firstChange.get().getMessage() instanceof ServiceProviderMessage) { + Map>> map = changes.stream() + .collect(Collectors.groupingBy(c -> c.getMessage().getClass())); + for (Entry>> changeSet : map.entrySet()) { + URI spURI = ((ServiceProviderMessage) changeSet.getKey()).getServiceProviderUri(); + persistChanges(spURI, changeSet.getValue()); + } + } else { + persistChanges(defaultGraph, changes); + } + + return changes; + } + + private void persistChanges(URI spURI, Collection> value) { + try { + persistUpdates(spURI, value); + } catch (StoreAccessException e) { + log.error("Failed to persist updates", e); + } + + persistDeletions(spURI, value); + } + + private void persistDeletions(URI spURI, Collection> value) { + Collection deletedResources = value.stream() + .filter(c -> c.getHistoryResource().getChangeKindEnum() == ChangeKind.DELETION) + .map(c -> c.getResource().getAbout()).collect(Collectors.toList()); + store.deleteResources(spURI, deletedResources.toArray(new URI[0])); + } + + private void persistUpdates(URI spURI, Collection> value) throws StoreAccessException { + Collection updatedResources = value.stream() + .filter(c -> c.getHistoryResource().getChangeKindEnum() == ChangeKind.CREATION + || c.getHistoryResource().getChangeKindEnum() == ChangeKind.MODIFICATION) + .map(c -> c.getResource()).collect(Collectors.toList()); + store.appendResources(spURI, updatedResources); + } + +} diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/handlers/package-info.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/handlers/package-info.java new file mode 100644 index 0000000..c27aae5 --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/handlers/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author andrew + * + */ +package org.eclipse.lyo.store.sync.handlers; diff --git a/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/package-info.java b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/package-info.java new file mode 100644 index 0000000..74482ab --- /dev/null +++ b/src/store-sync/src/main/java/org/eclipse/lyo/store/sync/package-info.java @@ -0,0 +1,10 @@ +/** + * Common primitives for updating store. + * {@link org.eclipse.lyo.store.sync.StoreUpdateRunnable} is scheduled by + * {@link org.eclipse.lyo.store.sync.StoreUpdateManager}. + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 0.18.0 + */ +package org.eclipse.lyo.store.sync; diff --git a/src/store-sync/src/test/java/org/eclipse/lyo/store/sync/TestHistoryResource.java b/src/store-sync/src/test/java/org/eclipse/lyo/store/sync/TestHistoryResource.java new file mode 100644 index 0000000..2b91965 --- /dev/null +++ b/src/store-sync/src/test/java/org/eclipse/lyo/store/sync/TestHistoryResource.java @@ -0,0 +1,86 @@ +package org.eclipse.lyo.store.sync; + +import org.apache.jena.rdf.model.Model; +import java.net.URI; +import java.time.Instant; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; +import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper; +import org.eclipse.lyo.store.Store; +import org.eclipse.lyo.store.StoreFactory; +import org.eclipse.lyo.store.sync.change.ChangeKind; +import org.eclipse.lyo.store.sync.change.HistoryResource; +import org.junit.Test; + +/** + * TestTrsHistoryResource is . + * + * @author Andrew Berezovskyi (andriib@kth.se) + * @version $version-stub$ + * @since 4.0.0 + */ +public class TestHistoryResource { + public static final URI RESOURCE_URI = URI.create("test:test"); + private final Store store = StoreFactory.inMemory(); + + @Test + public void testResourceIsMarshalled() throws Exception { + HistoryResource resource = new HistoryResource(ChangeKind.CREATION, + Date.from(Instant.now()), TestHistoryResource.RESOURCE_URI); + + Model model = JenaModelHelper.createJenaModel(new Object[] { resource }); + assertThat(model.size()).isGreaterThan(0); + } + + @Test + public void testResourceContainsStatements() throws Exception { + HistoryResource resource = new HistoryResource(ChangeKind.CREATION, + Date.from(Instant.now()), TestHistoryResource.RESOURCE_URI); + resource.setAbout(TestHistoryResource.RESOURCE_URI); + + Model model = JenaModelHelper.createJenaModel(new Object[] { resource }); + assertThat(model.size()).isGreaterThan(1); + } + + @Test + public void testResourceIsPersisted() throws Exception { + HistoryResource resource = new HistoryResource(ChangeKind.CREATION, + Date.from(Instant.now()), TestHistoryResource.RESOURCE_URI); + resource.setAbout(TestHistoryResource.RESOURCE_URI); + + assertThat(store.keySet()).hasSize(0); + store.putResources(resource.getAbout(), Collections.singletonList(resource)); + assertThat(store.keySet()).hasSize(1); + } + + @Test + public void testResourceIsRestored() throws Exception { + HistoryResource resource = new HistoryResource(ChangeKind.CREATION, + Date.from(Instant.now()), TestHistoryResource.RESOURCE_URI); + resource.setAbout(TestHistoryResource.RESOURCE_URI); + + store.putResources(resource.getAbout(), Collections.singletonList(resource)); + List resources = store.getResources(TestHistoryResource.RESOURCE_URI, HistoryResource.class); + assertThat(resources).hasSize(1); + } + + @Test + public void testResourceIsRestoredWithProperties() throws Exception { + String testResourceURI = "lyo:testtest"; + Date timestamp = Date.from(Instant.now()); + HistoryResource resource = new HistoryResource(ChangeKind.CREATION, timestamp, + URI.create(testResourceURI)); + resource.setAbout(TestHistoryResource.RESOURCE_URI); + + store.putResources(resource.getAbout(), Collections.singletonList(resource)); + + List resources = store.getResources(TestHistoryResource.RESOURCE_URI, HistoryResource.class); + HistoryResource storeResource = resources.get(0); + + assertThat(storeResource.getChangeKind()).isEqualToIgnoringCase(String.valueOf(ChangeKind.CREATION)); + assertThat(storeResource.getTimestamp()).isEqualTo(timestamp); + assertThat(storeResource.getResourceURI().toASCIIString()).isEqualTo(testResourceURI); + } +} diff --git a/src/store-sync/src/test/resources/log4j.properties b/src/store-sync/src/test/resources/log4j.properties new file mode 100644 index 0000000..9f4c714 --- /dev/null +++ b/src/store-sync/src/test/resources/log4j.properties @@ -0,0 +1,26 @@ +### +# #%L +# Contributors: +# Andrew Berezovskyi - initial template (based on https://logging.apache.org/log4j/1.2/manual.html) +# +# NOTICE: +# Copyright © 2000-2002 The Apache Software Foundation. All rights reserved. This software is published under the terms of the Apache Software License version 2.0, a copy of which has been included in the LICENSE file shipped with the log4j distribution. This document is based on the article "Log4j delivers control over logging" published in November 2000 edition of JavaWorld. However, the present article containsKey more detailed and up to date information. The present short manual also borrows some text from "The complete log4j manual" by the same author (yours truly). +# %% +# Copyright (C) 2016 KTH Royal Institute of Technology +# %% +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# #L% +### +# Set root logger level to DEBUG and its only appender to rootAppender. +log4j.rootLogger=DEBUG, rootAppender + +log4j.appender.rootAppender=org.apache.log4j.ConsoleAppender +log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout +log4j.appender.rootAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n + + +log4j.logger.org.apache.jena=INFO +log4j.logger.com.hp.hpl.jena=INFO