`
cywhoyi
  • 浏览: 421093 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

A Simple Plugin System for Web Applications

 
阅读更多

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:

01 com
02    company
03       pkg
04          Foo.class
05          Bar.class
06 web
07    plugin-name
08        css
09            main.css
10        js
11           foo.js
12           bar.js
13        images
14           logo.png
15        views
16           foo.jsp
17           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 ibyoung
 */
@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.

分享到:
评论

相关推荐

    Bootstrap.By.Example.1785288

    By the end of this book, you will be familiar with the development of a plugin for the framework and Bootstrap's world which is popular for fast paced front-end web development, used in countless ...

    sip-4.15.5.zip

    a plugin system that encourages the decoupling, and easy reuse, of components through the use of services and extension points a declarative type system where class attributes define the types of ...

    sip-4.15.5.tar.gz

    a plugin system that encourages the decoupling, and easy reuse, of components through the use of services and extension points a declarative type system where class attributes define the types of ...

    spring-boot-reference.pdf

    50.5. Hypermedia for Actuator Web Endpoints 50.6. Actuator Web Endpoint Paths 50.7. CORS Support 50.8. Implementing Custom Endpoints 50.8.1. Receiving Input Input type conversion 50.8.2. Custom Web ...

    Prentice.Hall.C++.GUI.Programming.with.Qt.4.2nd.Edition.2008.chm

    Using QTextBrowser as a Simple Help Engine Using Qt Assistant for Powerful Online Help Part III: Advanced Qt Chapter 18. Internationalization Working with Unicode Making Applications ...

    ICS delphixe10源码版

    ICS V7 is a stable release that may still be updated for major bugs, but not for new releases of Delphi, latest it supported was XE3. ICS V8 is the current development release which is held in a ...

    CE中文版-启点CE过NP中文.exe

    Fixed the plugin system for DBVM Fixed DBVM memory allocations when smaller than 4KB Additions and changes: Added translation strings for the all type settings You can now drop files into the auto ...

    EurekaLog_7.5.0.0_Enterprise

    18)..Fixed: Possible "Unit XYZ was compiled with a different version of ABC" when using packages 19)..Fixed: FastMM shared MM compatibility 20)..Fixed: Minor bugs in stack tracing (which usually ...

    ModernGadgets_1.6.3.rmskin

    ModernGadgets requires HWiNFO, a free system monitoring utility, for full functionality. Alternatively, some information in CPU Meter may be retrieved through the CoreTemp or SpeedFan applications. ...

    CommonsWare.The.Busy.Coders.Guide.to.Android.Development.Version.8.2.2017

    Along the way, it covers how to embed the WebKit Web browser in your application, how to have your application use data from other installed applications (and vice versa!) or off the Internet, and ...

    Visual C++ 编程资源大全(英文源码 表单)

    (400KB)&lt;END&gt;&lt;br&gt;78,AdvancedPrev.zip A simple class that helps provide faster Print Preview within MFC Doc/View applications(38KB)&lt;END&gt;&lt;br&gt;79,mditab.zip A dockable bar containing a tabbed list ...

    超级有影响力霸气的Java面试题大全文档

    超级有影响力的Java面试题大全文档 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。...

    《activmq in action 》

    6.2.1. Configuring the Simple Authentication Plugin ............. 159 6.2.2. Configuring the JAAS Plugin ....................................... 161 6.3. Authorization ...................................

Global site tag (gtag.js) - Google Analytics