Project

General

Profile

« Previous | Next » 

Revision 34296

Added by Nikon Gasparis almost 10 years ago

imported the interfaces needed for the validator

View differences:

modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/persistance/Registry.java
1
package eu.dnetlib.validator.engine.persistance;
2

  
3
import java.io.Serializable;
4

  
5
import eu.dnetlib.validator.engine.Validator;
6

  
7
/**
8
 * A Registry used to store serializable objects.
9
 * Registries are used by {@link Validator} to store the objects they need to run.
10
 * @author Manos Karvounis
11
 *
12
 * @param <T>
13
 */
14
public abstract class Registry<T extends Serializable> {
15

  
16
	public final String name;
17
	
18
	public Registry(String name) {
19
		super();
20
		this.name = name;
21
	}
22

  
23
	public abstract T getObject(int id);
24

  
25
	public abstract void addObject(int id, T obj);
26
	
27
}
0 28

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/CompletedTask.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import java.util.Date;
4
import java.util.List;
5

  
6
/**
7
 * Represents a CompletedTask.
8
 * @author Manos Karvounis
9
 * @author Nikon Gasparis
10
 *
11
 */
12
public class CompletedTask {
13

  
14
	/**
15
	 * If the rule was applied successfully.
16
	 */
17
	public final boolean success;
18
	/**
19
	 * The id of the object on which the rule was applied.
20
	 */
21
	public final String valobjId;
22
	/**
23
	 * The id of the rule that was applied.
24
	 */
25
	public final int ruleId;
26
	/**
27
	 * The date the task started.
28
	 */
29
	public final Date started;
30
	/**
31
	 * The date the task finished.
32
	 */
33
	public final Date finished;
34
	/**
35
	 * The reasons why the rule failed to apply on the object.
36
	 */
37
	public final List<String> errors;
38
	/**
39
	 * Any exception that might have been raised while the rule was applied on the object.
40
	 */
41
	public final Exception exception;
42

  
43
	public CompletedTask(boolean success, String valobjId, int ruleId, Date started, Date finished, Exception exception, List<String> errors) {
44
		super();
45
		this.success = success;
46
		this.valobjId = valobjId;
47
		this.ruleId = ruleId;
48
		this.started = started;
49
		this.finished = finished;
50
		this.exception = exception;
51
		this.errors = errors;
52
	}
53

  
54
}
0 55

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/JobListener.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import java.util.List;
4
import java.util.Map;
5

  
6
/**
7
 * A Listener observing an executing job.
8
 * @author Manos Karvounis
9
 *
10
 */
11
public interface JobListener {
12
	/**
13
	 * <p>Indicates that some tasks of the submitted job have finished unsuccessfully because of an exception.</p>
14
	 * <p>There might be other tasks waiting to be executed.</p>
15
	 * @param tasks The tasks that finished.
16
	 * @param jobId The id of the job that defined the tasks.
17
	 * @param object 
18
	 * @param t The exception that was thrown.
19
	 */
20
	public void currentResults(List<CompletedTask> tasks, int jobId, Object record, Map<String, Object> recordContext, Throwable t);
21
	
22
	/**
23
	 * <p>Indicates that some tasks of the submitted job have finished.</p>
24
	 * <p>There might be other tasks waiting to be executed.</p>
25
	 * @param tasks The tasks that finished.
26
	 * @param jobId The id of the job that defined the tasks.
27
	 */
28
	public void currentResults(List<CompletedTask> tasks, int jobId, Object record, Map<String, Object> recordContext);
29
	
30
	/**
31
	 * Indicates that all tasks of a job have finished.
32
	 * @param jobId
33
	 */
34
	public void finished(int jobId, Map<String, Object> jobContext);
35
	
36
	/**
37
	 * Indicates that a job finished unsuccessfully because of an exception.
38
	 * @param jobId
39
	 * @param t
40
	 */
41
	public void failed(int jobId, Map<String, Object> jobContext, Throwable t);
42
}
0 43

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/ValidationObject.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import eu.dnetlib.validator.engine.data.Provider;
4
import eu.dnetlib.validator.engine.data.Rule;
5

  
6
/**
7
 * Represents an on which a {@link Rule} may be applied
8
 * @author Manos Karvounis
9
 * @see Provider
10
 */
