/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ejb3.singleton.aop.impl.concurrency.bridge;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import javax.ejb.Lock;
import javax.ejb.LockType;
import org.jboss.ejb3.metadata.MetaDataBridge;
import org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData;
import org.jboss.metadata.ejb.jboss.JBossSessionBean31MetaData;
import org.jboss.metadata.ejb.spec.ConcurrentMethodMetaData;
import org.jboss.metadata.ejb.spec.MethodParametersMetaData;
import org.jboss.metadata.ejb.spec.NamedMethodMetaData;
import org.jboss.metadata.spi.signature.DeclaredMethodSignature;
/**
* An implementation of {@link MetaDataBridge} which is responsible for
* resolving the {@link Lock} annotation from EJB metadata
*
* @author Jaikiran Pai
* @version $Revision: $
*/
public class LockMetaDataBridge implements MetaDataBridge<JBossEnterpriseBeanMetaData>
{
/**
* @see org.jboss.ejb3.metadata.MetaDataBridge#retrieveAnnotation(java.lang.Class, java.lang.Object, java.lang.ClassLoader)
*/
@Override
public <A extends Annotation> A retrieveAnnotation(Class<A> annotationClass, JBossEnterpriseBeanMetaData metaData,
ClassLoader classLoader)
{
if (annotationClass == null || annotationClass.equals(Lock.class) == false)
{
return null;
}
// only session beans and that too of type JBossSessionBean31MetaData
if (metaData.isSession() == false || (metaData instanceof JBossSessionBean31MetaData) == false)
{
return null;
}
JBossSessionBean31MetaData sessionBean = (JBossSessionBean31MetaData) metaData;
// get the bean level lock type
LockType lockType = sessionBean.getLockType();
// if no bean level lock type specified, then just return null
if (lockType == null)
{
return null;
}
Lock lock = new LockImpl(lockType);
return annotationClass.cast(lock);
}
/**
* @see org.jboss.ejb3.metadata.MetaDataBridge#retrieveAnnotation(java.lang.Class, java.lang.Object, java.lang.ClassLoader, org.jboss.metadata.spi.signature.DeclaredMethodSignature)
*/
@Override
public <A extends Annotation> A retrieveAnnotation(Class<A> annotationClass, JBossEnterpriseBeanMetaData metaData,
ClassLoader classLoader, DeclaredMethodSignature method)
{
if (annotationClass == null || annotationClass.equals(Lock.class) == false)
{
return null;
}
// only session beans and that too of type JBossSessionBean31MetaData
if (metaData.isSession() == false || (metaData instanceof JBossSessionBean31MetaData) == false)
{
return null;
}
JBossSessionBean31MetaData sessionBean = (JBossSessionBean31MetaData) metaData;
// create a named method metadata to represent the method being queried
NamedMethodMetaData namedMethod = new NamedMethodMetaData();
namedMethod.setName(method.getName());
if (method.getParameters() != null)
{
MethodParametersMetaData methodParams = new MethodParametersMetaData();
methodParams.addAll(Arrays.asList(method.getParameters()));
// set the method params on the named method metadata
namedMethod.setMethodParams(methodParams);
}
// get the concurrency method metadata for this named method
ConcurrentMethodMetaData concurrentMethodMetaData = sessionBean.getConcurrentMethods().get(namedMethod);
LockType lockType = null;
// if this named method did not have concurrency metadata or lock metadata, then
// check for the method named "*" and see if that has the lock type set
if (concurrentMethodMetaData == null || concurrentMethodMetaData.getLockType() == null)
{
// get lock type for method "*"
lockType = getLockTypeApplicableForAllMethods(sessionBean);
}
// lock type was not specified for this method nor for the
// method "*"
if (lockType == null)
{
return null;
}
Lock lock = new LockImpl(lockType);
return annotationClass.cast(lock);
}
/**
* Returns the {@link LockType} specified for the method "*". Returns null
* if there is no lock type specified.
*
* @param sessionBean Session bean metadata
* @return
*/
private LockType getLockTypeApplicableForAllMethods(JBossSessionBean31MetaData sessionBean)
{
NamedMethodMetaData allMethods = new NamedMethodMetaData();
allMethods.setName("*");
ConcurrentMethodMetaData concurrentMethod = sessionBean.getConcurrentMethods().get(allMethods);
if (concurrentMethod == null)
{
return null;
}
return concurrentMethod.getLockType();
}
/**
*
* Implementation of {@link Lock} annotation
*
* @author Jaikiran Pai
* @version $Revision: $
*/
private class LockImpl implements Lock
{
private LockType lockType;
/**
* Create a {@link LockImpl} from a {@link LockType}
*
* @param lockType
*/
public LockImpl(LockType lockType)
{
this.lockType = lockType;
}
/**
* @see javax.ejb.Lock#value()
*/
@Override
public LockType value()
{
return this.lockType;
}
/**
* @see java.lang.annotation.Annotation#annotationType()
*/
@Override
public Class<? extends Annotation> annotationType()
{
return Lock.class;
}
}
}