That Java Thing, Part 10: Expanding the Plugin - Serving Resources
Thu Nov 12 12:02:24 EST 2015
- That Java Thing, Part 1: The Java Problem in the Community
- That Java Thing, Part 2: Intro to OSGi
- That Java Thing, Part 3: Eclipse Prep
- That Java Thing, Part 4: Creating the Plugin
- That Java Thing, Part 5: Expanding the Plugin
- That Java Thing, Part 6: Creating the Feature and Update Site
- That Java Thing, Part 7: Adding a Managed Bean to the Plugin
- That Java Thing, Part 8: Source Bundles
- That Java Thing, Part 9: Expanding the Plugin - Jars
- That Java Thing, Part 10: Expanding the Plugin - Serving Resources
- That Java Thing, Interlude: Effective Java
- That Java Thing, Part 11: Diagnostics
- That Java Thing, Part 12: Expanding the Plugin - JAX-RS
- That Java Thing, Part 13: Introduction to Maven
- That Java Thing, Part 14: Maven Environment Setup
- That Java Thing, Part 15: Converting the Projects
- That Java Thing, Part 16: Maven Fallout
- That Java Thing, Part 17: My Current XPages Plug-in Dev Environment
After sharing code, one of the handiest uses of a plugin is sharing web resources - stylesheets, JavaScript files, and so forth. This process is similar to the last couple steps in that, though it is not very complicated on the whole, it's pretty non-obvious how to get around to doing it.
To start with, we'll create some resources to serve up. Expand the src/main/resources
folder in your project (it will be slightly more useful to use the "normal" folder version and not the source folder with the brown package icon, due to Eclipse's UI) and go to New → Other, then pick "Folder" within "General". Name the folder "web":
The create within it a folder named "example", and within that folders named "css" and "js":
Then, populate it with some files - I created a basic stylesheet named "style.css" and a JavaScript file name "script.js" and placed them within the css
and js
folders, respectively. It doesn't matter what they contain, as long as it's something you can test later; you could also drag in any files you have from the filesystem.
As a side note, as I did this, creating folders within the hierarchy, Eclipse got a bit wonky about refreshing the list, presumably because of the combined source/normal folder nature of src/main/resources
. This inconvenience is a concession to the way m2e, the Maven integrator for Eclipse, will act later - it makes this folder a source folder automatically, so we may as well get used to it.
Next, create a theme file within src/main/resources
directly (which is to say, not in the web
folder) and name it "example.theme". When you create it, Eclipse will likely have trouble opening up an editor: it will try to use one from the OS, which will almost definitely be invalid. Instead, right-click the file and choose Open With → Text Editor:
Set its contents to:
<theme extends="webstandard" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="platform:/plugin/com.ibm.designer.domino.stylekits/schema/stylekit.xsd" > <resources> <styleSheet href="/.ibmxspres/.extlib/example/css/style.css"/> <script clientSide="true" src="/.ibmxspres/.extlib/example/js/script.js"/> </resources> </theme>
Those paths look so weird because we'll be piggybacking on the ExtLib's resource-provider system.
Now, back to Java to create the classes that will provide these resources to the platform. Because it's less terrifying, we'll start with the StyleKitFactory
, which provides the theme to the server. Right-click on src/main/java
and create a new Java class:
- Set its package to "com.example.xsp.theme"
- Set its name to "ExampleStyleKitFactory"
- Add two interfaces:
com.ibm.xsp.stylekit.StyleKitFactory
andcom.ibm.xsp.stylekit.StyleKitListFactory
. The former will provide the theme to the server, while the latter will tell Designer about the name it can use
Set the class contents to this:
package com.example.xsp.theme; import java.io.InputStream; import java.util.Arrays; import java.util.List; import com.ibm.xsp.stylekit.StyleKitFactory; import com.ibm.xsp.stylekit.StyleKitListFactory; public class ExampleStyleKitFactory implements StyleKitFactory, StyleKitListFactory { private static final String[] THEMES = { "example" //$NON-NLS-1$ }; private static final List<String> THEMES_LIST = Arrays.asList(THEMES); @Override public String[] getThemeIds() { return THEMES; } @Override public InputStream getThemeAsStream(String themeId, int scope) { if (scope == StyleKitFactory.STYLEKIT_GLOBAL) { if (THEMES_LIST.contains(themeId)) { return getThemeFromBundle(themeId + ".theme"); //$NON-NLS-1$ } } return null; } @Override public InputStream getThemeFragmentAsStream(String themeId, int scope) { return null; } private InputStream getThemeFromBundle(final String fileName) { ClassLoader cl = getClass().getClassLoader(); return cl.getResourceAsStream(fileName); } }
There's quite a bit going on here! Some of it is due to the nature of the task and some of it comes from my own built-up habits that come in handy as the class grows. Towards the top of the class, THEMES
contains an array of theme names known by the plugin - in this case, just the one, but it's good to have this standardized. THEMES_LIST
contains a List
wrapper around that array for programmatic convenience later.
The getThemeIds
method is from StyleKitListFactory
and is used by Designer to generate its list of available themes in the GUI. Implementing this interface isn't required, but it's a cross-the-Ts sort of thing.
The remaining methods implement StyleKitFactory
, which provides the server with the theme data itself. getThemeAsStream
is called by the runtime when it's searching for a theme requested by the app, so it contains a couple checks to make sure that the request is indeed intended for a plugin-based (global) theme with a name that this plugin knows about. It then uses a small utility method, getThemeFromBundle
, to get the theme as an InputStream
from the plugin's internal filesystem. Technically, this could be anything - it could construct the theme on the fly, fetch it from a URL, or get it from anywhere else, as long as it returns an InputStream
, but this version is the most common.
getThemeFragmentAsStream
is an interesting beast. The Extension Library uses it to hook in extra theme info for its Bootstrap themes on the fly. This is presumably useful for ad-hoc theme hierarchies and avoiding the theme-inheritance cap, but we don't have any need for it here.
Finally, the "$NON-NLS-1$" comment business is because I've developed a habit of enabling Eclipse's translated-strings warnings - the comments denote to the IDE that the hard-coded strings on those lines are not intended to be translated. You can ignore those if you so desire.
Moving on, now it's time to set up our resource provider, which will serve up the web resources. Before that, we'll take a minor detour back to the Activator
class to fix up a method signature. Add "public" to the getContext
method:
public static BundleContext getContext() { return context; }
Now, we're going to implement the resource-loading class, modeled after the one from Bootstrap4XPages. Create a new class:
- Set its package to "com.example.xsp.minifier"
- Set its name to "ExampleLoader"
- Set its superclass to
com.ibm.xsp.extlib.minifier.ExtLibLoaderExtension
This class will have three methods of importance to us: getOSGiBundle
(which is obligatory), loadCSSShortcuts
, and getResourceURL
:
package com.example.xsp.minifier; import java.net.URL; import javax.servlet.http.HttpServletRequest; import org.osgi.framework.Bundle; import com.example.xsp.Activator; import com.ibm.commons.util.DoubleMap; import com.ibm.xsp.extlib.minifier.ExtLibLoaderExtension; import com.ibm.xsp.extlib.util.ExtLibUtil; public class ExampleLoader extends ExtLibLoaderExtension { private static final String[] LIBRARY_RESOURCE_NAMESPACES = { "example" //$NON-NLS-1$ }; @Override public Bundle getOSGiBundle() { return Activator.getContext().getBundle(); } @Override public void loadCSSShortcuts(DoubleMap<String, String> aliases, DoubleMap<String, String> prefixes) { if(prefixes != null) { for(int i = 0; i < LIBRARY_RESOURCE_NAMESPACES.length; i++) { String namespace = LIBRARY_RESOURCE_NAMESPACES[i]; prefixes.put("9T0a" + i, "/.ibmxspres/.extlib/" + namespace); //$NON-NLS-1$ //$NON-NLS-2$ } } } @Override public URL getResourceURL(HttpServletRequest request, String name) { for(String namespace : LIBRARY_RESOURCE_NAMESPACES) { if(name.startsWith(namespace)) { String path = "/web/" + name; //$NON-NLS-1$ return ExtLibUtil.getResourceURL(getOSGiBundle(), path); } } return null; } }
Once again, there's a lot going on, but you can see some general similarities to the theme provider. The getOSGiBundle
method makes use of the Activator.getContext
method we just modified, while the other two methods use the same sort of "internal array of Strings" pattern as before to make it easy to add more entries to the list down the line.
Things are a little strange in the loadCSSShortcuts
method. The goal of this method is to provide the shorthand codes used in the minified URLs for resources, and so each plugin should make sure to provide unique ones. However, I don't know of any coordinated enforcer class that does this "making sure" for us, so you kind of just have to make up a unique string of characters that should be unique. Here, I picked "9T0a" as the base, just because they usually look like that.
The getResourceURL
method is a little simpler, and is the equivalent of the earlier getThemeAsStream
- given the incoming request for a resource name, it checks to see if it falls within its bailiwick and, if so, uses a method to provide a URL to the server environment.
These methods, like the ones in the theme provider, can be generally used as a "just drop them in the plugin" sort of thing without modification. There are other potential tweaks you can make, but it's probably best to keep it simple.
There's two more steps to getting these classes working. The theme provider needs to be registered in the plugin.xml
and the resource loader needs to be added in the Activator
. First, for the theme provider. Add another extension point entry to the plugin.xml
:
Your plugin.xml source should look something like:
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <plugin> <extension point="com.ibm.commons.Extension"> <service class="com.example.xsp.ExampleLibrary" type="com.ibm.xsp.Library"> </service> </extension> <extension point="com.ibm.commons.Extension"> <service class="com.example.xsp.theme.ExampleStyleKitFactory" type="com.ibm.xsp.stylekit.StyleKitFactory"> </service> </extension> </plugin>
For the loader, open the Activator
class and add a new line to the start
method to add the resource loader to the ExtLib's bank. The class should now look like:
package com.example.xsp; import java.util.logging.Level; import java.util.logging.Logger; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import com.example.xsp.minifier.ExampleLoader; import com.ibm.xsp.extlib.minifier.ExtLibLoaderExtension; public class Activator implements BundleActivator { private static BundleContext context; public static final Logger log = Logger.getLogger(Activator.class.getPackage().getName()); static { log.setLevel(Level.FINEST); } public static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; if(log.isLoggable(Level.INFO)) { log.info("Starting Example XPages Library"); } ExtLibLoaderExtension.getExtensions().add(new ExampleLoader()); } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } }
(Despite appearances, the "start" method there is not actually commented out - its blue appearance is a bug in the JavaScript syntax highlighter used in this blog)
Now to see if all this work has paid off: build the update site and install it in Designer and Domino. When you relaunch Designer and open the "Xsp Properties" page of the NSF you're working with, you should see the "example" theme in the list of options (as long as you have a recent ExtLib release installed in Designer - the capability was added post-9.0.1):
Additionally, when you open the app on the web, you should see your CSS and JS files included. One final note about these resources: though the URLs in the theme start with "/.ibmxspres/.extlib/example", URLs references from non-XSP files (say, referencing font files from with CSS) should start instead with "/xsp/.ibmxspres/.extlib/example". The XPages runtime adds that "/xsp" when generating URLs for pages, but it doesn't do any such processing for static references.
So... that was more complicated than it seemed at the start! Still, once it's in place, it's all pretty much "set it and forget it" - in the future, you can add files and themes more easily, just adjusting the static arrays in the appropriate classes as necessary.
Finally, commit the changes and take a relaxing breath.
Tony Westwell - Mon Nov 30 10:08:07 EST 2015
After uploading the new site to both the server and the domino designer, I see the option for the new theme in the drop down list. But when I create an XPage there is no reference to the css and js files that I added.
Tony Westwell - Fri Dec 04 14:16:05 EST 2015
Found the problem, the url in the theme should have had "/web/" in there too.