com.github.bpark.cdi.LockSupplierStorage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdi-locking-api Show documentation
Show all versions of cdi-locking-api Show documentation
CDI Locking Interceptor implementation to use EJB3 like locking
The newest version!
/*
* 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 com.github.bpark.cdi;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.interceptor.InvocationContext;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.github.bpark.cdi.LockType.READ;
@ApplicationScoped
@Typed(LockSupplierStorage.class)
public class LockSupplierStorage implements LockFactory {
private final ConcurrentMap locks = new ConcurrentHashMap<>();
// read or write
private final ConcurrentMap lockSuppliers = new ConcurrentHashMap<>();
@Inject
private BeanManager beanManager;
protected LockSupplier getLockSupplier(final InvocationContext ic) {
final Method key = ic.getMethod();
LockSupplier operation = lockSuppliers.get(key);
if (operation == null) {
final Class declaringClass = key.getDeclaringClass();
final AnnotatedType