refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Implementing library

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115235 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-05 09:43:53 +00:00
parent d2174296d6
commit 942e1451b7
27 changed files with 241 additions and 73 deletions

11
pom.xml
View File

@ -45,16 +45,7 @@
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-validator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
</dependency>
<!-- CouchDB libraries -->
<dependency>
<groupId>org.ektorp</groupId>

View File

@ -17,13 +17,13 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.gcube.accounting.datamodel.validators.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validators.ValidLong;
import org.gcube.accounting.datamodel.validators.ValidOperationResult;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.datamodel.annotations.ValidOperationResult;
import org.gcube.accounting.datamodel.annotations.ValidityChecker;
import org.gcube.accounting.datamodel.validators.FieldValidator;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.FieldValidator;
import org.gcube.common.validator.annotations.NotEmpty;
import org.gcube.common.validator.annotations.ValidityChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -4,10 +4,10 @@
package org.gcube.accounting.datamodel.aggregated;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.validators.ValidInteger;
import org.gcube.accounting.datamodel.validators.ValidLong;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.datamodel.annotations.ValidInteger;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.NotEmpty;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/

View File

@ -0,0 +1,15 @@
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.accounting.datamodel.validators.NotEmptyValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@ValidityChecker(managed=NotEmptyValidator.class)
public @interface NotEmpty {
}

View File

@ -1,15 +1,15 @@
package org.gcube.accounting.datamodel.validators;
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.common.validator.annotations.ValidityChecker;
import org.gcube.accounting.datamodel.validators.NotEmptyIfNotNullValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@ValidityChecker(managed=NotEmptyIfNotNullValidator.class)
public @interface NotEmptyIfNotNull {
}

View File

@ -0,0 +1,15 @@
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.accounting.datamodel.validators.NotNullValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@ValidityChecker(managed=NotNullValidator.class)
public @interface NotNull {
}

View File

@ -1,11 +1,11 @@
package org.gcube.accounting.datamodel.validators;
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.common.validator.annotations.ValidityChecker;
import org.gcube.accounting.datamodel.validators.ValidIPValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)

View File

@ -1,11 +1,11 @@
package org.gcube.accounting.datamodel.validators;
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.common.validator.annotations.ValidityChecker;
import org.gcube.accounting.datamodel.validators.ValidIntegerValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)

View File

@ -1,11 +1,11 @@
package org.gcube.accounting.datamodel.validators;
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.common.validator.annotations.ValidityChecker;
import org.gcube.accounting.datamodel.validators.ValidLongValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)

View File

@ -1,11 +1,11 @@
package org.gcube.accounting.datamodel.validators;
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.common.validator.annotations.ValidityChecker;
import org.gcube.accounting.datamodel.validators.ValidOperationResultValidator;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)

View File

@ -0,0 +1,25 @@
/**
*
*/
package org.gcube.accounting.datamodel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.accounting.datamodel.validators.FieldValidator;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ValidityChecker {
Class<? extends FieldValidator<?>> managed();
}

View File

@ -6,10 +6,10 @@ package org.gcube.accounting.datamodel.implementations;
import java.util.Calendar;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.validators.ValidInteger;
import org.gcube.accounting.datamodel.validators.ValidLong;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.datamodel.annotations.ValidInteger;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.NotEmpty;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/

View File

@ -4,8 +4,8 @@
package org.gcube.accounting.datamodel.implementations;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.NotEmpty;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/

View File

@ -4,10 +4,10 @@
package org.gcube.accounting.datamodel.implementations;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.validators.ValidIP;
import org.gcube.accounting.datamodel.validators.ValidInteger;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.datamodel.annotations.ValidIP;
import org.gcube.accounting.datamodel.annotations.ValidInteger;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.NotEmpty;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/

View File

@ -4,12 +4,12 @@
package org.gcube.accounting.datamodel.implementations;
import org.gcube.accounting.datamodel.aggregated.StorageStatusUsageRecord;
import org.gcube.accounting.datamodel.validators.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validators.ValidIP;
import org.gcube.accounting.datamodel.validators.ValidInteger;
import org.gcube.accounting.datamodel.validators.ValidLong;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.annotations.ValidIP;
import org.gcube.accounting.datamodel.annotations.ValidInteger;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.NotEmpty;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/

View File

@ -6,10 +6,10 @@ package org.gcube.accounting.datamodel.implementations;
import java.util.Calendar;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.datamodel.validators.ValidInteger;
import org.gcube.accounting.datamodel.validators.ValidLong;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.datamodel.annotations.ValidInteger;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.common.validator.annotations.NotEmpty;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/

View File

@ -0,0 +1,20 @@
/**
*
*/
package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import java.lang.annotation.Annotation;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public interface FieldValidator<T extends Annotation> {
Class<T> annotation();
boolean isValid(Serializable toValidate);
String getErrorSuffix();
}

View File