11
public interface ValidationObject {
12

  
13
	public String getId();
14
	public String getStatus();
15
	public void setId(String id);
16
	public Object getContentAsObject();
17
}
0 18

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/Task.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import java.util.List;
4

  
5
import org.apache.log4j.Logger;
6

  
7
import eu.dnetlib.validator.engine.data.Rule;
8
import eu.dnetlib.validator.engine.data.RuleException;
9

  
10
/**
11
 * A Rule that is applied on a Validation Object.
12
 * @author manos
13
 * @author Nikon Gasparis
14
 *
15
 */
16
public class Task implements Runnable {
17

  
18
	public final ValidationObject valobj;
19
	public final Rule rule;
20

  
21
	private boolean success;
22
	private List<String> errors;
23
	private Exception exception = null;;
24

  
25
	public Task(ValidationObject valobj, Rule rule) {
26
		super();
27
		this.valobj = valobj;
28
		this.rule = rule;
29
	}
30

  
31
	@Override
32
	public void run() {
33
		Logger log = Logger.getLogger(Task.class);
34
		try {
35
			this.success = rule.apply(valobj);
36
			this.setErrors(rule.getErrors());
37
		} catch (RuleException e) {
38
			log.error("Error applyling rule on task", e);
39
			this.exception = e;
40
		}
41
	}
42

  
43
	public boolean isSuccess() {
44
		return success;
45
	}
46

  
47
	public Exception getException() {
48
		return exception;
49
	}
50

  
51
	public List<String> getErrors() {
52
		return errors;
53
	}
54

  
55
	public void setErrors(List<String> errors) {
56
		this.errors = errors;
57
	}
58

  
59
}
0 60

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/Job.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import java.io.Serializable;
4
import java.util.HashSet;
5
import java.util.Properties;
6
import java.util.Set;
7

  
8
import eu.dnetlib.validator.engine.data.Rule;
9

  
10
/**
11
 * <p>
12
 * Represents a validation job. A validation job consists of a <i>single</i>
13
 * provider and a set of rules that will be applied on the validation objects
14
 * retrieved by the provider.
15
 * </p>
16
 * <p>
17
 * Please note that to execute a validation, you might need to submit multiple
18
 * jobs, containing various rules and providers.
19
 * </p>
20
 * 
21
 * @author Manos Karvounis
22
 * 
23
 */
24
public class Job implements Serializable {
25

  
26
	private static final long serialVersionUID = -8331921926659185511L;
27

  
28
	public final int providerId;
29
	public final Properties providerProps;
30
	public final Set<Rule> rules;
31
	public final int id;
32

  
33
	public Job(int id, int providerId, Set<Rule> rules, Properties providerProps) {
34
		super();
35
		this.id = id;
36
		this.providerId = providerId;
37
		this.rules = new HashSet<Rule>();
38
		this.rules.addAll(rules);
39
		this.providerProps = new Properties();
40
		this.providerProps.putAll(providerProps);
41
	}
42

  
43
}
0 44

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/Executor.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import java.util.concurrent.ExecutionException;
4
import java.util.concurrent.Future;
5
import java.util.concurrent.TimeoutException;
6

  
7
public interface Executor {
8

  
9
	public Future<?> execute(TaskList ltasks) throws TimeoutException, ExecutionException;
10
}
0 11

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/execution/TaskList.java
1
package eu.dnetlib.validator.engine.execution;
2

  
3
import java.util.ArrayList;
4
import java.util.Date;
5
import java.util.List;
6

  
7
/**
8
 * A List of Tasks to be executed.
9
 * @author Manos Karvounis
10
 * @author Nikon Gasparis
11
 * @see Task
12
 */
