📄 repositorystartupservlet.java
字号:
/** * Creates a new Repository based on the configuration and initializes the * {@link #repository} field if successful. * * @throws ServletException if an error occurs */ private void initRepository() throws ServletException { // get repository config File repHome; try { repHome = new File(config.getRepositoryHome()).getCanonicalFile(); } catch (IOException e) { log.error("Repository startup configuration invalid: " + e.toString()); throw new ServletException("Repository startup configuration invalid: " + e.toString()); } String repConfig = config.getRepositoryConfig(); InputStream in = getServletContext().getResourceAsStream(repConfig); if (in == null) { try { in = new FileInputStream(new File(repConfig)); } catch (FileNotFoundException e) { // fallback to old config try { in = new FileInputStream(new File(repHome, repConfig)); } catch (FileNotFoundException e1) { log.error("Repository startup configuration invalid: " + e1.toString()); throw new ServletException("Repository startup configuration invalid: " + e.toString()); } } } try { repository = createRepository(new InputSource(in), repHome); } catch (RepositoryException e) { throw new ServletException("Error while creating repository", e); } } /** * Shuts down the repository. If the repository is an instanceof * {@link JackrabbitRepository} it's {@link JackrabbitRepository#shutdown()} * method is called. in any case, the {@link #repository} field is * <code>nulled</code>. */ private void shutdownRepository() { if (repository instanceof JackrabbitRepository) { ((JackrabbitRepository) repository).shutdown(); } repository = null; } /** * Creates the repository instance for the given config and homedir. * Subclasses may override this method of providing own implementations of * a {@link Repository}. * * @param is input source of the repository config * @param homedir the repository home directory * @return a new jcr repository. * @throws RepositoryException if an error during creation occurs. */ protected Repository createRepository(InputSource is, File homedir) throws RepositoryException { RepositoryConfig config = RepositoryConfig.create(is, homedir.getAbsolutePath()); return RepositoryImpl.create(config); } /** * Binds the repository to the JNDI context * @throws ServletException if an error occurs. */ private void registerJNDI() throws ServletException { JNDIConfig jc = config.getJndiConfig(); if (jc.isValid() && jc.enabled()) { try { jndiContext = new InitialContext(jc.getJndiEnv()); jndiContext.bind(jc.getJndiName(), repository); log.info("Repository bound to JNDI with name: " + jc.getJndiName()); } catch (NamingException e) { throw new ServletException("Unable to bind repository using JNDI.", e); } } } /** * Unbinds the repository from the JNDI context. */ private void unregisterJNDI() { if (jndiContext != null) { try { jndiContext.unbind(config.getJndiConfig().getJndiName()); } catch (NamingException e) { log("Error while unbinding repository from JNDI: " + e); } } } /** * Registers the repository to an RMI registry configured in the web * application. See <a href="#registerAlgo">Registration with RMI</a> in the * class documentation for a description of the algorithms used to register * the repository with an RMI registry. * @throws ServletException if an error occurs. */ private void registerRMI() throws ServletException { RMIConfig rc = config.getRmiConfig(); if (!rc.isValid() || !rc.enabled()) { return; } // try to create remote repository Remote remote; try { Class clazz = Class.forName(getRemoteFactoryDelegaterClass()); RemoteFactoryDelegater rmf = (RemoteFactoryDelegater) clazz.newInstance(); remote = rmf.createRemoteRepository(repository); } catch (RemoteException e) { throw new ServletException("Unable to create remote repository.", e); } catch (NoClassDefFoundError e) { throw new ServletException("Unable to create RMI repository. jcr-rmi.jar might be missing.", e); } catch (Exception e) { throw new ServletException("Unable to create RMI repository. jcr-rmi.jar might be missing.", e); } try { System.setProperty("java.rmi.server.useCodebaseOnly", "true"); Registry reg = null; // first try to create the registry, which will fail if another // application is already running on the configured host/port // or if the rmiHost is not local try { // find the server socket factory: use the default if the // rmiHost is not configured RMIServerSocketFactory sf; if (rc.getRmiHost().length() > 0) { log.debug("Creating RMIServerSocketFactory for host " + rc.getRmiHost()); InetAddress hostAddress = InetAddress.getByName(rc.getRmiHost()); sf = getRMIServerSocketFactory(hostAddress); } else { // have the RMI implementation decide which factory is the // default actually log.debug("Using default RMIServerSocketFactory"); sf = null; } // create a registry using the default client socket factory // and the server socket factory retrieved above. This also // binds to the server socket to the rmiHost:rmiPort. reg = LocateRegistry.createRegistry(rc.rmiPort(), null, sf); } catch (UnknownHostException uhe) { // thrown if the rmiHost cannot be resolved into an IP-Address // by getRMIServerSocketFactory log.info("Cannot create Registry", uhe); } catch (RemoteException e) { // thrown by createRegistry if binding to the rmiHost:rmiPort // fails, for example due to rmiHost being remote or another // application already being bound to the port log.info("Cannot create Registry", e); } // if creation of the registry failed, we try to access an // potentially active registry. We do not check yet, whether the // registry is actually accessible. if (reg == null) { log.debug("Trying to access existing registry at " + rc.getRmiHost() + ":" + rc.getRmiPort()); try { reg = LocateRegistry.getRegistry(rc.getRmiHost(), rc.rmiPort()); } catch (RemoteException re) { log.error("Cannot create the reference to the registry at " + rc.getRmiHost() + ":" + rc.getRmiPort(), re); } } // if we finally have a registry, register the repository with the // rmiName if (reg != null) { log.debug("Registering repository as " + rc.getRmiName() + " to registry " + reg); reg.bind(rc.getRmiName(), remote); // when successfull, keep references this.rmiRepository = remote; log.info("Repository bound via RMI with name: " + rc.getRmiUri()); } else { log.info("RMI registry missing, cannot bind repository via RMI"); } } catch (RemoteException e) { throw new ServletException("Unable to bind repository via RMI.", e); } catch (AlreadyBoundException e) { throw new ServletException("Unable to bind repository via RMI.", e); } } /** * Unregisters the repository from the RMI registry, if it has previously * been registered. */ private void unregisterRMI() { if (rmiRepository != null) { // drop strong referenece to remote repository rmiRepository = null; // unregister repository try { Naming.unbind(config.getRmiConfig().getRmiUri()); } catch (Exception e) { log("Error while unbinding repository from JNDI: " + e); } } } /** * Return the fully qualified name of the class providing the remote * repository. The class whose name is returned must implement the * {@link RemoteFactoryDelegater} interface. * <p/> * Subclasses may override this method for providing a name of a own * implementation. * * @return getClass().getName() + "$RMIRemoteFactoryDelegater" */ protected String getRemoteFactoryDelegaterClass() { return getClass().getName() + "$RMIRemoteFactoryDelegater"; } /** * Returns an <code>RMIServerSocketFactory</code> used to create the server * socket for a locally created RMI registry. * <p/> * This implementation returns a new instance of a simple * <code>RMIServerSocketFactory</code> which just creates instances of * the <code>java.net.ServerSocket</code> class bound to the given * <code>hostAddress</code>. Implementations may overwrite this method to * provide factory instances, which provide more elaborate server socket * creation, such as SSL server sockets. * * @param hostAddress The <code>InetAddress</code> instance representing the * the interface on the local host to which the server sockets are * bound. * @return A new instance of a simple <code>RMIServerSocketFactory</code> * creating <code>java.net.ServerSocket</code> instances bound to * the <code>rmiHost</code>. */ protected RMIServerSocketFactory getRMIServerSocketFactory( final InetAddress hostAddress) { return new RMIServerSocketFactory() { public ServerSocket createServerSocket(int port) throws IOException { return new ServerSocket(port, -1, hostAddress); } }; } /** * optional class for RMI, will only be used, if RMI server is present */ protected static abstract class RemoteFactoryDelegater { public abstract Remote createRemoteRepository(Repository repository) throws RemoteException; } /** * optional class for RMI, will only be used, if RMI server is present */ protected static class RMIRemoteFactoryDelegater extends RemoteFactoryDelegater { // only used to enforce linking upon Class.forName() static String FactoryClassName = ServerAdapterFactory.class.getName(); public Remote createRemoteRepository(Repository repository) throws RemoteException { return new ServerAdapterFactory().getRemoteRepository(repository); } } //-------------------------------------------------< Installer Routines >--- /** * {@inheritDoc} */ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (repository == null) { redirect(req, resp, "/bootstrap/missing.html"); } else { redirect(req, resp, "/bootstrap/running.html"); } } /** * {@inheritDoc} */ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (repository != null) { redirect(req, resp, "/bootstrap/reconfigure.html"); } else { int rc = new Installer(bootstrapConfigFile, getServletContext()).installRepository(req); switch (rc) { case Installer.C_INSTALL_OK: // restart rep restart(); if (repository == null) { redirect(req, resp, "/bootstrap/error.html"); } else { redirect(req, resp, "/bootstrap/success.html"); } break; case Installer.C_INVALID_INPUT: redirect(req, resp, "/bootstrap/missing.html"); break; case Installer.C_CONFIG_EXISTS: case Installer.C_BOOTSTRAP_EXISTS: case Installer.C_HOME_EXISTS: redirect(req, resp, "/bootstrap/exists.html"); break; case Installer. C_HOME_MISSING: case Installer.C_CONFIG_MISSING: redirect(req, resp, "/bootstrap/notexists.html"); break; case Installer.C_INSTALL_ERROR: redirect(req, resp, "/bootstrap/error.html"); break; } } } /** * Helper function to send a redirect response respecting the context path. * * @param req the request * @param resp the response * @param loc the location for the redirect * @throws ServletException if an servlet error occurs. * @throws IOException if an I/O error occurs. */ private void redirect(HttpServletRequest req, HttpServletResponse resp, String loc) throws ServletException, IOException { String cp = req.getContextPath(); if (cp == null || cp.equals("/")) { cp = ""; } resp.sendRedirect(cp + loc); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -