Skip to content

Commit 598ffab

Browse files
authored
Merge pull request #1199 from rainboyan
* pr/1199: Refactor GrailsApplication Closes gh-1199
2 parents e166042 + b903934 commit 598ffab

8 files changed

Lines changed: 30 additions & 60 deletions

File tree

grace-api/src/main/groovy/grails/core/GrailsApplication.java

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004-2022 the original author or authors.
2+
* Copyright 2004-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import org.springframework.core.io.Resource;
2121

2222
import grails.config.Config;
23-
import grails.util.Metadata;
2423

2524
import org.grails.datastore.mapping.model.MappingContext;
2625

@@ -29,19 +28,19 @@
2928
* main purpose is to provide a mechanism for analysing the conventions within a Grails
3029
* application as well as providing metadata and information about the execution environment.
3130
*
32-
* <p>The GrailsApplication interface interfacts with {@link ArtefactHandler} instances
31+
* <p>The GrailsApplication interface interacts with {@link ArtefactHandler} instances
3332
* which are capable of analysing different artefact types (controllers, domain classes etc.) and introspecting
3433
* the artefact conventions
3534
*
36-
* <p>Implementors of this inteface should be aware that a GrailsApplication is only initialised when the initialise() method
35+
* <p>Implementors of this interface should be aware that a GrailsApplication is only initialised when the initialise() method
3736
* is called. In other words GrailsApplication instances are lazily initialised by the Grails runtime.
3837
*
3938
* @see #initialise()
4039
* @see ArtefactHandler
4140
*
4241
* @author Graeme Rocher
4342
* @author Steven Devijver
44-
*
43+
* @author Michael Yan
4544
* @since 0.1
4645
*/
4746
public interface GrailsApplication extends ApplicationContextAware {
@@ -51,24 +50,6 @@ public interface GrailsApplication extends ApplicationContextAware {
5150
*/
5251
String APPLICATION_ID = "grailsApplication";
5352

54-
/**
55-
* The name of the class that provides configuration
56-
*/
57-
@Deprecated
58-
String CONFIG_CLASS = "Config";
59-
60-
/**
61-
* The name of the DataSource class
62-
*/
63-
@Deprecated
64-
String DATA_SOURCE_CLASS = "DataSource";
65-
66-
/**
67-
* The name of the project metadata file
68-
*/
69-
@Deprecated
70-
String PROJECT_META_FILE = "application.properties";
71-
7253
/**
7354
* The name of the transaction manager bean
7455
*/
@@ -118,7 +99,7 @@ public interface GrailsApplication extends ApplicationContextAware {
11899

119100
/**
120101
* Returns the Spring context for this application. Note that this
121-
* will return <code>null</code> until the application is fully
102+
* will return {@code null} until the application is fully
122103
* initialised. This context contains all the application artifacts,
123104
* plugin beans, the works.
124105
*/
@@ -149,7 +130,7 @@ public interface GrailsApplication extends ApplicationContextAware {
149130
ApplicationContext getParentContext();
150131

151132
/**
152-
* Retrieves a class for the given name within the GrailsApplication or returns null
133+
* Retrieves a class for the given name within the GrailsApplication or returns {@code null}
153134
*
154135
* @param className The name of the class
155136
* @return The class or null
@@ -164,7 +145,7 @@ public interface GrailsApplication extends ApplicationContextAware {
164145
/**
165146
* Rebuilds this Application throwing away the class loader and re-constructing it from the loaded
166147
* resources again. Can only be called in development mode and an error will be thrown if called
167-
* in a different enivronment
148+
* in a different environment
168149
*/
169150
void rebuild();
170151

@@ -205,7 +186,7 @@ public interface GrailsApplication extends ApplicationContextAware {
205186

206187
/**
207188
* <p>Gets the GrailsClass associated with the named artefact class</p>
208-
* <p>i.e. to get the GrailsClass for controller called "BookController" you pass the name "BookController"</p>
189+
* <p>i.e. to get the GrailsClass for controller called "BookController" you pass the name "BookController"</p>
209190
* @param artefactType The type of artefact to retrieve, i.e. "Controller"
210191
* @param name The name of an artefact such as "BookController"
211192
* @return The associated GrailsClass or null
@@ -295,17 +276,9 @@ public interface GrailsApplication extends ApplicationContextAware {
295276
*/
296277
boolean isInitialised();
297278

298-
/**
299-
* <p>Get access to the project's metadata, specified in application.yml and grails.build.info if it is present</p>
300-
* <p>This provides access to information like required grails version, application name, version etc
301-
* but <b>NOT</b> general application settings.</p>
302-
* @return A read-only Map of data about the application, not environment specific
303-
*/
304-
Metadata getMetadata();
305-
306279
/**
307280
* Retrieves an artefact by its logical property name. For example the logical property name of
308-
* BookController would be book.
281+
* BookController would be 'book'.
309282
* @param type The artefact type
310283
* @param logicalName The logical name
311284
* @return The GrailsClass or null if it doesn't exist
@@ -326,13 +299,13 @@ public interface GrailsApplication extends ApplicationContextAware {
326299
boolean isWarDeployed();
327300

328301
/**
329-
* Adds an artefact that can be overriden by user defined classes
302+
* Adds an artefact that can be overridden by user defined classes
330303
* @param artefact An overridable artefact
331304
*/
332305
void addOverridableArtefact(Class<?> artefact);
333306

334307
/**
335-
* Fired to inform the application when the Config.groovy file changes.
308+
* Fired to inform the application when the config files changed.
336309
*/
337310
void configChanged();
338311

grace-core/src/main/groovy/grails/core/DefaultGrailsApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public Class<?> getClassForName(String className) {
366366
}
367367

368368
/**
369-
* Refreshes this GrailsApplication, rebuilding all of the artefact definitions as
369+
* Refreshes this GrailsApplication, rebuilding all the artefact definitions as
370370
* defined by the registered ArtefactHandler instances.
371371
*/
372372
public void refresh() {
@@ -473,7 +473,7 @@ protected GrailsClass getFirstArtefact(String artefactType) {
473473
}
474474

475475
/**
476-
* Returns all of the GrailsClass instances for the given artefactType as defined by the ArtefactHandler
476+
* Returns all the GrailsClass instances for the given artefactType as defined by the ArtefactHandler
477477
*
478478
* @param artefactType The type of the artefact defined by the ArtefactHandler
479479
* @return An array of classes for the given artefact
@@ -525,7 +525,7 @@ public GrailsClass addArtefact(String artefactType, GrailsClass artefactGrailsCl
525525
* Registers a new ArtefactHandler that is responsible for identifying and managing a
526526
* particular artefact type that is defined by some convention.
527527
*
528-
* @param handler The ArtefactHandler to regster
528+
* @param handler The ArtefactHandler to register
529529
*/
530530
public void registerArtefactHandler(ArtefactHandler handler) {
531531
this.artefactHandlersByName.put(handler.getType(), handler);

grace-core/src/main/groovy/org/grails/core/AbstractGrailsApplication.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@
3434
import grails.core.support.GrailsConfigurationAware;
3535
import grails.util.Environment;
3636
import grails.util.Holders;
37-
import grails.util.Metadata;
3837

3938
import org.grails.config.PropertySourcesConfig;
4039

@@ -47,8 +46,6 @@ public abstract class AbstractGrailsApplication extends GroovyObjectSupport
4746

4847
protected ApplicationContext parentContext;
4948

50-
protected final Metadata applicationMeta = Metadata.getCurrent();
51-
5249
protected boolean contextInitialized;
5350

5451
@Override
@@ -59,11 +56,6 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
5956
}
6057
}
6158

62-
@Override
63-
public Metadata getMetadata() {
64-
return this.applicationMeta;
65-
}
66-
6759
@Override
6860
public boolean isWarDeployed() {
6961
return Environment.isWarDeployed();

grace-plugin-api/src/main/groovy/grails/plugins/DefaultGrailsPluginManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import grails.plugins.exceptions.PluginException;
5454
import grails.util.Environment;
5555
import grails.util.GrailsClassUtils;
56+
import grails.util.Metadata;
5657

5758
import org.grails.core.exceptions.GrailsConfigurationException;
5859
import org.grails.io.support.GrailsResourceUtils;
@@ -453,7 +454,7 @@ private boolean isCompatiblePlugin(GrailsPlugin plugin) {
453454
return true;
454455
}
455456

456-
String appGrailsVersion = this.application.getMetadata().getGrailsVersion();
457+
String appGrailsVersion = Metadata.getCurrent().getGrailsVersion();
457458
String pluginMinGrailsVersion = GrailsVersionUtils.getLowerVersion(pluginGrailsVersion);
458459
String pluginMaxGrailsVersion = GrailsVersionUtils.getUpperVersion(pluginGrailsVersion);
459460

grace-plugin-api/src/main/groovy/org/grails/plugins/AbstractGrailsPluginManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004-2024 the original author or authors.
2+
* Copyright 2004-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,6 +68,7 @@
6868
import grails.util.BuildSettings;
6969
import grails.util.Environment;
7070
import grails.util.GrailsNameUtils;
71+
import grails.util.Metadata;
7172

7273
import org.grails.config.NavigableMap;
7374
import org.grails.io.support.GrailsResourceUtils;
@@ -627,8 +628,8 @@ static ConfigSlurper getConfigSlurper(GrailsApplication application) {
627628
binding.put(CONFIG_BINDING_USER_HOME, System.getProperty("user.home"));
628629
binding.put(CONFIG_BINDING_GRAILS_HOME, System.getProperty("grails.home"));
629630
if (application != null) {
630-
binding.put(CONFIG_BINDING_APP_NAME, application.getMetadata().getApplicationName());
631-
binding.put(CONFIG_BINDING_APP_VERSION, application.getMetadata().getApplicationVersion());
631+
binding.put(CONFIG_BINDING_APP_NAME, Metadata.getCurrent().getApplicationName());
632+
binding.put(CONFIG_BINDING_APP_VERSION, Metadata.getCurrent().getApplicationVersion());
632633
binding.put(GrailsApplication.APPLICATION_ID, application);
633634
}
634635
configSlurper.setBinding(binding);

grace-plugin-api/src/test/groovy/grails/plugins/DefaultGrailsPluginManagerSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class DefaultGrailsPluginManagerSpec extends Specification {
5555

5656
def stubGrailsApplicationWithVersion(def version) {
5757
GrailsApplication app = Mock(GrailsApplication)
58-
app.getMetadata() >> Metadata.getInstance(new ByteArrayInputStream("""
58+
Metadata.getCurrent() >> Metadata.getInstance(new ByteArrayInputStream("""
5959
info:
6060
app:
6161
grailsVersion: $version

grace-plugin-datasource/src/main/groovy/org/grails/plugins/datasource/TomcatJDBCPoolMBeanExporter.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004-2022 the original author or authors.
2+
* Copyright 2004-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import org.springframework.jmx.export.MBeanExporter
2727
import org.springframework.jmx.support.RegistrationPolicy
2828

2929
import grails.core.GrailsApplication
30+
import grails.util.Metadata
3031

3132
@CompileStatic
3233
class TomcatJDBCPoolMBeanExporter extends MBeanExporter {
@@ -72,7 +73,7 @@ class TomcatJDBCPoolMBeanExporter extends MBeanExporter {
7273
throws MalformedObjectNameException {
7374
Hashtable<String, String> properties = new Hashtable<String, String>()
7475
properties.type = 'ConnectionPool'
75-
properties.application = ((grailsApplication?.getMetadata()?.getApplicationName()) ?: 'grailsApplication')
76+
properties.application = ((Metadata.getCurrent().getApplicationName()) ?: 'grailsApplication')
7677
.replaceAll(/[,=;:]/, '_')
7778
String poolName = dataSource.pool.poolProperties.name
7879

grace-web/src/main/groovy/grails/web/servlet/context/support/GrailsEnvironment.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2022 the original author or authors.
2+
* Copyright 2011-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,13 @@
2424

2525
import grails.core.GrailsApplication;
2626
import grails.util.Environment;
27+
import grails.util.Metadata;
2728

2829
/**
2930
* Bridges Grails' existing environment API with the new Spring 3.1 environment profiles API.
3031
*
3132
* @author Graeme Rocher
33+
* @author Michael Yan
3234
* @since 2.0
3335
*/
3436
public class GrailsEnvironment extends StandardServletEnvironment {
@@ -51,8 +53,8 @@ protected Set<String> doGetActiveProfiles() {
5153
protected class GrailsConfigPropertySource extends PropertySource<GrailsApplication> {
5254

5355
public GrailsConfigPropertySource() {
54-
super(StringUtils.hasText(GrailsEnvironment.this.grailsApplication.getMetadata().getApplicationName())
55-
? GrailsEnvironment.this.grailsApplication.getMetadata().getApplicationName()
56+
super(StringUtils.hasText(Metadata.getCurrent().getApplicationName())
57+
? Metadata.getCurrent().getApplicationName()
5658
: "grailsApplication", GrailsEnvironment.this.grailsApplication);
5759
}
5860

0 commit comments

Comments
 (0)