Skip to content

Commit

Permalink
added a little file lock so you can start up many JVMs on the same co…
Browse files Browse the repository at this point in the history
…mputer without having clashes over the maven indexer folder
  • Loading branch information
jstrachan committed Apr 18, 2013
1 parent 1184a95 commit 0a0a2e8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
62 changes: 62 additions & 0 deletions hawtio-core/src/main/java/io/hawt/util/FileLocker.java
@@ -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();
}
}
}
@@ -1,6 +1,7 @@
package io.hawt.maven.indexer;

import io.hawt.config.ConfigFacade;
import io.hawt.util.FileLocker;
import io.hawt.util.Strings;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
Expand Down Expand Up @@ -71,13 +72,17 @@ public class MavenIndexerFacade implements MavenIndexerFacadeMXBean {
private boolean updateIndexOnStartup = true;
private ObjectName objectName;
private MBeanServer mBeanServer;
private int maximumIndexersPerMachine = 1000;
private String[] repositories = {
"http://repo.fusesource.com/nexus/content/repositories/releases@id=fusesource.release.repo",
"http://repo1.maven.org/maven2@central"
};
private String cacheDirName;
private File cacheDirectory;
private Map<String, IndexingContext> indexContexts = new HashMap<String, IndexingContext>();
private FileLocker fileLock;
private String lockFileName = "hawtio.lock";


public MavenIndexerFacade() throws PlexusContainerException, ComponentLookupException {
this.plexusContainer = new DefaultPlexusContainer();
Expand Down Expand Up @@ -178,6 +183,9 @@ public void transferCompleted(TransferEvent transferEvent) {
}

public void destroy() throws IOException, MBeanRegistrationException, InstanceNotFoundException {
if (fileLock != null) {
fileLock.destroy();
}
if (objectName != null && mBeanServer != null) {
mBeanServer.unregisterMBean(objectName);
}
Expand Down Expand Up @@ -230,6 +238,18 @@ public void setRepositories(String[] repositories) {
this.repositories = repositories;
}

public int getMaximumIndexersPerMachine() {
return maximumIndexersPerMachine;
}

/**
* If we do not specify the directory to use for caches, then create a directory per process
* up to this maximum number
*/
public void setMaximumIndexersPerMachine(int maximumIndexersPerMachine) {
this.maximumIndexersPerMachine = maximumIndexersPerMachine;
}

public String getCacheDirName() {
return cacheDirName;
}
Expand All @@ -244,13 +264,28 @@ public File getCacheDirectory() {
if (Strings.isNotBlank(name)) {
cacheDirectory = new File(name);
} else {
File dir = new File(".");
name = "mavenIndex";
ConfigFacade configFacade = ConfigFacade.getSingleton();
if (configFacade != null) {
cacheDirectory = new File(configFacade.getConfigDirectory(), "mavenIndex");
dir = configFacade.getConfigDirectory();
}

String postfix = "";
for (int i = 2; i < maximumIndexersPerMachine; i++) {
File tryDir = new File(dir, name + postfix);
fileLock = FileLocker.getLock(new File(tryDir, lockFileName));
if (fileLock != null) {
cacheDirectory = tryDir;
break;
}
postfix = "-" + i;
}
if (cacheDirectory == null) {
LOG.warn("Could not find a directory inside of " + dir.getAbsolutePath()
+ " which did not have a lock file " + lockFileName
+ " so giving up after " + maximumIndexersPerMachine + " attempt(s).");
}
}
if (cacheDirectory == null) {
cacheDirectory = new File("mavenIndex");
}
}
return cacheDirectory;
Expand Down

0 comments on commit 0a0a2e8

Please sign in to comment.