2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 import java
.io
.RandomAccessFile
;
23 import helper
.OSHelper
;
25 public class SimpleFileSemaphore
/* extends *//* implements */
27 String m_sInternSemaphoreFile
;
28 File m_aInternSemaphoreFile
;
29 GlobalLogWriter m_aLog
;
31 public static void sleep( int _nSeconds
)
36 java
.lang
.Thread
.sleep(_nSeconds
* 1000);
38 catch (java
.lang
.InterruptedException e2
)
43 public SimpleFileSemaphore() throws IllegalArgumentException
45 String sInternFileName
;
46 if (OSHelper
.isWindows())
48 sInternFileName
= "C:/Temp/ConvwatchOOoSemaphore.txt";
50 else if (OSHelper
.isUnix())
52 sInternFileName
= "/tmp/ConvwatchOOoSemaphore.txt";
56 m_sInternSemaphoreFile
= null;
57 throw new IllegalArgumentException("Unknown System, can't initialise SimpleFileSemaphore");
60 m_sInternSemaphoreFile
= sInternFileName
;
61 m_aInternSemaphoreFile
= new File(sInternFileName
);
64 public File
getSemaphoreFile()
66 return m_aInternSemaphoreFile
;
68 // ------------------------------------------------------------------------------
69 // wait until resource is available
70 public void P(File _aSemaphore
)
75 while ( nCheckLoop
== 1)
77 // check if resource is available, if not, wait.
78 if ( _aSemaphore
.exists() )
80 GlobalLogWriter
.get().println( "Active wait since " + nCount
+ "sec..");
87 if ( _aSemaphore
.exists() )
90 GlobalLogWriter
.get().println( "ups...");
99 // block resource by ourself
102 RandomAccessFile aWriter
= new RandomAccessFile(_aSemaphore
, "rw");
103 aWriter
.writeByte(1);
107 catch (java
.io
.FileNotFoundException fne
)
109 GlobalLogWriter
.get().println( "caught: FileNotFoundException");
111 catch(java
.io
.IOException ie
)
113 GlobalLogWriter
.get().println( "caught: IOException");
117 // ------------------------------------------------------------------------------
119 public void V(File _aSemaphore
)
122 if ( _aSemaphore
.exists() )
124 _aSemaphore
.delete();
128 GlobalLogWriter
.get().println("Could be a problem here? No resource block found.");
132 // --------------------------------- Unit test ---------------------------------
134 private static boolean SEMAPHORE_SHOULD_EXIST
= true;
135 private static boolean SEMAPHORE_SHOULD_NOT_EXIST
= false;
137 private static void assure(boolean _b
, String _sText
)
139 System
.out
.print(_sText
);
140 System
.out
.print(" ");
143 System
.out
.println("ok");
147 System
.out
.println("FAILED");
151 private static void testSemaphoreFile(SimpleFileSemaphore a
, boolean _bShouldFileExists
)
153 System
.out
.println("Check if semaphore file exists.");
154 File aSemaphoreFile
= a
.getSemaphoreFile();
155 if (aSemaphoreFile
.exists())
157 System
.out
.println("Name is: " + aSemaphoreFile
.getAbsolutePath());
158 assure(_bShouldFileExists
== SEMAPHORE_SHOULD_EXIST
, "Semaphore should exist!");
162 assure(_bShouldFileExists
== SEMAPHORE_SHOULD_NOT_EXIST
, "Semaphore should not exist!");
166 public static void main( String
[] argv
)
168 SimpleFileSemaphore a
= new SimpleFileSemaphore();
170 testSemaphoreFile(a
, SEMAPHORE_SHOULD_NOT_EXIST
);
172 a
.P(a
.getSemaphoreFile());
174 testSemaphoreFile(a
, SEMAPHORE_SHOULD_EXIST
);
176 a
.V(a
.getSemaphoreFile());
178 testSemaphoreFile(a
, SEMAPHORE_SHOULD_NOT_EXIST
);