@ -1,16 +1,17 @@
package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import java.util.Map;
import org.gcube.common.validator.annotations.FieldValidator;
import org.gcube.common.validator.annotations.NotEmpty;
public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmpty>{
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
public Class<NotEmpty> annotation() {
return NotEmpty.class;
public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotNull>{
public Class<NotEmptyIfNotNull> annotation() {
return NotEmptyIfNotNull.class;
}
public boolean isValid(Object toValidate) {
public boolean isValid(Serializable toValidate) {
if (toValidate == null) return true;
if (toValidate.getClass().isArray() ){
return ((Object[])toValidate).length>0;

View File

@ -0,0 +1,31 @@
package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import java.util.Map;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
public class NotEmptyValidator implements FieldValidator<NotEmpty>{
public Class<NotEmpty> annotation() {
return NotEmpty.class;
}
public boolean isValid(Serializable toValidate) {
if (toValidate == null) return false;
if (toValidate.getClass().isArray() ){
return ((Object[])toValidate).length>0;
}else if ( toValidate instanceof Iterable<?>){
return ((Iterable<?>) toValidate).iterator().hasNext();
} else if (toValidate instanceof Map<?,?>){
return ((Map<?,?>) toValidate).size()>0;
} else if (toValidate instanceof String ){
return !((String)toValidate).isEmpty();
} else return true;
}
public String getErrorSuffix() {
return "is empty";
}
}

View File

@ -0,0 +1,22 @@
package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.annotations.NotNull;
public class NotNullValidator implements FieldValidator<NotNull>{
public Class<NotNull> annotation() {
return NotNull.class;
}
public boolean isValid(Serializable toValidate) {
return toValidate!=null;
}
public String getErrorSuffix() {
return "is null";
}
}

View File

@ -4,7 +4,7 @@ import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gcube.common.validator.annotations.FieldValidator;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -43,10 +43,10 @@ public class ValidIPValidator implements FieldValidator<ValidLong>{
return ipV6Matcher.matches();
}
public boolean isValid(Object toValidate) {
if(toValidate instanceof String || toValidate instanceof Serializable){
public boolean isValid(Serializable toValidate) {
try {
return isIpAddress((String) toValidate);
}
}catch(Exception e){}
return false;
}

View File

@ -1,6 +1,9 @@
package org.gcube.accounting.datamodel.validators;
import org.gcube.common.validator.annotations.FieldValidator;
import java.io.Serializable;
import org.gcube.accounting.datamodel.annotations.ValidLong;
public class ValidIntegerValidator implements FieldValidator<ValidLong>{
@ -8,7 +11,7 @@ public class ValidIntegerValidator implements FieldValidator<ValidLong>{
return ValidLong.class;
}
public boolean isValid(Object toValidate) {
public boolean isValid(Serializable toValidate) {
if(toValidate instanceof Integer){
return true;
}

View File

@ -1,6 +1,9 @@
package org.gcube.accounting.datamodel.validators;
import org.gcube.common.validator.annotations.FieldValidator;
import java.io.Serializable;
import org.gcube.accounting.datamodel.annotations.ValidLong;
public class ValidLongValidator implements FieldValidator<ValidLong>{
@ -8,7 +11,7 @@ public class ValidLongValidator implements FieldValidator<ValidLong>{
return ValidLong.class;
}
public boolean isValid(Object toValidate) {
public boolean isValid(Serializable toValidate) {
if(toValidate instanceof Long){
return true;
}

View File

@ -3,7 +3,7 @@ package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.common.validator.annotations.FieldValidator;
import org.gcube.accounting.datamodel.annotations.ValidOperationResult;
public class ValidOperationResultValidator implements FieldValidator<ValidOperationResult>{
@ -11,18 +11,16 @@ public class ValidOperationResultValidator implements FieldValidator<ValidOperat
return ValidOperationResult.class;
}
public boolean isValid(Object toValidate) {
public boolean isValid(Serializable toValidate) {
if(toValidate instanceof OperationResult){
return true;
}
if(toValidate instanceof String || toValidate instanceof Serializable){
try {
OperationResult operationResult = OperationResult.valueOf((String) toValidate);
if(operationResult !=null){
return true;
}
}catch(Exception e){}
}
try {
OperationResult operationResult = OperationResult.valueOf((String) toValidate);
if(operationResult !=null){
return true;
}
}catch(Exception e){}
return false;
}

View File

@ -0,0 +1,25 @@
/**
*
*/
package org.gcube.accounting.exception;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class InternallyReservedKeywordException extends Exception {
/**
* Generated serial Version UID
*/
private static final long serialVersionUID = -1696928739600823255L;
public InternallyReservedKeywordException() {
super();
}
public InternallyReservedKeywordException(String message) {
super(message);
}
}

View File

@ -2,6 +2,9 @@ package org.gcube.accounting.exception;
public class InvalidValueException extends Exception {
/**
* Generated serial Version UID
*/
private static final long serialVersionUID = 4403699127526286772L;
public InvalidValueException() {

View File

@ -12,9 +12,6 @@ import java.util.concurrent.Executors;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.provider.ProviderFactory;
import org.gcube.smartgears.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,15 +30,34 @@ public abstract class Persistence {
protected static Persistence persistence;
protected static FallbackPersistence fallback;
private static File file(File file) throws IllegalArgumentException {
if (file.isDirectory())
throw new IllegalArgumentException(file.getAbsolutePath() + " cannot be used in write mode because it's folder");
//create folder structure it does not exist
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
return file;
}
static {
File file;
/*
try {
ContainerContext containerContext = ProviderFactory.provider().containerContext();
org.gcube.smartgears.persistence.Persistence smartgearPersistence = containerContext.persistence();
file = smartgearPersistence.writefile(ACCOUTING_FALLBACK_FILENAME);
} catch(Exception e){
file = Utils.fileAt(new File(".", ACCOUTING_FALLBACK_FILENAME).getAbsolutePath()).toWrite();
file = file(new File(".", ACCOUTING_FALLBACK_FILENAME).getAbsolutePath());
}
*/
file = file(new File(".", ACCOUTING_FALLBACK_FILENAME));
fallback = new FallbackPersistence(file);
try {
/*