List of usage examples for org.springframework.beans.factory.config BeanReference getBeanName
String getBeanName();
From source file:org.cloudfoundry.reconfiguration.util.StandardPropertyAugmenter.java
private Properties extractProperties(BeanReference beanReference, ConfigurableListableBeanFactory beanFactory) { BeanDefinition beanDefinition = getBeanDefinition(beanFactory, beanReference.getBeanName()); return getMapWrappingBeanProperties(beanDefinition); }
From source file:org.guicerecipes.spring.converter.SpringConverter.java
@SuppressWarnings("unchecked") protected void generateBeanDefinition(ModuleGenerator generator, String name, BeanDefinition definition, String className) {//from w ww . j a v a 2 s .com String shortClassName = addImport(className); ProduceMethod method = generator.startProvides(name, shortClassName); ConstructorArgumentValues constructors = definition.getConstructorArgumentValues(); Map map = constructors.getIndexedArgumentValues(); for (int i = 0, size = map.size(); i < size; i++) { ValueHolder valueHolder = (ValueHolder) map.get(i); if (valueHolder != null) { Object value = valueHolder.getValue(); if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; String text = stringValue.getValue(); System.out.printf("param %s=\"%s\"\n", i, text); String expression = null; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + text + "\""; } method.addConstructorExpression(expression); } else if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addConstructorExpression(expression); } } } MutablePropertyValues propertyValues = definition.getPropertyValues(); PropertyValue[] propertyArray = propertyValues.getPropertyValues(); for (PropertyValue propertyValue : propertyArray) { String property = getSetterMethod(propertyValue); Object value = propertyValue.getConvertedValue(); if (value == null) { value = propertyValue.getValue(); } if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value; addChildBeanDefinition(generator, method, name, propertyValue, beanDefinitionHolder.getBeanDefinition()); } else if (value instanceof ChildBeanDefinition) { ChildBeanDefinition childBeanDefinition = (ChildBeanDefinition) value; addChildBeanDefinition(generator, method, name, propertyValue, childBeanDefinition); } else { if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; value = stringValue.getValue(); } String valueType = (value == null) ? null : value.getClass().getName(); System.out.printf("property %s=%s of type %s\n", property, value, valueType); String expression; if (value instanceof String) { String text = (String) value; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + value + "\""; } } else if (value == null) { expression = "null"; } else { expression = value.toString(); } method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } } }
From source file:org.guiceyfruit.spring.converter.SpringConverter.java
protected void generateBeanDefinition(ModuleGenerator generator, String name, BeanDefinition definition, String className) {/*from www . j a va 2 s . c o m*/ String shortClassName = addImport(className); ProduceMethod method = generator.startProvides(name, shortClassName); ConstructorArgumentValues constructors = definition.getConstructorArgumentValues(); Map map = constructors.getIndexedArgumentValues(); for (int i = 0, size = map.size(); i < size; i++) { ValueHolder valueHolder = (ValueHolder) map.get(i); if (valueHolder != null) { Object value = valueHolder.getValue(); if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; String text = stringValue.getValue(); System.out.printf("param %s=\"%s\"\n", i, text); String expression = null; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + text + "\""; } method.addConstructorExpression(expression); } else if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addConstructorExpression(expression); } } } MutablePropertyValues propertyValues = definition.getPropertyValues(); PropertyValue[] propertyArray = propertyValues.getPropertyValues(); for (PropertyValue propertyValue : propertyArray) { String property = getSetterMethod(propertyValue); Object value = propertyValue.getConvertedValue(); if (value == null) { value = propertyValue.getValue(); } if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value; addChildBeanDefinition(generator, method, name, propertyValue, beanDefinitionHolder.getBeanDefinition()); } else if (value instanceof ChildBeanDefinition) { ChildBeanDefinition childBeanDefinition = (ChildBeanDefinition) value; addChildBeanDefinition(generator, method, name, propertyValue, childBeanDefinition); } else { if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; value = stringValue.getValue(); } String valueType = (value == null) ? null : value.getClass().getName(); System.out.printf("property %s=%s of type %s\n", property, value, valueType); String expression; if (value instanceof String) { String text = (String) value; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + value + "\""; } } else if (value == null) { expression = "null"; } else { expression = value.toString(); } method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } } }
From source file:org.jdal.aop.SerializableReference.java
private Object readResolve() throws ObjectStreamException { if (beanFactory == null) { log.error("Can't not deserialize reference without bean factory"); return null; }// ww w. jav a 2 s . c o m if (useMemoryCache) { Object ret = serializedObjects.remove(this.id); if (ret != null) { if (log.isDebugEnabled()) log.debug("Removed a serialized reference. serialized objects size [" + serializedObjects.size() + "]"); return getSerializableProxy(ret); } } if (targetBeanName != null) { if (log.isDebugEnabled()) log.debug("Resolving serializable object to bean name [" + targetBeanName + "]"); return getSerializableProxy(); } if (log.isDebugEnabled()) log.debug("Resolving serializable object for [" + descriptor.getDependencyName() + "]"); Field field = descriptor.getField(); // Check bean definition BeanDefinition rbd = beanFactory.getBeanDefinition(beanName); PropertyValues pvs = rbd.getPropertyValues(); if (pvs.contains(field.getName())) { Object value = pvs.getPropertyValue(field.getName()); if (value instanceof BeanReference) { // cache the bean name this.targetBeanName = ((BeanReference) value).getBeanName(); return getSerializableProxy(); } } // Check Autowired try { Object bean = beanFactory.resolveDependency(descriptor, beanName); if (bean != null) return getSerializableProxy(bean); } catch (BeansException be) { // dependency not found. } // Check Resource annotation if (field.isAnnotationPresent(Resource.class)) { Resource r = field.getAnnotation(Resource.class); String name = StringUtils.isEmpty(r.name()) ? descriptor.getField().getName() : r.name(); if (beanFactory.containsBean(name)) { this.targetBeanName = name; return getSerializableProxy(); } } // Try with depend beans. String[] dependentBeans = beanFactory.getDependenciesForBean(beanName); List<String> candidates = new ArrayList<String>(); for (String name : dependentBeans) { if (beanFactory.isTypeMatch(name, descriptor.getDependencyType())) ; candidates.add(name); } if (candidates.size() == 1) return getSerializableProxy(beanFactory.getBean(candidates.get(0))); if (candidates.size() > 1) { for (PropertyValue pv : pvs.getPropertyValues()) { if (pv.getValue() instanceof BeanReference) { BeanReference br = (BeanReference) pv.getValue(); if (candidates.contains(br.getBeanName())) return getSerializableProxy(beanFactory.getBean(br.getBeanName())); } } } log.error("Cant not resolve serializable reference wiht candidates " + candidates.toArray()); return null; }
From source file:org.springframework.security.config.method.GlobalMethodSecurityBeanDefinitionParser.java
public BeanDefinition parse(Element element, ParserContext pc) { CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element));//from ww w . ja v a2s . co m pc.pushContainingComponent(compositeDef); Object source = pc.extractSource(element); // The list of method metadata delegates ManagedList<BeanMetadataElement> delegates = new ManagedList<>(); boolean jsr250Enabled = "enabled".equals(element.getAttribute(ATT_USE_JSR250)); boolean useSecured = "enabled".equals(element.getAttribute(ATT_USE_SECURED)); boolean prePostAnnotationsEnabled = "enabled".equals(element.getAttribute(ATT_USE_PREPOST)); boolean useAspectJ = "aspectj".equals(element.getAttribute(ATT_MODE)); BeanDefinition preInvocationVoter = null; ManagedList<BeanMetadataElement> afterInvocationProviders = new ManagedList<>(); // Check for an external SecurityMetadataSource, which takes priority over other // sources String metaDataSourceId = element.getAttribute(ATT_META_DATA_SOURCE_REF); if (StringUtils.hasText(metaDataSourceId)) { delegates.add(new RuntimeBeanReference(metaDataSourceId)); } if (prePostAnnotationsEnabled) { Element prePostElt = DomUtils.getChildElementByTagName(element, INVOCATION_HANDLING); Element expressionHandlerElt = DomUtils.getChildElementByTagName(element, EXPRESSION_HANDLER); if (prePostElt != null && expressionHandlerElt != null) { pc.getReaderContext().error( INVOCATION_HANDLING + " and " + EXPRESSION_HANDLER + " cannot be used together ", source); } BeanDefinitionBuilder preInvocationVoterBldr = BeanDefinitionBuilder .rootBeanDefinition(PreInvocationAuthorizationAdviceVoter.class); // After-invocation provider to handle post-invocation filtering and // authorization expression annotations. BeanDefinitionBuilder afterInvocationBldr = BeanDefinitionBuilder .rootBeanDefinition(PostInvocationAdviceProvider.class); // The metadata source for the security interceptor BeanDefinitionBuilder mds = BeanDefinitionBuilder .rootBeanDefinition(PrePostAnnotationSecurityMetadataSource.class); if (prePostElt != null) { // Customized override of expression handling system String attributeFactoryRef = DomUtils .getChildElementByTagName(prePostElt, INVOCATION_ATTRIBUTE_FACTORY).getAttribute("ref"); String preAdviceRef = DomUtils.getChildElementByTagName(prePostElt, PRE_INVOCATION_ADVICE) .getAttribute("ref"); String postAdviceRef = DomUtils.getChildElementByTagName(prePostElt, POST_INVOCATION_ADVICE) .getAttribute("ref"); mds.addConstructorArgReference(attributeFactoryRef); preInvocationVoterBldr.addConstructorArgReference(preAdviceRef); afterInvocationBldr.addConstructorArgReference(postAdviceRef); } else { // The default expression-based system String expressionHandlerRef = expressionHandlerElt == null ? null : expressionHandlerElt.getAttribute("ref"); if (StringUtils.hasText(expressionHandlerRef)) { logger.info( "Using bean '" + expressionHandlerRef + "' as method ExpressionHandler implementation"); RootBeanDefinition lazyInitPP = new RootBeanDefinition( LazyInitBeanDefinitionRegistryPostProcessor.class); lazyInitPP.getConstructorArgumentValues().addGenericArgumentValue(expressionHandlerRef); pc.getReaderContext().registerWithGeneratedName(lazyInitPP); BeanDefinitionBuilder lazyMethodSecurityExpressionHandlerBldr = BeanDefinitionBuilder .rootBeanDefinition(LazyInitTargetSource.class); lazyMethodSecurityExpressionHandlerBldr.addPropertyValue("targetBeanName", expressionHandlerRef); BeanDefinitionBuilder expressionHandlerProxyBldr = BeanDefinitionBuilder .rootBeanDefinition(ProxyFactoryBean.class); expressionHandlerProxyBldr.addPropertyValue("targetSource", lazyMethodSecurityExpressionHandlerBldr.getBeanDefinition()); expressionHandlerProxyBldr.addPropertyValue("proxyInterfaces", MethodSecurityExpressionHandler.class); expressionHandlerRef = pc.getReaderContext() .generateBeanName(expressionHandlerProxyBldr.getBeanDefinition()); pc.registerBeanComponent(new BeanComponentDefinition( expressionHandlerProxyBldr.getBeanDefinition(), expressionHandlerRef)); } else { RootBeanDefinition expressionHandler = registerWithDefaultRolePrefix(pc, DefaultMethodSecurityExpressionHandlerBeanFactory.class); expressionHandlerRef = pc.getReaderContext().generateBeanName(expressionHandler); pc.registerBeanComponent(new BeanComponentDefinition(expressionHandler, expressionHandlerRef)); logger.info( "Expressions were enabled for method security but no SecurityExpressionHandler was configured. " + "All hasPermision() expressions will evaluate to false."); } BeanDefinitionBuilder expressionPreAdviceBldr = BeanDefinitionBuilder .rootBeanDefinition(ExpressionBasedPreInvocationAdvice.class); expressionPreAdviceBldr.addPropertyReference("expressionHandler", expressionHandlerRef); preInvocationVoterBldr.addConstructorArgValue(expressionPreAdviceBldr.getBeanDefinition()); BeanDefinitionBuilder expressionPostAdviceBldr = BeanDefinitionBuilder .rootBeanDefinition(ExpressionBasedPostInvocationAdvice.class); expressionPostAdviceBldr.addConstructorArgReference(expressionHandlerRef); afterInvocationBldr.addConstructorArgValue(expressionPostAdviceBldr.getBeanDefinition()); BeanDefinitionBuilder annotationInvocationFactory = BeanDefinitionBuilder .rootBeanDefinition(ExpressionBasedAnnotationAttributeFactory.class); annotationInvocationFactory.addConstructorArgReference(expressionHandlerRef); mds.addConstructorArgValue(annotationInvocationFactory.getBeanDefinition()); } preInvocationVoter = preInvocationVoterBldr.getBeanDefinition(); afterInvocationProviders.add(afterInvocationBldr.getBeanDefinition()); delegates.add(mds.getBeanDefinition()); } if (useSecured) { delegates.add(BeanDefinitionBuilder.rootBeanDefinition(SecuredAnnotationSecurityMetadataSource.class) .getBeanDefinition()); } if (jsr250Enabled) { RootBeanDefinition jsrMetadataSource = registerWithDefaultRolePrefix(pc, Jsr250MethodSecurityMetadataSourceBeanFactory.class); delegates.add(jsrMetadataSource); } // Now create a Map<String, ConfigAttribute> for each <protect-pointcut> // sub-element Map<String, List<ConfigAttribute>> pointcutMap = parseProtectPointcuts(pc, DomUtils.getChildElementsByTagName(element, PROTECT_POINTCUT)); if (pointcutMap.size() > 0) { if (useAspectJ) { pc.getReaderContext().error("You can't use AspectJ mode with protect-pointcut definitions", source); } // Only add it if there are actually any pointcuts defined. BeanDefinition mapBasedMetadataSource = new RootBeanDefinition( MapBasedMethodSecurityMetadataSource.class); BeanReference ref = new RuntimeBeanReference( pc.getReaderContext().generateBeanName(mapBasedMetadataSource)); delegates.add(ref); pc.registerBeanComponent(new BeanComponentDefinition(mapBasedMetadataSource, ref.getBeanName())); registerProtectPointcutPostProcessor(pc, pointcutMap, ref, source); } BeanReference metadataSource = registerDelegatingMethodSecurityMetadataSource(pc, delegates, source); // Check for additional after-invocation-providers.. List<Element> afterInvocationElts = DomUtils.getChildElementsByTagName(element, Elements.AFTER_INVOCATION_PROVIDER); for (Element elt : afterInvocationElts) { afterInvocationProviders.add(new RuntimeBeanReference(elt.getAttribute(ATT_REF))); } String accessManagerId = element.getAttribute(ATT_ACCESS_MGR); if (!StringUtils.hasText(accessManagerId)) { accessManagerId = registerAccessManager(pc, jsr250Enabled, preInvocationVoter); } String authMgrRef = element.getAttribute(ATT_AUTHENTICATION_MANAGER_REF); String runAsManagerId = element.getAttribute(ATT_RUN_AS_MGR); BeanReference interceptor = registerMethodSecurityInterceptor(pc, authMgrRef, accessManagerId, runAsManagerId, metadataSource, afterInvocationProviders, source, useAspectJ); if (useAspectJ) { BeanDefinitionBuilder aspect = BeanDefinitionBuilder.rootBeanDefinition( "org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect"); aspect.setFactoryMethod("aspectOf"); aspect.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); aspect.addPropertyValue("securityInterceptor", interceptor); String id = pc.getReaderContext().registerWithGeneratedName(aspect.getBeanDefinition()); pc.registerBeanComponent(new BeanComponentDefinition(aspect.getBeanDefinition(), id)); } else { registerAdvisor(pc, interceptor, metadataSource, source, element.getAttribute(ATT_ADVICE_ORDER)); AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(pc, element); } pc.popAndRegisterContainingComponent(); return null; }
From source file:org.springframework.security.config.method.GlobalMethodSecurityBeanDefinitionParser.java
private void registerAdvisor(ParserContext parserContext, BeanReference interceptor, BeanReference metadataSource, Object source, String adviceOrder) { if (parserContext.getRegistry().containsBeanDefinition(BeanIds.METHOD_SECURITY_METADATA_SOURCE_ADVISOR)) { parserContext.getReaderContext().error("Duplicate <global-method-security> detected.", source); }//from w w w . jav a 2 s . c om RootBeanDefinition advisor = new RootBeanDefinition(MethodSecurityMetadataSourceAdvisor.class); if (StringUtils.hasText(adviceOrder)) { advisor.getPropertyValues().addPropertyValue("order", adviceOrder); } // advisor must be an infrastructure bean as Spring's // InfrastructureAdvisorAutoProxyCreator will ignore it // otherwise advisor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); advisor.setSource(source); advisor.getConstructorArgumentValues().addGenericArgumentValue(interceptor.getBeanName()); advisor.getConstructorArgumentValues().addGenericArgumentValue(metadataSource); advisor.getConstructorArgumentValues().addGenericArgumentValue(metadataSource.getBeanName()); parserContext.getRegistry().registerBeanDefinition(BeanIds.METHOD_SECURITY_METADATA_SOURCE_ADVISOR, advisor); }