protected void checkWebResources(
      BundleContext bundleContext,
      String contextName,
      ClassLoader classLoader,
      ServiceRegistrations serviceRegistrations) {

    serviceRegistrations.addServiceRegistration(createDefaultServlet(bundleContext, contextName));
    serviceRegistrations.addServiceRegistration(
        createJspServlet(bundleContext, contextName, classLoader));
    serviceRegistrations.addServiceRegistration(
        createPortletServlet(bundleContext, contextName, classLoader));
    serviceRegistrations.addServiceRegistration(
        createRestrictPortletServletRequestFilter(bundleContext, contextName, classLoader));
  }
  protected void checkResourceBundles(
      BundleContext bundleContext,
      ClassLoader classLoader,
      com.liferay.portal.model.Portlet portletModel,
      ServiceRegistrations serviceRegistrations) {

    if (Validator.isBlank(portletModel.getResourceBundle())) {
      return;
    }

    for (Locale locale : LanguageUtil.getAvailableLocales()) {
      ResourceBundle resourceBundle =
          ResourceBundleUtil.getBundle(portletModel.getResourceBundle(), locale, classLoader);

      Dictionary<String, Object> properties = new HashMapDictionary<>();

      properties.put("javax.portlet.name", portletModel.getPortletId());
      properties.put("language.id", LocaleUtil.toLanguageId(locale));

      ServiceRegistration<ResourceBundle> serviceRegistration =
          bundleContext.registerService(ResourceBundle.class, resourceBundle, properties);

      serviceRegistrations.addServiceRegistration(serviceRegistration);
    }
  }
  @Override
  public void removedService(
      ServiceReference<Portlet> serviceReference, com.liferay.portal.model.Portlet portletModel) {

    portletModel.unsetReady();

    ServiceRegistrations serviceRegistrations =
        _serviceRegistrations.get(serviceReference.getBundle());

    if (serviceRegistrations == null) {
      return;
    }

    BundlePortletApp bundlePortletApp = serviceRegistrations.getBundlePortletApp();

    bundlePortletApp.removePortlet(portletModel);

    serviceRegistrations.removeServiceReference(serviceReference);

    BundleContext bundleContext = _componentContext.getBundleContext();

    bundleContext.ungetService(serviceReference);

    _portletInstanceFactory.destroy(portletModel);

    List<Company> companies = _companyLocalService.getCompanies();

    for (Company company : companies) {
      PortletCategory portletCategory =
          (PortletCategory) WebAppPool.get(company.getCompanyId(), WebKeys.PORTLET_CATEGORY);

      portletCategory.separate(portletModel.getRootPortletId());
    }

    PortletBag portletBag = PortletBagPool.remove(portletModel.getRootPortletId());

    if (portletBag != null) {
      portletBag.destroy();
    }
  }
  protected BundlePortletApp createBundlePortletApp(
      Bundle bundle, ClassLoader classLoader, ServiceRegistrations serviceRegistrations) {

    BundlePortletApp bundlePortletApp = serviceRegistrations.getBundlePortletApp();

    if (bundlePortletApp != null) {
      return bundlePortletApp;
    }

    com.liferay.portal.model.Portlet portalPortletModel =
        _portletLocalService.getPortletById(CompanyConstants.SYSTEM, PortletKeys.PORTAL);

    bundlePortletApp = new BundlePortletApp(bundle, portalPortletModel, _httpServiceEndpoint);

    createContext(bundle, bundlePortletApp, serviceRegistrations);

    initLogger(classLoader);

    serviceRegistrations.setBundlePortletApp(bundlePortletApp);

    serviceRegistrations.doConfiguration(classLoader);

    BundleContext bundleContext = bundle.getBundleContext();

    Dictionary<String, Object> properties = new HashMapDictionary<>();

    properties.put(
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
        bundlePortletApp.getServletContextName());
    properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, Boolean.TRUE.toString());

    serviceRegistrations.addServiceRegistration(
        bundleContext.registerService(ServletContextListener.class, bundlePortletApp, properties));

    return bundlePortletApp;
  }
  protected void createContext(
      Bundle bundle, PortletApp portletApp, ServiceRegistrations serviceRegistrations) {

    ServletContextHelper servletContextHelper = new BundlePortletServletContextHelper(bundle);

    BundleContext bundleContext = bundle.getBundleContext();

    Dictionary<String, Object> properties = new HashMapDictionary<>();

    properties.put(
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, portletApp.getServletContextName());
    properties.put(
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH,
        "/" + portletApp.getServletContextName());
    properties.put(Constants.SERVICE_RANKING, 1000);

    serviceRegistrations.addServiceRegistration(
        bundleContext.registerService(
            ServletContextHelper.class, servletContextHelper, properties));
  }
  protected com.liferay.portal.model.Portlet addingPortlet(
      ServiceReference<Portlet> serviceReference,
      Portlet portlet,
      String portletName,
      String portletId) {

    warnPorletProperties(portletName, serviceReference);

    Bundle bundle = serviceReference.getBundle();

    BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);

    ServiceRegistrations serviceRegistrations = getServiceRegistrations(bundle);

    BundlePortletApp bundlePortletApp =
        createBundlePortletApp(bundle, bundleWiring.getClassLoader(), serviceRegistrations);

    com.liferay.portal.model.Portlet portletModel = buildPortletModel(bundlePortletApp, portletId);

    portletModel.setPortletName(portletName);

    String displayName =
        GetterUtil.getString(
            serviceReference.getProperty("javax.portlet.display-name"), portletName);

    portletModel.setDisplayName(displayName);

    Class<?> portletClazz = portlet.getClass();

    portletModel.setPortletClass(portletClazz.getName());

    collectJxPortletFeatures(serviceReference, portletModel);
    collectLiferayFeatures(serviceReference, portletModel);

    PortletContextBag portletContextBag =
        new PortletContextBag(bundlePortletApp.getServletContextName());

    PortletContextBagPool.put(bundlePortletApp.getServletContextName(), portletContextBag);

    PortletBagFactory portletBagFactory = new BundlePortletBagFactory(portlet);

    portletBagFactory.setClassLoader(bundleWiring.getClassLoader());
    portletBagFactory.setServletContext(bundlePortletApp.getServletContext());
    portletBagFactory.setWARFile(true);

    try {
      portletBagFactory.create(portletModel);

      checkWebResources(
          bundle.getBundleContext(),
          bundlePortletApp.getServletContextName(),
          bundleWiring.getClassLoader(),
          serviceRegistrations);

      checkResourceBundles(
          bundle.getBundleContext(),
          bundleWiring.getClassLoader(),
          portletModel,
          serviceRegistrations);

      List<Company> companies = _companyLocalService.getCompanies();

      deployPortlet(serviceReference, portletModel, companies);

      checkResources(serviceReference, portletModel, companies);

      portletModel.setReady(true);

      if (_log.isInfoEnabled()) {
        _log.info("Added " + serviceReference);
      }

      serviceRegistrations.addServiceReference(serviceReference);

      return portletModel;
    } catch (Exception e) {
      _log.error("Portlet " + portletId + " from " + bundle + " failed to initialize", e);

      return null;
    }
  }