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