DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Flex for J2EE Developers: The Case for Granite Data Services
  • Top ALM Tools and Solutions Providers
  • How To REST With Rails and ActiveResource: Part Three
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Trending

  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • Docker Base Images Demystified: A Practical Guide
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise

A Simple Plugin System for Web Applications

By 
Bozhidar Bozhanov user avatar
Bozhidar Bozhanov
·
Aug. 06, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
10.8K Views

Join the DZone community and get the full member experience.

Join For Free

We need to make multiple web-based projects with a lot of shared functionality. For that, some sort of a plugin system would be a good option (as an alternative to copy-pasting stuff). Some frameworks (like grails) have the option to make web plugins, but most don’t, so something custom-made is to be implemented.

First, let’s define what is the required functionality. The “plugin”:

  • should be included simply by importing via maven/ivy
  • should register all classes (either automatically, or via a one-line configuration) in a dependency injection container, if one is used
  • should be vertical – i.e. contain all files, from javascript, css and templates, through controllers, to service layer classes
  • should not require complex configuration that needs to be copy-pasted from project to project
  • should allow easy development and debugging without redeployment

The java classes are put into a jar file and added to the lib directory, therefore to the classpath, so that’s the easy part. But we need to get the web resources extracted to the respective locations, where they can be used by the rest of the code. There are three general approaches to that: build-time extraction, runtime extraction and runtime loading from the classpath.

The last approach would require a controller (or servlet) that loads the resources from the classpath (the respective jar), cache them, and serve them. That has a couple of significant drawbacks, one of which is that being in a jar, they can’t be easily replaced during development. Working with classpath resources is also tricky, as you don’t know the names of the files in advance.

The other two approaches are very similar. Grails, for example, uses the build-time extraction – the plugin is a zip file, containing all the needed resources, and they are extracted to the respective locations while the project is built. This is fine, but it would require a little more configuration (maven, in our case), which would also probably have to be copied over from project to project.

So we picked the runtime extraction approach. It happens on startup – when the application is loaded, a startup listener of some sort (a spring components with @PostConstruct in our case) iterates through all jar files in the lib folder, and extracts the files from a specific folder (e.g. “web”). So, the structure of the jar file looks like this:

com
   company
      pkg
         Foo.class
         Bar.class
web
   plugin-name
       css
           main.css
       js
          foo.js
          bar.js
       images
          logo.png
       views
          foo.jsp
          bar.jsp

The end-result is that on after the application is started, you get all the needed web resources accessible from the application, so you can include them in the pages (views) of your main application.

And the code that does the extraction is rather simple (using zip4j for the zip part). This can be a servlet context listener, rather than a spring bean – it doesn’t make any difference.

/**
 * Component that locates modules (in the form of jar files) and extracts their web elements, if any, on startup
 *
 * @author Bozhidar
 */
@Component
public class ModuleExtractor {

	private static final Logger logger = LoggerFactory.getLogger(ModuleExtractor.class);

	@Inject
	private ServletContext ctx;

	@SuppressWarnings("unchecked")
	@PostConstruct
	public void init() {
		File lib = new File(ctx.getRealPath("/WEB-INF/lib"));
		File[] jars = lib.listFiles();
		String targetPath = ctx.getRealPath("/");
		String viewPath = "/WEB-INF/views"; //that can be made configurable
		for (File jar : jars) {
			try {
				ZipFile file = new ZipFile(jar);
				for (FileHeader header : (List<FileHeader>) file.getFileHeaders()) {
					if (header.getFileName().startsWith("web/") && !fileExists(header)) {
						// extract views in WEB-INF (inaccessible to the outside world)
						// all other files are extracted in the root of the application
						if (header.getFileName().contains("/views/")) {
							file.extractFile(header, targetPath + viewPath);
						} else {
							file.extractFile(header, targetPath);
						}
					}
				}
			} catch (ZipException ex) {
				logger.warn("Error opening jar file and looking for a web-module in: " + jar, ex);
			}
		}
	}

	private boolean fileExists(FileHeader header) {
		return new File(ctx.getRealPath(header.getFileName())).exists();
	}
}

So, in order to make a plugin, you just make a maven project with jar packaging, and add it as dependency to your main project, everything else is taken care of. You might need to register the ModuleExtractor if classpath scanning for beans is not enabled (or you choose to make it a listener), but that’s it.

Note: this solution doesn’t aim to be a full-featured plugin system that solves all problems. It doesn’t support versioning, submodules, etc. That’s why the title is “simple”. But you can do many things with it, and it’s has a very low complexity.

Note 2: Servlet 3.0 has a native way of doing almost the same thing, but it doesn’t allow dynamically changing the assets. If you don’t need to change them and don’t need save-and-refresh, then it’s probably the better option.


application Web Service

Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Flex for J2EE Developers: The Case for Granite Data Services
  • Top ALM Tools and Solutions Providers
  • How To REST With Rails and ActiveResource: Part Three
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!