Example usage for org.springframework.core.type.classreading CachingMetadataReaderFactory CachingMetadataReaderFactory

List of usage examples for org.springframework.core.type.classreading CachingMetadataReaderFactory CachingMetadataReaderFactory

Introduction

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

Prototype

public CachingMetadataReaderFactory() 

Source Link

Document

Create a new CachingMetadataReaderFactory for the default class loader, using a local resource cache.

Usage

From source file:org.syncope.hibernate.HibernateEnhancer.java

public static void main(final String[] args) throws Exception {

    if (args.length != 1) {
        throw new IllegalArgumentException("Expecting classpath as single argument");
    }/*from   w  w w .ja va  2 s  .c  om*/

    ClassPool classPool = ClassPool.getDefault();
    classPool.appendClassPath(args[0]);

    PathMatchingResourcePatternResolver resResolver = new PathMatchingResourcePatternResolver(
            classPool.getClassLoader());
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    for (Resource resource : resResolver.getResources("classpath*:org/syncope/core/**/*.class")) {

        MetadataReader metadataReader = cachingMetadataReaderFactory.getMetadataReader(resource);
        if (metadataReader.getAnnotationMetadata().isAnnotated(Entity.class.getName())) {

            Class entity = Class.forName(metadataReader.getClassMetadata().getClassName());
            classPool.appendClassPath(new ClassClassPath(entity));
            CtClass ctClass = ClassPool.getDefault().get(entity.getName());
            if (ctClass.isFrozen()) {
                ctClass.defrost();
            }
            ClassFile classFile = ctClass.getClassFile();
            ConstPool constPool = classFile.getConstPool();

            for (Field field : entity.getDeclaredFields()) {
                if (field.isAnnotationPresent(Lob.class)) {
                    AnnotationsAttribute typeAttr = new AnnotationsAttribute(constPool,
                            AnnotationsAttribute.visibleTag);
                    Annotation typeAnnot = new Annotation("org.hibernate.annotations.Type", constPool);
                    typeAnnot.addMemberValue("type",
                            new StringMemberValue("org.hibernate.type.StringClobType", constPool));
                    typeAttr.addAnnotation(typeAnnot);

                    CtField lobField = ctClass.getDeclaredField(field.getName());
                    lobField.getFieldInfo().addAttribute(typeAttr);
                }
            }

            ctClass.writeFile(args[0]);
        }
    }
}

From source file:org.syncope.core.util.SpringPersistenceUnitPostProcessor.java