13
public class TaskList implements Runnable {
14

  
15
	public final List<Task> tasks;
16

  
17
	private List<CompletedTask> ctasks;
18

  
19
	public TaskList(List<Task> tasks) {
20
		super();
21
		this.tasks = tasks;
22
		ctasks = new ArrayList<CompletedTask>();
23
	}
24

  
25
	@Override
26
	public void run() {
27
		for (Task task : tasks) {
28
			Date started = new Date();
29
			task.run();
30
			Date finished = new Date();
31
			CompletedTask ctask = new CompletedTask(task.isSuccess(), task.valobj.getId(), task.rule.getId(), started, finished, task.getException(), task.getErrors());
32
			ctasks.add(ctask);
33
		}
34
	}
35

  
36
	public List<CompletedTask> getCtasks() {
37
		return ctasks;
38
	}
39

  
40
}
0 41

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/ValidatorException.java
1
package eu.dnetlib.validator.engine;
2

  
3
public class ValidatorException extends Exception {
4

  
5
	private static final long serialVersionUID = -5124043465552948242L;
6

  
7
	public ValidatorException() {
8
		super();
9
	}
10

  
11
	public ValidatorException(String message) {
12
		super(message);
13
	}
14

  
15
	public ValidatorException(String message, Throwable cause) {
16
		super(message, cause);
17
	}
18

  
19
	public ValidatorException(Throwable cause) {
20
		super(cause);
21
	}
22
}
0 23

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/Validator.java
1
package eu.dnetlib.validator.engine;
2

  
3
import java.io.Serializable;
4
import java.util.Map;
5
import java.util.Set;
6

  
7
import eu.dnetlib.validator.engine.data.Provider;
8
import eu.dnetlib.validator.engine.data.Rule;
9
import eu.dnetlib.validator.engine.execution.Job;
10
import eu.dnetlib.validator.engine.execution.JobListener;
11

  
12
/**
13
 * <p>
14
 * The front-end interface for a Validator.
15
 * </p>
16
 * <p>
17
 * A Validator receives Job execution requests and executes them.
18
 * </p>
19
 * <p>
20
 * It is very important that a Validator is Persistable, i.e., it can be stored
21
 * in permanent storage and may be retrieved later.
22
 * </p>
23
 * 
24
 * @author Manos Karvounis
25
 */
26
public interface Validator {
27

  
28
	/**
29
	 * Used to store an object to a Registry. Usually these objects are the
30
	 * rules and providers that are contained in the validator.
31
	 * 
32
	 * @param <T>
33
	 *            The serializable object to be added in the registry.
34
	 * @param objid
35
	 *            A unique id for the object.
36
	 * @param obj
37
	 *            The object itself.
38
	 * @param registryName
39
	 *            The name of the registry. The registry must have been already
40
	 *            added to the Validator.
41
	 * @see Provider
42
	 * @see Rule
43
	 * @see Validator#addRegistry(String)
44
	 */
45
	public <T extends Serializable> void addToRegistry(int objid, T obj,
46
			String registryName);
47

  
48
	/**
49
	 * Retrieves a previously stored object from a Registry.
50
	 * 
51
	 * @param objid
52
	 *            The id of the object, as specified when the object was stored
53
	 *            using
54
	 *            {@link Validator#addToRegistry(int, Serializable, String)}
55
	 * @param registryName
56
	 *            The name of the registry
57
	 * @return The object
58
	 * @see Validator#addToRegistry(int, Serializable, String)
59
	 */
60
	public Serializable getFromRegistry(int objid, String registryName)
61
			throws ValidatorException;
62

  
63
	/**
64
	 * Adds a new registry to the Validator. The registries are used to store
65
	 * objects the validator needs. Usually they are rules and providers.
66
	 * 
67
	 * @param <T>
68
	 *            The type of objects that the registry will contain.
69
	 * @param name
70
	 *            A unique name for the registry.
71
	 * @see Validator#addToRegistry(int, Serializable, String)
72
	 */
73
	public <T extends Serializable> void addRegistry(String name);
74

  
75
	/**
76
	 * Submit a new validation job to be executed at some time. The submission
77
	 * must not be blocking. The external application is notified on the
78
	 * execution progress by registering a listener.
79
	 * 
80
	 * @param job
81
	 * @param workers
82
	 * @param listener
83
	 */
84
	public void submitJob(Job job, int workers, JobListener... listeners)
85
			throws ValidatorException;
86

  
87
	/**
88
	 * Submit a new validation job to be executed at some time. The submission
89
	 * must not be blocking. The external application is notified on the
90
	 * execution progress by registering a listener.
91
	 * 
92
	 * @param job
93
	 * @param workers
94
	 * @param listener
95
	 */
96

  
97
	public void submitJobForCris(Job job,
98
			Map<String, Set<Rule>> rulesPerEntity,
99
			Map<String, Set<Rule>> entityChosenRulesMapReferential,
100
			JobListener... listeners) throws ValidatorException;
101

  
102
	/**
103
	 * Shuts down the validator. All resources held by the validator are
104
	 * released. 
105
	 * 
106
	 * @throws ValidatorException
107
	 */
108
	public void shutdown() throws ValidatorException;
109

  
110
	/**
111
	 * Starts the validator. This method must be invoked before any other method
112
	 * is invoked.
113

  
114
	 * @throws ValidatorException
115
	 */
116
	public void start() throws ValidatorException;
117
}
0 118

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/data/RuleException.java
1
package eu.dnetlib.validator.engine.data;
2

  
3
import eu.dnetlib.validator.engine.ValidatorException;
4

  
5
public class RuleException extends ValidatorException {
6

  
7
	private static final long serialVersionUID = 7060357707549236243L;
8

  
9
	public RuleException() {
10
		super();
11
	}
12

  
13
	public RuleException(String msg) {
14
		super(msg);
15
	}
16
}
0 17

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/data/ConfigurableObject.java
1
package eu.dnetlib.validator.engine.data;
2

  
3
import java.io.IOException;
4
import java.io.ObjectInputStream;
5
import java.io.Serializable;
6
import java.util.Properties;
7

  
8
import org.apache.log4j.Logger;
9

  
10
/**
11
 * An object identified by a unique id and parametrized by Properties.
12
 * 
13
 * @author Manos Karvounis
14
 * @see Rule
15
 * @see Provider
16
 */
