Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a little file lock so you can start up many JVMs on the same co…
…mputer without having clashes over the maven indexer folder
- Loading branch information
Showing
2 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.hawt.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* A simple API to a file lock | ||
*/ | ||
public class FileLocker { | ||
private final File lockFile; | ||
|
||
/** | ||
* Attempts to grab the lock for the given file, returning a FileLock if | ||
* the lock has been created; otherwise it returns null | ||
*/ | ||
public static FileLocker getLock(File lockFile) { | ||
lockFile.getParentFile().mkdirs(); | ||
if (!lockFile.exists()) { | ||
try { | ||
IOHelper.write(lockFile, "I have the lock!"); | ||
lockFile.deleteOnExit(); | ||
return new FileLocker(lockFile); | ||
} catch (IOException e) { | ||
// Ignore | ||
} | ||
} | ||
return null; | ||
|
||
} | ||
|
||
public FileLocker(File lockFile) { | ||
this.lockFile = lockFile; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "FileLock(" + lockFile + ")"; | ||
} | ||
|
||
public void destroy() { | ||
if (lockFile.exists()) { | ||
lockFile.delete(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters