Java Code Examples for org.springframework.core.annotation.AnnotationAttributes#getString()

The following examples show how to use org.springframework.core.annotation.AnnotationAttributes#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HazelcastHttpSessionConfiguration.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public void setImportMetadata(AnnotationMetadata importMetadata) {
	Map<String, Object> attributeMap = importMetadata
			.getAnnotationAttributes(EnableHazelcastHttpSession.class.getName());
	AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
	this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
	String sessionMapNameValue = attributes.getString("sessionMapName");
	if (StringUtils.hasText(sessionMapNameValue)) {
		this.sessionMapName = sessionMapNameValue;
	}
	FlushMode flushMode = attributes.getEnum("flushMode");
	HazelcastFlushMode hazelcastFlushMode = attributes.getEnum("hazelcastFlushMode");
	if (flushMode == FlushMode.ON_SAVE && hazelcastFlushMode != HazelcastFlushMode.ON_SAVE) {
		flushMode = hazelcastFlushMode.getFlushMode();
	}
	this.flushMode = flushMode;
	this.saveMode = attributes.getEnum("saveMode");
}
 
Example 2
Source File: DurableClientConfiguration.java    From spring-boot-data-geode with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("all")
public void setImportMetadata(AnnotationMetadata importMetadata) {

	if (isAnnotationPresent(importMetadata)) {

		AnnotationAttributes enableDurableClientAttributes = getAnnotationAttributes(importMetadata);

		this.durableClientId = enableDurableClientAttributes.containsKey("id")
			? enableDurableClientAttributes.getString("id")
			: null;

		this.durableClientTimeout = enableDurableClientAttributes.containsKey("timeout")
			? enableDurableClientAttributes.getNumber("timeout")
			: DEFAULT_DURABLE_CLIENT_TIMEOUT;

		this.keepAlive = enableDurableClientAttributes.containsKey("keepAlive")
			? enableDurableClientAttributes.getBoolean("keepAlive")
			: DEFAULT_KEEP_ALIVE;

		this.readyForEvents = enableDurableClientAttributes.containsKey("readyForEvents")
			? enableDurableClientAttributes.getBoolean("readyForEvents")
			: DEFAULT_READY_FOR_EVENTS;
	}
}
 
Example 3
Source File: AnnotationMetadataTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void assertMultipleAnnotationsWithIdenticalAttributeNames(AnnotationMetadata metadata) {
	AnnotationAttributes attributes1 = (AnnotationAttributes) metadata.getAnnotationAttributes(
			NamedAnnotation1.class.getName(), false);
	String name1 = attributes1.getString("name");
	assertThat("name of NamedAnnotation1", name1, is("name 1"));

	AnnotationAttributes attributes2 = (AnnotationAttributes) metadata.getAnnotationAttributes(
			NamedAnnotation2.class.getName(), false);
	String name2 = attributes2.getString("name");
	assertThat("name of NamedAnnotation2", name2, is("name 2"));

	AnnotationAttributes attributes3 = (AnnotationAttributes) metadata.getAnnotationAttributes(
			NamedAnnotation3.class.getName(), false);
	String name3 = attributes3.getString("name");
	assertThat("name of NamedAnnotation3", name3, is("name 3"));
}
 
Example 4
Source File: MBeanExportConfiguration.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void setupServer(AnnotationMBeanExporter exporter, AnnotationAttributes enableMBeanExport) {
	String server = enableMBeanExport.getString("server");
	if (StringUtils.hasLength(server) && this.environment != null) {
		server = this.environment.resolvePlaceholders(server);
	}
	if (StringUtils.hasText(server)) {
		Assert.state(this.beanFactory != null, "No BeanFactory set");
		exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
	}
	else {
		SpecificPlatform specificPlatform = SpecificPlatform.get();
		if (specificPlatform != null) {
			MBeanServer mbeanServer = specificPlatform.getMBeanServer();
			if (mbeanServer != null) {
				exporter.setServer(mbeanServer);
			}
		}
	}
}
 
Example 5
Source File: ConfigurationClassParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Process the given <code>@PropertySource</code> annotation metadata.
 * @param propertySource metadata for the <code>@PropertySource</code> annotation found
 * @throws IOException if loading a property source failed
 */
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
	String name = propertySource.getString("name");
	if (!StringUtils.hasLength(name)) {
		name = null;
	}
	String encoding = propertySource.getString("encoding");
	if (!StringUtils.hasLength(encoding)) {
		encoding = null;
	}
	String[] locations = propertySource.getStringArray("value");
	Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
	boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");

	Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
	PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
			DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));

	for (String location : locations) {
		try {
			String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
			Resource resource = this.resourceLoader.getResource(resolvedLocation);
			addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
		}
		catch (IllegalArgumentException | FileNotFoundException | UnknownHostException ex) {
			// Placeholders not resolvable or resource not found when trying to open it
			if (ignoreResourceNotFound) {
				if (logger.isInfoEnabled()) {
					logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
				}
			}
			else {
				throw ex;
			}
		}
	}
}
 
