comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/ArtifactCallContext.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactCallContext.java@089c6f7794b5
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010, 2011 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8 package de.intevation.artifactdatabase;
9
10 import java.util.LinkedList;
11
12 import org.apache.log4j.Logger;
13
14 import de.intevation.artifacts.CallMeta;
15 import de.intevation.artifacts.Message;
16
17 import de.intevation.artifactdatabase.Backend.PersistentArtifact;
18
19
20 /**
21 * Class that implements the call context handed to the methods calls
22 * describe(), feed(), etc. of the artifact.
23 *
24 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
25 */
26 public class ArtifactCallContext extends AbstractCallContext {
27
28 private static Logger logger = Logger.getLogger(ArtifactCallContext.class);
29
30
31 /**
32 * Error message issued if an artifact wants to translate itself
33 * into a none valid persistent state.
34 */
35 public static final String INVALID_CALL_STATE = "Invalid after call state";
36
37 /**
38 * Error message issued if one tries to remove a requested artifact
39 * from the list of artifacts running in background which is
40 * not in this list.
41 */
42 public static final String NOT_IN_BACKGROUND = "Not in background";
43
44
45 /**
46 * The persistence wrapper around the living artifact
47 */
48 protected PersistentArtifact artifact;
49
50
51 public ArtifactCallContext(
52 ArtifactDatabaseImpl artifactDatabase,
53 int action,
54 CallMeta callMeta,
55 PersistentArtifact artifact)
56 {
57 super(artifactDatabase, action, callMeta);
58
59 this.artifact = artifact;
60 }
61
62
63 public void afterCall(int action) {
64 this.action = action;
65 if (action == BACKGROUND) {
66 database.addIdToBackground(artifact.getId());
67 }
68 }
69
70
71 public void afterBackground(int action) {
72 if (this.action != BACKGROUND) {
73 throw new IllegalStateException(NOT_IN_BACKGROUND);
74 }
75 database.fromBackground(artifact, action);
76 }
77
78
79 public boolean isInBackground() {
80 return database.getLockedIds().contains(artifact.getId());
81 }
82
83
84 public void addBackgroundMessage(Message msg) {
85 database.addBackgroundMessage(artifact.getArtifact().identifier(), msg);
86 }
87
88
89 public LinkedList<Message> getBackgroundMessages() {
90 return database.getBackgroundMessages(
91 artifact.getArtifact().identifier());
92 }
93
94
95 public Long getTimeToLive() {
96 return artifact.getTTL();
97 }
98
99
100 /**
101 * Dispatches and executes the persistence action after
102 * the return of the concrete artifact call.
103 */
104 public void postCall() {
105 try {
106 switch (action) {
107 case NOTHING:
108 break;
109 case TOUCH:
110 artifact.touch();
111 break;
112 case STORE:
113 artifact.store();
114 break;
115 case BACKGROUND:
116 logger.warn(
117 "BACKGROUND processing is not fully implemented, yet!");
118 artifact.store();
119 break;
120 default:
121 logger.error(INVALID_CALL_STATE + ": " + action);
122 throw new IllegalStateException(INVALID_CALL_STATE);
123 }
124 }
125 finally {
126 super.postCall();
127 }
128 }
129 }
130 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org