<commit_before># Encoding: utf-8
# IBM WebSphere Application Server Liberty Buildpack
# Copyright 2014 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'base64'
require 'rexml/document'
require 'liberty_buildpack/services/client_jar_utils'
require 'liberty_buildpack/services/mongo'
require 'open3'

module LibertyBuildpack::Services

  #------------------------------------------------------------------------------------
  # The ComposeMongo class is the class for Compose for Mongo database resources.
  #------------------------------------------------------------------------------------
  class ComposeMongo < Mongo

    #------------------------------------------------------------------------------------
    # Initialize
    #
    # @param type - the vcap_services type
    # @param config - a hash containing the configuration data from the yml file.
    #------------------------------------------------------------------------------------
    def initialize(type, config, context)
      super(type, config)
      @config_type = 'compose-mongo'
      @app_dir = context[:app_dir]
      @java_home = context[:java_home]
    end

    #-----------------------------------------------------------------------------------------
    # parse the vcap services and create cloud properties
    #
    # @param element - the root element of the REXML document for runtime-vars.xml
    # @param instance - the hash containing the vcap_services data for this instance
    #------------------------------------------------------------------------------------------
    def parse_vcap_services(element, instance)
      super

      credentials = instance['credentials'] || {}

      # fix the database name as the relational_db uses 'name' from vcap_services
      # which does not match the actual database name in compose
      service_uri = credentials['uri']
      parsed_uri = Mongo.parse_url(service_uri)
      db_var_name = "cloud.services.#{@service_name}.connection.db"
      new_element = REXML::Element.new('variable', element)
      new_element.add_attribute('name', db_var_name)
      new_element.add_attribute('value', parsed_uri['db'])
      @db_name = "${#{db_var_name}}"

      @service_cert = credentials['ca_certificate_base64']
      # raise "Resource #{@service_name} does not contain a uri property" if @service_cert.nil?
    end

    #------------------------------------------------------------------------------------
    # Method to create a datasource stanza (and all related sub-artifacts such as the JDBCDriver) in server.xml.
    #
    # @param doc - the REXML::Document root element for server.xml
    # @param server_dir - the server directory which is the location for bootstrap.properties and jvm.options
    # @param driver_dir - the symbolic name of the directory where client jars are installed
    # @param available_jars - an array containing the names of all installed client driver jars.
    # @raise if a problem was discovered (incoherent or inconsistent existing configuration, for example)
    #------------------------------------------------------------------------------------
    def create(doc, server_dir, driver_dir, available_jars)
      super

      # add_certificate
      # add_key_store(doc)
      # add_custom_ssl(doc)
    end

    #------------------------------------------------------------------------------------
    # Method to create/update a datasource stanza (and all related sub-artifacts such as the JDBCDriver) in server.xml.
    #
    # @param doc - the REXML::Document root element for server.xml
    # @param server_dir - the server directory which is the location for bootstrap.properties and jvm.options
    # @param driver_dir - the symbolic name of the directory where client jars are installed
    # @param available_jars - an array containing the names of all installed client driver jars.
    # @param [Integer] number_instances - the number of mongo service instances.
    # @raise if a problem was discovered (incoherent or inconsistent existing configuration, for example)
    #------------------------------------------------------------------------------------
    def update(doc, server_dir, driver_dir, available_jars, number_instances)
      super

      # add_certificate
      # update_keystore_config(doc)
      # update_ssl_config(doc)
    end

    protected

    #------------------------------------------------------------------------------------
    # Method to customize properties - called on create
    #
    # @param properties_element - the properties element
    #------------------------------------------------------------------------------------
    def add_properties(properties_element)
      properties_element.add_attribute('sslEnabled', 'true')
      properties_element.add_attribute('sslRef', 'composeMongoSSLConfig')
    end

    #------------------------------------------------------------------------------------
    # Method to update properties - called on update.
    #
    # @param properties_element - the properties element
    #------------------------------------------------------------------------------------<commit_msg>Update compose_mongo.rb<commit_after># Encoding: utf-8
