/** * Specifies default wiring mode. */ public PetiteConfig setDefaultWiringMode(final WiringMode defaultWiringMode) { if ((defaultWiringMode == null) || (defaultWiringMode == WiringMode.DEFAULT)) { throw new PetiteException("Invalid default wiring mode: " + defaultWiringMode); } this.defaultWiringMode = defaultWiringMode; return this; } /**
/** * Defines bean name. */ public BeanProvider bean(final String beanName) { if (type != null) { throw new PetiteException("Petite provider type already defined"); } this.beanName = beanName; return this; }
/** * Defines bean type. */ public BeanProvider type(final Class type) { if (beanName != null) { throw new PetiteException("Petite provider bean name already defined"); } this.type = type; return this; }
public ProviderDefinition(final String name, final Method staticMethod) { Objects.requireNonNull(name); Objects.requireNonNull(staticMethod); this.name = name; if (!Modifier.isStatic(staticMethod.getModifiers())) { throw new PetiteException("Provider method is not static: " + staticMethod); } this.method = staticMethod; this.beanName = null; }
/** * Delegates to {@link jodd.petite.scope.Scope#lookup(String)}. */ protected Object scopeLookup() { if (scope == null) { throw new PetiteException("Scope not defined"); } return scope.lookup(name); }
/** * Returns request from current thread. */ protected HttpSession getCurrentHttpSession() { HttpServletRequest request = RequestContextListener.getRequest(); if (request == null) { throw new PetiteException("No HTTP request bound to the current thread. Is RequestContextListener registered?"); } return request.getSession(); }
/** * Lookups for existing {@link jodd.petite.BeanDefinition bean definition}. * Throws exception if bean is not found. */ protected BeanDefinition lookupExistingBeanDefinition(final String name) { BeanDefinition beanDefinition = lookupBeanDefinition(name); if (beanDefinition == null) { throw new PetiteException("Bean not found: " + name); } return beanDefinition; }
/** * Returns request from current thread. */ protected HttpServletRequest getCurrentHttpRequest() { HttpServletRequest request = RequestContextListener.getRequest(); if (request == null) { throw new PetiteException("No HTTP request bound to the current thread. Is RequestContextListener registered?"); } return request; }
/** * Calls destroy methods on given BeanData. Destroy methods are called * without any order. */ public void callDestroyMethods() { for (final DestroyMethodPoint destroyMethodPoint : beanDefinition.destroyMethodPoints()) { try { destroyMethodPoint.method.invoke(bean); } catch (Exception ex) { throw new PetiteException("Invalid destroy method: " + destroyMethodPoint.method, ex); } } }
/** * Returns petite bean property value. */ public Object getBeanProperty(final String name) { int ndx = name.indexOf('.'); if (ndx == -1) { throw new PetiteException("Only bean name is specified, missing property name: " + name); } String beanName = name.substring(0, ndx); Object bean = getBean(beanName); if (bean == null) { throw new PetiteException("Bean doesn't exist: " + name); } try { return BeanUtil.declared.getProperty(bean, name.substring(ndx + 1)); } catch (Exception ex) { throw new PetiteException("Invalid bean property: " + name, ex); } }
/** * Invokes init methods. */ public void invokeInitMethods(final InitMethodInvocationStrategy invocationStrategy) { for (final InitMethodPoint initMethod : beanDefinition.initMethodPoints()) { if (invocationStrategy != initMethod.invocationStrategy) { continue; } try { initMethod.method.invoke(bean); } catch (Exception ex) { throw new PetiteException("Invalid init method: " + initMethod, ex); } } }
@SuppressWarnings({"unchecked"}) protected Class<T> resolveSetType(final PropertyDescriptor propertyDescriptor) { Class<T> type = (Class<T>) propertyDescriptor.getType(); if (ClassUtil.isTypeOf(type, Collection.class)) { return type; } throw new PetiteException("Unsupported Petite set type: " + type.getName()); }
/** * Resolves and registers scope from a scope type. */ @SuppressWarnings("unchecked") public <S extends Scope> S resolveScope(final Class<S> scopeType) { S scope = (S) scopes.get(scopeType); if (scope == null) { try { scope = newInternalInstance(scopeType, (PetiteContainer) this); } catch (Exception ex) { throw new PetiteException("Invalid Petite scope: " + scopeType.getName(), ex); } registerScope(scopeType, scope); scopes.put(scopeType, scope); } return scope; }
/** * Registers static method provider. * * @param providerName provider name * @param type class type * @param staticMethodName static method name * @param arguments method argument types */ public void registerPetiteProvider(final String providerName, final Class type, final String staticMethodName, final Class[] arguments) { ClassDescriptor cd = ClassIntrospector.get().lookup(type); MethodDescriptor md = cd.getMethodDescriptor(staticMethodName, arguments, true); if (md == null) { throw new PetiteException("Provider method not found: " + staticMethodName); } ProviderDefinition providerDefinition = new ProviderDefinition(providerName, md.getMethod()); providers.put(providerName, providerDefinition); }
public SetInjectionPoint(final PropertyDescriptor propertyDescriptor) { Objects.requireNonNull(propertyDescriptor); this.propertyDescriptor = propertyDescriptor; this.type = resolveSetType(propertyDescriptor); // resolve component type Class targetClass = null; MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor(); FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor(); if (writeMethodDescriptor != null) { targetClass = writeMethodDescriptor.getSetterRawComponentType(); } if (targetClass == null && fieldDescriptor != null) { targetClass = fieldDescriptor.getRawComponentType(); } this.targetClass = targetClass; if (targetClass == null) { throw new PetiteException("Unknown Petite set component type " + type.getSimpleName() + '.' + propertyDescriptor.getName()); } }
/** * Takes given parameters references and returns reference set for given method or constructor. */ public BeanReferences[] resolveReferenceFromValues(final Executable methodOrCtor, final String... parameterReferences) { BeanReferences[] references = convertRefToReferences(parameterReferences); if (references == null || references.length == 0) { references = buildDefaultReferences(methodOrCtor); } if (methodOrCtor.getParameterTypes().length != references.length) { throw new PetiteException("Different number of method parameters and references for: " + methodOrCtor.getDeclaringClass().getName() + '#' + methodOrCtor.getName()); } removeAllDuplicateNames(references); return references; }
private BeanReferences[] updateReferencesWithDefaultsIfNeeded(final Executable methodOrCtor, BeanReferences[] references) { BeanReferences[] defaultReferences = buildDefaultReferences(methodOrCtor); if (references == null || references.length == 0) { references = defaultReferences; } if (methodOrCtor.getParameterTypes().length != references.length) { throw new PetiteException( "Different number of parameters and references for: " + methodOrCtor.getName()); } // apply default parameters for (int i = 0; i < references.length; i++) { BeanReferences parameterReferences = references[i]; if (parameterReferenceIsNotSet(parameterReferences)) { references[i] = defaultReferences[i]; } } return references; }
/** * Registers set injection point. * * @param beanName bean name * @param property set property name */ public void registerPetiteSetInjectionPoint(final String beanName, final String property) { BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName); ClassDescriptor cd = ClassIntrospector.get().lookup(beanDefinition.type); PropertyDescriptor propertyDescriptor = cd.getPropertyDescriptor(property, true); if (propertyDescriptor == null) { throw new PetiteException("Property not found: " + beanDefinition.type.getName() + '#' + property); } SetInjectionPoint sip = new SetInjectionPoint(propertyDescriptor); beanDefinition.addSetInjectionPoint(sip); }
/** * Configures {@link jodd.petite.PetiteContainer} with specified class path. */ public void configure() { long elapsed = System.currentTimeMillis(); final ClassScanner classScanner = new ClassScanner(); classScanner.detectEntriesMode(true); classScanner.scanDefaultClasspath(); classScannerConsumers.accept(classScanner); registerAsConsumer(classScanner); try { classScanner.start(); } catch (Exception ex) { throw new PetiteException("Scan classpath error", ex); } elapsed = System.currentTimeMillis() - elapsed; log.info("Petite configured in " + elapsed + " ms. Total beans: " + container.beansCount()); }
/** * Registers property injection point. * * @param beanName bean name * @param property property name * @param reference explicit injection reference, may be <code>null</code> */ public void registerPetitePropertyInjectionPoint(final String beanName, final String property, final String reference) { BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName); ClassDescriptor cd = ClassIntrospector.get().lookup(beanDefinition.type); PropertyDescriptor propertyDescriptor = cd.getPropertyDescriptor(property, true); if (propertyDescriptor == null) { throw new PetiteException("Property not found: " + beanDefinition.type.getName() + '#' + property); } BeanReferences ref = referencesResolver.resolveReferenceFromValue(propertyDescriptor, reference); PropertyInjectionPoint pip = new PropertyInjectionPoint(propertyDescriptor, ref); beanDefinition.addPropertyInjectionPoint(pip); }