Example usage for org.springframework.core.type.classreading SimpleMetadataReaderFactory getMetadataReader

List of usage examples for org.springframework.core.type.classreading SimpleMetadataReaderFactory getMetadataReader

Introduction

In this page you can find the example usage for org.springframework.core.type.classreading SimpleMetadataReaderFactory getMetadataReader.

Prototype

@Override
    public MetadataReader getMetadataReader(Resource resource) throws IOException 

Source Link

Usage

From source file:org.wallride.tools.Hbm2ddl.java

public static void main(String[] args) throws Exception {
    String locationPattern = "classpath:/org/wallride/domain/*";

    final BootstrapServiceRegistry registry = new BootstrapServiceRegistryBuilder().build();
    final MetadataSources metadataSources = new MetadataSources(registry);
    final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(registry);

    registryBuilder.applySetting(AvailableSettings.DIALECT,
            ExtendedMySQL5InnoDBDialect.class.getCanonicalName());
    registryBuilder.applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, true);
    registryBuilder.applySetting(AvailableSettings.PHYSICAL_NAMING_STRATEGY,
            PhysicalNamingStrategySnakeCaseImpl.class);

    final PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    final Resource[] resources = resourcePatternResolver.getResources(locationPattern);
    final SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    for (Resource resource : resources) {
        MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
        AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
        if (metadata.hasAnnotation(Entity.class.getName())) {
            metadataSources.addAnnotatedClass(Class.forName(metadata.getClassName()));
        }/*www  .ja v  a 2  s .co m*/
    }

    final StandardServiceRegistryImpl registryImpl = (StandardServiceRegistryImpl) registryBuilder.build();
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(registryImpl);

    new SchemaExport().setHaltOnError(true).setDelimiter(";").create(EnumSet.of(TargetType.STDOUT),
            metadataBuilder.build());
}

From source file:com.sxj.jsonrpc.client.spring.AutoJsonRpcClientProxyCreator.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext);
    DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
    String resolvedPath = resolvePackageToScan();
    LOG.debug(format("Scanning '%s' for JSON-RPC service interfaces.", resolvedPath));
    try {//  w w  w  .ja va 2 s . co  m
        for (Resource resource : applicationContext.getResources(resolvedPath)) {
            if (resource.isReadable()) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
                String jsonRpcPathAnnotation = JsonRpcService.class.getName();
                if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) {
                    String className = classMetadata.getClassName();
                    String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation)
                            .get("value");
                    boolean useNamedParams = (Boolean) annotationMetadata
                            .getAnnotationAttributes(jsonRpcPathAnnotation).get("useNamedParams");
                    LOG.debug(format("Found JSON-RPC service to proxy [%s] on path '%s'.", className, path));
                    registerJsonProxyBean(dlbf, className, path, useNamedParams);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(format("Cannot scan package '%s' for classes.", resolvedPath), e);
    }
}

From source file:com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext);
    DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
    String resolvedPath = resolvePackageToScan();
    LOG.debug(format("Scanning '%s' for JSON-RPC service interfaces.", resolvedPath));
    try {/*from   w w w. j  a va 2 s.  co m*/
        for (Resource resource : applicationContext.getResources(resolvedPath)) {
            if (resource.isReadable()) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
                String jsonRpcPathAnnotation = JsonRpcService.class.getName();
                if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) {
                    String className = classMetadata.getClassName();
                    String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation)
                            .get("value");
                    boolean useNamedParams = (Boolean) annotationMetadata
                            .getAnnotationAttributes(jsonRpcPathAnnotation).get("useNamedParams");
                    LOG.debug(format("Found JSON-RPC service to proxy [%s] on path '%s'.", className, path));
                    registerJsonProxyBean(dlbf, className, path, useNamedParams);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(format("Cannot scan package '%s' for classes.", resolvedPath), e);
    }
}

From source file:org.codehaus.mojo.gwt.webxml.ServletAnnotationFinder.java

/**
 * @param packageName//from   ww  w.j  a va  2  s  .  c om
 * @return cannot return <code>null</null>
 * @throws IOException
 */
public Set<ServletDescriptor> findServlets(String packageName, String startPath, ClassLoader classLoader)
        throws IOException {
    Set<ServletDescriptor> servlets = new LinkedHashSet<ServletDescriptor>();
    PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver(
            classLoader);
    String patternFinder = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
            + ClassUtils.convertClassNameToResourcePath(packageName) + "/**/*.class";

    Resource[] resources = pathMatchingResourcePatternResolver.getResources(patternFinder);
    SimpleMetadataReaderFactory simpleMetadataReaderFactory = new SimpleMetadataReaderFactory();
    getLogger().debug("springresource " + resources.length + " for pattern " + patternFinder);
    for (Resource resource : resources) {
        getLogger().debug("springresource " + resource.getFilename());
        MetadataReader metadataReader = simpleMetadataReaderFactory.getMetadataReader(resource);

        if (metadataReader.getAnnotationMetadata().hasAnnotation(RemoteServiceRelativePath.class.getName())) {
            Map<String, Object> annotationAttributes = metadataReader.getAnnotationMetadata()
                    .getAnnotationAttributes(RemoteServiceRelativePath.class.getName());
            getLogger().debug("found RemoteServiceRelativePath annotation for class "
                    + metadataReader.getClassMetadata().getClassName());
            if (StringUtils.isNotBlank(startPath)) {
                StringBuilder path = new StringBuilder();
                if (!startPath.startsWith("/")) {
                    path.append('/');
                }
                path.append(startPath);
                String annotationPathValue = (String) annotationAttributes.get("value");
                if (!annotationPathValue.startsWith("/")) {
                    path.append('/');
                }
                path.append(annotationPathValue);
                ServletDescriptor servletDescriptor = new ServletDescriptor(path.toString(),
                        metadataReader.getClassMetadata().getClassName());
                servlets.add(servletDescriptor);
            } else {
                StringBuilder path = new StringBuilder();
                String annotationPathValue = (String) annotationAttributes.get("value");
                if (!annotationPathValue.startsWith("/")) {
                    path.append('/');
                }
                path.append(annotationPathValue);
                ServletDescriptor servletDescriptor = new ServletDescriptor(path.toString(),
                        metadataReader.getClassMetadata().getClassName());
                servlets.add(servletDescriptor);
            }
        }
    }
    return servlets;
}