Example 6
Source File: ChoerodonRedisHttpSessionConfiguration.java    From oauth-server with Apache License 2.0 5 votes vote down vote up
public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> enableAttrMap = importMetadata.getAnnotationAttributes(EnableRedisHttpSession.class.getName());
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
    this.maxInactiveIntervalInSeconds = enableAttrs.getNumber("maxInactiveIntervalInSeconds");
    String redisNamespaceValue = enableAttrs.getString("redisNamespace");
    if (StringUtils.hasText(redisNamespaceValue)) {
        this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue);
    }

    this.redisFlushMode = enableAttrs.getEnum("redisFlushMode");
}
 
Example 7
Source File: ImportAwareTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void indirectlyAnnotatedWithImport() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(IndirectlyImportingConfig.class);
	ctx.refresh();
	assertNotNull(ctx.getBean("importedConfigBean"));

	ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
	AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
	assertThat("import metadata was not injected", importMetadata, notNullValue());
	assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName()));
	AnnotationAttributes enableAttribs = AnnotationConfigUtils.attributesFor(importMetadata, EnableImportedConfig.class);
	String foo = enableAttribs.getString("foo");
	assertThat(foo, is("xyz"));
}
 
Example 8
Source File: ImportAwareTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void indirectlyAnnotatedWithImport() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(IndirectlyImportingConfig.class);
	ctx.refresh();
	assertNotNull(ctx.getBean("importedConfigBean"));

	ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
	AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
	assertThat("import metadata was not injected", importMetadata, notNullValue());
	assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName()));
	AnnotationAttributes enableAttribs = AnnotationConfigUtils.attributesFor(importMetadata, EnableImportedConfig.class);
	String foo = enableAttribs.getString("foo");
	assertThat(foo, is("xyz"));
}
 
Example 9
Source File: MBeanExportConfiguration.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void setupDomain(AnnotationMBeanExporter exporter, AnnotationAttributes enableMBeanExport) {
	String defaultDomain = enableMBeanExport.getString("defaultDomain");
	if (StringUtils.hasLength(defaultDomain) && this.environment != null) {
		defaultDomain = this.environment.resolvePlaceholders(defaultDomain);
	}
	if (StringUtils.hasText(defaultDomain)) {
		exporter.setDefaultDomain(defaultDomain);
	}
}
 
Example 10
Source File: AlfrescoRestRegistrar.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
private void processDispatcherWebscript(AnnotationAttributes webscriptAttributes, BeanDefinitionRegistry registry) {
	String webscript = webscriptAttributes.getString("name");
	Assert.hasText(webscript, "Webscript name cannot be empty!");

	Class<?> servletContext = webscriptAttributes.getClass("servletContext");

	ServletConfigOptions[] servletConfigOptions = (ServletConfigOptions[]) webscriptAttributes
			.get("servletConfigOptions");
	Class<? extends WebApplicationContext> servletContextClass = webscriptAttributes
			.getClass("servletContextClass");
	HttpMethod[] httpMethods = (HttpMethod[]) webscriptAttributes.get("httpMethods");
	boolean inheritGlobalProperties = (Boolean) webscriptAttributes.get("inheritGlobalProperties");

	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(DispatcherWebscript.class);

	DispatcherWebscript ws = new DispatcherWebscript(webscript, inheritGlobalProperties);
	ws.setContextClass(servletContextClass);
	ws.setContextConfigLocation(servletContext.getName());
	ws.addServletConfigOptions(servletConfigOptions);
	beanDefinition.setInstanceSupplier(() -> ws);
	beanDefinition.setRole(BeanDefinition.ROLE_APPLICATION);

	registry.registerBeanDefinition(webscript, beanDefinition);

	for (HttpMethod httpMethod : httpMethods) {
		registry.registerAlias(webscript, getWebscriptName(webscript, httpMethod));
	}
}
 
Example 11
Source File: RedissonWebSessionConfiguration.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableRedissonWebSession.class.getName());
    AnnotationAttributes attrs = AnnotationAttributes.fromMap(map);
    keyPrefix = attrs.getString("keyPrefix");
    maxInactiveIntervalInSeconds = attrs.getNumber("maxInactiveIntervalInSeconds");
}
 
Example 12
Source File: MergedSqlConfig.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static String getString(AnnotationAttributes attributes, String attributeName, String defaultValue) {
	String value = attributes.getString(attributeName);
	if ("".equals(value)) {
		value = defaultValue;
	}
	return value;
}
 
Example 13
Source File: MBeanExportConfiguration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void setupDomain(AnnotationMBeanExporter exporter, AnnotationAttributes enableMBeanExport) {
	String defaultDomain = enableMBeanExport.getString("defaultDomain");
	if (StringUtils.hasLength(defaultDomain) && this.environment != null) {
		defaultDomain = this.environment.resolvePlaceholders(defaultDomain);
	}
	if (StringUtils.hasText(defaultDomain)) {
		exporter.setDefaultDomain(defaultDomain);
	}
}
 
Example 14
Source File: AbstractImportRegistrar.java    From onetwo with Apache License 2.0 5 votes vote down vote up
final protected String resolveName(AnnotationAttributes attributes, String defName) {
	String name = attributes.getString(ATTRS_NAME);
	if (!StringUtils.hasText(name)) {
		name = defName;
	}
	name = resolve(name);
	return name;
}
 