# IBM WebSphere Application Server Liberty Buildpack
# Copyright 2014 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'base64'
require 'rexml/document'
require 'liberty_buildpack/services/client_jar_utils'
require 'liberty_buildpack/services/mongo'
require 'open3'

module LibertyBuildpack::Services

  #------------------------------------------------------------------------------------
  # The ComposeMongo class is the class for Compose for Mongo database resources.
  #------------------------------------------------------------------------------------
  class ComposeMongo < Mongo

    #------------------------------------------------------------------------------------
    # Initialize
    #
    # @param type - the vcap_services type
    # @param config - a hash containing the configuration data from the yml file.
    #------------------------------------------------------------------------------------
    def initialize(type, config, context)
      super(type, config)
      @config_type = 'compose-mongo'
      @app_dir = context[:app_dir]
      @java_home = context[:java_home]
    end

    #-----------------------------------------------------------------------------------------
    # parse the vcap services and create cloud properties
    #
    # @param element - the root element of the REXML document for runtime-vars.xml
    # @param instance - the hash containing the vcap_services data for this instance
    #------------------------------------------------------------------------------------------
    def parse_vcap_services(element, instance)
      super

      credentials = instance['credentials'] || {}

      # fix the database name as the relational_db uses 'name' from vcap_services
      # which does not match the actual database name in compose
      service_uri = credentials['uri']
      parsed_uri = Mongo.parse_url(service_uri)
      db_var_name = "cloud.services.#{@service_name}.connection.db"
      new_element = REXML::Element.new('variable', element)
      new_element.add_attribute('name', db_var_name)
      new_element.add_attribute('value', parsed_uri['db'])
      @db_name = "${#{db_var_name}}"

      @service_cert = credentials['ca_certificate_base64']
      # raise "Resource #{@service_name} does not contain a uri property" if @service_cert.nil?
    end

    #------------------------------------------------------------------------------------
    # Method to create a datasource stanza (and all related sub-artifacts such as the JDBCDriver) in server.xml.
    #
    # @param doc - the REXML::Document root element for server.xml
    # @param server_dir - the server directory which is the location for bootstrap.properties and jvm.options
    # @param driver_dir - the symbolic name of the directory where client jars are installed
    # @param available_jars - an array containing the names of all installed client driver jars.
    # @raise if a problem was discovered (incoherent or inconsistent existing configuration, for example)
    #------------------------------------------------------------------------------------
    def create(doc, server_dir, driver_dir, available_jars)
      super
      unless @service_cert.nil?
        add_certificate
        add_key_store(doc)
        add_custom_ssl(doc)
      end
    end

    #------------------------------------------------------------------------------------
    # Method to create/update a datasource stanza (and all related sub-artifacts such as the JDBCDriver) in server.xml.
    #
    # @param doc - the REXML::Document root element for server.xml
    # @param server_dir - the server directory which is the location for bootstrap.properties and jvm.options
    # @param driver_dir - the symbolic name of the directory where client jars are installed
    # @param available_jars - an array containing the names of all installed client driver jars.
    # @param [Integer] number_instances - the number of mongo service instances.
    # @raise if a problem was discovered (incoherent or inconsistent existing configuration, for example)
    #------------------------------------------------------------------------------------
    def update(doc, server_dir, driver_dir, available_jars, number_instances)
      super
      
      unless @service_cert.nil?
        add_certificate
        update_keystore_config(doc)
        update_ssl_config(doc)
      end
    end

    protected

    #------------------------------------------------------------------------------------
    # Method to customize properties - called on create
    #
    # @param properties_element - the properties element
    #------------------------------------------------------------------------------------
    def add_properties(properties_element)
      properties_element.add_attribute('sslEnabled', 'true')
      properties_element.add_attribute('sslRef', 'composeMongoSSLConfig')
    end

    #------------------------------------------------------------------------------------
    # Method to update properties - called on update.
    #
    # @param properties_element - the properties element
    #------------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.2648110985079836, 'fraction_numerical': 0.0024430677951313147, 'mean_word_length': 1.5757303370786517, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2266287', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2494, 'n_words': 911}