KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > lock > LockKey


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.services.lock;
14
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.jahia.content.ContentContainerKey;
18 import org.jahia.content.ContentContainerListKey;
19 import org.jahia.content.ContentFieldKey;
20 import org.jahia.content.ContentPageKey;
21 import org.jahia.content.ObjectKey;
22 import java.io.Serializable JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * <p>Title: Jahia locking system implementation.</p>
27  * <p>Description:
28  * Implement a unique lock key system. A lock key is defined by three parameters :
29  * - An action : Kind of operation which is perfomed on a resource.
30  * - A name : which is the lock name given by the engine names or something else.
31  * - An identifier : Given by a content object, a site, ...
32  * </p>
33  * <p>Copyright: MAP (Jahia Solutions Sàrl 2003)</p>
34  * <p>Company: Jahia Solutions Sàrl</p>
35  * @author unascribed
36  * @version 1.0
37  */

38 public class LockKey implements Serializable JavaDoc {
39
40     private static final String JavaDoc UPDATE_ACTION = "Update";
41     private static final String JavaDoc ADD_ACTION = "Add";
42     private static final String JavaDoc DELETE_ACTION = "Delete";
43     private static final String JavaDoc RESTORE_ACTION = "Restore";
44     private static final String JavaDoc MODIFY_ACTION = "Modify";
45     private static final String JavaDoc EXTRACT_ACTION = "Extract";
46     private static final String JavaDoc WORKFLOW_ACTION = "Workflow";
47
48     private static final String JavaDoc VIRTUALSITE_LOCKNAME = "VirtualSite";
49     private static final String JavaDoc DATABASE_LOCKNAME = "DataBase";
50     private static final String JavaDoc WAITING_FOR_APPROVAL_LOCKNAME = "WaitingForApproval";
51
52     public static final String JavaDoc UPDATE_CONTAINERLIST_TYPE = UPDATE_ACTION + "_" +
53             ContentContainerListKey.CONTAINERLIST_TYPE;
54     public static final String JavaDoc ADD_CONTAINER_TYPE = ADD_ACTION + "_" +
55             ContentContainerListKey.CONTAINERLIST_TYPE;
56     public static final String JavaDoc DELETE_CONTAINER_TYPE = DELETE_ACTION + "_" +
57             ContentContainerKey.CONTAINER_TYPE;
58     public static final String JavaDoc UPDATE_CONTAINER_TYPE = UPDATE_ACTION + "_" +
59             ContentContainerKey.CONTAINER_TYPE;
60     public static final String JavaDoc UPDATE_FIELD_TYPE = UPDATE_ACTION + "_" +
61             ContentFieldKey.FIELD_TYPE;
62     public static final String JavaDoc UPDATE_PAGE_TYPE = UPDATE_ACTION + "_" +
63             ContentPageKey.PAGE_TYPE;
64     public static final String JavaDoc RESTORE_SUBSITE_TYPE = RESTORE_ACTION + "_" +
65             ContentPageKey.PAGE_TYPE;
66     public static final String JavaDoc WORKFLOW_TYPE = WORKFLOW_ACTION + "_" +
67             ContentPageKey.PAGE_TYPE;
68     public static final String JavaDoc WAITING_FOR_APPROVAL_TYPE = MODIFY_ACTION + "_" +
69             WAITING_FOR_APPROVAL_LOCKNAME;
70     public static final String JavaDoc DELETE_VIRTUAL_SITE_TYPE = DELETE_ACTION + "_" +
71             VIRTUALSITE_LOCKNAME;
72     public static final String JavaDoc EXTRACT_VIRTUAL_SITE_TYPE = EXTRACT_ACTION + "_" +
73             VIRTUALSITE_LOCKNAME;
74     public static final String JavaDoc RESTORE_VIRTUAL_SITE_TYPE = RESTORE_ACTION + "_" +
75             VIRTUALSITE_LOCKNAME;
76     public static final String JavaDoc EXTRACT_DATABASE_TYPE = EXTRACT_ACTION + "_" +
77             DATABASE_LOCKNAME;
78     public static final String JavaDoc RESTORE_DATABASE_TYPE = RESTORE_ACTION + "_" +
79             DATABASE_LOCKNAME;
80     public static final String JavaDoc MODIFY_DATABASE_TYPE = MODIFY_ACTION + "_" +
81             DATABASE_LOCKNAME;
82
83
84     private LockKey(String JavaDoc name, int id, String JavaDoc action, int pageID) {
85         this.name = name;
86         this.id = id;
87         this.action = action;
88         this.pageID = pageID;
89     }
90
91     public String JavaDoc getAction() {
92         return action;
93     }
94
95     public String JavaDoc getName() {
96         return name;
97     }
98
99     public int getId() {
100         return id;
101     }
102
103     public String JavaDoc getType() {
104         return action + "_" + name;
105     }
106
107     public ObjectKey getObjectKey() {
108         String JavaDoc objectKey = name + ObjectKey.KEY_SEPARATOR + id;
109         try {
110             return ObjectKey.getInstance(objectKey);
111         } catch (ClassNotFoundException JavaDoc cnfe) {
112             // Displaying stack trace is not necessary because we effectively
113
// try to get a content object from the LockKey; other objects
114
// or critical section can be locked.
115
logger.error("Object key '" + objectKey + "' not found !");
116             return null;
117         }
118     }
119
120     public String JavaDoc toString() {
121         return action + "_" + name + "_" + id;
122     }
123
124     public static LockKey composeLockKey(String JavaDoc lockType, int id, int pageID) {
125         int index = lockType.indexOf("_");
126         return new LockKey(lockType.substring(index + 1),
127                            id,
128                            lockType.substring(0, index), pageID);
129     }
130
131     public static LockKey composeLockKey(String JavaDoc lockKeyStr) {
132         StringTokenizer JavaDoc lockKeyToken = new StringTokenizer JavaDoc(lockKeyStr, "_");
133         String JavaDoc action = lockKeyToken.nextToken();
134         if (!lockKeyToken.hasMoreTokens()) return null;
135         String JavaDoc name = lockKeyToken.nextToken();
136         if (!lockKeyToken.hasMoreTokens()) return null;
137         int id = Integer.parseInt(lockKeyToken.nextToken());
138         return new LockKey(name, id, action, -1);
139     }
140
141     public boolean equals(Object JavaDoc obj) {
142         if (this == obj) {
143             return true;
144         }
145         if (!(obj instanceof LockKey)) {
146             return false;
147         }
148         LockKey lockKey = (LockKey)obj;
149         return lockKey.getAction().equals(this.action) &&
150             lockKey.getName().equals(this.name) &&
151             lockKey.getId() == this.id;
152     }
153
154     public int hashCode() {
155         return this.toString().hashCode();
156     }
157
158     public int getPageID() {
159         return pageID;
160     }
161
162     // Serialization methods
163

164     private void readObject(java.io.ObjectInputStream JavaDoc stream)
165         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
166         name = (String JavaDoc) stream.readObject();
167         id = stream.readInt();
168         action = (String JavaDoc) stream.readObject();
169     }
170
171     private void writeObject(java.io.ObjectOutputStream JavaDoc stream)
172         throws IOException JavaDoc {
173         stream.writeObject(name);
174         stream.writeInt(id);
175         stream.writeObject(action);
176     }
177
178     private String JavaDoc name;
179     private String JavaDoc action;
180     private int id;
181
182     // This is not serialized but it is used to resolve the page ID of
183
// objects that only exist in memory like container lists that are empty
184
private int pageID = -1;
185
186     private static org.apache.log4j.Logger logger =
187             org.apache.log4j.Logger.getLogger(LockKey.class);
188 }
Popular Tags