/*
 * 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 org.apache.servicemix.kernel.main;

import java.io.RandomAccessFile;
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.util.Properties;

public class SimpleFileLock implements Lock {

    private RandomAccessFile lockFile;
    private FileLock lock;

    public SimpleFileLock(Properties props) {
        try {
            File base = new File(props.getProperty("servicemix.base"));
            lockFile = new RandomAccessFile(new File(base, "lock"), "rw");
        } catch (IOException e) {
            throw new RuntimeException("Could not create file lock", e);
        }
    }

    public boolean lock() throws Exception {
        if (lock == null) {
            lock = lockFile.getChannel().tryLock();
        }
        return lock != null;
    }

    public void release() throws Exception {
        if (lock != null && lock.isValid()) {
            lock.release();
            lock.channel().close();
        }
        lock = null;
    }
}

@@ -1055,163 +1055,472 @@
 ate 
-RandomAccessFile lockFile;%0A    private FileLock lock;%0A%0A    public SimpleFileLock(Properties props) %7B%0A        try %7B%0A            File base = new File(
+static final String PROPERTY_LOCK_DIR = %22servicemix.lock.dir%22;%0A    private static final String PROP_SERVICEMIX_BASE = %22servicemix.base%22;%0A    private RandomAccessFile lockFile;%0A    private FileLock lock;%0A%0A    public SimpleFileLock(Properties props) %7B%0A        try %7B%0A            String lock = props.getProperty(PROPERTY_LOCK_DIR);%0A%0A            if (lock != null) %7B%0A                File servicemixLock = getServiceMixLock(new File(lock), props);%0A                
 props.
-g
+s
 etPr
@@ -1526,33 +1526,262 @@
 roperty(
-%22servicemix.base%22
+PROPERTY_LOCK_DIR, servicemixLock.getPath());%0A            %7D else %7B%0A                props.setProperty(PROPERTY_LOCK_DIR, System.getProperty(PROP_SERVICEMIX_BASE));%0A            %7D%0A%0A            File base = new File(props.getProperty(PROPERTY_LOCK_DIR
 ));%0A    
@@ -2338,10 +2338,1225 @@
 ;%0A    %7D%0A
+%0A    private static File getServiceMixLock(File lock,Properties props) %7B%0A        File rc = null;%0A%0A        String path = lock.getPath();%0A        if (path != null) %7B%0A            rc = validateDirectoryExists(path, %22Invalid %22 + PROPERTY_LOCK_DIR + %22 system property%22);%0A        %7D%0A%0A        if (rc == null) %7B%0A            path = props.getProperty(PROP_SERVICEMIX_BASE);%0A            if (path != null) %7B%0A                rc = validateDirectoryExists(path, %22Invalid %22 + PROP_SERVICEMIX_BASE + %22 property%22);%0A            %7D%0A        %7D%0A%0A        if (rc == null) %7B%0A            rc = lock;%0A        %7D%0A        return rc;%0A    %7D%0A%0A    private static File validateDirectoryExists(String path, String errPrefix) %7B%0A        File rc;%0A        try %7B%0A            rc = new File(path).getCanonicalFile();%0A        %7D catch (IOException e) %7B%0A            throw new IllegalArgumentException(errPrefix + %22 '%22 + path + %22' : %22 + e.getMessage());%0A        %7D%0A        if (!rc.exists()) %7B%0A            throw new IllegalArgumentException(errPrefix + %22 '%22 + path + %22' : does not exist%22);%0A        %7D%0A        if (!rc.isDirectory()) %7B%0A            throw new IllegalArgumentException(errPrefix + %22 '%22 + path + %22' : is not a directory%22);%0A        %7D%0A        return rc;%0A    %7D%0A%0A
 %7D%0A

Support configurable lock directory for container level locking (patch submitted by Jamie Goodyear)