1 13 package org.jahia.services.lock; 14 15 import java.util.StringTokenizer ; 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 ; 23 import java.io.IOException ; 24 25 38 public class LockKey implements Serializable { 39 40 private static final String UPDATE_ACTION = "Update"; 41 private static final String ADD_ACTION = "Add"; 42 private static final String DELETE_ACTION = "Delete"; 43 private static final String RESTORE_ACTION = "Restore"; 44 private static final String MODIFY_ACTION = "Modify"; 45 private static final String EXTRACT_ACTION = "Extract"; 46 private static final String WORKFLOW_ACTION = "Workflow"; 47 48 private static final String VIRTUALSITE_LOCKNAME = "VirtualSite"; 49 private static final String DATABASE_LOCKNAME = "DataBase"; 50 private static final String WAITING_FOR_APPROVAL_LOCKNAME = "WaitingForApproval"; 51 52 public static final String UPDATE_CONTAINERLIST_TYPE = UPDATE_ACTION + "_" + 53 ContentContainerListKey.CONTAINERLIST_TYPE; 54 public static final String ADD_CONTAINER_TYPE = ADD_ACTION + "_" + 55 ContentContainerListKey.CONTAINERLIST_TYPE; 56 public static final String DELETE_CONTAINER_TYPE = DELETE_ACTION + "_" + 57 ContentContainerKey.CONTAINER_TYPE; 58 public static final String UPDATE_CONTAINER_TYPE = UPDATE_ACTION + "_" + 59 ContentContainerKey.CONTAINER_TYPE; 60 public static final String UPDATE_FIELD_TYPE = UPDATE_ACTION + "_" + 61 ContentFieldKey.FIELD_TYPE; 62 public static final String UPDATE_PAGE_TYPE = UPDATE_ACTION + "_" + 63 ContentPageKey.PAGE_TYPE; 64 public static final String RESTORE_SUBSITE_TYPE = RESTORE_ACTION + "_" + 65 ContentPageKey.PAGE_TYPE; 66 public static final String WORKFLOW_TYPE = WORKFLOW_ACTION + "_" + 67 ContentPageKey.PAGE_TYPE; 68 public static final String WAITING_FOR_APPROVAL_TYPE = MODIFY_ACTION + "_" + 69 WAITING_FOR_APPROVAL_LOCKNAME; 70 public static final String DELETE_VIRTUAL_SITE_TYPE = DELETE_ACTION + "_" + 71 VIRTUALSITE_LOCKNAME; 72 public static final String EXTRACT_VIRTUAL_SITE_TYPE = EXTRACT_ACTION + "_" + 73 VIRTUALSITE_LOCKNAME; 74 public static final String RESTORE_VIRTUAL_SITE_TYPE = RESTORE_ACTION + "_" + 75 VIRTUALSITE_LOCKNAME; 76 public static final String EXTRACT_DATABASE_TYPE = EXTRACT_ACTION + "_" + 77 DATABASE_LOCKNAME; 78 public static final String RESTORE_DATABASE_TYPE = RESTORE_ACTION + "_" + 79 DATABASE_LOCKNAME; 80 public static final String MODIFY_DATABASE_TYPE = MODIFY_ACTION + "_" + 81 DATABASE_LOCKNAME; 82 83 84 private LockKey(String name, int id, String action, int pageID) { 85 this.name = name; 86 this.id = id; 87 this.action = action; 88 this.pageID = pageID; 89 } 90 91 public String getAction() { 92 return action; 93 } 94 95 public String getName() { 96 return name; 97 } 98 99 public int getId() { 100 return id; 101 } 102 103 public String getType() { 104 return action + "_" + name; 105 } 106 107 public ObjectKey getObjectKey() { 108 String objectKey = name + ObjectKey.KEY_SEPARATOR + id; 109 try { 110 return ObjectKey.getInstance(objectKey); 111 } catch (ClassNotFoundException cnfe) { 112 logger.error("Object key '" + objectKey + "' not found !"); 116 return null; 117 } 118 } 119 120 public String toString() { 121 return action + "_" + name + "_" + id; 122 } 123 124 public static LockKey composeLockKey(String 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 lockKeyStr) { 132 StringTokenizer lockKeyToken = new StringTokenizer (lockKeyStr, "_"); 133 String action = lockKeyToken.nextToken(); 134 if (!lockKeyToken.hasMoreTokens()) return null; 135 String 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 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 164 private void readObject(java.io.ObjectInputStream stream) 165 throws IOException , ClassNotFoundException { 166 name = (String ) stream.readObject(); 167 id = stream.readInt(); 168 action = (String ) stream.readObject(); 169 } 170 171 private void writeObject(java.io.ObjectOutputStream stream) 172 throws IOException { 173 stream.writeObject(name); 174 stream.writeInt(id); 175 stream.writeObject(action); 176 } 177 178 private String name; 179 private String action; 180 private int id; 181 182 private int pageID = -1; 185 186 private static org.apache.log4j.Logger logger = 187 org.apache.log4j.Logger.getLogger(LockKey.class); 188 } | Popular Tags |