001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 * 
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 * 
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.oozie.service;
019
020import org.apache.oozie.util.Instrumentable;
021import org.apache.oozie.util.Instrumentation;
022import org.apache.oozie.util.MemoryLocks;
023
024public class MemoryLocksService implements Service, Instrumentable {
025    private static final String INSTRUMENTATION_GROUP = "locks";
026    private MemoryLocks locks;
027
028    /**
029     * Initialize the memory locks service
030     *
031     * @param services services instance.
032     */
033    @Override
034    public void init(Services services) {
035        locks = new MemoryLocks();
036    }
037
038    /**
039     * Destroy the memory locks service.
040     */
041    @Override
042    public void destroy() {
043        locks = null;
044    }
045
046    /**
047     * Return the public interface for the memory locks services
048     *
049     * @return {@link MemoryLocksService}.
050     */
051    @Override
052    public Class<? extends Service> getInterface() {
053        return MemoryLocksService.class;
054    }
055
056    /**
057     * Instruments the memory locks service.
058     *
059     * @param instr instance to instrument the memory locks service to.
060     */
061    public void instrument(Instrumentation instr) {
062        final MemoryLocks finalLocks = this.locks;
063        instr.addVariable(INSTRUMENTATION_GROUP, "locks", new Instrumentation.Variable<Long>() {
064            public Long getValue() {
065                return (long) finalLocks.size();
066            }
067        });
068    }
069
070    /**
071     * Obtain a READ lock for a source.
072     *
073     * @param resource resource name.
074     * @param wait time out in milliseconds to wait for the lock, -1 means no timeout and 0 no wait.
075     * @return the lock token for the resource, or <code>null</code> if the lock could not be obtained.
076     * @throws InterruptedException thrown if the thread was interrupted while waiting.
077     */
078    public MemoryLocks.LockToken getReadLock(String resource, long wait) throws InterruptedException {
079        return locks.getReadLock(resource, wait);
080    }
081
082    /**
083     * Obtain a WRITE lock for a source.
084     *
085     * @param resource resource name.
086     * @param wait time out in milliseconds to wait for the lock, -1 means no timeout and 0 no wait.
087     * @return the lock token for the resource, or <code>null</code> if the lock could not be obtained.
088     * @throws InterruptedException thrown if the thread was interrupted while waiting.
089     */
090    public MemoryLocks.LockToken getWriteLock(String resource, long wait) throws InterruptedException {
091        return locks.getWriteLock(resource, wait);
092    }
093
094}