17
public class ConfigurableObject implements Serializable {
18

  
19
	protected transient Logger log = Logger.getLogger(ConfigurableObject.class);
20

  
21
	private static final long serialVersionUID = -1573173142135631726L;
22

  
23
	protected Properties pros;
24
	private int id;
25

  
26
	public ConfigurableObject(Properties pros, int id) {
27
		super();
28
//		log.debug("Creating a configurable object with id " + id + " and props " + pros);
29
		log.debug("Creating a configurable object with id " + id);
30
		this.pros = pros;
31
		this.id = id;
32
	}
33

  
34
	public Properties getConfiguration() {
35
		return this.pros;
36
	}
37
	
38
	public int getId() {
39
		return this.id;
40
	}
41

  
42
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
43
		in.defaultReadObject();
44
		log = Logger.getLogger(ConfigurableObject.class);
45
	}
46

  
47
}
0 48

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/data/Rule.java
1
package eu.dnetlib.validator.engine.data;
2

  
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Properties;
6

  
7
import eu.dnetlib.validator.engine.execution.ValidationObject;
8

  
9
/**
10
 * A rule is applied on a Validation Object retrieved by a {@link Provider}
11
 * @author Manos Karvounis
12
 * @author Nikon Gasparis
13
 *
14
 */
15
public abstract class Rule extends ConfigurableObject {
16
	
17
	private static final long serialVersionUID = 5118099751892948569L;
18
	
19
	private Provider provider;
20
	private String valObjId;
21
	private List<String> errors;
22

  
23
	public Rule(Properties pros, int id) {
24
		super(pros, id);
25
		setErrors(new ArrayList<String>());
26
	}
27

  
28
	/**
29
	 * @param obj
30
	 * @return True if the rule was successful, otherwise false
31
	 * @throws RuleException If an exception was raised during the application of the rule.
32
	 */
33
	
34
	public abstract boolean apply(ValidationObject obj) throws RuleException;
35

  
36
	public Provider getProvider() {
37
		return provider;
38
	}
39

  
40
	public void setProvider(Provider provider) {
41
		this.provider = provider;
42
	}
43

  
44
	public String getValObjId() {
45
		return valObjId;
46
	}
47

  
48
	public void setValObjId(String valObjId) {
49
		this.valObjId = valObjId;
50
	}
51

  
52
	public List<String> getErrors() {
53
		return errors;
54
	}
55

  
56
	public void setErrors(List<String> errors) {
57
		this.errors = errors;
58
	}
59
	
60
	
61

  
62
}
0 63

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/data/DataException.java
1
package eu.dnetlib.validator.engine.data;
2

  
3
import eu.dnetlib.validator.engine.ValidatorException;
4

  
5
public class DataException extends ValidatorException {
6

  
7
	private static final long serialVersionUID = -4341747976225287737L;
8

  
9
	
10
	public DataException() {
11
		super();
12
	}
13
	
14
	public DataException(String msg) {
15
		super(msg);
16
	}
17
}
0 18

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/data/ResultSet.java
1
package eu.dnetlib.validator.engine.data;
2

  
3
public interface ResultSet<T> {
4

  
5
	/**
6
	 * The first call on next() must position the cursor to the first object.
7
	 * @return
8
	 * @throws DataException
9
	 */
10
	public boolean next() throws DataException;
11
	public T get() throws DataException;
12
	public String getError();
13
	
14
}
0 15

  
modules/uoa-validator-engine/trunk/src/main/java/eu/dnetlib/validator/engine/data/Provider.java
1
package eu.dnetlib.validator.engine.data;
2

  
3
import java.util.Properties;
4

  
5
import org.apache.log4j.Logger;
6

  
7
import eu.dnetlib.validator.engine.ValidatorException;
8
import eu.dnetlib.validator.engine.execution.ValidationObject;
9

  
10
/**
11
 * A Provider is used to retrieve Validation Objects on which rules are applied.
12
 * Certain rules may only work with certain kinds of providers.
13
 * 
14
 * @author manos
15
 * @see Rule
16
 */
