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