Search in sources :

Example 1 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class TagLibArtefactHandler method initialize.

/**
     * Creates a map of tags (keyed on "${namespace}:${tagName}") to tag libraries.
     */
@Override
public void initialize(ArtefactInfo artefacts) {
    tag2libMap = new HashMap<String, GrailsTagLibClass>();
    for (GrailsClass aClass : artefacts.getGrailsClasses()) {
        GrailsTagLibClass taglibClass = (GrailsTagLibClass) aClass;
        String namespace = taglibClass.getNamespace();
        namespace2tagLibMap.put(namespace, taglibClass);
        for (Object o : taglibClass.getTagNames()) {
            String tagName = namespace + ":" + o;
            if (!tag2libMap.containsKey(tagName)) {
                tag2libMap.put(tagName, taglibClass);
            } else {
                GrailsTagLibClass current = tag2libMap.get(tagName);
                if (!taglibClass.equals(current)) {
                    LogFactory.getLog(TagLibArtefactHandler.class).info("There are conflicting tags: " + taglibClass.getFullName() + "." + tagName + " vs. " + current.getFullName() + "." + tagName + ". The former will take precedence.");
                    tag2libMap.put(tagName, taglibClass);
                }
            }
        }
    }
}
Also used : GrailsClass(grails.core.GrailsClass) GrailsTagLibClass(grails.core.GrailsTagLibClass) DefaultGrailsTagLibClass(org.grails.core.DefaultGrailsTagLibClass)

Example 2 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GrailsDomainConfigurationUtil method configureDomainClassRelationships.

/**
     * Configures the relationships between domain classes after they have been all loaded.
     *
     * @param domainClasses The domain classes to configure relationships for
     * @param domainMap     The domain class map
     */
public static void configureDomainClassRelationships(GrailsClass[] domainClasses, Map<?, ?> domainMap) {
    // and configure how domain class properties reference each other
    for (GrailsClass grailsClass : domainClasses) {
        GrailsDomainClass domainClass = (GrailsDomainClass) grailsClass;
        if (!domainClass.isRoot()) {
            Class<?> superClass = grailsClass.getClazz().getSuperclass();
            while (!superClass.equals(Object.class) && !superClass.equals(GroovyObject.class)) {
                GrailsDomainClass gdc = (GrailsDomainClass) domainMap.get(superClass.getName());
                if (gdc == null || gdc.getSubClasses() == null) {
                    break;
                }
                gdc.getSubClasses().add((GrailsDomainClass) grailsClass);
                superClass = superClass.getSuperclass();
            }
        }
        GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
        for (GrailsDomainClassProperty prop : props) {
            if (prop != null && prop.isAssociation()) {
                GrailsDomainClass referencedGrailsDomainClass = (GrailsDomainClass) domainMap.get(prop.getReferencedPropertyType().getName());
                prop.setReferencedDomainClass(referencedGrailsDomainClass);
            }
        }
    }
    // now configure so that the 'other side' of a property can be resolved by the property itself
    for (GrailsClass domainClass1 : domainClasses) {
        GrailsDomainClass domainClass = (GrailsDomainClass) domainClass1;
        GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
        for (GrailsDomainClassProperty prop : props) {
            if (prop == null || !prop.isAssociation()) {
                continue;
            }
            GrailsDomainClass referenced = prop.getReferencedDomainClass();
            if (referenced == null) {
                continue;
            }
            boolean isOwnedBy = referenced.isOwningClass(domainClass.getClazz());
            prop.setOwningSide(isOwnedBy);
            String refPropertyName = null;
            try {
                refPropertyName = prop.getReferencedPropertyName();
            } catch (UnsupportedOperationException e) {
            // ignore (to support Hibernate entities)
            }
            if (!StringUtils.hasText(refPropertyName)) {
                GrailsDomainClassProperty[] referencedProperties = referenced.getPersistentProperties();
                for (GrailsDomainClassProperty referencedProp : referencedProperties) {
                    // to be equal to self
                    if (prop.equals(referencedProp) && prop.isBidirectional()) {
                        continue;
                    }
                    if (isCandidateForOtherSide(domainClass, prop, referencedProp)) {
                        prop.setOtherSide(referencedProp);
                        break;
                    }
                }
            } else {
                GrailsDomainClassProperty otherSide = referenced.getPropertyByName(refPropertyName);
                prop.setOtherSide(otherSide);
                otherSide.setOtherSide(prop);
            }
        }
    }
}
Also used : GrailsClass(grails.core.GrailsClass) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty)

Example 3 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GrailsConfigUtils method executeGrailsBootstraps.

/**
     * Executes Grails bootstrap classes
     *
     * @param application The Grails ApplicationContext instance
     * @param webContext The WebApplicationContext instance
     * @param servletContext The ServletContext instance
     */
public static void executeGrailsBootstraps(GrailsApplication application, WebApplicationContext webContext, ServletContext servletContext) {
    PersistenceContextInterceptor interceptor = null;
    String[] beanNames = webContext.getBeanNamesForType(PersistenceContextInterceptor.class);
    if (beanNames.length > 0) {
        interceptor = (PersistenceContextInterceptor) webContext.getBean(beanNames[0]);
    }
    if (interceptor != null) {
        interceptor.init();
    }
    // init the Grails application
    try {
        GrailsClass[] bootstraps = application.getArtefacts(BootstrapArtefactHandler.TYPE);
        for (GrailsClass bootstrap : bootstraps) {
            final GrailsBootstrapClass bootstrapClass = (GrailsBootstrapClass) bootstrap;
            final Object instance = bootstrapClass.getReferenceInstance();
            webContext.getAutowireCapableBeanFactory().autowireBeanProperties(instance, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
            bootstrapClass.callInit(servletContext);
        }
        if (interceptor != null) {
            interceptor.flush();
        }
    } finally {
        if (interceptor != null) {
            interceptor.destroy();
        }
    }
}
Also used : GrailsClass(grails.core.GrailsClass) PersistenceContextInterceptor(grails.persistence.support.PersistenceContextInterceptor) GrailsBootstrapClass(grails.web.servlet.bootstrap.GrailsBootstrapClass)

Example 4 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GroovyPagesTemplateEngine method createDomainClassMap.

/**
     * The domainClassMap is used in GSP binding to "auto-import" domain classes in packages without package prefix.
     * real imports aren't used, instead each class is added to the binding
     *
     * This feature has existed earlier, the code has just been refactored and moved to GroovyPagesTemplateEngine
     * to prevent using the static cache that was used previously.
     */
private Map<String, Class<?>> createDomainClassMap() {
    Map<String, Class<?>> domainsWithoutPackage = new HashMap<String, Class<?>>();
    if (grailsApplication != null) {
        GrailsClass[] domainClasses = grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE);
        for (GrailsClass domainClass : domainClasses) {
            final Class<?> theClass = domainClass.getClazz();
            domainsWithoutPackage.put(theClass.getName(), theClass);
        }
    }
    return domainsWithoutPackage;
}
Also used : GrailsClass(grails.core.GrailsClass) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GrailsClass(grails.core.GrailsClass)

Example 5 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class DefaultCodecLookup method registerCodecs.

protected void registerCodecs() {
    List<GrailsClass> codecs = Arrays.asList(grailsApplication.getArtefacts(CodecArtefactHandler.TYPE));
    Collections.sort(codecs, OrderComparator.INSTANCE);
    Collections.reverse(codecs);
    for (GrailsClass grailsClass : codecs) {
        registerCodec((GrailsCodecClass) grailsClass);
    }
}
Also used : GrailsClass(grails.core.GrailsClass)

Aggregations

GrailsClass (grails.core.GrailsClass)12 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 AbstractGrailsClass (org.grails.core.AbstractGrailsClass)3 Test (org.junit.jupiter.api.Test)3 PersistenceContextInterceptor (grails.persistence.support.PersistenceContextInterceptor)2 GrailsBootstrapClass (grails.web.servlet.bootstrap.GrailsBootstrapClass)2 Config (grails.config.Config)1 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)1 GrailsApplication (grails.core.GrailsApplication)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 GrailsDomainClass (grails.core.GrailsDomainClass)1 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)1 GrailsTagLibClass (grails.core.GrailsTagLibClass)1 GrailsUrlMappingsClass (grails.core.GrailsUrlMappingsClass)1 InjectableGrailsClass (grails.core.InjectableGrailsClass)1 ArtefactAdditionEvent (grails.core.events.ArtefactAdditionEvent)1 UrlConverter (grails.web.UrlConverter)1 UrlMapping (grails.web.mapping.UrlMapping)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1