I have a plugin that's shared between multiple applications and I need to set some application specific options that will be used in a service in the plugin. It's an in house written plugin, however, the plugin has to be unaware of which application it's running in.
The best way I can think of doing this is to have some code run in the application right after the service is created in the plugin and call a method on the service to set the options. Is this possible?
If it's not possible, what other design could I implement to pass options to the plugin from the application.
Btw, these options need to be set when the application starts as well as throughout the running of the application.
It's best to use Bootstrap.groovy to to call a method(s) on a Grails service when an application starts. This service can be provided by the application or plugins in the application. Here is a quick example of how to do so:
Bootstrap.groovy
class BootStrap {
def myExampleService
def init = { servletContext ->
myExampleService.someMethodOnTheService()
}
}
Related
I need to separate a grails service into a dedicated lilbrary in order to use this service across many applications. This works fine for the service itself as I am able to register the service bean in the resources.groovy (see https://docs.grails.org/latest/guide/spring.html).
This service happens to use a quartz job to get some functionality triggered regularly. So naturally I would move this job into the library and need to register it in the main application.
How can that be achieved? Thanks for you time!
Create grails plugin using this guide:
https://docs.grails.org/latest/guide/plugins.html
Then in src/main/groovy/YOUR_PACKAGE/YourPluginGrailsPlugin.groovy you can add bean configuration in doWithSpring() method (in same as resources.groovy way).
I have a Grails plugin I've created which is intended to support a number of applications. This plugin has an Employee domain object. The problem is that, when used in the main application, domain objects from that application need to refer back to the Employee object. So, my main application might have an Address which belongsTo the Employee class from the plugin.
How can one handle this properly in Grails 2.5.0?
Thanks in advance.
It looks like that your main and plugin depend on each other. In this case you should add the plugin location of one to another:
grails.plugin.location.<PLUGIN-NAME> = "<PATH TO YOUR PLUGIN>" Ex: "../myPlugin" assuming it is located on the same folder structure as the plugin
and in your plugin
grails.plugin.location.<APP-NAME>= "<PATH TO YOUR APP>" EX: "../myApp"
I have a fairly complicated grails plugin dependency structure within my project and I am having problems overriding classes from the security plugin.
My structure is a little something like this:
Web App
|_ Audit Plugin
|_ Spring Security Core Plugin
|_ Security Wrapper Plugin
|_ Audit Plugin
|_ Spring Security Core Plugin
The reason it is like this is audit is shared between some apps which have the security wrapper, and some what don't, which is why it pulls in Security-Core (it needs at least the ability to get the current principal).
Similarly the wrapper is shared between multiple web apps therefore we put it in a plugin. My problem comes after upgrading Spring-Security-Core to version 2.
My wrapper has a customer auth.gsp and LoginController.groovy. In the older version of security this was fine, as the plugin templated those and made them available in the source of the installing plugin.
However now these files are internal to the plugin, and although I know you can override them within the main app, when trying to override them within another plugin I get some bizarre results.
The Spring-Security-Core version of the login page always overrides my custom login page. I cannot get mine to take precedence.
The second problem is that the LoginController.groovy from the Spring-Security-Core plugin sometimes takes precedence over my one from the wrapper. It seems almost random between builds as to which one will be in use.
Is there any correct way to go about making sure my views and controllers take precedence?
OK playing around with things I found a solution that seems to work for me:
Firstly I couldn't change the order in which the plugins load because the security wrapper does a lot with spring beans and it has to load after the core plugin for this to work. So after a bit of digging in the (DefaultSecurityConfig.groovy) I noticed that you can set the following properties:
grails.plugin.springsecurity.failureHandler.defaultFailureUrl = '/login/authfail? login_error=1'
grails.plugin.springsecurity.failureHandler.ajaxAuthFailUrl = '/login/authfail?ajax=true'
grails.plugin.springsecurity.auth.loginFormUrl = '/login/auth'
So I created a custom controller and login page which have a different name to the ones use in the core plugin and changed these properties to point to my locations.
To neaten this up, in the UrlMappings for the wrapper (named: SecWrapperUrlMappings) I put a mapping from /login/** to /seclogin/**.
Make sure that these new locations aren't locked down so that people can access them and that seems to work well. I now reliable know, whichever order they load in my login page and login controller are used.
In Grails-4.013 and spring-security-core-4.0.4, I did the following trick.
In my custom plugin instead of LoginController and LogoutController I named them as SigninController and SignoutController respectively. And in UrlMappings.groovy of App mapped them like..
static mappings = {
"/login/$action?"(controller: "signin")
"/logout/$action?"(controller: "signout")
....
....
}
Plugins in Grails are great method to modularise an application.The documentation suggest to override the artifacts from the plugin in the application, which uses this plugin.
Is it realy the best approach?
Let's describe it by example: There is a domain class "org.User" defined in the plugin. The application overrides this domain class. If I use "grails run-app" then there are no warnings and it works. But Eclipse (GGTS) complains about "Invalid duplicate class definition of class org.User". For some developers it wouldn't matter, but I like the IDE helping on coding by stuf like "autocomplete".
At the end both classes are compiled an put on the java class loader. The application version of the class is loaded before the version of the plugin. The class resolver finds it first and that's why it works. Please correct me if I'm wrong at this point. Is it realy a good idea to have two versions of a class in one class loader?
What are the alternatives?
You can do like Spring Security Core plugin does, provide the User class as a template, so your application that use this plugin can choose between creating his own class or installing your default User class.
The plugin user template is here, and the script responsible to create this in the application is here.
You will need also a config value to know the class to use, and use it dynamic.
P.S: there are good security plugins like Shiro and Spring Security, maybe it's easier to check them instead of create your own.
First I'm new to grails as well website development.I started grails project and studying.
I'm clear about grails concepts like Domain class, controller, view, agile development like this.
While executing grails run-app command, at which point does grails start execution in the framework and run (like main() method in Java)?
Which is the first entry place domain or controller or view or main.gsp in my project from where it is coming from grails framework?
When server starts up, Bootstrap.groovy is executed.
For listening to each request you probably would need to define your own filter.
However the very beginning of every request is the org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.
When a request comes in, grails determines the controller and the action (based on the URL and any UrlMappings you've specified) and calls it. So from your application code's point of view, the starting point is one of your actions.
For example:
If a user requests http://abc.com/book/list, where abc.com is your site, the method def list() in your BookController.groovy is the starting point.
Internally, grails calls each closure in AppFilters.groovy (and other filters defined by you or the plugins you are using) if any before calling your controller's action. If you are developing a very simple app, those wouldn't matter.
Grails incorporates the powerful build system Gant, which is a Groovy wrapper around Apache Ant.
When you run the command : Grails [commad-name],
Grails searches in the following directories for Gant scripts to execute:
USER_HOME/.grails/scripts
PROJECT_HOME/src/main/scripts/
PROJECT_HOME/plugins/*/scripts
GRAILS_HOME/scripts
When you execute Grails run-app command, It will execute the file RunApp.groovy file from above mentioned paths. These are groovy files, once you look into files, you will understand the code inside that.