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

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

Introduction

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

Prototype

@Override
    public MetadataReader getMetadataReader(Resource resource) throws IOException 

Source Link

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  v  a 2  s.  com

    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.  ja  v a  2 s  .  co  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: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 {/*from  w  ww  .j av a  2s.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.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.  j  a va 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 {/*from w w  w  .ja  va  2s  .  co  m*/
        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 {//  www. j ava2  s  .  c  o m
        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.libreplan.web.common.entrypoints.RedirectorSynthetiser.java

private void addIfSuitable(List<Class<?>> accumulatedResult, CachingMetadataReaderFactory metadataReaderFactory,
        Resource resource) {//from w  w  w.j  a  v  a2  s.com
    try {
        if (resource.isReadable()) {
            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            ClassMetadata classMetadata = metadataReader.getClassMetadata();

            if (classMetadata.isInterface()
                    && annotationMetadata.getAnnotationTypes().contains(EntryPoints.class.getName())) {

                Class<?> klass = Class.forName(classMetadata.getClassName());
                if (klass.isInterface()) {
                    accumulatedResult.add(klass);
                }
            }
        }
    } catch (Exception e) {
        LOG.warn("exception processing " + resource, e);
    }
}