private Version _getVersion(String version) {
    if (version.equals("${current-version}")) {
      version = ReleaseInfo.getVersion();
    }

    return Version.getInstance(version);
  }
  protected String getMainReleaseVersion() {
    if (_mainReleaseVersion != null) {
      return _mainReleaseVersion;
    }

    String releaseVersion = ReleaseInfo.getVersion();

    int pos = releaseVersion.lastIndexOf(StringPool.PERIOD);

    _mainReleaseVersion = releaseVersion.substring(0, pos) + ".0";

    return _mainReleaseVersion;
  }
  protected void registerPortalInitialized() {
    Registry registry = RegistryUtil.getRegistry();

    Map<String, Object> properties = new HashMap<>();

    properties.put("module.service.lifecycle", "portal.initialized");
    properties.put("service.vendor", ReleaseInfo.getVendor());
    properties.put("service.version", ReleaseInfo.getVersion());

    _portalModuleServiceLifecycleServiceRegistration =
        registry.registerService(
            ModuleServiceLifecycle.class, new ModuleServiceLifecycle() {}, properties);

    properties = new HashMap<>();

    properties.put("bean.id", ServletContext.class.getName());
    properties.put("original.bean", Boolean.TRUE);
    properties.put("service.vendor", ReleaseInfo.getVendor());

    _servletContextServiceRegistration =
        registry.registerService(ServletContext.class, getServletContext(), properties);
  }
/** @author Michael C. Han */
public class XMLDefinitionExporter implements DefinitionExporter {

  public void afterPropertiesSet() {
    _namespace = "urn:liferay.com:liferay-workflow_" + _version;
    _schemaVersion = StringUtil.replace(_version, StringPool.PERIOD, StringPool.UNDERLINE);
  }

  @Override
  public String export(long kaleoDefinitionId) throws PortalException {
    Definition definition = _definitionBuilder.buildDefinition(kaleoDefinitionId);

    return doExport(definition);
  }

  @Override
  public String export(long companyId, String name, int version) throws PortalException {

    Definition definition = _definitionBuilder.buildDefinition(companyId, name, version);

    return doExport(definition);
  }

  public void setDefinitionBuilder(DefinitionBuilder definitionBuilder) {
    _definitionBuilder = definitionBuilder;
  }

  public void setVersion(String version) {
    _version = version;
  }

  protected String doExport(Definition definition) {
    try {
      Document document = SAXReaderUtil.createDocument();

      Element workflowDefinitionElement = document.addElement("workflow-definition");

      workflowDefinitionElement.addAttribute(
          "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      workflowDefinitionElement.addAttribute(
          "xsi:schemaLocation",
          "urn:liferay.com:liferay-workflow_"
              + _version
              + " http://www.liferay.com/dtd/liferay-workflow-definition_"
              + _schemaVersion
              + ".xsd");
      workflowDefinitionElement.addNamespace("", "urn:liferay.com:liferay-workflow_" + _version);

      Element nameElement = workflowDefinitionElement.addElement("name", _namespace);

      nameElement.addText(definition.getName());

      if (Validator.isNotNull(definition.getDescription())) {
        Element descriptionElement =
            workflowDefinitionElement.addElement("description", _namespace);

        descriptionElement.addText(definition.getDescription());
      }

      Element versionElement = workflowDefinitionElement.addElement("version", _namespace);

      versionElement.addText(String.valueOf(definition.getVersion()));

      Collection<Node> nodes = definition.getNodes();

      for (Node node : nodes) {
        NodeExporter nodeExporter = NodeExporterRegistry.getNodeExporter(node.getNodeType());

        nodeExporter.exportNode(node, workflowDefinitionElement, _namespace);
      }

      return document.formattedString();
    } catch (IOException ioe) {
      throw new SystemException("Unable to export definition", ioe);
    }
  }