17
public abstract class Provider extends ConfigurableObject {
18

  
19
	private static final long serialVersionUID = -5297066032993163543L;
20
	
21
	public Provider(Integer id) {
22
		super(null, id);
23
	}
24

  
25
	public void setConfiguration(Properties pros) {
26
		Logger log = Logger.getLogger(Provider.class);
27
		log.debug("Setting properties of provider type "+this.getId()+" to "+this.pros);
28
		this.pros = pros;
29
	}
30

  
31
	
32
	/**
33
	 * Retrieves the Validation Objects.
34
	 * 
35
	 * @return The validation objects.
36
	 * @throws ProviderException
37
	 */
38
	public abstract ResultSet<ValidationObject> getValidationObjects() throws ProviderException;
39
	
40
	/**
41
	 * Retrieves the Validation Objects given an entity name.
42
	 * 
43
	 * @param valObjId
44
	 * 
45
	 * @return The validation objects.
46
	 * @throws ProviderException
47
	 */
48
	public abstract ResultSet<ValidationObject> getValidationObjects(String entity) throws ProviderException;
49

  
50
	/**
51
	 * <p>
52
	 * Retrieves the ids of the Validation Objects.
53
	 * </p>
54
	 * <p>
55
	 * The Validation Objects themselves might be retrieved by
56
	 * {@link Provider#getValidationObject(String)}.
57
	 * </p>
58
	 * <p>
59
	 * Not all providers need to support such a method.
60
	 * </p>
61
	 * 
62
	 * @return The ids of the validation objects
63
	 * @throws ProviderException
64
	 * @throws UnsupportedOperationException
65
	 *             If the provider does not support this method.
66
	 */
67
	public abstract ResultSet<String> getValidationObjectIds() throws ProviderException, UnsupportedOperationException;
68

  
69
	/**
70
	 * Retrieves a validation object given its id. Not all providers need to
71
	 * support such a method.
72
	 * 
73
	 * @param valObjId
74
	 *            The id of the object.
75
	 * @return The object itself.
76
	 * @throws ProviderException
77
	 * @throws UnsupportedOperationException
78
	 *             UnsupportedOperationException
79
	 */
80
	public abstract ValidationObject getValidationObject(String valObjId) throws ProviderException, UnsupportedOperationException;
81

  
82
	public class ProviderException extends ValidatorException {
83

  
84
		private static final long serialVersionUID = 5574587409338211253L;
85

  
86
		public ProviderException() {
87
			super();
88
		}
89

  
90
		public ProviderException(String msg) {
91
			super(msg);
92
		}
93
	}
94
}
0 95

  
modules/uoa-validator-engine/trunk/pom.xml
13 13
	<scm>
14 14
		<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-validator-engine/trunk</developerConnection>
15 15
	</scm>
16
	</dependencies>
17 16
</project>

Also available in: Unified diff