Update ooo320-m1
[ooovba.git] / qadevOOo / runner / convwatch / SimpleFileSemaphore.java
blobbc0b72761f2ff268a42c581402ba35f2d7d38970
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SimpleFileSemaphore.java,v $
10 * $Revision: 1.3.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package convwatch;
33 import java.io.File;
34 import java.io.RandomAccessFile;
35 import helper.OSHelper;
37 public class SimpleFileSemaphore /* extends *//* implements */
39 String m_sInternSemaphoreFile;
40 File m_aInternSemaphoreFile;
41 GlobalLogWriter m_aLog;
43 public static void sleep( int _nSeconds)
45 // wait a second here
46 try
48 java.lang.Thread.sleep(_nSeconds * 1000);
50 catch (java.lang.InterruptedException e2)
55 public SimpleFileSemaphore() throws IllegalArgumentException
57 String sInternFileName;
58 if (OSHelper.isWindows())
60 sInternFileName = "C:/Temp/ConvwatchOOoSemaphore.txt";
62 else if (OSHelper.isUnix())
64 sInternFileName = "/tmp/ConvwatchOOoSemaphore.txt";
66 else
68 m_sInternSemaphoreFile = null;
69 throw new IllegalArgumentException("Unknown System, can't initialise SimpleFileSemaphore");
72 m_sInternSemaphoreFile = sInternFileName;
73 m_aInternSemaphoreFile = new File(sInternFileName);
76 public File getSemaphoreFile()
78 return m_aInternSemaphoreFile;
80 // ------------------------------------------------------------------------------
81 // wait until resource is available
82 public void P(File _aSemaphore)
84 int nCount = 0;
85 int nCheckLoop = 1;
87 while ( nCheckLoop == 1)
89 // check if resource is available, if not, wait.
90 if ( _aSemaphore.exists() )
92 m_aLog.get().println( "Active wait since " + nCount + "sec..");
93 nCount ++;
94 sleep( 1 );
96 else
98 sleep( 1 );
99 if ( _aSemaphore.exists() )
101 // ups
102 m_aLog.get().println( "ups...");
104 else
106 nCheckLoop = 0;
111 // block resource by ourself
114 RandomAccessFile aWriter = new RandomAccessFile(_aSemaphore, "rw");
115 aWriter.writeByte((int)1);
116 aWriter.close();
119 catch (java.io.FileNotFoundException fne)
121 m_aLog.get().println( "caught: FileNotFoundException");
123 catch(java.io.IOException ie)
125 m_aLog.get().println( "caught: IOException");
129 // ------------------------------------------------------------------------------
130 // block a resource
131 public void V(File _aSemaphore)
134 if ( _aSemaphore.exists() )
136 _aSemaphore.delete();
138 else
140 m_aLog.get().println("Could be a problem here? No resource block found.");
144 // --------------------------------- Unit test ---------------------------------
146 private static boolean SEMAPHORE_SHOULD_EXIST = true;
147 private static boolean SEMAPHORE_SHOULD_NOT_EXIST = false;
149 private static void assure(boolean _b, String _sText)
151 System.out.print(_sText);
152 System.out.print(" ");
153 if (_b)
155 System.out.println("ok");
157 else
159 System.out.println("FAILED");
163 private static void testSemaphoreFile(SimpleFileSemaphore a, boolean _bShouldFileExists)
165 System.out.println("Check if semaphore file exists.");
166 File aSemaphoreFile = a.getSemaphoreFile();
167 if (aSemaphoreFile.exists())
169 System.out.println("Name is: " + aSemaphoreFile.getAbsolutePath());
170 assure(_bShouldFileExists == SEMAPHORE_SHOULD_EXIST, "Semaphore should exist!");
172 else
174 assure(_bShouldFileExists == SEMAPHORE_SHOULD_NOT_EXIST, "Semaphore should not exist!");
178 public static void main( String[] argv )
180 SimpleFileSemaphore a = new SimpleFileSemaphore();
182 testSemaphoreFile(a, SEMAPHORE_SHOULD_NOT_EXIST);
184 a.P(a.getSemaphoreFile());
186 testSemaphoreFile(a, SEMAPHORE_SHOULD_EXIST);
188 a.V(a.getSemaphoreFile());
190 testSemaphoreFile(a, SEMAPHORE_SHOULD_NOT_EXIST);