  private DefinitionBuilder _definitionBuilder;
  private String _namespace;
  private String _schemaVersion;
  private String _version = ReleaseInfo.getVersion();
}
  private Set<String> _readThemes(
      String servletContextName,
      ServletContext servletContext,
      String themesPath,
      boolean loadFromServletContext,
      String xml,
      PluginPackage pluginPackage)
      throws Exception {

    Set<String> themeIds = new HashSet<String>();

    if (xml == null) {
      return themeIds;
    }

    Document document = SAXReaderUtil.read(xml, true);

    Element rootElement = document.getRootElement();

    Version portalVersion = _getVersion(ReleaseInfo.getVersion());

    boolean compatible = false;

    Element compatibilityElement = rootElement.element("compatibility");

    if (compatibilityElement != null) {
      List<Element> versionElements = compatibilityElement.elements("version");

      for (Element versionElement : versionElements) {
        Version version = _getVersion(versionElement.getTextTrim());

        if (version.includes(portalVersion)) {
          compatible = true;

          break;
        }
      }
    }

    if (!compatible) {
      _log.error("Themes in this WAR are not compatible with " + ReleaseInfo.getServerInfo());

      return themeIds;
    }

    ThemeCompanyLimit companyLimit = null;

    Element companyLimitElement = rootElement.element("company-limit");

    if (companyLimitElement != null) {
      companyLimit = new ThemeCompanyLimit();

      Element companyIncludesElement = companyLimitElement.element("company-includes");

      if (companyIncludesElement != null) {
        companyLimit.setIncludes(_getCompanyLimitIncludes(companyIncludesElement));
      }

      Element companyExcludesElement = companyLimitElement.element("company-excludes");

      if (companyExcludesElement != null) {
        companyLimit.setExcludes(_getCompanyLimitExcludes(companyExcludesElement));
      }
    }

    ThemeGroupLimit groupLimit = null;

    Element groupLimitElement = rootElement.element("group-limit");

    if (groupLimitElement != null) {
      groupLimit = new ThemeGroupLimit();

      Element groupIncludesElement = groupLimitElement.element("group-includes");

      if (groupIncludesElement != null) {
        groupLimit.setIncludes(_getGroupLimitIncludes(groupIncludesElement));
      }

      Element groupExcludesElement = groupLimitElement.element("group-excludes");

      if (groupExcludesElement != null) {
        groupLimit.setExcludes(_getGroupLimitExcludes(groupExcludesElement));
      }
    }

    long timestamp = ServletContextUtil.getLastModified(servletContext);

    List<Element> themeElements = rootElement.elements("theme");

    for (Element themeElement : themeElements) {
      ContextReplace themeContextReplace = new ContextReplace();

      themeContextReplace.addValue("themes-path", themesPath);

      String themeId = themeElement.attributeValue("id");

      if (servletContextName != null) {
        themeId = themeId + PortletConstants.WAR_SEPARATOR + servletContextName;
      }

      themeId = PortalUtil.getJsSafePortletId(themeId);

      themeContextReplace.addValue("theme-id", themeId);

      themeIds.add(themeId);

      Theme theme = _themes.get(themeId);

      if (theme == null) {
        theme = new ThemeImpl(themeId);
      }

      theme.setTimestamp(timestamp);

      PluginSetting pluginSetting = pluginSettingLocalService.getDefaultPluginSetting();

      theme.setPluginPackage(pluginPackage);
      theme.setDefaultPluginSetting(pluginSetting);

      theme.setThemeCompanyLimit(companyLimit);
      theme.setThemeGroupLimit(groupLimit);

      if (servletContextName != null) {
        theme.setServletContextName(servletContextName);
      }

      theme.setLoadFromServletContext(loadFromServletContext);

      String name = GetterUtil.getString(themeElement.attributeValue("name"), theme.getName());

      String rootPath =
          GetterUtil.getString(themeElement.elementText("root-path"), theme.getRootPath());

      rootPath = themeContextReplace.replace(rootPath);

      themeContextReplace.addValue("root-path", rootPath);

      String templatesPath =
          GetterUtil.getString(
              themeElement.elementText("templates-path"), theme.getTemplatesPath());

      templatesPath = themeContextReplace.replace(templatesPath);
      templatesPath = StringUtil.safePath(templatesPath);

      themeContextReplace.addValue("templates-path", templatesPath);

      String cssPath =
          GetterUtil.getString(themeElement.elementText("css-path"), theme.getCssPath());

      cssPath = themeContextReplace.replace(cssPath);
      cssPath = StringUtil.safePath(cssPath);

      themeContextReplace.addValue("css-path", cssPath);

      String imagesPath =
          GetterUtil.getString(themeElement.elementText("images-path"), theme.getImagesPath());

      imagesPath = themeContextReplace.replace(imagesPath);
      imagesPath = StringUtil.safePath(imagesPath);

      themeContextReplace.addValue("images-path", imagesPath);

      String javaScriptPath =
          GetterUtil.getString(
              themeElement.elementText("javascript-path"), theme.getJavaScriptPath());

      javaScriptPath = themeContextReplace.replace(javaScriptPath);
      javaScriptPath = StringUtil.safePath(javaScriptPath);

      themeContextReplace.addValue("javascript-path", javaScriptPath);

      String virtualPath =
          GetterUtil.getString(themeElement.elementText("virtual-path"), theme.getVirtualPath());

      String templateExtension =
          GetterUtil.getString(
              themeElement.elementText("template-extension"), theme.getTemplateExtension());

      theme.setName(name);
      theme.setRootPath(rootPath);
      theme.setTemplatesPath(templatesPath);
      theme.setCssPath(cssPath);
      theme.setImagesPath(imagesPath);
      theme.setJavaScriptPath(javaScriptPath);
      theme.setVirtualPath(virtualPath);
      theme.setTemplateExtension(templateExtension);

      Element settingsElement = themeElement.element("settings");

      if (settingsElement != null) {
        List<Element> settingElements = settingsElement.elements("setting");

        for (Element settingElement : settingElements) {
          boolean configurable =
              GetterUtil.getBoolean(settingElement.attributeValue("configurable"));
          String key = settingElement.attributeValue("key");
          String[] options = StringUtil.split(settingElement.attributeValue("options"));
          String type = settingElement.attributeValue("type");
          String value = settingElement.attributeValue("value");
          String script = settingElement.getTextTrim();

          theme.addSetting(key, value, configurable, type, options, script);
        }
      }

      theme.setWapTheme(
          GetterUtil.getBoolean(themeElement.elementText("wap-theme"), theme.isWapTheme()));

      Element rolesElement = themeElement.element("roles");

      if (rolesElement != null) {
        List<Element> roleNameElements = rolesElement.elements("role-name");

        for (Element roleNameElement : roleNameElements) {
          pluginSetting.addRole(roleNameElement.getText());
        }
      }

      _readColorSchemes(themeElement, theme.getColorSchemesMap(), themeContextReplace);
      _readColorSchemes(themeElement, theme.getColorSchemesMap(), themeContextReplace);

      Element layoutTemplatesElement = themeElement.element("layout-templates");

      if (layoutTemplatesElement != null) {
        Element standardElement = layoutTemplatesElement.element("standard");

        if (standardElement != null) {
          layoutTemplateLocalService.readLayoutTemplate(
              servletContextName,
              servletContext,
              null,
              standardElement,
              true,
              themeId,
              pluginPackage);
        }

        Element customElement = layoutTemplatesElement.element("custom");

        if (customElement != null) {
          layoutTemplateLocalService.readLayoutTemplate(
              servletContextName,
              servletContext,
              null,
              customElement,
              false,
              themeId,
              pluginPackage);
        }
      }

      if (!theme.isWapTheme()) {
        _setSpriteImages(servletContext, theme, imagesPath);
      }

      if (!_themes.containsKey(themeId)) {
        _themes.put(themeId, theme);
      }
    }

    return themeIds;
  }
  private Map<String, String> _buildFrameworkProperties(Class<?> clazz) {
    Map<String, String> properties = new HashMap<String, String>();

    properties.put(Constants.BUNDLE_DESCRIPTION, ReleaseInfo.getReleaseInfo());
    properties.put(Constants.BUNDLE_NAME, ReleaseInfo.getName());
    properties.put(Constants.BUNDLE_VENDOR, ReleaseInfo.getVendor());
    properties.put(Constants.BUNDLE_VERSION, ReleaseInfo.getVersion());
    properties.put(FrameworkPropsKeys.FELIX_FILEINSTALL_DIR, _getFelixFileInstallDir());
    properties.put(FrameworkPropsKeys.FELIX_FILEINSTALL_LOG_LEVEL, _getFelixFileInstallLogLevel());
    properties.put(
        FrameworkPropsKeys.FELIX_FILEINSTALL_POLL,
        String.valueOf(PropsValues.MODULE_FRAMEWORK_AUTO_DEPLOY_INTERVAL));
    properties.put(
        FrameworkPropsKeys.FELIX_FILEINSTALL_TMPDIR,
        SystemProperties.get(SystemProperties.TMP_DIR));
    properties.put(
        Constants.FRAMEWORK_BEGINNING_STARTLEVEL,
        String.valueOf(PropsValues.MODULE_FRAMEWORK_BEGINNING_START_LEVEL));
    properties.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_APP);
    properties.put(Constants.FRAMEWORK_STORAGE, PropsValues.MODULE_FRAMEWORK_STATE_DIR);

    properties.put("eclipse.security", null);
    properties.put("java.security.manager", null);
    properties.put("org.osgi.framework.security", null);

    ProtectionDomain protectionDomain = clazz.getProtectionDomain();

    CodeSource codeSource = protectionDomain.getCodeSource();

    URL codeSourceURL = codeSource.getLocation();

    properties.put(FrameworkPropsKeys.OSGI_FRAMEWORK, codeSourceURL.toExternalForm());

    File frameworkFile = new File(codeSourceURL.getFile());

    properties.put(FrameworkPropsKeys.OSGI_INSTALL_AREA, frameworkFile.getParent());

    Properties extraProperties =
        PropsUtil.getProperties(PropsKeys.MODULE_FRAMEWORK_PROPERTIES, true);

    for (Map.Entry<Object, Object> entry : extraProperties.entrySet()) {
      String key = (String) entry.getKey();
      String value = (String) entry.getValue();

      // We need to support an empty string and a null value distinctly.
      // This is due to some different behaviors between OSGi
      // implementations. If a property is passed as xyz= it will be
      // treated as an empty string. Otherwise, xyz=null will be treated
      // as an explicit null value.

      if (value.equals(StringPool.NULL)) {
        value = null;
      }

      properties.put(key, value);
    }

    String systemPackagesExtra = _getSystemPackagesExtra();

    properties.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, systemPackagesExtra);

    return properties;
  }
  protected void processStartupEvents() throws Exception {

    // Print release information

    Class<?> clazz = getClass();

    ClassLoader classLoader = clazz.getClassLoader();

    try (InputStream inputStream =
        classLoader.getResourceAsStream("com/liferay/portal/events/dependencies/startup.txt")) {

      System.out.println(_toString(inputStream));
    }

    System.out.println("Starting " + ReleaseInfo.getReleaseInfo() + "\n");

    if (_log.isDebugEnabled()) {
      _log.debug("Portal Resiliency - NOT SUPPORTED");
    }

    // Shutdown hook

    if (_log.isDebugEnabled()) {
      _log.debug("Add shutdown hook");
    }

    Runtime runtime = Runtime.getRuntime();

    runtime.addShutdownHook(new Thread(new ShutdownHook()));

    // MySQL version

    DB db = DBManagerUtil.getDB();

    if ((db.getDBType() == DBType.MYSQL) && GetterUtil.getFloat(db.getVersionString()) < 5.6F) {

      throw new ServletException(
          "Please upgrade to at least MySQL 5.6.4. The portal no "
              + "longer supports older versions of MySQL.");
    }

    // Check required build number

    if (_log.isDebugEnabled()) {
      _log.debug("Check required build number");
    }

    DBUpgrader.checkRequiredBuildNumber(ReleaseInfo.getParentBuildNumber());

    Registry registry = RegistryUtil.getRegistry();

    Map<String, Object> properties = new HashMap<>();

    properties.put("module.service.lifecycle", "database.initialized");
    properties.put("service.vendor", ReleaseInfo.getVendor());
    properties.put("service.version", ReleaseInfo.getVersion());

    _dbModuleServiceLifecycleServiceRegistration =
        registry.registerService(
            ModuleServiceLifecycle.class, new ModuleServiceLifecycle() {}, properties);

    // Check class names

    if (_log.isDebugEnabled()) {
      _log.debug("Check class names");
    }

    ClassNameLocalServiceUtil.checkClassNames();
  }