In this tutorial we will show how to run ModeShape from within a Java EE application deployed on JBoss AS 7, as a back-end for a PrimeFaces GUI component (The FileUpload).
Installing ModeShape on JBoss AS 7
The first thing we will need to do is downloading the latest ModeShape AS7 kit (version 3.1.2.Final or later).
This kit is a ZIP archive that is intended to be unzipped directly into the AS7 installation. Doing so will not override any of the files in a standard AS7 installation.
Once unzipped the ModeShape AS7 kit into your AS7 distribution you will get the following upgrades:
- ModeShape schema (modeshape_1_0.xsd)
- ModeShape modules (javax/jcr/, org/modeshape,org/hibernate/search-engine/4.1/,org/apache/lucene/3.5/)
- ModeShape configuration (standalone-modeshape.xml)
Besides this, the kit also deploys the "modeshape-rest" web application as an exploded WAR file. This web application provides the RESTful API to all of the repositories and their workspaces. If you don't plan to use the RESTful API, simply undeploy it or remove it.
Finally, regarding security, one of the ModeShape modules contains two files (under modules/org/modeshape/main/conf) that define the users and roles for the default security domain used by ModeShape:
You can edit these files to add users, or define your different security domain by defining for each user the ModeShape roles found in the modeshape-roles.properties file.
ModeShape in action !
Now let's test ModeShape with an AS7 application which uses PrimeFaces to pickup a file to be uploaded. In our case, instead of copying the file into the Server filesystem, we will upload it into the JCR repository.
Here's the sample upload.xhtml page: (Please refer to this tutorial for more information about this component)
And this is the FileUploadController ManagedBean which performs the Job:
Two things to notice: the first is that we are injecting the JCR Repository directly in our application which avoids us lots of headaches (starting/stopping the engine)
Next, as you can see, there's one more copyFile2 method which shows how to performs uploading with a single call using the JcrTools utility class.
If you are curious to know where the sample JCR repository stores its data, just have a look at the modeshape subsystem:
As you can see this basic repository uses as file store the folder JBOSS_HOME/standalone/data and runs in NON_XA mode.
That's all. In order to compile and package your application, we need to include in our pom.xml, the required JBoss AS (and Java EE) + ModeShape + PrimeFaces dependencies and a couple of additional dependencies such as org.apache.commons and commons-fileupload which are required for running PrimeFaces' file uploader
Here's the application GUI in action:
As you can see, once you have gone through some uploads, the filestore's binary subfolder gets crowded with the pictures you have uploaded.