There are three main persistence APIs which are used in Confluence:
Because Bandana is the primary persistence API used by plugin developers, it will be covered in more detail below.
Confluence uses the open source persistence framework Hibernate. Confluence 2.2.x uses Hibernate version 2.1.8.
Each object to be persisted has a *.hbm.xml file which sits in the same directory as the associated class in the Confluence web application. For example, Label.class has an associated Label.hbm.xml which describes how label objects will be persisted. The particular details vary from class to class, but typically include:
All this data is expressed in the standard Hibernate mapping format. In some cases, there is a single mapping file for all subclasses of a particular class. For example, ContentEntityObject.hbm.xml includes mappings for pages, news, mail and space descriptions.
The Hibernate mapping files are listed in mappingResources bean in applicationContext.xml.
Although it might be possible to extend Confluence's database through Hibernate, this is not recommended. There are a few downfalls with extending our Hibernate configuration:
Avoid using Confluence's database to store custom data – use content properties or Bandana instead.
Bandana is an Atlassian framework for persistence which uses XStream to convert arbitrary Java objects into XML for storage. The concepts used in Bandana are very simple:
String and the value can be any Object (it should typically implement Serializable).
If you are defining your own type within a plugin, please provide a no argument constructor to avoid class loading issues |
Based on this design, the BandanaManager has methods for storing and retrieving values from a context by key:
void setValue(BandanaContext context, String key, Object value) - store a value against a key in the Bandana context.Object getValue(BandanaContext context, String key) - get a key's value from the Bandana context. Returns null if no matching context and key exists.Object getValue(BandanaContext context, String key, boolean lookUp) - same as above, except if lookUp is true and the context is a space context, this method will also check the global context if no matching key is found in the space context.For plugins, it is recommended to use a key for your Bandana values that includes the full package name of your plugin. For example, a theme plugin might use a key like org.acme.confluence.mytheme.importantPreference.
Prior to Confluence 2.3, this XML was written to the filesystem in the Confluence home directory. The file config/confluence-global.bandana.xml stores the global context, and there is a file config/spaceKey/confluence-space.bandana.xml with the configuration for each space. In Confluence 2.3 and above, Bandana data is written to the BANDANA table in the database, with three columns for context, key and an XML-serialized value.
To get access to the BandanaManager from your plugin code, normally you only need to include a private BandanaManager field with an associated setter method. Spring will automatically call the setter method before the first time your plugin is called.
public class MyMacro extends BaseMacro {
private BandanaManager bandanaManager;
// setter called by Spring
public void setBandanaManager(BandanaManager bandanaManager) {
this.bandanaManager = bandanaManager;
}
// main method of macro
public String execute(...) {
// do stuff with bandanaManager
return "...";
}
}
|
Another form of persistence, content properties are key-value pairs associated with a ContentEntityObject and stored in the database.