1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package convwatch;
29 
30 import java.io.File;
31 import java.io.RandomAccessFile;
32 import helper.OSHelper;
33 
34 public class SimpleFileSemaphore /* extends *//* implements */
35 {
36     String m_sInternSemaphoreFile;
37     File m_aInternSemaphoreFile;
38     GlobalLogWriter m_aLog;
39 
40     public static void sleep( int _nSeconds)
41         {
42             // wait a second here
43             try
44             {
45                 java.lang.Thread.sleep(_nSeconds * 1000);
46             }
47             catch (java.lang.InterruptedException e2)
48             {
49             }
50         }
51 
52     public SimpleFileSemaphore() throws IllegalArgumentException
53         {
54             String sInternFileName;
55             if (OSHelper.isWindows())
56             {
57                 sInternFileName = "C:/Temp/ConvwatchOOoSemaphore.txt";
58             }
59             else if (OSHelper.isUnix())
60             {
61                 sInternFileName = "/tmp/ConvwatchOOoSemaphore.txt";
62             }
63             else
64             {
65                 m_sInternSemaphoreFile = null;
66                 throw new IllegalArgumentException("Unknown System, can't initialise SimpleFileSemaphore");
67             }
68 
69             m_sInternSemaphoreFile = sInternFileName;
70             m_aInternSemaphoreFile = new File(sInternFileName);
71         }
72 
73     public File getSemaphoreFile()
74         {
75             return m_aInternSemaphoreFile;
76         }
77 // ------------------------------------------------------------------------------
78 // wait until resource is available
79     public void P(File _aSemaphore)
80         {
81             int nCount = 0;
82             int nCheckLoop = 1;
83 
84             while ( nCheckLoop == 1)
85             {
86                 // check if resource is available, if not, wait.
87                 if ( _aSemaphore.exists() )
88                 {
89                     m_aLog.get().println( "Active wait since " + nCount + "sec..");
90                     nCount ++;
91                     sleep( 1 );
92                 }
93                 else
94                 {
95                     sleep( 1 );
96                     if ( _aSemaphore.exists() )
97                     {
98                         // ups
99                         m_aLog.get().println( "ups...");
100                     }
101                     else
102                     {
103                         nCheckLoop = 0;
104                     }
105                 }
106             }
107 
108             // block resource by ourself
109             try
110             {
111                 RandomAccessFile aWriter = new RandomAccessFile(_aSemaphore, "rw");
112                 aWriter.writeByte((int)1);
113                 aWriter.close();
114             }
115 
116             catch (java.io.FileNotFoundException fne)
117             {
118                 m_aLog.get().println( "caught: FileNotFoundException");
119             }
120             catch(java.io.IOException ie)
121             {
122                 m_aLog.get().println( "caught: IOException");
123             }
124         }
125 
126 // ------------------------------------------------------------------------------
127 // block a resource
128     public void V(File _aSemaphore)
129         {
130 
131             if ( _aSemaphore.exists() )
132             {
133                 _aSemaphore.delete();
134             }
135             else
136             {
137                 m_aLog.get().println("Could be a problem here? No resource block found.");
138             }
139         }
140 
141     // --------------------------------- Unit test ---------------------------------
142 
143     private static boolean SEMAPHORE_SHOULD_EXIST = true;
144     private static boolean SEMAPHORE_SHOULD_NOT_EXIST = false;
145 
146     private static void assure(boolean _b, String _sText)
147         {
148             System.out.print(_sText);
149             System.out.print("  ");
150             if (_b)
151             {
152                 System.out.println("ok");
153             }
154             else
155             {
156                 System.out.println("FAILED");
157             }
158         }
159 
160     private static void testSemaphoreFile(SimpleFileSemaphore a, boolean _bShouldFileExists)
161         {
162             System.out.println("Check if semaphore file exists.");
163             File aSemaphoreFile = a.getSemaphoreFile();
164             if (aSemaphoreFile.exists())
165             {
166                 System.out.println("Name is: " + aSemaphoreFile.getAbsolutePath());
167                 assure(_bShouldFileExists == SEMAPHORE_SHOULD_EXIST, "Semaphore should exist!");
168             }
169             else
170             {
171                 assure(_bShouldFileExists == SEMAPHORE_SHOULD_NOT_EXIST, "Semaphore should not exist!");
172             }
173         }
174 
175     public static void main( String[] argv )
176         {
177             SimpleFileSemaphore a = new SimpleFileSemaphore();
178 
179             testSemaphoreFile(a, SEMAPHORE_SHOULD_NOT_EXIST);
180 
181             a.P(a.getSemaphoreFile());
182 
183             testSemaphoreFile(a, SEMAPHORE_SHOULD_EXIST);
184 
185             a.V(a.getSemaphoreFile());
186 
187             testSemaphoreFile(a, SEMAPHORE_SHOULD_NOT_EXIST);
188         }
189 }
190