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