@Override
  public com.liferay.portal.model.Portlet addingService(
      ServiceReference<Portlet> serviceReference) {

    BundleContext bundleContext = _componentContext.getBundleContext();

    Portlet portlet = bundleContext.getService(serviceReference);

    String portletName = (String) serviceReference.getProperty("javax.portlet.name");

    if (Validator.isNull(portletName)) {
      Class<?> clazz = portlet.getClass();

      portletName =
          StringUtil.replace(clazz.getName(), new String[] {".", "$"}, new String[] {"_", "_"});
    }

    String portletId =
        StringUtil.replace(portletName, new String[] {".", "$"}, new String[] {"_", "_"});

    if (portletId.length() > PortletInstance.PORTLET_INSTANCE_KEY_MAX_LENGTH) {

      _log.error(
          "Portlet ID "
              + portletId
              + " has more than "
              + PortletInstance.PORTLET_INSTANCE_KEY_MAX_LENGTH
              + " characters");

      bundleContext.ungetService(serviceReference);

      return null;
    }

    com.liferay.portal.model.Portlet portletModel = _portletLocalService.getPortletById(portletId);

    if (portletModel != null) {
      _log.error("Portlet id " + portletId + " is already in use");

      bundleContext.ungetService(serviceReference);

      return null;
    }

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

    portletModel = addingPortlet(serviceReference, portlet, portletName, portletId);

    if (portletModel == null) {
      bundleContext.ungetService(serviceReference);
    }

    return portletModel;
  }
  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;
    }
  }