1 14 41 42 package org.jahia.engines.lock; 43 44 import java.util.Enumeration ; 45 import java.util.HashMap ; 46 47 import org.jahia.data.JahiaData; 48 import org.jahia.engines.EngineToolBox; 49 import org.jahia.engines.JahiaEngine; 50 import org.jahia.exceptions.JahiaException; 51 import org.jahia.params.ParamBean; 52 import org.jahia.registries.ServicesRegistry; 53 import org.jahia.services.lock.LockKey; 54 import org.jahia.services.lock.LockPrerequisites; 55 import org.jahia.services.lock.LockPrerequisitesResult; 56 import org.jahia.services.lock.LockService; 57 58 66 public class LockEngine implements JahiaEngine { 67 68 public static final String ENGINE_NAME = "lock"; 69 public static final String LOCK_ENGINE_JSP = "lock"; 70 private static final String TEMPLATE_JSP = "lock"; 71 72 73 private static LockEngine instance = null; 74 private EngineToolBox toolBox; 75 76 77 private static final org.apache.log4j.Logger logger = 78 org.apache.log4j.Logger.getLogger (LockEngine.class); 79 80 81 private LockEngine () { 82 logger.debug ("***** Starting " + LockEngine.class.getName () + " engine *****"); 83 toolBox = EngineToolBox.getInstance (); 84 } 85 86 89 public static synchronized LockEngine getInstance () { 90 91 if (instance == null) { 92 logger.debug ("Instanciate Lock engine"); 93 instance = new LockEngine (); 94 } 95 return instance; 96 } 97 98 103 public boolean authoriseRender (ParamBean jParams) { 104 return true; 107 } 108 109 114 public boolean needsJahiaData (ParamBean jParams) { 115 return false; 116 } 117 118 128 public String renderLink (ParamBean jParams, Object theObj) 129 throws JahiaException { 130 LockKey lockKey = (LockKey) theObj; 131 return jParams.composeEngineUrl ("lock", "?action=display&lockKey=" + lockKey); 132 } 133 134 143 public void handleActions (ParamBean jParams, JahiaData jData) 144 throws JahiaException { 145 HashMap engineMap = new HashMap (); 146 processAction (jParams, engineMap); 147 } 148 149 154 public final String getName () { 155 return ENGINE_NAME; 156 } 157 158 public void redirect (ParamBean jParams, HashMap engineMap, LockKey lockKey) 159 throws JahiaException { 160 String redirectedToLockEngine = jParams.getParameter ("redirectedToLockEngine"); 161 if (redirectedToLockEngine == null) { 162 initLockEngine (jParams, engineMap, lockKey); 163 toolBox.displayScreen (jParams, engineMap); 164 } else { 165 processAction (jParams, engineMap); 166 } 167 } 168 169 private void processAction (ParamBean jParams, HashMap engineMap) 170 throws JahiaException { 171 String actionScreen = jParams.getParameter ("action"); 172 if ("display".equals (actionScreen)) { 174 String lockKeyStr = jParams.getParameter ("lockKey"); 175 if (lockKeyStr != null) { 176 LockKey lockKey = LockKey.composeLockKey (lockKeyStr); 177 LockPrerequisitesResult lpr = LockPrerequisites.getInstance (). 178 getLockPrerequisitesResult (lockKey); 179 String showDetails = jParams.getParameter ("showDetails"); 180 if (showDetails != null) { 181 lpr.setShowDetails (Boolean.valueOf (showDetails).booleanValue ()); 182 } 183 initLockEngine (jParams, engineMap, lockKey); 184 LockService lockRegistry = ServicesRegistry.getInstance ().getLockService (); 185 Long timeRemaining = lockRegistry.getTimeRemaining (lockKey); 186 if (timeRemaining != null && timeRemaining.longValue () < 0) { 187 engineMap.put ("jspSource", "close"); 188 } 189 toolBox.displayScreen (jParams, engineMap); 190 } 191 } 192 else if ("save".equals (actionScreen) || "apply".equals (actionScreen)) { 194 Enumeration paramNames = jParams.getRequest ().getParameterNames (); 195 while (paramNames.hasMoreElements ()) { 196 String paramName = (String ) paramNames.nextElement (); 197 LockKey lockKey = LockKey.composeLockKey (paramName); 198 if (lockKey != null) { 199 LockService lockRegistry = ServicesRegistry.getInstance ().getLockService (); 200 if (LockPrerequisites.getInstance ().isLockAlreadyAcquired (lockKey)) { 201 lockRegistry.steal (lockKey, jParams.getUser (), jParams.getSessionID ()); 202 } else { 203 lockRegistry.nuke (lockKey, jParams.getUser (), jParams.getSessionID ()); 204 } 205 } 206 } 207 if ("apply".equals (actionScreen)) { 208 engineMap.put ("jspSource", "apply"); 209 } else { 210 engineMap.put ("jspSource", "close"); 211 } 212 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 213 toolBox.displayScreen (jParams, engineMap); 214 } 215 } 216 217 private void initLockEngine (ParamBean jParams, HashMap engineMap, LockKey lockKey) 218 throws JahiaException { 219 engineMap.put ("lockKey", lockKey); 220 LockPrerequisitesResult lockPrerequisitesResult = 221 LockPrerequisites.getInstance ().getLockPrerequisitesResult (lockKey); 222 engineMap.put ("lockPrerequisitesResult", lockPrerequisitesResult); 223 engineMap.put ("jspSource", TEMPLATE_JSP); 224 engineMap.put (ENGINE_NAME_PARAM, "Jahia Locks"); 225 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 226 jParams.getRequest ().setAttribute ("engineTitle", "Lock"); } 228 229 } | Popular Tags |