Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/obinary/cms/util/AccessLock.java


1   /**
2    *
3    * Magnolia and its source-code is licensed under the LGPL.
4    * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5    * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6    * you are required to provide proper attribution to obinary.
7    * If you reproduce or distribute the document without making any substantive modifications to its content,
8    * please use the following attribution line:
9    *
10   * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11   *
12   * */
13  
14  
15  
16  
17  
18  package com.obinary.cms.util;
19  
20  
21  import com.obinary.cms.core.PathUtil;
22  
23  import java.io.File;
24  
25  
26  /**
27   * Date: Nov 4, 2003
28   * Time: 5:00:26 PM
29   * @author Sameer Charles
30   * @version 1.0
31   */
32  
33  
34  public class AccessLock {
35  
36  
37  
38      /**
39       * <p>
40       * sets the access lock.
41       * No content access method check for this lock accept EntryServlet
42       * while delivering the final page or any resource (images/documents....)
43       * </p>
44       * */
45      public static void set() throws Exception {
46          /* create a tmp file as a lock flag */
47          File lockFile = getLockFile();
48          boolean success = lockFile.createNewFile();
49          if (!success)
50              throw new Exception("Cannot create lock file [ .AccessLock ] under "+PathUtil.getTempDirectoryPath());
51      }
52  
53  
54  
55      /**
56       * <p>
57       * releases any lock set by the set method (if any)
58       * </p>
59       * */
60      public static void release() throws Exception {
61          File lockFile = getLockFile();
62          if (lockFile.exists())
63              lockFile.delete();
64          else
65              throw new Exception("Lock does not exist!");
66      }
67  
68  
69  
70      /**
71       * <p>
72       * Checks is the access lock has been set
73       * </p>
74       * */
75      public static boolean isSet() {
76          File lockFile = getLockFile();
77          if (lockFile.exists())
78              return true;
79          return false;
80      }
81  
82  
83  
84      /**
85       * @return lock file instance
86       * */
87      private static File getLockFile() {
88          try {
89              return new File(PathUtil.getTempDirectoryPath()+"/.AccessLock");
90          } catch (NullPointerException e) {
91              /* TBD: setup magnolia log writing (Log4J) */
92              System.out.println("Fatal : Check Java -D options for magnolia tmp file");
93          }
94          return null;
95      }
96  
97  
98  
99  }