private Collection<MethodDescriptor> extractMethods( Class clazz, JavaTypeDescriptor declaringType, JandexPivot jandexPivot) { final Method[] declaredMethods = clazz.getDeclaredMethods(); final Method[] methods = clazz.getMethods(); if (declaredMethods.length <= 0 && methods.length <= 0) { return Collections.emptyList(); } final List<MethodDescriptor> methodDescriptors = CollectionHelper.arrayList(methods.length); for (Method method : declaredMethods) { methodDescriptors.add(fromMethod(method, declaringType, jandexPivot)); } for (Method method : methods) { if (clazz.equals(method.getDeclaringClass())) { continue; } final JavaTypeDescriptor methodDeclarer = getType(buildName(method.getDeclaringClass().getName())); methodDescriptors.add(fromMethod(method, methodDeclarer, jandexPivot)); } return methodDescriptors; }
private List<JavaTypeDescriptor> extractTypeParameters(ResolvedType resolvedType) { if (resolvedType.getTypeParameters().isEmpty()) { return Collections.emptyList(); } final List<JavaTypeDescriptor> result = CollectionHelper.arrayList(resolvedType.getTypeParameters().size()); for (ResolvedType typeParameter : resolvedType.getTypeParameters()) { result.add(getType(buildName(typeParameter.getErasedSignature()))); } return result; }
private Collection<InterfaceDescriptor> extractInterfaces(Class clazz) { final Class[] interfaces = clazz.getInterfaces(); if (interfaces == null || interfaces.length <= 0) { return Collections.emptyList(); } final Collection<InterfaceDescriptor> interfaceTypes = CollectionHelper.arrayList(interfaces.length); for (Class anInterface : interfaces) { interfaceTypes.add((InterfaceDescriptor) getType(buildName(anInterface.getName()))); } return interfaceTypes; }
private MethodDescriptor fromMethod( Method method, JavaTypeDescriptor declaringType, JandexPivot jandexPivot) { final Class[] parameterTypes = method.getParameterTypes(); final Collection<JavaTypeDescriptor> argumentTypes; if (parameterTypes.length == 0) { argumentTypes = Collections.emptyList(); } else { argumentTypes = CollectionHelper.arrayList(parameterTypes.length); for (Class parameterType : parameterTypes) { argumentTypes.add(toTypeDescriptor(parameterType)); } } return new MethodDescriptorImpl( method.getName(), declaringType, method.getModifiers(), toTypeDescriptor(method.getReturnType()), argumentTypes, jandexPivot.methodAnnotations.get(buildMethodAnnotationsKey(method))); }
private Collection<FieldDescriptor> extractFields( Class clazz, JavaTypeDescriptor declaringType, JandexPivot jandexPivot) { final Field[] declaredFields = clazz.getDeclaredFields(); final Field[] fields = clazz.getFields(); if (declaredFields.length <= 0 && fields.length <= 0) { return Collections.emptyList(); } final List<FieldDescriptor> fieldDescriptors = CollectionHelper.arrayList(fields.length); for (Field field : declaredFields) { fieldDescriptors.add( new FieldDescriptorImpl( field.getName(), toTypeDescriptor(field.getType()), field.getModifiers(), declaringType, jandexPivot.fieldAnnotations.get(field.getName()))); } for (Field field : fields) { if (clazz.equals(field.getDeclaringClass())) { continue; } final JavaTypeDescriptor fieldDeclarer = getType(buildName(field.getDeclaringClass().getName())); fieldDescriptors.add( new FieldDescriptorImpl( field.getName(), toTypeDescriptor(field.getType()), field.getModifiers(), fieldDeclarer, jandexPivot.fieldAnnotations.get(field.getName()))); } return fieldDescriptors; }
private ArrayList<MetadataSourceType> resolveInitialSourceProcessOrdering( ConfigurationService configService) { final ArrayList<MetadataSourceType> initialSelections = new ArrayList<MetadataSourceType>(); final String sourceProcessOrderingSetting = configService.getSetting( AvailableSettings.ARTIFACT_PROCESSING_ORDER, StandardConverters.STRING); if (sourceProcessOrderingSetting != null) { final String[] orderChoices = StringHelper.split(",; ", sourceProcessOrderingSetting, false); initialSelections.addAll( CollectionHelper.<MetadataSourceType>arrayList(orderChoices.length)); for (String orderChoice : orderChoices) { initialSelections.add(MetadataSourceType.parsePrecedence(orderChoice)); } } if (initialSelections.isEmpty()) { initialSelections.add(MetadataSourceType.HBM); initialSelections.add(MetadataSourceType.CLASS); } return initialSelections; }
private void prepare() { // assume 20 services for initial sizing this.serviceBindingMap = CollectionHelper.concurrentMap(20); this.serviceList = CollectionHelper.arrayList(20); }
/** @author Steve Ebersole */ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImplementor, ServiceBinding.ServiceLifecycleOwner { private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, AbstractServiceRegistryImpl.class.getName()); private final ServiceRegistryImplementor parent; private final ConcurrentHashMap<Class, ServiceBinding> serviceBindingMap = CollectionHelper.concurrentMap(20); // IMPL NOTE : the list used for ordered destruction. Cannot used map above because we need to // iterate it in reverse order which is only available through ListIterator // assume 20 services for initial sizing private final List<ServiceBinding> serviceBindingList = CollectionHelper.arrayList(20); @SuppressWarnings({"UnusedDeclaration"}) protected AbstractServiceRegistryImpl() { this((ServiceRegistryImplementor) null); } protected AbstractServiceRegistryImpl(ServiceRegistryImplementor parent) { this.parent = parent; } public AbstractServiceRegistryImpl(BootstrapServiceRegistry bootstrapServiceRegistry) { if (!ServiceRegistryImplementor.class.isInstance(bootstrapServiceRegistry)) { throw new IllegalArgumentException("Boot-strap registry was not "); } this.parent = (ServiceRegistryImplementor) bootstrapServiceRegistry; } @SuppressWarnings({"unchecked"}) protected <R extends Service> void createServiceBinding(ServiceInitiator<R> initiator) { serviceBindingMap.put(initiator.getServiceInitiated(), new ServiceBinding(this, initiator)); } protected <R extends Service> void createServiceBinding(ProvidedService<R> providedService) { ServiceBinding<R> binding = locateServiceBinding(providedService.getServiceRole(), false); if (binding == null) { binding = new ServiceBinding<R>( this, providedService.getServiceRole(), providedService.getService()); serviceBindingMap.put(providedService.getServiceRole(), binding); } registerService(binding, providedService.getService()); } @Override @SuppressWarnings({"unchecked"}) public ServiceRegistry getParentServiceRegistry() { return parent; } @Override @SuppressWarnings({"unchecked"}) public <R extends Service> ServiceBinding<R> locateServiceBinding(Class<R> serviceRole) { return locateServiceBinding(serviceRole, true); } @SuppressWarnings({"unchecked"}) protected <R extends Service> ServiceBinding<R> locateServiceBinding( Class<R> serviceRole, boolean checkParent) { ServiceBinding<R> serviceBinding = serviceBindingMap.get(serviceRole); if (serviceBinding == null && checkParent && parent != null) { // look in parent serviceBinding = parent.locateServiceBinding(serviceRole); } return serviceBinding; } @Override public <R extends Service> R getService(Class<R> serviceRole) { final ServiceBinding<R> serviceBinding = locateServiceBinding(serviceRole); if (serviceBinding == null) { throw new UnknownServiceException(serviceRole); } R service = serviceBinding.getService(); if (service == null) { service = initializeService(serviceBinding); } return service; } protected <R extends Service> void registerService(ServiceBinding<R> serviceBinding, R service) { serviceBinding.setService(service); synchronized (serviceBindingList) { serviceBindingList.add(serviceBinding); } } private <R extends Service> R initializeService(ServiceBinding<R> serviceBinding) { if (LOG.isTraceEnabled()) { LOG.tracev("Initializing service [role={0}]", serviceBinding.getServiceRole().getName()); } // PHASE 1 : create service R service = createService(serviceBinding); if (service == null) { return null; } // PHASE 2 : inject service (***potentially recursive***) serviceBinding.getLifecycleOwner().injectDependencies(serviceBinding); // PHASE 3 : configure service serviceBinding.getLifecycleOwner().configureService(serviceBinding); // PHASE 4 : Start service serviceBinding.getLifecycleOwner().startService(serviceBinding); startService(serviceBinding); return service; } @SuppressWarnings({"unchecked"}) protected <R extends Service> R createService(ServiceBinding<R> serviceBinding) { final ServiceInitiator<R> serviceInitiator = serviceBinding.getServiceInitiator(); if (serviceInitiator == null) { // this condition should never ever occur throw new UnknownServiceException(serviceBinding.getServiceRole()); } try { R service = serviceBinding.getLifecycleOwner().initiateService(serviceInitiator); // IMPL NOTE : the register call here is important to avoid potential stack overflow issues // from recursive calls through #configureService registerService(serviceBinding, service); return service; } catch (ServiceException e) { throw e; } catch (Exception e) { throw new ServiceException( "Unable to create requested service [" + serviceBinding.getServiceRole().getName() + "]", e); } } @Override public <R extends Service> void injectDependencies(ServiceBinding<R> serviceBinding) { final R service = serviceBinding.getService(); applyInjections(service); if (ServiceRegistryAwareService.class.isInstance(service)) { ((ServiceRegistryAwareService) service).injectServices(this); } } private <R extends Service> void applyInjections(R service) { try { for (Method method : service.getClass().getMethods()) { InjectService injectService = method.getAnnotation(InjectService.class); if (injectService == null) { continue; } processInjection(service, method, injectService); } } catch (NullPointerException e) { LOG.error("NPE injecting service deps : " + service.getClass().getName()); } } @SuppressWarnings({"unchecked"}) private <T extends Service> void processInjection( T service, Method injectionMethod, InjectService injectService) { if (injectionMethod.getParameterTypes() == null || injectionMethod.getParameterTypes().length != 1) { throw new ServiceDependencyException( "Encountered @InjectService on method with unexpected number of parameters"); } Class dependentServiceRole = injectService.serviceRole(); if (dependentServiceRole == null || dependentServiceRole.equals(Void.class)) { dependentServiceRole = injectionMethod.getParameterTypes()[0]; } // todo : because of the use of proxies, this is no longer returning null here... final Service dependantService = getService(dependentServiceRole); if (dependantService == null) { if (injectService.required()) { throw new ServiceDependencyException( "Dependency [" + dependentServiceRole + "] declared by service [" + service + "] not found"); } } else { try { injectionMethod.invoke(service, dependantService); } catch (Exception e) { throw new ServiceDependencyException("Cannot inject dependency service", e); } } } @Override @SuppressWarnings({"unchecked"}) public <R extends Service> void startService(ServiceBinding<R> serviceBinding) { if (Startable.class.isInstance(serviceBinding.getService())) { ((Startable) serviceBinding.getService()).start(); } if (Manageable.class.isInstance(serviceBinding.getService())) { getService(JmxService.class) .registerService( (Manageable) serviceBinding.getService(), serviceBinding.getServiceRole()); } } @Override @SuppressWarnings({"unchecked"}) public void destroy() { synchronized (serviceBindingList) { ListIterator<ServiceBinding> serviceBindingsIterator = serviceBindingList.listIterator(serviceBindingList.size()); while (serviceBindingsIterator.hasPrevious()) { final ServiceBinding serviceBinding = serviceBindingsIterator.previous(); serviceBinding.getLifecycleOwner().stopService(serviceBinding); } serviceBindingList.clear(); } serviceBindingMap.clear(); } @Override public <R extends Service> void stopService(ServiceBinding<R> binding) { final Service service = binding.getService(); if (Stoppable.class.isInstance(service)) { try { ((Stoppable) service).stop(); } catch (Exception e) { LOG.unableToStopService(service.getClass(), e.toString()); } } } }