PageRenderTime 64ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/grails-core/src/main/groovy/org/codehaus/groovy/grails/commons/GrailsApplication.java

https://github.com/aclement/grails-core
Java | 428 lines | 85 code | 53 blank | 290 comment | 0 complexity | 72419423fb01e671f9ee68013374a72b MD5 | raw file
  1. /*
  2. * Copyright 2004-2005 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.codehaus.groovy.grails.commons;
  17. import grails.util.Environment;
  18. import grails.util.Metadata;
  19. import groovy.util.ConfigObject;
  20. import org.springframework.context.ApplicationContext;
  21. import org.springframework.context.ApplicationContextAware;
  22. import org.springframework.core.io.Resource;
  23. import java.util.Map;
  24. /**
  25. * <p>The main interface representing a running Grails application. This interface's
  26. * main purpose is to provide a mechanism for analysing the conventions within a Grails
  27. * application as well as providing metadata and information about the execution environment.
  28. *
  29. * <p>The GrailsApplication interface interfacts with {@link org.codehaus.groovy.grails.commons.ArtefactHandler} instances
  30. * which are capable of analysing different artefact types (controllers, domain classes etc.) and introspecting
  31. * the artefact conventions
  32. *
  33. * <p>Implementors of this inteface should be aware that a GrailsApplication is only initialised when the initialise() method
  34. * is called. In other words GrailsApplication instances are lazily initialised by the Grails runtime.
  35. *
  36. * @see #initialise()
  37. * @see ArtefactHandler
  38. *
  39. * @author Graeme Rocher
  40. * @author Steven Devijver
  41. *
  42. * @since 0.1
  43. */
  44. public interface GrailsApplication extends ApplicationContextAware {
  45. /**
  46. * The name of the system property whose value contains the location, during development, of the Grails working directory where temporary files are generated to
  47. *
  48. * @deprecated Use {@link grails.util.BuildSettings#WORK_DIR} instead.
  49. */
  50. @Deprecated
  51. String WORK_DIR = "grails.work.dir";
  52. /**
  53. * The directory where temporary project resources and plug-ins are kept
  54. *
  55. * @deprecated Use {@link grails.util.BuildSettings#PROJECT_WORK_DIR} instead.
  56. */
  57. @Deprecated
  58. String PROJECT_WORK_DIR = "grails.project.work.dir";
  59. /**
  60. * The path to the plug-ins directory for the application
  61. *
  62. * @deprecated Use {@link grails.util.BuildSettings#PLUGINS_DIR} instead.
  63. */
  64. @Deprecated
  65. String PLUGINS_DIR = "grails.project.plugins.dir";
  66. /**
  67. * The path to the global plug-ins directory for the application
  68. *
  69. * @deprecated Use {@link grails.util.BuildSettings#GLOBAL_PLUGINS_DIR} instead.
  70. */
  71. @Deprecated
  72. String GLOBAL_PLUGINS_DIR = "grails.global.plugins.dir";
  73. /**
  74. * The name of the system property whose value contains the location, during development, of the current Grails projects resources directory
  75. *
  76. * @deprecated Use {@link grails.util.BuildSettings#PROJECT_RESOURCES_DIR} instead.
  77. */
  78. @Deprecated
  79. String PROJECT_RESOURCES_DIR = "grails.project.resource.dir";
  80. /**
  81. * The name of the system property whose value contains the location, during development, of the current Grails projects resources directory
  82. *
  83. * @deprecated Use {@link grails.util.BuildSettings#PROJECT_CLASSES_DIR} instead.
  84. */
  85. @Deprecated
  86. String PROJECT_CLASSES_DIR = "grails.project.class.dir";
  87. /**
  88. * The name of the system property whose value contains the location, during development, of the current Grails projects resources directory
  89. *
  90. * @deprecated Use {@link grails.util.BuildSettings#PROJECT_TEST_CLASSES_DIR} instead.
  91. */
  92. @Deprecated
  93. String PROJECT_TEST_CLASSES_DIR = "grails.project.test.class.dir";
  94. /**
  95. * The id of the grails application within a bean context
  96. */
  97. String APPLICATION_ID = "grailsApplication";
  98. /**
  99. * Constant used to resolve the environment via System.getProperty(ENVIRONMENT)
  100. *
  101. * @deprecated Use {@link grails.util.Environment#KEY} instead.
  102. */
  103. @Deprecated
  104. String ENVIRONMENT = Environment.KEY;
  105. /**
  106. * Constants that indicates whether this GrailsApplication is running in the default environment
  107. *
  108. * @deprecated Use {@link grails.util.Environment#DEFAULT} instead.
  109. */
  110. @Deprecated
  111. String ENVIRONMENT_DEFAULT = "grails.env.default";
  112. /**
  113. * Constant for the development environment
  114. *
  115. * @deprecated Use {@link grails.util.Environment#DEVELOPMENT} instead.
  116. */
  117. @Deprecated
  118. String ENV_DEVELOPMENT = Environment.DEVELOPMENT.getName();
  119. /**
  120. * Constant for the application data source, primarly for backward compatability for those applications
  121. * that use ApplicationDataSource.groovy
  122. *
  123. * @deprecated Use {@link grails.util.Environment#APPLICATION} instead.
  124. */
  125. @Deprecated
  126. String ENV_APPLICATION = Environment.APPLICATION.getName();
  127. /**
  128. * Constant for the production environment.
  129. *
  130. * @deprecated Use {@link grails.util.Environment#PRODUCTION} instead.
  131. */
  132. @Deprecated
  133. String ENV_PRODUCTION = Environment.PRODUCTION.getName();
  134. /**
  135. * Constant for the test environment.
  136. *
  137. * @deprecated Use {@link grails.util.Environment#TEST} instead.
  138. */
  139. @Deprecated
  140. String ENV_TEST = Environment.TEST.getName();
  141. /**
  142. * The name of the class that provides configuration
  143. */
  144. String CONFIG_CLASS = "Config";
  145. String DATA_SOURCE_CLASS = "DataSource";
  146. String PROJECT_META_FILE = "application.properties";
  147. /**
  148. * Returns the ConfigObject instance.
  149. *
  150. * @return The ConfigObject instance
  151. */
  152. ConfigObject getConfig();
  153. /**
  154. * Returns the flatten ConfigObject for use from Java classes.
  155. * @return The flattened config
  156. */
  157. Map<String, Object> getFlatConfig();
  158. /**
  159. * Returns the class loader instance for the Grails application.
  160. *
  161. * @return The ClassLoader instance
  162. */
  163. ClassLoader getClassLoader();
  164. /**
  165. * Retrieves all java.lang.Class instances loaded by the Grails class loader
  166. * @return An array of classes
  167. */
  168. @SuppressWarnings("rawtypes")
  169. Class[] getAllClasses();
  170. /**
  171. * Retrieves all java.lang.Class instances considered Artefacts loaded by the Grails class loader
  172. * @return An array of classes
  173. */
  174. @SuppressWarnings("rawtypes")
  175. Class[] getAllArtefacts();
  176. /**
  177. * Returns the Spring context for this application. Note that this
  178. * will return <code>null</code> until the application is fully
  179. * initialised. This context contains all the application artifacts,
  180. * plugin beans, the works.
  181. */
  182. ApplicationContext getMainContext();
  183. /**
  184. * Sets the main Spring context for this application.
  185. */
  186. void setMainContext(ApplicationContext context);
  187. /**
  188. * Returns the Spring application context that contains this
  189. * application instance. It is the parent of the context returned
  190. * by {@link #getMainContext()}.
  191. */
  192. ApplicationContext getParentContext();
  193. /**
  194. * Retrieves a class for the given name within the GrailsApplication or returns null
  195. *
  196. * @param className The name of the class
  197. * @return The class or null
  198. */
  199. @SuppressWarnings("rawtypes")
  200. Class getClassForName(String className);
  201. /**
  202. * Rebuilds the constraint definitions.
  203. * TODO move this out? Why ORM dependencies in here?
  204. */
  205. void refreshConstraints();
  206. /**
  207. * This method will refresh the entire application
  208. */
  209. void refresh();
  210. /**
  211. * Rebuilds this Application throwing away the class loader and re-constructing it from the loaded
  212. * resources again. Can only be called in development mode and an error will be thrown if called
  213. * in a different enivronment
  214. */
  215. void rebuild();
  216. /**
  217. * Retrieves a Resource instance for the given Grails class or null it doesn't exist.
  218. *
  219. * @param theClazz The Grails class
  220. * @return A Resource or null
  221. */
  222. @SuppressWarnings("rawtypes")
  223. Resource getResourceForClass(Class theClazz);
  224. /**
  225. * <p>Call this to find out if the class you have is an artefact loaded by grails.</p>
  226. * @param theClazz A class to test
  227. * @return True if and only if the class was loaded from grails-app/
  228. * @since 0.5
  229. */
  230. @SuppressWarnings("rawtypes")
  231. boolean isArtefact(Class theClazz);
  232. /**
  233. * <p>Check if the specified artefact Class has been loaded by Grails already AND is
  234. * of the type expected</p>
  235. * @param artefactType A string identifying the artefact type to check for
  236. * @param theClazz The class to check
  237. * @return True if Grails considers the class to be managed as an artefact of the type specified.
  238. * @since 0.5
  239. */
  240. @SuppressWarnings("rawtypes")
  241. boolean isArtefactOfType(String artefactType, Class theClazz);
  242. /**
  243. * <p>Check if the artefact Class with the name specified is of the type expected</p>
  244. * @param artefactType A string identifying the artefact type to check for
  245. * @param className The name of a class to check
  246. * @return True if Grails considers the class to be managed as an artefact of the type specified.
  247. * @since 0.5
  248. */
  249. boolean isArtefactOfType(String artefactType, String className);
  250. /**
  251. * <p>Gets the GrailsClass associated with the named artefact class</p>
  252. * <p>i.e. to get the GrailsClass for controller called "BookController" you pass the name "BookController"</p>
  253. * @param artefactType The type of artefact to retrieve, i.e. "Controller"
  254. * @param name The name of an artefact such as "BookController"
  255. * @return The associated GrailsClass or null
  256. * @since 0.5
  257. */
  258. GrailsClass getArtefact(String artefactType, String name);
  259. /**
  260. * Returns the ArtefactHandler for the given class or null
  261. * @param theClass The class
  262. * @return The ArtefactHandler
  263. */
  264. @SuppressWarnings("rawtypes")
  265. ArtefactHandler getArtefactType(Class theClass);
  266. /**
  267. * <p>Obtain all the class information about the artefactType specified</p>
  268. * @param artefactType An artefact type identifier i.e. "Domain"
  269. * @return The artefact info or null if the artefactType is not recognized
  270. * @since 0.5
  271. */
  272. ArtefactInfo getArtefactInfo(String artefactType);
  273. /**
  274. * <p>Get an array of all the GrailsClass instances relating to artefacts of the specified type.</p>
  275. * @param artefactType The type of artefact to retrieve, i.e. "Controller"
  276. * @return An array of GrailsClasses which may empty by not null
  277. * @since 0.5
  278. */
  279. GrailsClass[] getArtefacts(String artefactType);
  280. /**
  281. * <p>Get an artefact GrailsClass by a "feature" which depending on the artefact may be a URI or tag name
  282. * for example</p>
  283. * @param artefactType The type ID of the artefact, i.e. "TagLib"
  284. * @param featureID The "feature" ID, say a URL or tag name
  285. * @return The grails class or null if none is found
  286. * @since 0.5
  287. */
  288. GrailsClass getArtefactForFeature(String artefactType, Object featureID);
  289. /**
  290. * <p>Registers a new artefact</p>
  291. * @param artefactType The type ID of the artefact, i.e. "TagLib"
  292. * @param artefactClass The class of the artefact. A new GrailsClass will be created automatically and added
  293. * to internal structures, using the appropriate ArtefactHandler
  294. * @return The new grails class for the artefact class
  295. * @since 0.5
  296. */
  297. @SuppressWarnings("rawtypes")
  298. GrailsClass addArtefact(String artefactType, Class artefactClass);
  299. /**
  300. * <p>Registers a new artefact</p>
  301. * @param artefactType The type ID of the artefact, i.e. "TagLib"
  302. * @param artefactGrailsClass The GrailsClass of the artefact.
  303. * @return The supplied grails class for the artefact class
  304. * @since 0.5
  305. */
  306. GrailsClass addArtefact(String artefactType, GrailsClass artefactGrailsClass);
  307. /**
  308. * <p>Register a new artefact handler</p>
  309. * @param handler The new handler to add
  310. */
  311. void registerArtefactHandler(ArtefactHandler handler);
  312. /**
  313. * <p>Test whether an artefact handler exists for a given type</p>
  314. * @param type The type of the handler
  315. * @return True if it does
  316. */
  317. boolean hasArtefactHandler(String type);
  318. /**
  319. * <p>Obtain a list of all the artefact handlers</p>
  320. * @return The list, possible empty but not null, of all currently registered handlers
  321. */
  322. ArtefactHandler[] getArtefactHandlers();
  323. /**
  324. * Initialise this GrailsApplication.
  325. */
  326. void initialise();
  327. /**
  328. * Returns whether this GrailsApplication has been initialised or not.
  329. * @return True if it has been initialised
  330. */
  331. boolean isInitialised();
  332. /**
  333. * <p>Get access to the project's metadata, specified in application.properties</p>
  334. * <p>This provides access to information like required grails version, application name, version etc
  335. * but <b>NOT</b> general application settings.</p>
  336. * @return A read-only Map of data about the application, not environment specific
  337. */
  338. Metadata getMetadata();
  339. /**
  340. * Retrieves an artefact by its logical property name. For example the logical property name of
  341. * BookController would be book.
  342. * @param type The artefact type
  343. * @param logicalName The logical name
  344. * @return The GrailsClass or null if it doesn't exist
  345. */
  346. GrailsClass getArtefactByLogicalPropertyName(String type, String logicalName);
  347. /**
  348. * Adds the given artefact, attempting to determine type from
  349. * @param artefact The artefact to add
  350. */
  351. @SuppressWarnings("rawtypes")
  352. void addArtefact(Class artefact);
  353. /**
  354. * Returns true if this application has been deployed as a WAR file
  355. *
  356. * @return True if the application is WAR deployed
  357. */
  358. boolean isWarDeployed();
  359. /**
  360. * Adds an artefact that can be overriden by user defined classes
  361. * @param artefact An overridable artefact
  362. */
  363. @SuppressWarnings("rawtypes")
  364. void addOverridableArtefact(Class artefact);
  365. /**
  366. * Fired to inform the application when the Config.groovy file changes.
  367. */
  368. void configChanged();
  369. /**
  370. * Returns the ArtefactHandler for the given type
  371. * @param type The artefact handler type
  372. * @return The artefact handler
  373. */
  374. ArtefactHandler getArtefactHandler(String type);
  375. }