Example 15
Source File: AbstractLimiterAnnotationParser.java    From Limiter with Apache License 2.0 4 votes vote down vote up
public String getErrorHandler(AnnotationAttributes attributes) {
    return attributes.getString("errorHandler");
}
 
Example 16
Source File: ConfigurationClassBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Read the given {@link BeanMethod}, registering bean definitions
 * with the BeanDefinitionRegistry based on its contents.
 */
private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
	ConfigurationClass configClass = beanMethod.getConfigurationClass();
	MethodMetadata metadata = beanMethod.getMetadata();
	String methodName = metadata.getMethodName();

	// Do we need to mark the bean as skipped by its condition?
	if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
		configClass.skippedBeanMethods.add(methodName);
		return;
	}
	if (configClass.skippedBeanMethods.contains(methodName)) {
		return;
	}

	// Consider name and any aliases
	AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
	List<String> names = new ArrayList<String>(Arrays.asList(bean.getStringArray("name")));
	String beanName = (!names.isEmpty() ? names.remove(0) : methodName);

	// Register aliases even when overridden
	for (String alias : names) {
		this.registry.registerAlias(beanName, alias);
	}

	// Has this effectively been overridden before (e.g. via XML)?
	if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
		if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
			throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(),
					beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() +
					"' clashes with bean name for containing configuration class; please make those names unique!");
		}
		return;
	}

	ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
	beanDef.setResource(configClass.getResource());
	beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));

	if (metadata.isStatic()) {
		// static @Bean method
		beanDef.setBeanClassName(configClass.getMetadata().getClassName());
		beanDef.setFactoryMethodName(methodName);
	}
	else {
		// instance @Bean method
		beanDef.setFactoryBeanName(configClass.getBeanName());
		beanDef.setUniqueFactoryMethodName(methodName);
	}
	beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
	beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

	AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata);

	Autowire autowire = bean.getEnum("autowire");
	if (autowire.isAutowire()) {
		beanDef.setAutowireMode(autowire.value());
	}

	String initMethodName = bean.getString("initMethod");
	if (StringUtils.hasText(initMethodName)) {
		beanDef.setInitMethodName(initMethodName);
	}

	String destroyMethodName = bean.getString("destroyMethod");
	if (destroyMethodName != null) {
		beanDef.setDestroyMethodName(destroyMethodName);
	}

	// Consider scoping
	ScopedProxyMode proxyMode = ScopedProxyMode.NO;
	AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class);
	if (attributes != null) {
		beanDef.setScope(attributes.getString("value"));
		proxyMode = attributes.getEnum("proxyMode");
		if (proxyMode == ScopedProxyMode.DEFAULT) {
			proxyMode = ScopedProxyMode.NO;
		}
	}

	// Replace the original bean definition with the target one, if necessary
	BeanDefinition beanDefToRegister = beanDef;
	if (proxyMode != ScopedProxyMode.NO) {
		BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(
				new BeanDefinitionHolder(beanDef, beanName), this.registry,
				proxyMode == ScopedProxyMode.TARGET_CLASS);
		beanDefToRegister = new ConfigurationClassBeanDefinition(
				(RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata);
	}

	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Registering bean definition for @Bean method %s.%s()",
				configClass.getMetadata().getClassName(), beanName));
	}

	this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}
 
Example 17
Source File: AbstractLimiterAnnotationParser.java    From Limiter with Apache License 2.0 4 votes vote down vote up
public String getFallback(AnnotationAttributes attributes) {
    return attributes.getString("fallback");
}
 
Example 18
Source File: AbstractLimiterAnnotationParser.java    From Limiter with Apache License 2.0 4 votes vote down vote up
public String getLimiter(AnnotationAttributes attributes) {
    return attributes.getString("limiter");
}
 
Example 19
Source File: ContextConfigurationAttributes.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Construct a new {@link ContextConfigurationAttributes} instance for the
 * supplied {@link AnnotationAttributes} (parsed from a
 * {@link ContextConfiguration @ContextConfiguration} annotation) and
 * the {@linkplain Class test class} that declared them.
 * @param declaringClass the test class that declared {@code @ContextConfiguration}
 * @param annAttrs the annotation attributes from which to retrieve the attributes
 */
@SuppressWarnings("unchecked")
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
	this(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getClassArray("classes"),
			annAttrs.getBoolean("inheritLocations"),
			(Class<? extends ApplicationContextInitializer<?>>[]) annAttrs.getClassArray("initializers"),
			annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"), annAttrs.getClass("loader"));
}
 
Example 20
Source File: OnMissingPropertyCondition.java    From spring-boot-data-geode with Apache License 2.0 2 votes vote down vote up
private String getPrefix(AnnotationAttributes annotationAttributes) {

		String prefix = annotationAttributes.getString("prefix");

		return StringUtils.hasText(prefix) ? prefix.trim().endsWith(".") ? prefix.trim() : prefix.trim() + "." : "";
	}