Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/optimistic lock #8

Open
wants to merge 3 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
171 changes: 171 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Created by .ignore support plugin (hsz.mobi)
### Eclipse template
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# 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/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

*.iml
.idea
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<repositories>
<repository>
<id>onesait platform releases</id>
<url>http://nexus.onesaitplatform.com/nexus/content/repositories/releases/</url>
<url>https://nexus.onesaitplatform.com/nexus/content/repositories/releases/</url>
</repository>
<repository>
<id>central Maven</id>
Expand Down
47 changes: 41 additions & 6 deletions springboot-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
<version>1.3.2-RELEASE</version>
<relativePath>..</relativePath>
</parent>

<artifactId>onesaitplatform-iotclient4springboot</artifactId>

<version>1.3.2-RELEASE</version>
<packaging>jar</packaging>
<name>onesaitplatform-iotclient4springboot</name>
<description>Client of the IoT Broker for use with Spring Boot</description>
<description>Client of the IoT Broker for use with Spring Boot</description>
<properties>
<license.dir>${project.parent.basedir}</license.dir>
</properties>

<dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.minsait.onesait.platform</groupId>
<artifactId>onesaitplatform-comms-protocol</artifactId>
Expand All @@ -41,12 +41,47 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.5.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
Expand All @@ -57,6 +92,6 @@
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj.version}</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,54 @@
/*******************************************************************************
* Indra Sistemas, S.A.
* 2013 - 2017 SPAIN
*
*
* All rights reserved
******************************************************************************/
package com.minsait.onesait.platform.client.springboot.proxy.operations;

import java.lang.reflect.Method;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.minsait.onesait.platform.client.Transaction;
import com.minsait.onesait.platform.client.springboot.autoconfigure.ClientIoTBroker;
import com.minsait.onesait.platform.client.springboot.fromjson.UpdateResult;
import com.minsait.onesait.platform.client.springboot.proxy.operations.Transaction.OperationType;
import com.minsait.onesait.platform.comms.protocol.enums.SSAPQueryType;
import com.minsait.onesait.platform.comms.protocol.exception.SSAPConnectionException;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.OptimisticLockException;
import javax.persistence.Version;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

@Component("IoTBrokerUpdate")
@Slf4j
public class Update implements Operation {

protected static final String QUERY_BY_OBJECT_ID="select _id, c.* from %s as c where _id = %s";
@Autowired
private OperationUtil util;

private Field getVersionField(Class c){
List<Field> fieldsWithVersion=FieldUtils.getFieldsListWithAnnotation(c, Version.class);
if(fieldsWithVersion.size()==0){
return null;
}else if(fieldsWithVersion.size()==1) {
return fieldsWithVersion.get(0);
}else{
throw new SSAPConnectionException("Entity only can have one Version Field");
}
}
@Override
public Object operation(Method method, Object[] args, ClientIoTBroker client, String ontology,
Class<?> parametrizedType, boolean renewSession) throws SSAPConnectionException {
ObjectMapper mapper = new ObjectMapper();
String instanceInString = null;
String idInstance = null;
String idInstance ;
Object entityInstance;
try {
if (args.length < 2) {
log.error("We need at least 2 parameters: update(String identification,Object objectToUpdate)");
Expand All @@ -61,22 +76,37 @@ public Object operation(Method method, Object[] args, ClientIoTBroker client, St
}
if (args[0].getClass() == String.class) {
idInstance = (String) args[0];
instanceInString = mapper.writeValueAsString(args[1]);
entityInstance = args[1];
} else {
idInstance = (String) args[1];
instanceInString = mapper.writeValueAsString(args[0]);
entityInstance = args[0];
}

Field versionField= getVersionField(entityInstance.getClass());
if(versionField!=null){
versionField.setAccessible(true);
List<JsonNode> results=client.init().query(ontology, String.format(QUERY_BY_OBJECT_ID, ontology, idInstance), SSAPQueryType.SQL);
if(results.size()>0){
Object theReturn=new ObjectMapper().readValue(results.get(0).toString(), entityInstance.getClass());
if(versionField.get(entityInstance).equals(versionField.get(theReturn))){
log.debug("Version is the same");
}else{
throw new OptimisticLockException("Bad version value in updated object");
}

}
}
String instanceInString = mapper.writeValueAsString(entityInstance);
if (method.getReturnType().getName().equals("void")) {
client.init().update(ontology, instanceInString, idInstance);
return null;
} else {
}else {
JsonNode data = client.init().updateWithConfirmation(ontology, instanceInString, idInstance);
Object toReturn = new ObjectMapper().readValue(data.toString(), method.getReturnType());
return toReturn;
return new ObjectMapper().readValue(data.toString(), method.getReturnType());
}

} catch (SSAPConnectionException e) {

} catch (SSAPConnectionException | OptimisticLockException e) {
throw e;
} catch (Exception e) {
log.error("Error in Update operation", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright Indra Soluciones Tecnologías de la Información, S.L.U.
* 2013-2019 SPAIN
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.minsait.onesait.platform.client.springboot.proxy.operations;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {
}
Loading