Java Code Examples for org.springframework.util.ClassUtils#getPackageName()

The following examples show how to use org.springframework.util.ClassUtils#getPackageName() . 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: JpaConfig.java    From thymeleafexamples-layouts with Apache License 2.0 6 votes vote down vote up
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource);

    String entities = ClassUtils.getPackageName(Application.class);
    String converters = ClassUtils.getPackageName(Jsr310JpaConverters.class);
    entityManagerFactoryBean.setPackagesToScan(entities, converters);

    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    Properties jpaProperties = new Properties();
    jpaProperties.put(Environment.DIALECT, dialect);
    jpaProperties.put(Environment.HBM2DDL_AUTO, hbm2ddlAuto);
    jpaProperties.put(Environment.SHOW_SQL, showSql);
    jpaProperties.put(Environment.FORMAT_SQL, formatSql);
    jpaProperties.put(Environment.USE_SQL_COMMENTS, useSqlComments);
    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
}
 
Example 2
Source File: StoreFragmentDetector.java    From spring-content with Apache License 2.0 6 votes vote down vote up
@Override
public boolean test(BeanDefinition definition) {
	Assert.notNull(definition, "BeanDefinition must not be null!");

	String beanClassName = definition.getBeanClassName();

	if (beanClassName == null /*|| isExcluded(beanClassName, getExcludeFilters())*/) {
		return false;
	}

	String beanPackage = ClassUtils.getPackageName(beanClassName);
	String shortName = ClassUtils.getShortName(beanClassName);
	String localName = shortName.substring(shortName.lastIndexOf('.') + 1);

	return localName.equals(getImplementationClassName(interfaceName)) //
			&& basePackages.stream().anyMatch(it -> beanPackage.startsWith(it));

}
 
Example 3
Source File: ControllerClassNameHandlerMapping.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Build a path prefix for the given controller bean class.
 * @param beanClass the controller bean class to generate a mapping for
 * @return the path prefix, potentially including subpackage names as path elements
 */
private StringBuilder buildPathPrefix(Class<?> beanClass) {
	StringBuilder pathMapping = new StringBuilder();
	if (this.pathPrefix != null) {
		pathMapping.append(this.pathPrefix);
		pathMapping.append("/");
	}
	else {
		pathMapping.append("/");
	}
	if (this.basePackage != null) {
		String packageName = ClassUtils.getPackageName(beanClass);
		if (packageName.startsWith(this.basePackage)) {
			String subPackage = packageName.substring(this.basePackage.length()).replace('.', '/');
			pathMapping.append(this.caseSensitive ? subPackage : subPackage.toLowerCase());
			pathMapping.append("/");
		}
	}
	return pathMapping;
}
 
Example 4
Source File: NetworkAssemble.java    From network-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	ClassPathScanningCandidateComponentProvider scanner = new ScanningComponent(Boolean.FALSE, this.environment);
	scanner.setResourceLoader(this.resourceLoader);

	AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(Network.class);
	scanner.addIncludeFilter(annotationTypeFilter);

	String packageName = ClassUtils.getPackageName(importingClassMetadata.getClassName());
	Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(packageName);
	candidateComponents.forEach(beanDefinition -> {
		AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
		AnnotationMetadata annotationMetadata = annotatedBeanDefinition.getMetadata();
		BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(NetworkFactory.class);
		String className = annotationMetadata.getClassName();
		definition.addPropertyValue(NetworkFactoryConstants.PROPERTY_VALUE.getValue(), className);
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

		AbstractBeanDefinition abstractBeanDefinition = definition.getBeanDefinition();
		BeanDefinitionHolder holder = new BeanDefinitionHolder(abstractBeanDefinition, className);
		BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);

	});

}
 
Example 5
Source File: ControllerClassNameHandlerMapping.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build a path prefix for the given controller bean class.
 * @param beanClass the controller bean class to generate a mapping for
 * @return the path prefix, potentially including subpackage names as path elements
 */
