-
Notifications
You must be signed in to change notification settings - Fork 951
/
DefaultGrailsApplication.java
890 lines (781 loc) · 35.2 KB
/
DefaultGrailsApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.commons;
import grails.util.GrailsUtil;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObjectSupport;
import groovy.util.ConfigObject;
import groovy.util.ConfigSlurper;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.grails.commons.spring.GrailsResourceHolder;
import org.codehaus.groovy.grails.commons.cfg.ConfigurationHelper;
import org.codehaus.groovy.grails.compiler.GrailsClassLoader;
import org.codehaus.groovy.grails.compiler.injection.ClassInjector;
import org.codehaus.groovy.grails.compiler.injection.DefaultGrailsDomainClassInjector;
import org.codehaus.groovy.grails.compiler.injection.GrailsAwareClassLoader;
import org.codehaus.groovy.grails.compiler.support.GrailsResourceLoader;
import org.codehaus.groovy.grails.exceptions.GrailsConfigurationException;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Default implementation of the GrailsApplication interface that manages application loading,
* state, and artifact instances.
* <p/>
* Upon loading this GrailsApplication will insepct each class using its registered ArtefactHandler instances. Each
* ArtefactHandler provides knownledge about the conventions used to establish its artefact type. For example
* controllers use the ControllerArtefactHandler to establish this knowledge.
* <p/>
* New ArtefactHandler instances can be registed with the GrailsApplication thus allowing application extensibility.
*
* @author Marc Palmer
* @author Steven Devijver
* @author Graeme Rocher
* @todo Review all synching on new artefact cache stuff, what is sync policy for grailsapplication?
* @see org.codehaus.groovy.grails.plugins.GrailsPluginManager
* @see org.codehaus.groovy.grails.plugins.DefaultGrailsPluginManager
* @see org.codehaus.groovy.grails.commons.ArtefactHandler
* @see org.codehaus.groovy.grails.commons.ArtefactInfo
* @since 0.1
* <p/>
* Created: Jul 2, 2005
*/
public class DefaultGrailsApplication extends GroovyObjectSupport implements GrailsApplication, BeanClassLoaderAware {
private static final Pattern GETCLASSESPROP_PATTERN = Pattern.compile("(\\w+)(Classes)");
private static final Pattern GETCLASSESMETH_PATTERN = Pattern.compile("(get)(\\w+)(Classes)");
private static final Pattern ISCLASS_PATTERN = Pattern.compile("(is)(\\w+)(Class)");
private static final Pattern GETCLASS_PATTERN = Pattern.compile("(get)(\\w+)Class");
private static final String PROJECT_META_FILE = "application.properties";
private static final String DATA_SOURCE_CLASS = "DataSource";
private GroovyClassLoader cl = null;
private Class[] allClasses = new Class[0];
private static Log log = LogFactory.getLog(DefaultGrailsApplication.class);
private ApplicationContext parentContext;
private Set loadedClasses = new HashSet();
private GrailsResourceLoader resourceLoader;
private ArtefactHandler[] artefactHandlers;
private Map artefactHandlersByName = new HashMap();
private Set allArtefactClasses = new HashSet();
private Map artefactInfo = new HashMap();
private boolean suspectArtefactInit;
private Class[] allArtefactClassesArray;
private Map applicationMeta;
private Resource[] resources;
private boolean initialised = false;
private ClassLoader beanClassLoader;
private static final String CONFIG_BINDING_USER_HOME = "userHome";
private static final String CONFIG_BINDING_GRAILS_HOME = "grailsHome";
private static final String CONFIG_BINDING_APP_NAME = "appName";
private static final String CONFIG_BINDING_APP_VERSION = "appVersion";
private static final String META_GRAILS_WAR_DEPLOYED = "grails.war.deployed";
/**
* Creates a new empty Grails application
*/
public DefaultGrailsApplication() {
this.cl = new GroovyClassLoader();
}
/**
* Creates a new GrailsApplication instance using the given classes and GroovyClassLoader
*
* @param classes The classes that make up the GrailsApplication
* @param classLoader The GroovyClassLoader to use
*/
public DefaultGrailsApplication(final Class[] classes, GroovyClassLoader classLoader) {
if (classes == null) {
throw new IllegalArgumentException("Constructor argument 'classes' cannot be null");
}
for (int i = 0; i < classes.length; i++) {
Class aClass = classes[i];
loadedClasses.add(aClass);
}
this.allClasses = classes;
this.cl = classLoader;
this.applicationMeta = loadMetadata();
}
/**
* Constructs a GrailsApplication with the given set of groovy sources specified as Spring Resource instances
*
* @param resources An array or Groovy sources provides by Spring Resource instances
* @throws IOException Thrown when an error occurs reading a Groovy source
*/
public DefaultGrailsApplication(final Resource[] resources) throws IOException {
this(new GrailsResourceLoader(resources));
}
public DefaultGrailsApplication(GrailsResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
try {
this.applicationMeta = loadMetadata();
loadGrailsApplicationFromResources(resourceLoader.getResources());
} catch (IOException e) {
throw new GrailsConfigurationException("I/O exception loading Grails: " + e.getMessage(), e);
}
}
private void loadGrailsApplicationFromResources(Resource[] resources) throws IOException {
GrailsResourceHolder resourceHolder = new GrailsResourceHolder();
this.cl = configureClassLoader(resourceLoader);
Collection loadedResources = new ArrayList();
this.loadedClasses = new HashSet();
try {
for (int i = 0; resources != null && i < resources.length; i++) {
if (!loadedResources.contains(resources[i])) {
try {
String className = resourceHolder.getClassName(resources[i]);
log.debug("Loading groovy file from resource loader :[" + resources[i].getFile().getAbsolutePath() + "] with name [" + className + "]");
if (!StringUtils.isBlank(className)) {
Class c = cl.loadClass(className, true, false);
Assert.notNull(c, "Groovy Bug! GCL loadClass method returned a null class!");
loadedClasses.add(c);
log.debug("Added Groovy class [" + c + "] to loaded classes");
loadedResources = resourceLoader.getLoadedResources();
}
}
catch (ClassNotFoundException e) {
log.error("Class not found attempting to load class " + e.getMessage(), e);
}
} else {
Class c = null;
try {
log.debug("Loading groovy file from class loader :[" + resources[i].getFile().getAbsolutePath() + "]");
c = cl.loadClass(resourceHolder.getClassName(resources[i]));
}
catch (ClassNotFoundException e) {
log.error("Class not found attempting to load class " + e.getMessage(), e);
}
if (c != null)
loadedClasses.add(c);
log.debug("Added Groovy class [" + c + "] to loaded classes");
}
}
}
catch (CompilationFailedException e) {
if (GrailsUtil.isDevelopmentEnv()) {
// if we're in the development environement then there is no point in this exception propagating up the stack as it
// just clouds the actual error so log it as fatal and kill the server
log.fatal("Compilation error loading Grails application: " + e.getMessage(), e);
System.exit(1);
} else {
throw e;
}
}
}
protected Map loadMetadata() {
final Properties meta = new Properties();
Resource r = new ClassPathResource(PROJECT_META_FILE);
try {
meta.load(r.getInputStream());
}
catch (IOException e) {
log.warn("No application metadata file found at " + r);
}
if (System.getProperty(GrailsApplication.ENVIRONMENT) != null) {
meta.setProperty(GrailsApplication.ENVIRONMENT, System.getProperty(GrailsApplication.ENVIRONMENT));
}
return Collections.unmodifiableMap(meta);
}
/**
* Initialises the default set of ArtefactHandler instances
*
* @see org.codehaus.groovy.grails.commons.ArtefactHandler
*/
private void initArtefactHandlers() {
registerArtefactHandler(new DomainClassArtefactHandler());
registerArtefactHandler(new ControllerArtefactHandler());
registerArtefactHandler(new ServiceArtefactHandler());
registerArtefactHandler(new TagLibArtefactHandler());
registerArtefactHandler(new BootstrapArtefactHandler());
registerArtefactHandler(new CodecArtefactHandler());
registerArtefactHandler(new UrlMappingsArtefactHandler());
// Cache the list as an array
this.artefactHandlers = ((ArtefactHandler[]) this.artefactHandlersByName.values().toArray(
new ArtefactHandler[artefactHandlersByName.size()]));
}
/**
* Configures a GroovyClassLoader for the given GrailsInjectionOperation and GrailsResourceLoader
*
* @param resourceLoader The GrailsResourceLoader
* @return A GroovyClassLoader
*/
private GroovyClassLoader configureClassLoader(
final GrailsResourceLoader resourceLoader) {
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
ClassLoader rootLoader = DefaultGroovyMethods.getRootLoader(contextLoader);
ClassLoader parentLoader = beanClassLoader != null ? beanClassLoader : contextLoader;
GroovyClassLoader cl;
if (rootLoader != null) {
cl = new GrailsClassLoader(contextLoader, resourceLoader);
} else {
GrailsAwareClassLoader gcl = new GrailsAwareClassLoader(contextLoader);
if (resourceLoader != null)
gcl.setResourceLoader(resourceLoader);
gcl.setClassInjectors(new ClassInjector[]{new DefaultGrailsDomainClassInjector()});
cl = gcl;
}
Thread.currentThread().setContextClassLoader(cl);
return cl;
}
/**
* Returns all the classes identified as artefacts by ArtefactHandler instances
*
* @return An array of classes
*/
public Class[] getAllArtefacts() {
return allArtefactClassesArray;
}
private Class[] populateAllClasses() {
this.allClasses = (Class[]) loadedClasses.toArray(new Class[loadedClasses.size()]);
return allClasses;
}
/**
* Configures the loaded classes within the GrailsApplication instance using the registered ArtefactHandler instances
*
* @param classes The classes to configure
*/
private void configureLoadedClasses(Class[] classes) {
initArtefactHandlers();
artefactInfo.clear();
allArtefactClasses.clear();
allArtefactClassesArray = null;
this.allClasses = classes;
suspectArtefactInit = true;
try {
// first load the domain classes
log.debug("Going to inspect artefact classes.");
for (int i = 0; i < classes.length; i++) {
final Class theClass = classes[i];
log.debug("Inspecting [" + theClass.getName() + "]");
if (Modifier.isAbstract(theClass.getModifiers())) {
log.debug("[" + theClass.getName() + "] is abstract.");
continue;
}
if (allArtefactClasses.contains(theClass))
continue;
// check what kind of artefact it is and add to corrent data structure
for (int j = 0; j < artefactHandlers.length; j++) {
if (artefactHandlers[j].isArtefact(theClass)) {
log.debug("Adding artefact " + theClass + " of kind " + artefactHandlers[j].getType());
GrailsClass gclass = addArtefact(artefactHandlers[j].getType(), theClass);
// Also maintain set of all artefacts (!= all classes loaded)
allArtefactClasses.add(theClass);
// Update per-artefact cache
DefaultArtefactInfo info = getArtefactInfo(artefactHandlers[j].getType(), true);
info.addGrailsClass(gclass);
break;
}
}
}
refreshArtefactGrailsClassCaches();
} finally {
suspectArtefactInit = false;
}
allArtefactClassesArray = (Class[]) allArtefactClasses.toArray(new Class[allArtefactClasses.size()]);
// Tell all artefact handlers to init now we've worked out which classes are which artefacts
for (int j = 0; j < artefactHandlers.length; j++) {
initializeArtefacts(artefactHandlers[j]);
}
}
/**
* <p>Tell all our artefact info objects to update their internal state after we've added a bunch of classes</p>
*/
private void refreshArtefactGrailsClassCaches() {
for (Iterator it = artefactInfo.values().iterator(); it.hasNext();) {
DefaultArtefactInfo info = (DefaultArtefactInfo) it.next();
info.updateComplete();
}
}
private void addToLoaded(Class clazz) {
this.loadedClasses.add(clazz);
populateAllClasses();
}
public GrailsControllerClass getScaffoldingController(GrailsDomainClass domainClass) {
if (domainClass == null) {
return null;
}
ArtefactInfo info = getArtefactInfo(ControllerArtefactHandler.TYPE, true);
GrailsClass[] controllerClasses = info.getGrailsClasses();
for (int i = 0; i < controllerClasses.length; i++) {
GrailsControllerClass controllerClass = (GrailsControllerClass) controllerClasses[i];
if (controllerClass.isScaffolding()) {
Class scaffoldedClass = controllerClass.getScaffoldedClass();
if (scaffoldedClass != null) {
if (domainClass.getClazz().getName().equals(scaffoldedClass.getName())) {
return controllerClass;
}
} else if (domainClass.getName().equals(controllerClass.getName())) {
return controllerClass;
}
}
}
return null;
}
public GrailsResourceLoader getResourceLoader() {
return resourceLoader;
}
public GroovyClassLoader getClassLoader() {
return this.cl;
}
public ConfigObject getConfig() {
ConfigObject c = ConfigurationHolder.getConfig();
if (c == null) {
ConfigSlurper configSlurper = new ConfigSlurper(GrailsUtil.getEnvironment());
Map binding = new HashMap();
// configure config slurper binding
binding.put(CONFIG_BINDING_USER_HOME, System.getProperty("user.home"));
binding.put(CONFIG_BINDING_GRAILS_HOME, System.getProperty("grails.home"));
binding.put(CONFIG_BINDING_APP_NAME, getMetadata().get("app.name"));
binding.put(CONFIG_BINDING_APP_VERSION, getMetadata().get("app.version"));
configSlurper.setBinding(binding);
try {
Class scriptClass = getClassLoader()
.loadClass(CONFIG_CLASS);
c = configSlurper.parse(scriptClass);
} catch (ClassNotFoundException e) {
log.debug("Could not find config class [" + CONFIG_CLASS + "]. This is probably nothing to worry about, it is not required to have a config: " + e.getMessage(), e);
// ignore, it is ok not to have a configuration file
c = new ConfigObject();
}
try {
Class dataSourceClass = getClassLoader()
.loadClass(DATA_SOURCE_CLASS);
c.merge(configSlurper.parse(dataSourceClass));
} catch (ClassNotFoundException e) {
log.debug("Cound not find data source class [" + DATA_SOURCE_CLASS + "]. This may be what you are expecting, but will result in Grails loading with an in-memory database");
// ignore
}
}
if (c == null) c = new ConfigObject();
ConfigurationHelper.initConfig(c,null, getClassLoader());
ConfigurationHolder.setConfig(c);
return c;
}
/**
* Retrieves the number of artefacts registered for the given artefactType as defined by the ArtefactHandler
*
* @param artefactType The type of the artefact as defined by the ArtefactHandler
* @return The number of registered artefacts
*/
private int getArtefactCount(String artefactType) {
ArtefactInfo info = getArtefactInfo(artefactType);
return info == null ? 0 : info.getClasses().length;
}
/**
* Retrieves all classes loaded by the GrailsApplication
*
* @return All classes loaded by the GrailsApplication
*/
public Class[] getAllClasses() {
return this.allClasses;
}
/**
* Sets the parent ApplicationContext for the GrailsApplication
*
* @param applicationContext The ApplicationContext
* @throws BeansException Thrown when an error occurs setting the ApplicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.parentContext = applicationContext;
}
/**
* Retrieves the parent ApplicationContext for this GrailsApplication
*
* @return The parent ApplicationContext
*/
public ApplicationContext getParentContext() {
return this.parentContext;
}
/**
* Retrieves a class from the GrailsApplication for the given name
*
* @param className The class name
* @return Either the java.lang.Class instance or null if it doesn't exist
*/
public Class getClassForName(String className) {
if (StringUtils.isBlank(className)) {
return null;
}
for (int i = 0; i < allClasses.length; i++) {
Class c = allClasses[i];
if (c.getName().equals(className)) {
return c;
}
}
return null;
}
/**
* Refreshes constraints defined by the DomainClassArtefactHandler
*
* @TODO Move this out of GrailsApplication
*/
public void refreshConstraints() {
ArtefactInfo info = getArtefactInfo(DomainClassArtefactHandler.TYPE, true);
GrailsClass[] domainClasses = info.getGrailsClasses();
for (int i = 0; i < domainClasses.length; i++) {
((GrailsDomainClass) domainClasses[i]).refreshConstraints();
}
}
/**
* Refreshes this GrailsApplication, rebuilding all of the artefact definitions as defined by the registered ArtefactHandler instances
*/
public void refresh() {
configureLoadedClasses(this.cl.getLoadedClasses());
}
public void rebuild() {
this.initialised = false;
this.loadedClasses.clear();
initArtefactHandlers();
if (GrailsUtil.isDevelopmentEnv()) {
try {
loadGrailsApplicationFromResources(this.resources);
initialise();
} catch (IOException e) {
throw new GrailsConfigurationException("I/O error rebuilding GrailsApplication: " + e.getMessage(), e);
}
} else {
throw new IllegalStateException("Cannot rebuild GrailsApplication when not in development mode!");
}
}
/**
* Retrieves the Spring Resource that was used to load the given Class
*
* @param theClazz The class
* @return Either a Spring Resource or null if no Resource was found for the given class
*/
public Resource getResourceForClass(Class theClazz) {
if (this.resourceLoader == null) {
return null;
}
return this.resourceLoader.getResourceForClass(theClazz);
}
/**
* Returns true if the given class is an artefact identified by one of the registered ArtefactHandler instances.
* Uses class name equality to handle class reloading
*
* @param theClazz The class to check
* @return True if it is an artefact
*/
public boolean isArtefact(Class theClazz) {
String className = theClazz.getName();
for (Iterator i = allArtefactClasses.iterator(); i.hasNext();) {
Class artefactClass = (Class) i.next();
if (className.equals(artefactClass.getName())) {
return true;
}
}
return false;
}
/**
* Returns true if the specified class is of the given artefact type as defined by the ArtefactHandler
*
* @param artefactType The type of the artefact
* @param theClazz The class
* @return True if it is of the specified artefactType
* @see org.codehaus.groovy.grails.commons.ArtefactHandler
*/
public boolean isArtefactOfType(String artefactType, Class theClazz) {
ArtefactHandler handler = (ArtefactHandler) artefactHandlersByName.get(artefactType);
if (handler == null)
throw new GrailsConfigurationException("Unable to locate arefact handler for specified type: " + artefactType);
return handler.isArtefact(theClazz);
}
/**
* Returns true if the specified class name is of the given artefact type as defined by the ArtefactHandler
*
* @param artefactType The type of the artefact
* @param className The class name
* @return True if it is of the specified artefactType
* @see org.codehaus.groovy.grails.commons.ArtefactHandler
*/
public boolean isArtefactOfType(String artefactType, String className) {
return getArtefact(artefactType, className) != null;
}
/**
* Retrieves an artefact for the given type and nam
*
* @param artefactType The artefact type as defined by a registered ArtefactHandler
* @param name The name of the class
* @return A GrailsClass instance or null if none could be found for the given artefactType and name
*/
public GrailsClass getArtefact(String artefactType, String name) {
ArtefactInfo info = getArtefactInfo(artefactType);
return info == null ? null : info.getGrailsClass(name);
}
private GrailsClass getFirstArtefact(String artefactType) {
ArtefactInfo info = getArtefactInfo(artefactType);
// This will throw AIOB if we have none
return info == null ? null : info.getGrailsClasses()[0];
}
/**
* Returns all of the GrailsClass instances for the given artefactType as defined by the ArtefactHandler
*
* @param artefactType The type of the artefact defined by the ArtefactHandler
* @return An array of classes for the given artefact
*/
public GrailsClass[] getArtefacts(String artefactType) {
ArtefactInfo info = getArtefactInfo(artefactType, true);
return info.getGrailsClasses();
}
// This is next call is equiv to getControllerByURI / getTagLibForTagName
public GrailsClass getArtefactForFeature(String artefactType, Object featureID) {
ArtefactHandler handler = (ArtefactHandler) artefactHandlersByName.get(artefactType);
return handler.getArtefactForFeature(featureID);
}
/**
* Adds an artefact of the given type for the given Class.
*
* @param artefactType The type of the artefact as defined by a ArtefactHandler instance
* @param artefactClass A Class instance that matches the type defined by the ArtefactHandler
* @return The GrailsClass if successful or null if it couldn't be added
* @throws GrailsConfigurationException If the specified Class is not the same as the type defined by the ArtefactHandler
* @see org.codehaus.groovy.grails.commons.ArtefactHandler
*/
public GrailsClass addArtefact(String artefactType, Class artefactClass) {
// @todo should we filter abstracts here?
if (Modifier.isAbstract(artefactClass.getModifiers())) {
return null;
}
ArtefactHandler handler = (ArtefactHandler) artefactHandlersByName.get(artefactType);
if (handler.isArtefact(artefactClass)) {
GrailsClass artefactGrailsClass = handler.newArtefactClass(artefactClass);
// Store the GrailsClass in cache
DefaultArtefactInfo info = getArtefactInfo(artefactType, true);
info.addGrailsClass(artefactGrailsClass);
info.updateComplete();
addToLoaded(artefactClass);
if (isInitialised())
initializeArtefacts(artefactType);
return artefactGrailsClass;
} else {
throw new GrailsConfigurationException("Cannot add " + artefactType + " class ["
+ artefactClass + "]. It is not a " + artefactType + "!");
}
}
/**
* Adds an artefact of the given type for the given GrailsClass.
*
* @param artefactType The type of the artefact as defined by a ArtefactHandler instance
* @param artefactGrailsClass A GrailsClass instance that matches the type defined by the ArtefactHandler
* @return The GrailsClass if successful or null if it couldn't be added
* @throws GrailsConfigurationException If the specified GrailsClass is not the same as the type defined by the ArtefactHandler
* @see org.codehaus.groovy.grails.commons.ArtefactHandler
*/
public GrailsClass addArtefact(String artefactType, GrailsClass artefactGrailsClass) {
// @todo should we filter abstracts here?
if (Modifier.isAbstract(artefactGrailsClass.getClazz().getModifiers())) {
return null;
}
ArtefactHandler handler = (ArtefactHandler) artefactHandlersByName.get(artefactType);
if (handler.isArtefactGrailsClass(artefactGrailsClass)) {
// Store the GrailsClass in cache
DefaultArtefactInfo info = getArtefactInfo(artefactType, true);
info.addGrailsClass(artefactGrailsClass);
info.updateComplete();
initializeArtefacts(artefactType);
return artefactGrailsClass;
} else {
throw new GrailsConfigurationException("Cannot add " + artefactType + " class ["
+ artefactGrailsClass + "]. It is not a " + artefactType + "!");
}
}
/**
* Registers a new ArtefactHandler that is responsible for identifying and managing an particular artefact type that is defined by
* some convention
*
* @param handler The ArtefactHandler to regster
*/
public void registerArtefactHandler(ArtefactHandler handler) {
artefactHandlersByName.put(handler.getType(), handler);
}
public ArtefactHandler[] getArtefactHandlers() {
return artefactHandlers;
}
/**
* <p>Re-initialize the artefacts of the specified type. This gives handlers a chance to update caches etc</p>
*
* @param artefactType The type of artefact to init
*/
private void initializeArtefacts(String artefactType) {
ArtefactHandler handler = (ArtefactHandler) artefactHandlersByName.get(artefactType);
initializeArtefacts(handler);
}
/**
* <p>Re-initialize the artefacts of the specified type. This gives handlers a chance to update caches etc</p>
*
* @param handler The handler to register
*/
private void initializeArtefacts(ArtefactHandler handler) {
if (handler != null) {
ArtefactInfo info = getArtefactInfo(handler.getType());
// Only init those that have data
if (info != null) {
//System.out.println("Initialising artefacts of kind " + handler.getType() + " with registered artefacts" + info.getGrailsClassesByName());
handler.initialize(info);
}
}
}
/**
* <p>Get or create the cache of classes for the specified artefact type</p>
*
* @param artefactType The name of an artefact type
* @param create Set to true if you want non-existent caches to be created
* @return The cache of classes for the type, or null if no cache exists and create is false
*/
private DefaultArtefactInfo getArtefactInfo(String artefactType, boolean create) {
DefaultArtefactInfo cache = (DefaultArtefactInfo) artefactInfo.get(artefactType);
if ((cache == null) && create) {
cache = new DefaultArtefactInfo();
artefactInfo.put(artefactType, cache);
cache.updateComplete();
}
return cache;
}
/**
* <p>Get the cache of classes for the specified artefact type</p>
*
* @param artefactType The name of an artefact type
* @return The cache of classes for the type, or null if no cache exists
*/
public ArtefactInfo getArtefactInfo(String artefactType) {
return getArtefactInfo(artefactType, false);
}
/**
* <p>Overrides method invocation to return dynamic artefact methods</p>
* <p>We will support getXXXXClasses() and isXXXXClass(class)</p>
*
* @param methodName The name of the method
* @param args The arguments to the method
* @return The return value of the method
* @todo this is REALLY ugly
* @todo Need to add matches for add<Artefact>Class(java.lang.Class) and add<Artefact>Class(GrailsClass)
*/
public Object invokeMethod(String methodName, Object args) {
Object[] argsv = (Object[]) args;
Matcher match = GETCLASS_PATTERN.matcher(methodName);
// look for getXXXXClass(y)
match.find();
if (match.matches()) {
if (argsv.length > 0) {
if ((argsv.length != 1) || !(argsv[0] instanceof String)) {
throw new IllegalArgumentException("Dynamic method get<Artefact>Class(artefactName) requires a " +
"single String parameter");
} else {
return getArtefact(match.group(2), argsv[0].toString());
}
} else {
// It's a no-param getter
return super.invokeMethod(methodName, args);
}
} else {
// look for isXXXXClass(y)
match = ISCLASS_PATTERN.matcher(methodName);
// find match
match.find();
if (match.matches()) {
if ((argsv.length != 1) || !(argsv[0] instanceof Class)) {
throw new IllegalArgumentException("Dynamic method is<Artefact>Class(artefactClass) requires a " +
"single Class parameter");
} else {
return Boolean.valueOf(isArtefactOfType(match.group(2), (Class) argsv[0]));
}
} else {
// look for getXXXXClasses
match = GETCLASSESMETH_PATTERN.matcher(methodName);
// find match
match.find();
if (match.matches()) {
String artefactName = GrailsClassUtils.getClassNameRepresentation(match.group(2));
if (artefactHandlersByName.containsKey(artefactName)) {
return getArtefacts(match.group(2));
} else {
throw new IllegalArgumentException("Dynamic method get<Artefact>Classes() called for " +
"unrecognized artefact: " + match.group(2));
}
} else {
return super.invokeMethod(methodName, args);
}
}
}
}
/**
* <p>Override property access and hit on xxxxClasses to return class arrays of artefacts</p>
*
* @param propertyName The name of the property, if it ends in *Classes then match and invoke internal ArtefactHandler
* @return All the artifacts or delegate to super.getProperty
*/
public Object getProperty(String propertyName) {
// look for getXXXXClasses
final Matcher match = GETCLASSESPROP_PATTERN.matcher(propertyName);
// find match
match.find();
if (match.matches()) {
String artefactName = GrailsClassUtils.getClassNameRepresentation(match.group(1));
if (artefactHandlersByName.containsKey(artefactName)) {
return getArtefacts(artefactName);
}
}
return super.getProperty(propertyName);
}
public void initialise() {
// get all the classes that were loaded
if (log.isDebugEnabled()) {
log.debug("loaded classes: [" + loadedClasses + "]");
}
Class[] classes = populateAllClasses();
configureLoadedClasses(classes);
this.initialised = true;
}
public boolean isInitialised() {
return this.initialised;
}
public Map getMetadata() {
return applicationMeta;
}
public GrailsClass getArtefactByLogicalPropertyName(String type, String logicalName) {
ArtefactInfo info = getArtefactInfo(type);
return info == null ? null : info.getGrailsClassByLogicalPropertyName(logicalName);
}
public void addArtefact(Class artefact) {
for (int i = 0; i < artefactHandlers.length; i++) {
ArtefactHandler artefactHandler = artefactHandlers[i];
if (artefactHandler.isArtefact(artefact)) {
addArtefact(artefactHandler.getType(), artefact);
}
}
}
public boolean isWarDeployed() {
Map metadata = getMetadata();
if(metadata != null) {
Object val = metadata.get(META_GRAILS_WAR_DEPLOYED);
if(val != null && val.equals("true")) {
return true;
}
}
return false;
}
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
}