A HibernateRepositoryAdaptee
allows you to customize the loading of HibernateRepository
instances.
The following code shows you how to configure an adaptee (which reads the connections config from a
ServletContext
). When a RegistryLoader
invokes the ServletRepositoryLoader.load
method, it
passes in several parameters that the repository loader will use to dynamically instantiate the
appropriate Repository
implementation. In this case, if the Repository is of type
HibernateRepositoryAdaptor
,
then the method creates an adaptee and sets it on the repository. Now when the RepositoryLoader
invokes
repository.load
, the repository will delegate to the adaptee when loading.
public class ServletRepositoryLoader implements RepositoryLoader { <...> public Repository loadRepository(String fileUri, String repositoryClass, Hashtable initParams) throws IOException { Hashtable props = (initParams != null) ? initParams : new Hashtable(); if (fileUri == null || fileUri.trim().equals("")) throw new IOException("File uri must be provided."); if (repositoryClass == null || repositoryClass.trim().equals("")) throw new IOException("Repository class name must be provided: File Name = " + fileUri + ", Properties = " + props.toString()); Repository repository = null; InputStream inputStream = servletContext.getResourceAsStream(fileUri); if(inputStream == null) { logger.severe("Could not find " + fileUri); throw new IOException("Could not find " + fileUri); } String message = "File Name = " + fileUri + ", Repository Class = " + repositoryClass + ", Properties = " + props.toString(); try { Class c = Class.forName(repositoryClass); repository = (Repository) c.newInstance(); if(repository instanceof HibernateRepositoryAdaptor) { ServletHibernateRepositoryAdaptee adaptee = new ServletHibernateRepositoryAdaptee(servletContext); ((HibernateRepositoryAdaptor) repository).setHibernateRepositoryAdaptee(adaptee); } repository.setRepositoryRegistry(repositoryRegistry); repository.load(inputStream, props); } catch (IOException e) { throw new IOException("JV-REG-001:" + e.toString() + " : " + message); } catch (Exception e) { throw new IOException("JV-REG-001:" + e.toString() + " : " + message); } catch (Error e) { throw new IOException("JV-REG-001:" + e.toString() + " : " + message); } return repository; }