private StringBuilder buildPathPrefix(Class<?> beanClass) {
	StringBuilder pathMapping = new StringBuilder();
	if (this.pathPrefix != null) {
		pathMapping.append(this.pathPrefix);
		pathMapping.append("/");
	}
	else {
		pathMapping.append("/");
	}
	if (this.basePackage != null) {
		String packageName = ClassUtils.getPackageName(beanClass);
		if (packageName.startsWith(this.basePackage)) {
			String subPackage = packageName.substring(this.basePackage.length()).replace('.', '/');
			pathMapping.append(this.caseSensitive ? subPackage : subPackage.toLowerCase());
			pathMapping.append("/");
		}
	}
	return pathMapping;
}
 
Example 6
Source File: MetadataNamingStrategy.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the {@code ObjectName} from the source-level metadata associated
 * with the managed resource's {@code Class}.
 */
@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
	Class<?> managedClass = AopUtils.getTargetClass(managedBean);
	ManagedResource mr = this.attributeSource.getManagedResource(managedClass);

	// Check that an object name has been specified.
	if (mr != null && StringUtils.hasText(mr.getObjectName())) {
		return ObjectNameManager.getInstance(mr.getObjectName());
	}
	else {
		try {
			return ObjectNameManager.getInstance(beanKey);
		}
		catch (MalformedObjectNameException ex) {
			String domain = this.defaultDomain;
			if (domain == null) {
				domain = ClassUtils.getPackageName(managedClass);
			}
			Hashtable<String, String> properties = new Hashtable<String, String>();
			properties.put("type", ClassUtils.getShortName(managedClass));
			properties.put("name", beanKey);
			return ObjectNameManager.getInstance(domain, properties);
		}
	}
}
 
Example 7
Source File: MetadataNamingStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads the {@code ObjectName} from the source-level metadata associated
 * with the managed resource's {@code Class}.
 */
@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
	Class<?> managedClass = AopUtils.getTargetClass(managedBean);
	ManagedResource mr = this.attributeSource.getManagedResource(managedClass);

	// Check that an object name has been specified.
	if (mr != null && StringUtils.hasText(mr.getObjectName())) {
		return ObjectNameManager.getInstance(mr.getObjectName());
	}
	else {
		try {
			return ObjectNameManager.getInstance(beanKey);
		}
		catch (MalformedObjectNameException ex) {
			String domain = this.defaultDomain;
			if (domain == null) {
				domain = ClassUtils.getPackageName(managedClass);
			}
			Hashtable<String, String> properties = new Hashtable<String, String>();
			properties.put("type", ClassUtils.getShortName(managedClass));
			properties.put("name", beanKey);
			return ObjectNameManager.getInstance(domain, properties);
		}
	}
}
 
Example 8
Source File: IdentityNamingStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an instance of {@code ObjectName} based on the identity
 * of the managed resource.
 */
@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
	String domain = ClassUtils.getPackageName(managedBean.getClass());
	Hashtable<String, String> keys = new Hashtable<String, String>();
	keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean.getClass()));
	keys.put(HASH_CODE_KEY, ObjectUtils.getIdentityHexString(managedBean));
	return ObjectNameManager.getInstance(domain, keys);
}
 
Example 9
Source File: Log4j2CloudConfigLoggingSystem.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private String getBootPackagedConfigFile(String fileName) {
    String defaultPath = ClassUtils.getPackageName(Log4J2LoggingSystem.class);
    defaultPath = defaultPath.replace('.', '/');
    defaultPath = defaultPath + "/" + fileName;
    defaultPath = "classpath:" + defaultPath;
    return defaultPath;
}
 
Example 10
Source File: GradleTaskContainer.java    From initializr with Apache License 2.0 5 votes vote down vote up
/**
 * Customize a task matching a given type. If the task has already been customized,
 * the consumer can be used to further tune the existing task.
 * @param type the name of type. Can use the short form for well-known types such as
 * {@code JavaCompile}, use a fully qualified name if an import is required
 * @param task a callback to customize the task
 */
public void customizeWithType(String type, Consumer<Builder> task) {
	String packageName = ClassUtils.getPackageName(type);
	if (!StringUtils.isEmpty(packageName)) {
		this.importedTypes.add(type);
	}
	String shortName = ClassUtils.getShortName(type);
	task.accept(this.tasks.computeIfAbsent(shortName, (name) -> new Builder(name, type)));
}
 
Example 11
Source File: IdentityNamingStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an instance of {@code ObjectName} based on the identity
 * of the managed resource.
 */
