/**
  * Unregisters all JSP page servlets for a plugin.
  *
  * @param webXML the web.xml file containing JSP page names to servlet class file mappings.
  */
 public static void unregisterServlets(File webXML) {
   if (!webXML.exists()) {
     Log.error(
         "Could not unregister plugin servlets, file "
             + webXML.getAbsolutePath()
             + " does not exist.");
     return;
   }
   // Find the name of the plugin directory given that the webXML file
   // lives in plugins/[pluginName]/web/web.xml
   String pluginName = webXML.getParentFile().getParentFile().getParentFile().getName();
   try {
     SAXReader saxReader = new SAXReader(false);
     saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     Document doc = saxReader.read(webXML);
     // Find all <servelt-mapping> entries to discover name to URL mapping.
     List names = doc.selectNodes("//servlet-mapping");
     for (int i = 0; i < names.size(); i++) {
       Element nameElement = (Element) names.get(i);
       String url = nameElement.element("url-pattern").getTextTrim();
       // Destroy the servlet than remove from servlets map.
       GenericServlet servlet = servlets.get(pluginName + url);
       if (servlet != null) {
         servlet.destroy();
       }
       servlets.remove(pluginName + url);
       servlet = null;
     }
   } catch (Throwable e) {
     Log.error(e.getMessage(), e);
   }
 }
 /**
  * Handles a request for a Servlet. If one is found, request handling is passed to it. If no
  * servlet is found, a 404 error is returned.
  *
  * @param pathInfo the extra path info.
  * @param request the request object.
  * @param response the response object.
  * @throws ServletException if a servlet exception occurs while handling the request.
  * @throws IOException if an IOException occurs while handling the request.
  */
 private void handleServlet(
     String pathInfo, HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   // Strip the starting "/" from the path to find the JSP URL.
   GenericServlet servlet = getServlet(pathInfo);
   if (servlet != null) {
     servlet.service(request, response);
   } else {
     response.setStatus(HttpServletResponse.SC_NOT_FOUND);
   }
 }
 @Override
 public void init(ServletConfig config) throws ServletException {
   // TODO Auto-generated method stub
   super.init(config);
   String state = config.getInitParameter("st");
   System.out.println("OneServ.init() ---->" + state);
 }
 @Override
 public void init(final ServletConfig servletConfig) throws ServletException {
   super.init(servletConfig);
   final String brokerIdParameter = servletConfig.getInitParameter(BROKER_ID_PARAMETER_NAME);
   if (!EmptyCheck.isEmpty(brokerIdParameter)) {
     brokerId = brokerIdParameter;
   }
 }
 /**
  * Registers all JSP page servlets for a plugin.
  *
  * @param manager the plugin manager.
  * @param plugin the plugin.
  * @param webXML the web.xml file containing JSP page names to servlet class file mappings.
  */
 public static void registerServlets(PluginManager manager, Plugin plugin, File webXML) {
   pluginManager = manager;
   if (!webXML.exists()) {
     Log.error(
         "Could not register plugin servlets, file "
             + webXML.getAbsolutePath()
             + " does not exist.");
     return;
   }
   // Find the name of the plugin directory given that the webXML file
   // lives in plugins/[pluginName]/web/web.xml
   String pluginName = webXML.getParentFile().getParentFile().getParentFile().getName();
   try {
     // Make the reader non-validating so that it doesn't try to resolve external
     // DTD's. Trying to resolve external DTD's can break on some firewall configurations.
     SAXReader saxReader = new SAXReader(false);
     try {
       saxReader.setFeature(
           "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     } catch (SAXException e) {
       Log.warn("Error setting SAXReader feature", e);
     }
     Document doc = saxReader.read(webXML);
     // Find all <servlet> entries to discover name to class mapping.
     List classes = doc.selectNodes("//servlet");
     Map<String, Class> classMap = new HashMap<String, Class>();
     for (int i = 0; i < classes.size(); i++) {
       Element servletElement = (Element) classes.get(i);
       String name = servletElement.element("servlet-name").getTextTrim();
       String className = servletElement.element("servlet-class").getTextTrim();
       classMap.put(name, manager.loadClass(plugin, className));
     }
     // Find all <servelt-mapping> entries to discover name to URL mapping.
     List names = doc.selectNodes("//servlet-mapping");
     for (int i = 0; i < names.size(); i++) {
       Element nameElement = (Element) names.get(i);
       String name = nameElement.element("servlet-name").getTextTrim();
       String url = nameElement.element("url-pattern").getTextTrim();
       // Register the servlet for the URL.
       Class servletClass = classMap.get(name);
       if (servletClass == null) {
         Log.error("Unable to load servlet, " + name + ", servlet-class not found.");
         continue;
       }
       Object instance = servletClass.newInstance();
       if (instance instanceof GenericServlet) {
         // Initialize the servlet then add it to the map..
         ((GenericServlet) instance).init(servletConfig);
         servlets.put(pluginName + url, (GenericServlet) instance);
       } else {
         Log.warn("Could not load " + (pluginName + url) + ": not a servlet.");
       }
     }
   } catch (Throwable e) {
     Log.error(e.getMessage(), e);
   }
 }
  private void _addTaglibSupportFTL(Map<String, Object> contextObjects, PageContext pageContext)
      throws Exception {

    // FreeMarker servlet application

    final Servlet servlet = (Servlet) pageContext.getPage();

    GenericServlet genericServlet = null;

    if (servlet instanceof GenericServlet) {
      genericServlet = (GenericServlet) servlet;
    } else {
      genericServlet = new GenericServletWrapper(servlet);

      genericServlet.init(pageContext.getServletConfig());
    }

    ServletContextHashModel servletContextHashModel =
        new ServletContextHashModel(genericServlet, ObjectWrapper.DEFAULT_WRAPPER);

    contextObjects.put(
        PortletDisplayTemplateConstants.FREEMARKER_SERVLET_APPLICATION, servletContextHashModel);

    // FreeMarker servlet request

    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

    HttpRequestHashModel requestHashModel =
        new HttpRequestHashModel(request, response, ObjectWrapper.DEFAULT_WRAPPER);

    contextObjects.put(
        PortletDisplayTemplateConstants.FREEMARKER_SERVLET_REQUEST, requestHashModel);

    // Taglib Liferay hash

    TemplateHashModel taglibLiferayHash =
        FreeMarkerTaglibFactoryUtil.createTaglibFactory(pageContext.getServletContext());

    contextObjects.put(PortletDisplayTemplateConstants.TAGLIB_LIFERAY_HASH, taglibLiferayHash);
  }
  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);

    try {
      String home = servletConfig.getInitParameter("penrose.home");

      System.out.println("Starting Penrose Server at " + home + "...");
      penroseServer = new PenroseServer(home);
      penroseServer.start();

      System.out.println("Penrose Server started.");

    } catch (Exception e) {
      System.out.println("Failed starting Penrose Server: " + e.getMessage());
      throw new ServletException(e);
    }
  }
  /** Close OpenKM and free resources */
  public static synchronized void stop(GenericServlet gs) {
    if (!running) {
      throw new IllegalStateException("OpenKM not started");
    }

    // Shutdown plugin framework
    ExtensionManager.getInstance().shutdown();

    try {
      if (!Config.SYSTEM_OPENOFFICE_PATH.equals("")) {
        if (log == null && gs != null) {
          gs.log("*** Shutting down OpenOffice manager ***");
        } else {
          log.info("*** Shutting down OpenOffice manager ***");
        }

        DocConverter.getInstance().stop();
      }
    } catch (Throwable e) {
      log.warn(e.getMessage(), e);
    }

    if (hasConfiguredDataStore) {
      if (log == null && gs != null)
        gs.log("*** Shutting down datastore garbage collection... ***");
      else log.info("*** Shutting down datastore garbage collection... ***");
      dsgc.cancel();
    }

    if (Config.SCHEDULE_MAIL_IMPORTER > 0) {
      if (log == null && gs != null) gs.log("*** Shutting down user mail importer ***");
      else log.info("*** Shutting down user mail importer ***");
      umi.cancel();
    }

    if (Config.MANAGED_TEXT_EXTRACTION_SCHEDULE > 0) {
      if (log == null && gs != null) gs.log("*** Shutting down text extractor worker ***");
      else log.info("*** Shutting down text extractor worker ***");
      tew.cancel();
    }

    if (log == null && gs != null) gs.log("*** Shutting down repository info... ***");
    else log.info("*** Shutting down repository info... ***");
    ri.cancel();

    if (log == null && gs != null) gs.log("*** Shutting down UI Notification... ***");
    else log.info("*** Shutting down UI Notification... ***");
    uin.cancel();

    if (log == null && gs != null) gs.log("*** Shutting down cron... ***");
    else log.info("*** Shutting down cron... ***");
    cron.cancel();

    if (log == null && gs != null) gs.log("*** Shutting down watchdog... ***");
    else log.info("*** Shutting down watchdog... ***");
    wd.cancel();

    if (Config.UPDATE_INFO) {
      if (log == null && gs != null) gs.log("*** Shutting down update info... ***");
      else log.info("*** Shutting down update info... ***");
      ui.cancel();
    }

    // Cancel timers
    dsgcTimer.cancel();
    umiTimer.cancel();
    riTimer.cancel();
    cronTimer.cancel();
    uinTimer.cancel();
    wdTimer.cancel();
    uiTimer.cancel();
    tewTimer.cancel();

    if (log == null && gs != null) gs.log("*** Shutting down repository... ***");
    else log.info("*** Shutting down repository... ***");

    if (Config.USER_ITEM_CACHE) {
      // Serialize
      try {
        log.info("*** Cache serialization ***");
        UserItemsManager.serialize();
        UserNodeKeywordsManager.serialize();
      } catch (DatabaseException e) {
        log.warn(e.getMessage(), e);
      }
    }

    try {
      // Preserve system user config
      if (!Config.REPOSITORY_NATIVE) {
        JcrRepositoryModule.shutdown();
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }

    if (log == null && gs != null) gs.log("*** Repository shutted down ***");
    else log.info("*** Repository shutted down ***");

    try {
      if (log == null && gs != null) gs.log("*** Ejecute stop script ***");
      else log.info("*** Ejecute stop script ***");
      File script = new File(Config.HOME_DIR + File.separatorChar + Config.STOP_SCRIPT);
      ExecutionUtils.runScript(script);
      File jar = new File(Config.HOME_DIR + File.separatorChar + Config.STOP_JAR);
      ExecutionUtils.getInstance().runJar(jar);
    } catch (Throwable e) {
      log.warn(e.getMessage(), e);
    }

    if (log == null && gs != null) gs.log("*** Shutting down workflow engine... ***");
    else log.info("*** Shutting down workflow engine... ***");
    JbpmContext jbpmContext = JBPMUtils.getConfig().createJbpmContext();
    jbpmContext.getJbpmConfiguration().getJobExecutor().stop();
    jbpmContext.getJbpmConfiguration().close();
    jbpmContext.close();

    // OpenKM is stopped
    running = false;
  }