Created in: 2007-06-03 13:02:12
Author: martin
Size: 0 bytes
Last updated: 2007-06-03 13:02:12
There isn't related documents
If you are reading this article, probably you should know that jLibrary is a document management system, that allows you to do things like configuring document metadata, adding categories to documents, or creating relationships between documents among other things. Rules files are nothing more than plain documents with a specific definition language. This article, will show you how to integrate Drools editors and jLibrary editors transforming jLibrary into a Rules Management Repository. You will be able to use Drools editors and leverage all their feactures and at the same time you will be able to manage those rule files from jLibrary.
Note that this article can be extrapolated to any other tools providing plug-ins, and it could be seen like an article explaining how to integrate external plug-ins within jLibrary to handle jLibrary documents.
Please note that in addition to the jLibrary CVS modules that the article recommends to download, you must download the org.jlibrary.drools module for this article.
Drools IDE project is hosted under a Subversion system. So if you haven't done it yet, you should donwload and install Subclipse to be able to work with SVN repositories within Eclipse. There is a very good article in IBM developerWorks explaining how to install and use Subclipse subversion plug-in.
The Drools IDE project hosts all the Eclipse plug-ins that we want to use. You can download Drools IDE source code from the jboss subversion repository. To do that you can open the SVN Perspective, then right click on the SVN Repository view and select the menu option New -> Repository Location .... This will open the Add SVN Repository dialog. Enter the SVN repository location in that dialog just like the image shows.

After that, you can open the HEAD node and checkout the drools-ide project:

A new dialog will appear asking you how do you want to checkout the project. Select Checkout as project in the workspace and press Finish to checkout the project:

After the checkout process is executed you should have the drools-ide project in your project explorer view:

As the previous screenshot shows there are some compilation bugs that must be solved. The first problem is that the drools-ide project does not include all the library dependencies that it needs for running correctly. So you must download those dependencies by your own. There are several ways to do this:
Whatever step you choosed you should end up with a new directory named lib and with all the required libraries inside:

Ok. Now that the compilation problems have been solved it seems that the org.jlibrary.drools project is claiming about access restrictions and then is not able to access to drools-ide project resources.
To fix this problems you have to expose some of the drools-ide packages to other plug-ins. So, open the drools-ide plugin.xml file. Go to the Runtime tab. And then add the org.drools.ide.dsl.editor and org.drools.ide.editors packages within the Exported Packages section as the following image shows:

This should remove all the problems and now you should have a clean workspace:

Now that everything has been fixed you can run jLibrary with the new plug-ins. If you don't know how to create a jLibrary configuration, please take a look to the developers guide.
Before running the jLibrary configuration you have to enable the drools-ide and org.jlibrary.drools plug-ins and all their dependencies (deselect all, then select the workspace plug-ins and press the Add Required Plug-ins button).

Now, it's time for opening or creating a repository and for adding some DRL or DSL files. You have some test files under the samples folder within the org.jlibrary.drools project. After you have created those files you can open them. You will see how the Drools IDE editors are embedded within jLibrary editors.


And that's all. Now you can work with your rule files within jLibrary and leverage all the benefits that this offers.
Ok, so know I'm going start talking about the technical details and how I was able to embedd the Drools editors within jLibrary. As you will see, this is an extremely easy task.
jLibrary has a main multitab editor (org.jlibrary.client.ui.editor.GenericEditor) that provides all the basic infrastructure like the metadata tab, the relations tab and also forwards recursively some events like the doSave one, so you don't need to worry about that. Now, you only have to take care of overriding some methods to obtain all the desired features. I will talk about this on the next paragraphs with some code snippets of the org.jlibrary.drools.editors.JLibraryDSLEditor class. You can also take a look to the org.jlibrary.drools.editors.JLibraryDRLEditor, but they are practically clones.
Init method
public class JLibraryDSLEditor extends GenericEditor {
private DSLEditor dslEditor = null;
public void init(IEditorSite site,
IEditorInput editorInput) throws PartInitException {
super.init(site, editorInput);
dslEditor = new DSLEditor();
}
You can override the init method to perform any initialization tasks for your plug-in. In this example I only create a Drools editor instance and I store it on a class attribute.
Hiding pages
/**
* @see GenericEditor#createContentsPage()
*/
protected ContentsFormPage createContentsPage() {
return null;
}
The GenericEditor has methods to obtain the different jLibrary editor pages, like getContentsPage, getMetadataPage and getRelationsPage. You can overwrite this editors to create your own custom pages. Also, if you return null from one of these methods then the page won't be showed. In the example above you can see how I return null to do hide the default jLibrary contents page.
Adding new pages
/**
* @see GenericEditor#addStartPages()
*/
protected void addStartPages() {
super.addStartPages();
try {
FileEditorInput editorInput = createDSLEditorInput(getEditorInput());
if (editorInput != null) {
int index = addPage(dslEditor,editorInput);
setPageText(index,"DSL Editor");
}
} catch (Exception e) {
e.printStackTrace();
}
}
The snipped above shows how to add a new page to a jLibrary editor. This is the key point for integration. In the code above, I add the DSL editor that I created on the init method to a new page. So finally you end with a jLibrary editor showing a Drools editor in a new page. You can control the page title, location, etc. The only extra thing you have to care about is the editor input.
Handling editor inputs
One problem you can find integrating jLibrary with your editors is editor input incompatibility. jLibrary opens documents with a org.jlibrary.client.ui.editor.NodeEditorInput editor input. This is an internal editor input that contains many information about the document in addition to its binarry contents. If your plug-in isn't compatible with this kind of editor the you would have to transform it to a more suitable format. For example, if your plug-in is compatible with org.eclipse.ui.part.FileEditorInput editor input (like many) here you can see how to do such transformation:
protected FileEditorInput createDSLEditorInput(IEditorInput input) {
NodeEditorInput nodeEditorInput = (NodeEditorInput)input;
Node node = nodeEditorInput.getNode();
WorkspaceCache cache = (WorkspaceCache)
LocalCacheService.getInstance().getLocalCache();
IFile file;
try {
file = cache.getFile(node);
return new FileEditorInput(file);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
jLibrary workspace cache works internally with Eclipse resources, so it's really easy to obtain the opened resource and create a new FileEditorInput instance with that data. Now you can simply set this editor on your plug-in as it was showed on the above paragraphs.
As you can see, integrating jLibrary with external plug-ins is a really easy task. I'm sure that some improvements could be done on the architecture. If you have any suggerences please forward them to the developers mailing list.