1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
31 import java
.io
.RandomAccessFile
;
32 import helper
.OSHelper
;
34 public class SimpleFileSemaphore
/* extends *//* implements */
36 String m_sInternSemaphoreFile
;
37 File m_aInternSemaphoreFile
;
38 GlobalLogWriter m_aLog
;
40 public static void sleep( int _nSeconds
)
45 java
.lang
.Thread
.sleep(_nSeconds
* 1000);
47 catch (java
.lang
.InterruptedException e2
)
52 public SimpleFileSemaphore() throws IllegalArgumentException
54 String sInternFileName
;
55 if (OSHelper
.isWindows())
57 sInternFileName
= "C:/Temp/ConvwatchOOoSemaphore.txt";
59 else if (OSHelper
.isUnix())
61 sInternFileName
= "/tmp/ConvwatchOOoSemaphore.txt";
65 m_sInternSemaphoreFile
= null;
66 throw new IllegalArgumentException("Unknown System, can't initialise SimpleFileSemaphore");
69 m_sInternSemaphoreFile
= sInternFileName
;
70 m_aInternSemaphoreFile
= new File(sInternFileName
);
73 public File
getSemaphoreFile()
75 return m_aInternSemaphoreFile
;
77 // ------------------------------------------------------------------------------
78 // wait until resource is available
79 public void P(File _aSemaphore
)
84 while ( nCheckLoop
== 1)
86 // check if resource is available, if not, wait.
87 if ( _aSemaphore
.exists() )
89 m_aLog
.get().println( "Active wait since " + nCount
+ "sec..");
96 if ( _aSemaphore
.exists() )
99 m_aLog
.get().println( "ups...");
108 // block resource by ourself
111 RandomAccessFile aWriter
= new RandomAccessFile(_aSemaphore
, "rw");
112 aWriter
.writeByte((int)1);
116 catch (java
.io
.FileNotFoundException fne
)
118 m_aLog
.get().println( "caught: FileNotFoundException");
120 catch(java
.io
.IOException ie
)
122 m_aLog
.get().println( "caught: IOException");
126 // ------------------------------------------------------------------------------
128 public void V(File _aSemaphore
)
131 if ( _aSemaphore
.exists() )
133 _aSemaphore
.delete();
137 m_aLog
.get().println("Could be a problem here? No resource block found.");
141 // --------------------------------- Unit test ---------------------------------
143 private static boolean SEMAPHORE_SHOULD_EXIST
= true;
144 private static boolean SEMAPHORE_SHOULD_NOT_EXIST
= false;
146 private static void assure(boolean _b
, String _sText
)
148 System
.out
.print(_sText
);
149 System
.out
.print(" ");
152 System
.out
.println("ok");
156 System
.out
.println("FAILED");
160 private static void testSemaphoreFile(SimpleFileSemaphore a
, boolean _bShouldFileExists
)
162 System
.out
.println("Check if semaphore file exists.");
163 File aSemaphoreFile
= a
.getSemaphoreFile();
164 if (aSemaphoreFile
.exists())
166 System
.out
.println("Name is: " + aSemaphoreFile
.getAbsolutePath());
167 assure(_bShouldFileExists
== SEMAPHORE_SHOULD_EXIST
, "Semaphore should exist!");
171 assure(_bShouldFileExists
== SEMAPHORE_SHOULD_NOT_EXIST
, "Semaphore should not exist!");
175 public static void main( String
[] argv
)
177 SimpleFileSemaphore a
= new SimpleFileSemaphore();
179 testSemaphoreFile(a
, SEMAPHORE_SHOULD_NOT_EXIST
);
181 a
.P(a
.getSemaphoreFile());
183 testSemaphoreFile(a
, SEMAPHORE_SHOULD_EXIST
);
185 a
.V(a
.getSemaphoreFile());
187 testSemaphoreFile(a
, SEMAPHORE_SHOULD_NOT_EXIST
);