Is it possible to add Grails MVC classes at deployment time? - grails

I'm writing a Grails app which I'd like 3rd parties to augment at runtime. Ideally they would be able to add a JAR/WAR to the webapp directory which contains new domain, controller and service classes, new views, and other content.
Is there a simple way to do this within grails? Would it be simplest to create a startup script which copies the new classes etc. into the relevant directories and then updates grails.xml and web.xml?

You will be able to do this in version 2 of grails in which plugins will be also OSGI plugins http://jira.codehaus.org/browse/GRAILS/fixforversion/15421

It seems that the Grails plugins will actually fit quite well for this: http://www.grails.org/Understanding+Plugins
A plugin can do just about anything... One thing a plugin cannot do though is modify the web-app/WEB-INF/web.xml or web-app/WEB-INF/applicationContext.xml files. A plugin can participate in web.xml generation, but not modify the file or provide a replacement. A plugin can NEVER change the applicationContext.xml file, but can provide runtime bean definitions

Related

Vaadin #JavaScript for different builds

I use Vaadin #JavaScript annotation to load JavaScript files for my application. It works great but I would need different JavaScript loaded for differents builds.
The idea is to have something like these:
#JavaScript("url.from.properties.or.pom")
So for DEV I would get #JavaScript("https://example.com/test/js/embed.js") and for PROD #JavaScript("https://example.com/production/js/embed.js"). The script url value should be taken from application.properties or pom.xml.
I cannot figure out how to do it. I use Vaadin 8 with Maven and Spring Boot. Thank you in advance.
There's no direct support for what you want to do, but I can come up with three different solutions that you could consider.
Register a DependencyFilter that dynamically rewrites the dependency URL from the annotation depending on the situation.
Create separate Java classes for each case (with all the actual functionality in a shared super class). You can then have either runtime logic or use e.g. different Spring configurations to choose exactly which class to use.
Remove the #JavaScript annotation and instead call JavaScript.eval from onAttach to somehow dynamically inject the script you want.

How do i build and application that works as both OSGI bundles and a WAR

I am developing and application as a series of OSGI bundles which will run on Karaf.
The Bundles has a fair bit of interoperability, both exposing a bunch of services, and consuming services from each other.
However, I would very much like to be able to build a WAR file of the application alongside the bundles. There are a few clients that quite simply won't allow an OSGI container on their servers.
So my question is, what is the best way to separate the OSGI service logic from the application logic? The activators are no trouble, as they are just unused in the WAR deployment, but i have a lot of calls that retrives services from the bundlecontext, like bundleContext.getServiceReference(stuffISortOfNeed.class)scattered around my code. bundleContext is currently a static object set by the activator.
This won't do in a WAR container that knows nothing of OSGI.
Is it possible to somehow hide the BundleContext and the getServiceReference calls away from the actual application? Preferably i would love an injection-like approach where i could define my services with #Annotations and define and injector for OSGI and one for PlainOldJava.
To really have POJOs you could use Blueprint XML to register your Web Application Bundle (WAB).
A good example can be found in the pax web project.
By using XML over annotations you only have an extra resource file that can be ignored if you assemble you bundles as libs in a war file, no extra dependencies on specific annotations.

Include more than one filter from existing JAR into a Grails project

I have a Grails project and want to add existing filters from a JAR file.
I used the WebXmlConfig plugin, mentioned in this answer:
How to add filters to a Grails app
and that worked great for a single filter, but I can't figure out how to extend that to more than one filter.
Do I need to change approach and edit the web.xml template directly?
I'd use the pluginator plugin and put the definitions in doWithWebDescriptor just like you would in a plugin - you can add as many elements as you want. It's a slick plugin that lets apps do things that are generally only supported in plugins, like conveniently editing web.xml (although with a seriously weird DSL) and registering custom artifact types.

How to reuse Grails application's domain classes?

I have already create a Grails application. For some reasons I need to create another console application for someone to modify database data.
Is it possible to package Grails application as a JAR library, so that the console application can reuse those domain classes?
Or, I add/create some classes in Grails application and package it as a JAR and run as console application?
If no better answer, probably I will use the batch-launcher plugin to do that.
You can put the domain classes in a JAR and tell Grails that these are your domain classes by adding a Hibernate XML file (in grails-app/conf/hibernate) that refers to the classes in this JAR. You can use this JAR in any other Java/Groovy application, but obviously they'll only have the persistence methods (dynamic finders, save(), etc.) when used in a Grails app.

What is a Grails Plugin? What does it mean to Install a Plugin?

I used Grails recently, and added Grails plugin for JQuery, but I don't think it did anything more than just copy some jQuery files over.
So far, I have seen info only on 'how to install and use' plugins...but I can't find anything that describes the concept of a plugin.
Can somebody please tell me, what is a Grails Plugin? And what does it mean to 'Install' a plugin?
A Grails plugin is (or should be) a self-contained bundle of functionality that can be installed into a Grails application. When a Grails plugin is installed, it can do any of the following:
define additional Spring beans
modify the generated web.xml
add new methods to the application's artefacts (controllers, domain classes, services, etc.)
provide new tag libraries
make additional resources and classes available to the application
provide new Grails commands
For example, when you install the JQuery plugin
the JQuery JavaScript files are added to the application
a new Grails tag <jq:jquery> is added to the application
a new Grails command grails install-plugin jquery is added to the application
When you install a Grails plugin, that plugin's functionality is made available to the installing application. However, the plugin itself is not actually copied into the application, only the plugin name and version is added to the application's application.properties file. The plugin itself is downloaded to $HOME/.grails and the application loads it from there.
The structure of a Grails plugin project is identical to that of a Grails application, with the exception of a configuration file (known as a plugin descriptor) that is included in a plugin's root directory.
Well, a Grails plugin is some piece of software that extends the frameworks funcionalities in some manner. Generally, installing a plugin in Grails means copying it to your Grails folder, so projects can refer to it and Grails will know where to find it.
Grails plugins have this folder structure:
grails-app
controllers
domain
taglib
service
etc
lib
src
java
groovy
web-app
js
css
So anything it has there will also be available to the application that uses it. For example, the Searchable plugin has a service class which you can use to perform advanced searchs in your own domain classes .
The jQuery plugin you mentioned has the jQuery .js file, and a tag to include that file.
For information on creating plugins, see http://grails.org/doc/latest/guide/12.%20Plug-ins.html
A plugin is just a set of functionality around a desired purpose. So the Spring Security plugin provides a way to lock down your app, assign roles to users, restrict access, whatever. The Searchable plugin allows you to integrate advanced searching into your app. There are lots of plugins
The point is to provide useful functionality so that you don't have to implement hard things yourself. Someone did something useful, and they wanted to contribute back to the community, so they organized their functionality and made it available.
A plugin is code and configuration, like any functionality you would implement yourself.
There is some documentation here: http://grails.org/doc/latest/ref/Plug-ins/Usage.html

Resources