@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
	String domain = ClassUtils.getPackageName(managedBean.getClass());
	Hashtable<String, String> keys = new Hashtable<String, String>();
	keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean.getClass()));
	keys.put(HASH_CODE_KEY, ObjectUtils.getIdentityHexString(managedBean));
	return ObjectNameManager.getInstance(domain, keys);
}
 
Example 12
Source File: IdentityNamingStrategy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Returns an instance of {@code ObjectName} based on the identity
 * of the managed resource.
 */
@Override
public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException {
	String domain = ClassUtils.getPackageName(managedBean.getClass());
	Hashtable<String, String> keys = new Hashtable<>();
	keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean.getClass()));
	keys.put(HASH_CODE_KEY, ObjectUtils.getIdentityHexString(managedBean));
	return ObjectNameManager.getInstance(domain, keys);
}
 
Example 13
Source File: MetadataNamingStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Reads the {@code ObjectName} from the source-level metadata associated
 * with the managed resource's {@code Class}.
 */
@Override
public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException {
	Assert.state(this.attributeSource != null, "No JmxAttributeSource set");
	Class<?> managedClass = AopUtils.getTargetClass(managedBean);
	ManagedResource mr = this.attributeSource.getManagedResource(managedClass);

	// Check that an object name has been specified.
	if (mr != null && StringUtils.hasText(mr.getObjectName())) {
		return ObjectNameManager.getInstance(mr.getObjectName());
	}
	else {
		Assert.state(beanKey != null, "No ManagedResource attribute and no bean key specified");
		try {
			return ObjectNameManager.getInstance(beanKey);
		}
		catch (MalformedObjectNameException ex) {
			String domain = this.defaultDomain;
			if (domain == null) {
				domain = ClassUtils.getPackageName(managedClass);
			}
			Hashtable<String, String> properties = new Hashtable<>();
			properties.put("type", ClassUtils.getShortName(managedClass));
			properties.put("name", beanKey);
			return ObjectNameManager.getInstance(domain, properties);
		}
	}
}
 
Example 14
Source File: IdentityNamingStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Returns an instance of {@code ObjectName} based on the identity
 * of the managed resource.
 */
@Override
public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException {
	String domain = ClassUtils.getPackageName(managedBean.getClass());
	Hashtable<String, String> keys = new Hashtable<>();
	keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean.getClass()));
	keys.put(HASH_CODE_KEY, ObjectUtils.getIdentityHexString(managedBean));
	return ObjectNameManager.getInstance(domain, keys);
}
 
Example 15
Source File: MetadataNamingStrategy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Reads the {@code ObjectName} from the source-level metadata associated
 * with the managed resource's {@code Class}.
 */
@Override
public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException {
	Assert.state(this.attributeSource != null, "No JmxAttributeSource set");
	Class<?> managedClass = AopUtils.getTargetClass(managedBean);
	ManagedResource mr = this.attributeSource.getManagedResource(managedClass);

	// Check that an object name has been specified.
	if (mr != null && StringUtils.hasText(mr.getObjectName())) {
		return ObjectNameManager.getInstance(mr.getObjectName());
	}
	else {
		Assert.state(beanKey != null, "No ManagedResource attribute and no bean key specified");
		try {
			return ObjectNameManager.getInstance(beanKey);
		}
		catch (MalformedObjectNameException ex) {
			String domain = this.defaultDomain;
			if (domain == null) {
				domain = ClassUtils.getPackageName(managedClass);
			}
			Hashtable<String, String> properties = new Hashtable<>();
			properties.put("type", ClassUtils.getShortName(managedClass));
			properties.put("name", beanKey);
			return ObjectNameManager.getInstance(domain, properties);
		}
	}
}
 
Example 16
Source File: CandidateComponentsIndex.java    From java-technology-stack with MIT License 4 votes vote down vote up
Entry(String type) {
	this.type = type;
	this.packageName = ClassUtils.getPackageName(type);
}
 
Example 17
Source File: CandidateComponentsIndex.java    From spring-analysis-note with MIT License 4 votes vote down vote up
Entry(String type) {
	this.type = type;
	this.packageName = ClassUtils.getPackageName(type);
}