/** * 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); } } }
/** * Invokes provider to get a bean. */ protected Object invokeProvider(final ProviderDefinition provider) { if (provider.method != null) { final Object bean; if (provider.beanName != null) { // instance factory method bean = getBean(provider.beanName); } else { // static factory method bean = null; } try { return provider.method.invoke(bean); } catch (Exception ex) { throw new PetiteException("Invalid provider method: " + provider.method.getName(), ex); } } throw new PetiteException("Invalid provider"); }
public Object invokeMethodInjectionPoint(final MethodInjectionPoint methodRef) { final BeanReferences[] refNames = methodRef.references; final Object[] args = new Object[refNames.length]; for (int i = 0; i < refNames.length; i++) { final BeanReferences refName = refNames[i]; final Object value = pc.lookupMixingScopedBean(beanDefinition, refName); args[i] = value; if (value == null) { if ((beanDefinition.wiringMode == WiringMode.STRICT)) { throw new PetiteException("Wiring failed. Beans references: '" + refName + "' not found for method: " + beanDefinition.type.getName() + '#' + methodRef.method.getName()); } } } try { return methodRef.method.invoke(bean, args); } catch (Exception ex) { throw new PetiteException(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); } } }
throw new PetiteException("No constructor (annotated, single or default) founded as injection point for: " + beanDefinition.type.getName()); if (args[i] == null) { if ((beanDefinition.wiringMode == WiringMode.STRICT)) { throw new PetiteException( "Wiring constructor failed. References '" + beanDefinition.ctor.references[i] + "' not found for constructor: " + beanDefinition.ctor.constructor); bean = beanDefinition.ctor.constructor.newInstance(args); } catch (Exception ex) { throw new PetiteException("Failed to create new bean instance '" + beanDefinition.type.getName() + "' using constructor: " + beanDefinition.ctor.constructor, 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()); }
/** * Sets petite bean property. */ public void setBeanProperty(final String name, final Object value) { Object bean = null; int ndx = name.length(); while (true) { ndx = name.lastIndexOf('.', ndx); if (ndx == -1) { break; } String beanName = name.substring(0, ndx); bean = getBean(beanName); if (bean != null) { break; } ndx--; } if (bean == null) { throw new PetiteException("Invalid bean property: " + name); } try { BeanUtil.declared.setProperty(bean, name.substring(ndx + 1), value); } catch (Exception ex) { throw new PetiteException("Invalid bean property: " + name, ex); } }
public DestroyMethodPoint[] resolve(final Class<?> type) { // lookup methods List<DestroyMethodPoint> list = new ArrayList<>(); ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null); MethodDescriptor[] allMethods = cd.getAllMethodDescriptors(); for (MethodDescriptor methodDescriptor : allMethods) { Method method = methodDescriptor.getMethod(); PetiteDestroyMethod petiteDestroyMethod = method.getAnnotation(PetiteDestroyMethod.class); if (petiteDestroyMethod == null) { continue; } if (method.getParameterTypes().length > 0) { throw new PetiteException("Arguments are not allowed for Petite destroy method: " + type.getName() + '#' + method.getName()); } list.add(new DestroyMethodPoint(method)); } DestroyMethodPoint[] methods; if (list.isEmpty()) { methods = DestroyMethodPoint.EMPTY; } else { methods = list.toArray(new DestroyMethodPoint[0]); } return methods; }
/** * Injects all parameters. */ public void injectParams(final ParamManager paramManager, final boolean implicitParamInjection) { if (beanDefinition.name == null) { return; } if (implicitParamInjection) { // implicit final int len = beanDefinition.name.length() + 1; for (final String param : beanDefinition.params) { final Object value = paramManager.get(param); final String destination = param.substring(len); try { BeanUtil.declared.setProperty(bean, destination, value); } catch (Exception ex) { throw new PetiteException("Unable to set parameter: '" + param + "' to bean: " + beanDefinition.name, ex); } } } // explicit for (final ValueInjectionPoint pip : beanDefinition.values) { final String value = paramManager.parseKeyTemplate(pip.valueTemplate); try { BeanUtil.declared.setProperty(bean, pip.property, value); } catch (Exception ex) { throw new PetiteException("Unable to set value for: '" + pip.valueTemplate + "' to bean: " + beanDefinition.name, ex); } } }
public InitMethodPoint[] resolve(final Class<?> type) { // lookup methods List<InitMethodPoint> list = new ArrayList<>(); ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null); MethodDescriptor[] allMethods = cd.getAllMethodDescriptors(); for (MethodDescriptor methodDescriptor : allMethods) { Method method = methodDescriptor.getMethod(); PetiteInitMethod petiteInitMethod = method.getAnnotation(PetiteInitMethod.class); if (petiteInitMethod == null) { continue; } if (method.getParameterTypes().length > 0) { throw new PetiteException("Arguments are not allowed for Petite init method: " + type.getName() + '#' + method.getName()); } int order = petiteInitMethod.order(); list.add(new InitMethodPoint(method, order, petiteInitMethod.invoke())); } InitMethodPoint[] methods; if (list.isEmpty()) { methods = InitMethodPoint.EMPTY; } else { Collections.sort(list); methods = list.toArray(new InitMethodPoint[0]); } return methods; }
protected void wireProperties() { for (PropertyInjectionPoint pip : beanDefinition.properties) { final BeanReferences refNames = pip.references; final Object value = pc.lookupMixingScopedBean(this.definition(), refNames); if (value == null) { if ((beanDefinition.wiringMode == WiringMode.STRICT)) { throw new PetiteException("Wiring failed. Beans references: '" + refNames + "' not found for property: " + beanDefinition.type.getName() + '#' + pip.propertyDescriptor.getName()); } continue; } // BeanUtil.setDeclaredProperty(bean, pip.propertyDescriptor.getName(), value); final Setter setter = pip.propertyDescriptor.getSetter(true); try { setter.invokeSetter(bean, value); } catch (Exception ex) { throw new PetiteException("Wiring failed", ex); } } }