public void add(ServiceDefinition service) { if (!(service instanceof SpringService)) { throw new IllegalArgumentException("Service type must be " + SpringService.class.getName()); } super.add(service); }
/** * Merges the POJO component type into the web component type. * * @param webType the web component type to merge into * @param componentType the component type to merge * @param context the introspection context */ private void mergeComponentTypes(WebComponentType webType, InjectingComponentType componentType, IntrospectionContext context) { for (Map.Entry<String, ReferenceDefinition> entry : componentType.getReferences().entrySet()) { String name = entry.getKey(); AbstractReference reference = webType.getReferences().get(name); if (reference != null) { ServiceContract source = reference.getServiceContract(); ServiceContract target = entry.getValue().getServiceContract(); MatchResult result = matcher.isAssignableFrom(source, target, false); if (!result.isAssignable()) { // TODO display areas where it was not matching IncompatibleReferenceDefinitions failure = new IncompatibleReferenceDefinitions(name); context.addError(failure); } } else { webType.add(entry.getValue()); } } // apply all injection sites for (Map.Entry<InjectionSite, Injectable> entry : componentType.getInjectionSites().entrySet()) { webType.addMapping(componentType.getImplClass(), entry.getKey(), entry.getValue()); } }
private HibernateSessionResourceReference createSessionDefinition(String name, PersistenceContext annotation, InjectingComponentType componentType) { String unitName = annotation.unitName(); boolean multiThreaded = Scope.COMPOSITE.getScope().equals(componentType.getScope()); return new HibernateSessionResourceReference(name, unitName, factoryServiceContract, multiThreaded); }
public void visitField(PersistenceUnit annotation, Field field, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { FieldInjectionSite site = new FieldInjectionSite(field); PersistenceUnitResourceReference definition = createDefinition(annotation); componentType.add(definition, site); // record that the implementation requires JPA componentType.addRequiredCapability("jpa"); }
public void applyHeuristics(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { serviceHeuristic.applyHeuristics(componentType, implClass, context); if (componentType.getConstructor() == null) { Signature ctor = findConstructor(implClass, componentType, context); componentType.setConstructor(ctor); } if (componentType.getProperties().isEmpty() && componentType.getReferences().isEmpty() && componentType.getResourceReferences().isEmpty()) { evaluateConstructor(componentType, implClass, context); evaluateSetters(componentType, implClass, context); evaluateFields(componentType, implClass, context); } if (componentType.getInitMethod() == null) { Signature setUp = getCallback(implClass, "setUp"); if (setUp == null) { setUp = getCallback(implClass, Before.class); } componentType.setInitMethod(setUp); } if (componentType.getDestroyMethod() == null) { Signature tearDown = getCallback(implClass, "tearDown"); if (tearDown == null) { tearDown = getCallback(implClass, After.class); } componentType.setDestroyMethod(tearDown); } }
void evaluateSetters(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { Map<InjectionSite, Injectable> sites = componentType.getInjectionSites(); TypeMapping typeMapping = context.getTypeMapping(implClass); Set<Method> setters = helper.getInjectionMethods(implClass, componentType.getServices().values()); for (Method setter : setters) { InjectionSite site = new MethodInjectionSite(setter, 0); // skip sites that have already been mapped if (sites.containsKey(site)) { continue; } String name = helper.getSiteName(setter, null); Type parameterType = setter.getGenericParameterTypes()[0]; Annotation[] annotations = setter.getAnnotations(); processSite(componentType, typeMapping, name, setter, parameterType, site, annotations, context); } }
ServiceDefinition service = new ServiceDefinition(serviceName, contract); InjectingComponentType componentType = new InjectingComponentType(implClassName); componentType.add(service);
private void evaluateConstructor(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { Map<InjectionSite, Injectable> sites = componentType.getInjectionSites(); Constructor<?> constructor; try { if (componentType.getConstructor() == null) { // there was an error with the constructor previously, just return return; } constructor = componentType.getConstructor().getConstructor(implClass); } catch (ClassNotFoundException e) { throw new AssertionError(e); } catch (NoSuchMethodException e) { throw new AssertionError(e); } TypeMapping typeMapping = context.getTypeMapping(implClass); Type[] parameterTypes = constructor.getGenericParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { InjectionSite site = new ConstructorInjectionSite(constructor, i); // skip sites that have already been mapped if (sites.containsKey(site)) { continue; } Type parameterType = parameterTypes[i]; String name = helper.getSiteName(constructor, i, null); Annotation[] annotations = constructor.getParameterAnnotations()[i]; processSite(componentType, typeMapping, name, constructor, parameterType, site, annotations, context); } }
public PhysicalComponentDefinition generate(LogicalComponent<JUnitImplementation> component) throws GenerationException { ComponentDefinition<JUnitImplementation> definition = component.getDefinition(); JUnitImplementation implementation = definition.getImplementation(); InjectingComponentType type = implementation.getComponentType(); String scope = type.getScope(); ImplementationManagerDefinition managerDefinition = new ImplementationManagerDefinition(); managerDefinition.setComponentUri(component.getUri()); managerDefinition.setReinjectable(Scope.COMPOSITE.getScope().equals(scope)); managerDefinition.setConstructor(type.getConstructor()); managerDefinition.setInitMethod(type.getInitMethod()); managerDefinition.setDestroyMethod(type.getDestroyMethod()); managerDefinition.setImplementationClass(implementation.getImplementationClass()); helper.processInjectionSites(type, managerDefinition); JavaComponentDefinition physical = new JavaComponentDefinition(); physical.setScope(scope); physical.setManagerDefinition(managerDefinition); helper.processPropertyValues(component, physical); return physical; }
public InjectingComponentType introspect(String className, IntrospectionContext context) { InjectingComponentType componentType = new InjectingComponentType(className); componentType.setScope("STATELESS");
private <I> ScopedComponent createPhysicalComponent(LogicalComponent<?> logicalComponent, I instance) { URI uri = logicalComponent.getUri(); InjectingComponentType type = (InjectingComponentType) logicalComponent.getDefinition().getComponentType(); type.getInjectionSites(); SingletonComponent component = new SingletonComponent(uri, instance, type.getInjectionSites()); component.setClassLoaderId(BOOT_CONTRIBUTION); return component; }
public TimerImplementation load(XMLStreamReader reader, IntrospectionContext context) throws XMLStreamException { Location startLocation = reader.getLocation(); TimerImplementation implementation = new TimerImplementation(); validateAttributes(reader, context, implementation); if (!processImplementationClass(implementation, reader, context)) { // an error with the implementation class, return a dummy implementation InjectingComponentType type = new InjectingComponentType(null); implementation.setComponentType(type); return implementation; } TimerData data = new TimerData(); implementation.setTimerData(data); String poolName = reader.getAttributeValue(null, "poolName"); if (poolName != null) { data.setPoolName(poolName); } processInitialDelay(data, reader, startLocation, context); processTimeUnit(data, reader, startLocation, context); processIntervalClass(reader, context, implementation); processRepeatInterval(reader, startLocation, context, implementation); processRepeatFixedRate(reader, startLocation, context, implementation); processFireOnce(reader, startLocation, context, implementation); processIntervalMethod(context, implementation); validateData(startLocation, context, data); loaderHelper.loadPolicySetsAndIntents(implementation, reader, context); InjectingComponentType componentType = implementationProcessor.introspect(implementation.getImplementationClass(), context); implementation.setComponentType(componentType); LoaderUtil.skipToEndElement(reader); return implementation; }
private void processSite(InjectingComponentType componentType, TypeMapping typeMapping, String name, Member member, Type parameterType, InjectionSite site, Annotation[] annotations, IntrospectionContext context) { InjectableType type = helper.inferType(parameterType, typeMapping); switch (type) { case PROPERTY: addProperty(componentType, typeMapping, name, parameterType, site); break; case REFERENCE: addReference(componentType, typeMapping, name, parameterType, site, annotations, context); break; case CALLBACK: // ignore break; default: context.addError(new UnknownInjectionType(site, type, componentType.getImplClass(), member, componentType)); break; } }
public void visitMethod(PersistenceUnit annotation, Method method, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { MethodInjectionSite site = new MethodInjectionSite(method, 0); PersistenceUnitResourceReference definition = createDefinition(annotation); componentType.add(definition, site); // record that the implementation requires JPA componentType.addRequiredCapability("jpa"); }
public void processInjectionSites(InjectingComponentType componentType, ImplementationManagerDefinition managerDefinition) { Map<InjectionSite, Injectable> mappings = componentType.getInjectionSites(); // add injections for all the active constructor args Map<InjectionSite, Injectable> construction = managerDefinition.getConstruction(); Signature constructor = componentType.getConstructor(); Set<Injectable> byConstruction = new HashSet<Injectable>(constructor.getParameterTypes().size()); for (int i = 0; i < constructor.getParameterTypes().size(); i++) { InjectionSite site = new ConstructorInjectionSite(constructor, i); Injectable attribute = mappings.get(site); construction.put(site, attribute); byConstruction.add(attribute); } // add field/method injections Map<InjectionSite, Injectable> postConstruction = managerDefinition.getPostConstruction(); Map<InjectionSite, Injectable> reinjection = managerDefinition.getReinjectables(); for (Map.Entry<InjectionSite, Injectable> entry : mappings.entrySet()) { InjectionSite site = entry.getKey(); if (site instanceof ConstructorInjectionSite) { continue; } Injectable attribute = entry.getValue(); if (!byConstruction.contains(attribute)) { postConstruction.put(site, attribute); } reinjection.put(site, attribute); } }
void evaluateFields(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { Map<InjectionSite, Injectable> sites = componentType.getInjectionSites(); TypeMapping typeMapping = context.getTypeMapping(implClass); Set<Field> fields = helper.getInjectionFields(implClass); for (Field field : fields) { InjectionSite site = new FieldInjectionSite(field); // skip sites that have already been mapped if (sites.containsKey(site)) { continue; } String name = helper.getSiteName(field, null); Type parameterType = field.getGenericType(); Annotation[] annotations = field.getAnnotations(); processSite(componentType, typeMapping, name, field, parameterType, site, annotations, context); } }
public void introspect(WebImplementation implementation, IntrospectionContext context) { WebComponentType componentType = new WebComponentType(); implementation.setComponentType(componentType); // load the servlet, filter and context listener classes referenced in the web.xml descriptor List<Class<?>> artifacts = xmlIntrospector.introspectArtifactClasses(context); for (Class<?> artifact : artifacts) { // introspect each class and generate a component type that will be merged into the web component type WebArtifactImplementation artifactImpl = new WebArtifactImplementation(); InjectingComponentType type = new InjectingComponentType(artifact.getName()); artifactImpl.setComponentType(type); TypeMapping mapping = context.getTypeMapping(artifact); if (mapping == null) { mapping = new TypeMapping(); context.addTypeMapping(artifact, mapping); helper.resolveTypeParameters(artifact, mapping); } IntrospectionContext childContext = new DefaultIntrospectionContext(context); classVisitor.visit(type, artifact, childContext); if (childContext.hasErrors()) { context.addErrors(childContext.getErrors()); } if (childContext.hasWarnings()) { context.addWarnings(childContext.getWarnings()); } // TODO apply heuristics mergeComponentTypes(implementation.getComponentType(), type, context); } }
public void visitConstructorParameter(Cache annotation, Constructor<?> constructor, int index, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { String name = annotation.name(); Class<?> type = constructor.getParameterTypes()[index]; ResourceReferenceDefinition definition = create(name, annotation, type, constructor, componentType, context); componentType.add(definition); }
public void visitMethod(PersistenceUnit annotation, Method method, Class<?> implClass, I implementation, IntrospectionContext context) { MethodInjectionSite site = new MethodInjectionSite(method, 0); PersistenceUnitResource definition = createDefinition(annotation); InjectingComponentType componentType = implementation.getComponentType(); componentType.add(definition, site); // record that the implementation requires JPA componentType.addRequiredCapability("jpa"); }
private PersistenceContextResource createDefinition(PersistenceContext annotation, InjectingComponentType componentType) { String name = annotation.name(); String unitName = annotation.unitName(); PersistenceContextType type = annotation.type(); boolean multiThreaded = Scope.COMPOSITE.getScope().equals(componentType.getScope()); return new PersistenceContextResource(name, unitName, type, factoryServiceContract, multiThreaded); } }