@Override
public void postProcessPersistenceUnitInfo(final MutablePersistenceUnitInfo mpui) {

    if (locations.length == 0) {
        LOG.warn("No locations provided");
    }// w w  w .  j a va 2s  .c o  m

    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    try {
        for (String location : locations) {
            for (Resource resource : resResolver.getResources(location)) {
                MetadataReader metadataReader = cachingMetadataReaderFactory.getMetadataReader(resource);
                if (metadataReader.getAnnotationMetadata().isAnnotated(Entity.class.getName())) {

                    mpui.addManagedClassName(metadataReader.getClassMetadata().getClassName());
                }
            }
        }
        mpui.setExcludeUnlistedClasses(true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.crudetech.junit.categories.Categories.java

static Class<?>[] allTestClassesInClassPathMatchingPattern(String pattern) throws InitializationError {
    List<Class<?>> classes = new ArrayList<Class<?>>();

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metaDataReaderFactory = new CachingMetadataReaderFactory();
    try {/*w  w  w.ja v  a  2 s  . c  o  m*/
        String classPattern = "classpath:" + pattern.replace('.', '/') + ".class";
        Resource[] res = resolver.getResources(classPattern);
        for (Resource r : res) {
            if (!r.isReadable()) {
                continue;
            }
            MetadataReader reader = metaDataReaderFactory.getMetadataReader(r);
            Class<?> c = Class.forName(reader.getClassMetadata().getClassName());

            if (Modifier.isAbstract(c.getModifiers())) {
                continue;
            }
            classes.add(c);
        }
        return classes.toArray(new Class<?>[classes.size()]);
    } catch (Exception e) {
        throw new InitializationError(e);
    }
}

From source file:org.syncope.core.rest.controller.ConfigurationController.java

@PreAuthorize("hasRole('CONFIGURATION_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/validators")
public ModelAndView getValidators() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> validators = new HashSet<String>();
    try {//  w  w  w  .  j  a  v  a 2  s.c  om
        for (Resource resource : resResolver
                .getResources("classpath:org/syncope/core/persistence/validation/" + "attrvalue/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), Validator.class.getName())
                    || AbstractValidator.class.getName().equals(metadata.getSuperClassName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers())) {
                        validators.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Validator.class.getName(), e);
    }

    return new ModelAndView().addObject(validators);
}

From source file:org.syncope.core.rest.controller.ConfigurationController.java

@PreAuthorize("hasRole('CONFIGURATION_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/mailTemplates")
public ModelAndView getMailTemplates() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> htmlTemplates = new HashSet<String>();
    Set<String> textTemplates = new HashSet<String>();

    try {//from  w  w  w  .  j  a  va 2  s .  co  m
        for (Resource resource : resResolver.getResources("classpath:/mailTemplates/*.vm")) {

            String template = resource.getURL().toExternalForm();
            if (template.endsWith(".html.vm")) {
                htmlTemplates.add(template.substring(template.indexOf("mailTemplates/") + 14,
                        template.indexOf(".html.vm")));
            } else if (template.endsWith(".txt.vm")) {
                textTemplates.add(template.substring(template.indexOf("mailTemplates/") + 14,
                        template.indexOf(".txt.vm")));
            } else {
                LOG.warn("Unexpected template found: {}, ignoring...", template);
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Validator.class.getName(), e);
    }

    // Only templates available both as HTML and TEXT are considered
    htmlTemplates.retainAll(textTemplates);

    return new ModelAndView().addObject(htmlTemplates);
}

From source file:org.syncope.core.rest.controller.ReportController.java

@PreAuthorize("hasRole('REPORT_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/reportletClasses")
public ModelAndView getReportletClasses() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> reportletClasses = new HashSet<String>();
    try {/*from w w  w . ja v  a  2  s.c  o  m*/
        for (Resource resource : resResolver.getResources("classpath*:**/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), Reportlet.class.getName())
                    || AbstractReportlet.class.getName().equals(metadata.getSuperClassName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers())) {

                        reportletClasses.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Reportlet.class.getName(), e);
    }

    ModelAndView result = new ModelAndView();
    result.addObject(reportletClasses);
    return result;
}

From source file:org.syncope.core.rest.controller.TaskController.java

@PreAuthorize("hasRole('TASK_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/jobClasses")
public ModelAndView getJobClasses() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> jobClasses = new HashSet<String>();
    try {// www.  jav  a2s.c  om
        for (Resource resource : resResolver.getResources("classpath*:**/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), Job.class.getName())
                    || AbstractTaskJob.class.getName().equals(metadata.getSuperClassName())
                    || ArrayUtils.contains(metadata.getInterfaceNames(), StatefulJob.class.getName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers()) && !metadata.hasEnclosingClass()
                            && !jobClass.equals(SyncJob.class) && !jobClass.equals(ReportJob.class)
                            && !jobClass.equals(NotificationJob.class)) {

                        jobClasses.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Job.class.getName(), e);
    }

    ModelAndView result = new ModelAndView();
    result.addObject(jobClasses);
    return result;
}

From source file:org.syncope.core.rest.controller.TaskController.java

@PreAuthorize("hasRole('TASK_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/jobActionsClasses")
public ModelAndView getJobActionClasses() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> jobActionsClasses = new HashSet<String>();
    try {//  w  w w .ja v a  2 s . c  om
        for (Resource resource : resResolver.getResources("classpath*:**/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), SyncJobActions.class.getName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers())) {
                        jobActionsClasses.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", SyncJobActions.class.getName(), e);
    }

    ModelAndView result = new ModelAndView();
    result.addObject(jobActionsClasses);
    return result;
}

From source file:org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.java

/**
 * Return the MetadataReaderFactory used by this component provider.
 *//*from  w  ww. ja va2  s. c o  m*/
public final MetadataReaderFactory getMetadataReaderFactory() {
    if (this.metadataReaderFactory == null) {
        this.metadataReaderFactory = new CachingMetadataReaderFactory();
    }
    return this.metadataReaderFactory;
}