+ * The supported initialization parameters of this servlet are: + *
javax.jcr.Repository".
+ * jackrabbit-repository". The home directory is
+ * automatically created during servlet initialization if it does
+ * not already exist.
+ * repository.xml" within the configured repository home
+ * directory. A standard configuration file is automatically copied to
+ * the configured location during servlet initialization if the file
+ * does not already exist.
+ * + * The repository servlet can also be mapped to the URL space. See + * {@link AbstractRepositoryServlet} for the details. + * + * @since 1.4 + */ +public class JackrabbitRepositoryServlet extends AbstractRepositoryServlet { + + /** + * Serial version UID. + */ + private static final long serialVersionUID = 7102770011290708450L; + + /** + * Repository instance. + */ + private JackrabbitRepository repository; + + /** + * Starts the repository instance and makes it available in the + * servlet context. + * + * @throws ServletException if the repository can not be started + */ + public void init() throws ServletException { + try { + File home = new File(getInitParameter( + "repository.home", "jackrabbit-repository")); + if (!home.exists()) { + log("Creating repository home directory: " + home); + home.mkdirs(); + } + + File config = new File(getInitParameter( + "repository.config", + new File(home, "repository.xml").getPath())); + if (!config.exists()) { + log("Creating default repository configuration: " + config); + createDefaultConfiguration(config); + } + + repository = RepositoryImpl.create(RepositoryConfig.create( + config.toURI(), home.getPath())); + } catch (RepositoryException e) { + throw new ServletException("Failed to start Jackrabbit", e); + } + + super.init(); + } + + /** + * Removes the repository from the servlet context and shuts it down. + */ + public void destroy() { + super.destroy(); + repository.shutdown(); + } + + /** + * Returns the repository instance. + * + * @return repository instance + */ + protected Repository getRepository() { + return repository; + } + + /** + * Copies the default repository configuration file to the given location. + * + * @param config path of the configuration file + * @throws ServletException if the configuration file could not be copied + */ + private void createDefaultConfiguration(File config) + throws ServletException { + try { + OutputStream output = new FileOutputStream(config); + try { + InputStream input = + RepositoryImpl.class.getResourceAsStream("repository.xml"); + try { + byte[] buffer = new byte[8192]; + int n = input.read(buffer); + while (n != -1) { + output.write(buffer, 0, n); + n = input.read(buffer); + } + } finally { + input.close(); + } + } finally { + output.close(); + } + } catch (IOException e) { + throw new ServletException( + "Failed to copy default configuration: " + config, e); + } + } + +} Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/servlet/JackrabbitRepositoryServlet.java ------------------------------------------------------------------------------ svn:eol-style = native