# File lib/openshift-origin-controller/app/models/application.rb, line 443
  def configure_dependencies
    reply = ResultIO.new
    self.class.notify_observers(:before_application_configure, {:application => self, :reply => reply})

    elaborate_descriptor

    exceptions = []
    Rails.logger.debug "Configure order is #{self.configure_order.inspect}"
    #process new additions
    #TODO: fix configure after framework cartridge is no longer a requirement for adding embedded cartridges
    self.configure_order.each do |comp_inst_name|
      comp_inst = self.comp_instance_map[comp_inst_name]
      next if comp_inst.parent_cart_name == self.name
      group_inst = self.group_instance_map[comp_inst.group_instance_name]
      begin
        group_inst.fulfil_requirements(self)
        run_on_gears(group_inst.get_unconfigured_gears(comp_inst), reply) do |gear, r|
          doExpose = false
          if self.scalable and comp_inst.parent_cart_name!=self.proxy_cartridge
            doExpose = true if not gear.configured_components.include? comp_inst.name
          end
          r.append gear.configure(comp_inst, @init_git_url)
          begin
            r.append gear.expose_port(comp_inst) if doExpose
          rescue Exception=>e
          end
          process_cartridge_commands(r)
        end
      rescue Exception => e
        Rails.logger.debug e.message
        Rails.logger.debug e.backtrace.inspect        
 
        if e.message.kind_of?(Hash) and e.message[:exception] 
          successful_gears = []
          successful_gears = e.message[:successful].map{|g| g[:gear]} if e.message[:successful]
          failed_gears = []
          failed_gears = e.message[:failed].map{|g| g[:gear]} if e.message[:failed]
          gear_exception = e.message[:exception]

          #remove failed component from all gears
          run_on_gears(successful_gears, reply, false) do |gear, r|
            r.append gear.deconfigure(comp_inst)
            process_cartridge_commands(r)
          end
          run_on_gears(failed_gears, reply, false) do |gear, r|
            r.append gear.deconfigure(comp_inst, true)
            process_cartridge_commands(r)
          end
        else
          gear_exception = e
        end
        
        # destroy any unused gears
        # TODO : if the destroy fails below... the user still sees the error as configure failure
        #   Then to recover, if we re-elaborate (like in add_dependency), then the group instance will get lost
        #   and any failed gears below will leak (i.e. they exist on node, their destroy failed, but they do not have any handle in Mongo)
        run_on_gears(group_inst.gears, reply, false) do |gear, r|
          r.append group_inst.remove_gear(gear) if gear.configured_components.length == 0
        end

        self.save
        exceptions << gear_exception
      end
    end
    
    unless exceptions.empty?
      raise exceptions.first
    end
    
    self.save
    self.class.notify_observers(:after_application_configure, {:application => self, :reply => reply})
    reply
  end