org.jboss.weld.ejb.EjbDescriptors Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.ejb;
import static org.jboss.weld.util.reflection.Reflections.cast;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.util.collections.SetMultimap;
/**
* EJB descriptors by EJB implementation class or name
*
* @author Pete Muir
*/
public class EjbDescriptors implements Service, Iterable> {
// EJB name -> EJB descriptors map
private final Map> ejbByName;
private final SetMultimap, String> ejbByClass;
public static final EjbDescriptors EMPTY = new EjbDescriptors();
/**
* Constructor
*/
public EjbDescriptors() {
this.ejbByName = new HashMap>();
this.ejbByClass = SetMultimap.newSetMultimap();
}
/**
* Gets an iterator to the EJB descriptors for an EJB implementation class
*
* @param beanClass The EJB class
* @return An iterator
*/
public InternalEjbDescriptor get(String beanName) {
return cast(ejbByName.get(beanName));
}
/**
* Adds an EJB descriptor to the maps
*
* @param ejbDescriptor The EJB descriptor to add
*/
public void add(EjbDescriptor ejbDescriptor) {
InternalEjbDescriptor internalEjbDescriptor = InternalEjbDescriptor.of(ejbDescriptor);
ejbByName.put(ejbDescriptor.getEjbName(), internalEjbDescriptor);
ejbByClass.put(ejbDescriptor.getBeanClass(), internalEjbDescriptor.getEjbName());
}
/**
* Indicates if there are EJB descriptors available for an EJB implementation
* class
*
* @param beanClass The class to match
* @return True if present, otherwise false
*/
public boolean contains(String beanName) {
return ejbByName.containsKey(beanName);
}
/**
* Indicates if there are EJB descriptors available for an EJB implementation
* class
*
* @param beanClass The class to match
* @return True if present, otherwise false
*/
public boolean contains(Class> beanClass) {
return ejbByClass.containsKey(beanClass);
}
public InternalEjbDescriptor getUnique(Class beanClass) {
Set ejbs = ejbByClass.get(beanClass);
if (ejbs.size() > 1) {
throw BeanLogger.LOG.tooManyEjbsForClass(beanClass, ejbs);
} else if (ejbs.size() == 0) {
return null;
} else {
return get(ejbs.iterator().next());
}
}
/**
* Adds all EJB descriptors to the maps
*
* @param ejbDescriptors The descriptors to add
*/
public void addAll(Iterable> ejbDescriptors) {
for (EjbDescriptor> ejbDescriptor : ejbDescriptors) {
add(ejbDescriptor);
}
}
/**
* Clears both maps
*/
public void clear() {
ejbByName.clear();
}
public Iterator> iterator() {
return ejbByName.values().iterator();
}
public void cleanup() {
this.ejbByClass.clear();
this.ejbByName.clear();
}
}