1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package convwatch;
25 
26 import java.io.File;
27 import java.io.RandomAccessFile;
28 import helper.OSHelper;
29 
30 public class SimpleFileSemaphore /* extends *//* implements */
31 {
32     String m_sInternSemaphoreFile;
33     File m_aInternSemaphoreFile;
34     GlobalLogWriter m_aLog;
35 
sleep( int _nSeconds)36     public static void sleep( int _nSeconds)
37         {
38             // wait a second here
39             try
40             {
41                 java.lang.Thread.sleep(_nSeconds * 1000);
42             }
43             catch (java.lang.InterruptedException e2)
44             {
45             }
46         }
47 
SimpleFileSemaphore()48     public SimpleFileSemaphore() throws IllegalArgumentException
49         {
50             String sInternFileName;
51             if (OSHelper.isWindows())
52             {
53                 sInternFileName = "C:/Temp/ConvwatchOOoSemaphore.txt";
54             }
55             else if (OSHelper.isUnix())
56             {
57                 sInternFileName = "/tmp/ConvwatchOOoSemaphore.txt";
58             }
59             else
60             {
61                 m_sInternSemaphoreFile = null;
62                 throw new IllegalArgumentException("Unknown System, can't initialise SimpleFileSemaphore");
63             }
64 
65             m_sInternSemaphoreFile = sInternFileName;
66             m_aInternSemaphoreFile = new File(sInternFileName);
67         }
68 
getSemaphoreFile()69     public File getSemaphoreFile()
70         {
71             return m_aInternSemaphoreFile;
72         }
73 // ------------------------------------------------------------------------------
74 // wait until resource is available
P(File _aSemaphore)75     public void P(File _aSemaphore)
76         {
77             int nCount = 0;
78             int nCheckLoop = 1;
79 
80             while ( nCheckLoop == 1)
81             {
82                 // check if resource is available, if not, wait.
83                 if ( _aSemaphore.exists() )
84                 {
85                     m_aLog.get().println( "Active wait since " + nCount + "sec..");
86                     nCount ++;
87                     sleep( 1 );
88                 }
89                 else
90                 {
91                     sleep( 1 );
92                     if ( _aSemaphore.exists() )
93                     {
94                         // ups
95                         m_aLog.get().println( "ups...");
96                     }
97                     else
98                     {
99                         nCheckLoop = 0;
100                     }
101                 }
102             }
103 
104             // block resource by ourself
105             try
106             {
107                 RandomAccessFile aWriter = new RandomAccessFile(_aSemaphore, "rw");
108                 aWriter.writeByte((int)1);
109                 aWriter.close();
110             }
111 
112             catch (java.io.FileNotFoundException fne)
113             {
114                 m_aLog.get().println( "caught: FileNotFoundException");
115             }
116             catch(java.io.IOException ie)
117             {
118                 m_aLog.get().println( "caught: IOException");
119             }
120         }
121 
122 // ------------------------------------------------------------------------------
123 // block a resource
V(File _aSemaphore)124     public void V(File _aSemaphore)
125         {
126 
127             if ( _aSemaphore.exists() )
128             {
129                 _aSemaphore.delete();
130             }
131             else
132             {
133                 m_aLog.get().println("Could be a problem here? No resource block found.");
134             }
135         }
136 
137     // --------------------------------- Unit test ---------------------------------
138 
139     private static boolean SEMAPHORE_SHOULD_EXIST = true;
140     private static boolean SEMAPHORE_SHOULD_NOT_EXIST = false;
141 
assure(boolean _b, String _sText)142     private static void assure(boolean _b, String _sText)
143         {
144             System.out.print(_sText);
145             System.out.print("  ");
146             if (_b)
147             {
148                 System.out.println("ok");
149             }
150             else
151             {
152                 System.out.println("FAILED");
153             }
154         }
155 
testSemaphoreFile(SimpleFileSemaphore a, boolean _bShouldFileExists)156     private static void testSemaphoreFile(SimpleFileSemaphore a, boolean _bShouldFileExists)
157         {
158             System.out.println("Check if semaphore file exists.");
159             File aSemaphoreFile = a.getSemaphoreFile();
160             if (aSemaphoreFile.exists())
161             {
162                 System.out.println("Name is: " + aSemaphoreFile.getAbsolutePath());
163                 assure(_bShouldFileExists == SEMAPHORE_SHOULD_EXIST, "Semaphore should exist!");
164             }
165             else
166             {
167                 assure(_bShouldFileExists == SEMAPHORE_SHOULD_NOT_EXIST, "Semaphore should not exist!");
168             }
169         }
170 
main( String[] argv )171     public static void main( String[] argv )
172         {
173             SimpleFileSemaphore a = new SimpleFileSemaphore();
174 
175             testSemaphoreFile(a, SEMAPHORE_SHOULD_NOT_EXIST);
176 
177             a.P(a.getSemaphoreFile());
178 
179             testSemaphoreFile(a, SEMAPHORE_SHOULD_EXIST);
180 
181             a.V(a.getSemaphoreFile());
182 
183             testSemaphoreFile(a, SEMAPHORE_SHOULD_NOT_EXIST);
184         }
185 }
186