Using FreeMarker with Apache Sling - freemarker

I'm using a Content Repository (Adobe CQ5) that has Apache Sling underneath. I'd like to render web pages using FreeMarker instead of JSP.
For that to work should I upload the OSGi Freemarker extension to Sling, which is to be found in the contribution part of Sling source code, or should I upload an OSGi version of Freemarker instead? Both jars implement the interface javax.script.ScriptEngineFactory so I don't know which one to use, or to upload both.

I managed to get Freemarker working on CQ5.3 by doing the following:
get the source from svn checkout http://svn.apache.org/repos/asf/sling/trunk sling
go to the contrib/scripting/freemarker and remove all the logging and slf4j references from FreemarkerScriptEngine.java - it throws a 500 error due to NoClassDefFoundException - I think there is a conflict between CQ5 and a version of slf4j included in Freemarker (I didn't dig to find out what was wrong)
build it and install, it should appear in the Sling console under bundles and in the 'script engines' page, and let you use .ftl scripts now.
I did it slightly differently by creating the bundle inside CQ5, importing the code and adding a stock freemarker.jar into the bundle but this should work too as it's basically the same thing.
The problem I had was that the script engine does get listed but the logging prevents it from running, that's why you need to remove the log & slf4j from the engine.

I don't know about the OSGi version of Freemarker; we created our own bundling of Freemarker specially for Sling. If you have success using the OSGi version of Freemarker let us know ;-)
Testing is easy: Just install the bundle through the Web Console and check for the supported Script Engines in the Web Console Configuration Status. If you see the Freemarker Engine listed there, it should work.

Related

Spring boot and javascript node_modules

I´m currently building a spring-boot application, which also uses some javascript-stuff. I use yarn as a package-manager to manage the different js-libraries.
Now I wonder, how I would include these resources into my spring-boot-project? Simply including the whole node_module-folder as a resource seems to be overhead for me, as this doesn´t neccessarily contain only the required sources (for me it is more like my local maven-repo-path). How do I identify, which java-script-resources should be included in my jar in the end, so that I can also reference them in my Thymeleaf-HTML-templates.
I already found the 'frontend-maven-plugin' (https://github.com/eirslett/frontend-maven-plugin) which helps me to install all my yarn-dependencies during build, but it doesn´t care about the build-process, as far as I can see.
Thanks for your help!
Perhaps you should consider using webpack or some other javascript bundler/task runner to bundle your javascript and required dependencies into a single file. Then you can simply include that bundled file in your jar. For example: http://justincalleja.com/2016/04/17/serving-a-webpack-bundle-in-spring-boot/

Using Google App Engine modules for multi-thread backend update of a Cloud endpoints project

I'm building "read-only" webservice (Google Cloud Endpoints as backend for an Android App) so I created a project using maven:
mvn archetype:generate -Dappengine-version=1.9.10 -Dfilter=com.google.appengine.archetypes:
and selecting archetype hello-endpoints-archetype to have some sample code to work on.
This works well and my app is correctly calling the service as expected (and the service is correctly supplying the data in return).
Now I have to implement an "update" service to periodically (4 to 6 times a dya) update the data supplied to the app. So I added a servlet to my project to be called by cron. Trouble is: one of the library used during this update uses multi-threads which cause an AccessControlException to be thrown because apparently multi-thread is only allowed in backend modules...
But after having read dozens of pages on google app engine, I still don't know how to "break" my application into modules so that particular servlet would be run as a backend module while the already existing servlet keep working as they do. So far, all I got was that I should use an EAR application composed of several WAR modules, but I don't even know if my current application is an EAR or not...
I'm using Eclipse Luna, maven 3.2.1 (embeded with Eclipse), google app engine 1.9.10, writing in Java
Could anyone please help me by explaining the directory structure and/or configuration files I have to look at, modify and/or add?
Thanks for any help provided!
You can find an example of multi-modules project here.
However, note that even in backend modules the threading is limited to 50 threads, as stated here.

Does Apache Felix FileInstall works for dynamic configuration of an application?

I am trying to develop an application that can be configured by its users. I need the configuration to be done by installing/updating/stopping/uninstalling bundles. All this should be of course done dynamically during the run-time of the application.
I found a nice framework which is Apache Felix FileInstall that provides a directory in which it seems to add a bundle when you add the bundle file in the directory (update, and remove bundles similarly).
But I can see that this method does not work in my case. I need to have the bundles in the directory but to stop or even uninstall them by my application. And I want to install them when it is appropriate. This is how I am expecting the configuration of my application to be done.
Is what I am trying to achieve supported by Apache FileInstall? Am I making any wrong assumptions about this framework? What are other possible ways that would help me if Apache FileInstall is not enough? Thanks.
You don't need FileInstall for this, just use the OSGi APIs. You specifically mentioned installing, updating, stopping and uninstalling; these are supported with the following API calls respectively:
BundleContext.installBundle
Bundle.update
Bundle.stop
Bundle.uninstall
Incidentally these are exactly the same methods that are called by FileInstall to implements its directory-based bundle management.

Workflow clearification on OSGi Servlet and HTML UI

I need some clearification on the Workflow with OSGi Servlets and HTML Frontend. I am an old school HTML and Javascript programmer and new to all this OSGi, Ajax and JQuery stuff. I am working on an own OSGi server implementation. I created my OSGi Plugin with eclipse following this guide: https://docs.jboss.org/author/display/AS7/Creating+a+new+OSGi+Bundle+using+Eclipse
My Servlet is working perfectly fine so far. It is supposed to handle Database Requests. I coded a Website with HTML and Javascript(JQuery) from which I want to fire Ajax Requests to the Servlet.
My Problem is, that I have no idea how to integrate my HTML/Javascript/JQuery stuff into the OSGi Servlet. I tried to register it as resources within the Servlet Bundle, which worked fine but I'm not sure if this is a very elegant way... It needs to run on the same OSGi Server, but does it need to be in the same Bundle as the Servlet or in its own?
Which kind of project would I create in eclipse for the UI Bundle and how would I export it?
The only thing you need is to make your web resources (html, javascript and css files) available on a url. You could do this manually by hand with a Servlet. If you package the web resources in the bundle, you can get an InputStream to those resources from the BundleContext.getBundle().
An easier approach is to use the Amdatu ResourceHandler. This is a so called "extender" that has the sole purpose of making resources from bundles available on an url.
You need to do the following:
1) Make sure that your resources are packaged in the bundle
2) Add two extra headers to the manifest of the bundle that contains the resources
X-Web-Resource-Version: 1.0
X-Web-Resource: path/to/resources
3) Add the Apache Felix Whiteboard and Amdatu ResourceHandler bundles to your runtime.
Documentation can be found on the Amdatu website: http://amdatu.org/components/web.html (scroll to the bottom of the page).
The Pax Web project might be of interest for you, anyway there is a sample on how to publish resources available.
This sample should also work similar HTTP-Services like Apache Felix HTTP-Service, probably needs some minor tweaks :) .

How to make Axis2 run in an OSGi environment?

I need to access an webservice in an OSGi-Environment (Equinox). I do already have the code for using the webservice as a stand-alone application.
The sample code uses Axis2 this seems to be a problem as I need to add all required libraries as separate bundles. This does not work with Axis2 because the jar files provided by the project are not prepared for OSGi.
I tried to "bundelize" the JAR files via BND but in the end the bundles do not work because of a "Package uses conflict error: org.apache.axis2.addressing"
How to I make Axis2 OSGi-aware?
Alternatively - what is a simple and fool-proof variant for accessing a (soap) webservice in Equinox (plain not Eclipse)? I already tried Apache CXF which failed because it looks like it tries to generate code at runtime and Axis2 is also very difficult as my post shows.
You should consider to embed the Axis2 libraries (and dependencies) into your bundle. In this case Axis2 will not be deployed as own bundle - instead the classes/jars will be copied to into your bundle.
If you are using maven to build your bundle you should have a look at the maven-bundle-plugin and the Embedding dependencies section.
I also would recommend to have a closer look if no OSGi enabled Axis2 libraries are around (I do not use it, so I don't know it but a quick search results in org.apache.axis2.osgi).

Resources