<commit_before># Copyright 2008-2013 Red Hat, Inc, and individual contributors.
# 
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this software; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.

require 'torquebox/msc'
require 'torquebox/registry'
require 'torquebox/service_registry'

module TorqueBox

  class InjectionError < StandardError
  end

  def self.fetch(something)
    unless TorqueBox::Registry.has_key?(something.to_s)
      handler_registry = TorqueBox::ServiceRegistry['torquebox.core.injection.injectable-handler-registry']
      # handler_registry should only be nil when running outside of
      # TorqueBox so we just return the nil value and skip everything
      # else to facilitate testing outside the container
      return nil if handler_registry.nil?
      handler = handler_registry.get_handler(something)
      raise InjectionError.new("Invalid injection - #{something}") if handler.nil?
      injectable = handler.handle(something, true)
      service_name = injectable.get_service_name(TorqueBox::Registry['service-target'],
                                                 TorqueBox::Registry['deployment-unit'])
      service = TorqueBox::ServiceRegistry.registry.getService(service_name)
      raise InjectionError.new("Service not found for injection - #{something}") if service.nil?
      state = TorqueBox::MSC.wait_for_service_to_start(service, 45)
      raise InjectionError.new("Injected service failed to start - #{service_name}") if state != 'UP'
      value = service.value
      raise InjectionError.new("Injected service had no value - #{service_name}") if value.nil?
      value = value.convert(JRuby.runtime) if value.respond_to?(:convert)
      if something.to_s.start_with?("service:")
        # don't cache services to support zero-downtime deploys (TORQUE-1217)
        return value
      else
        TorqueBox::Registry.merge!(something.to_s => value)
      end
    end
    TorqueBox::Registry[something.to_s]
  end

  module Injectors

    def fetch(something)
      TorqueBox.fetch(something)
    end
    alias_method :__inject__, :fetch

    %w{ msc service cdi jndi queue topic }.each do |type|
      define_method("fetch_#{type}".to_sym) do |key|
        fetch(key)
      end
    end

  end
end

# Temporarily workaround TorqueSpec needing an inject(...) method
# @api private
module TorqueSpec<commit_msg>Permit the timeout period for a fetch call to be set via an optional :timeout parameter<commit_after># Copyright 2008-2013 Red Hat, Inc, and individual contributors.
# 
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this software; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.

require 'torquebox/msc'
require 'torquebox/registry'
require 'torquebox/service_registry'

module TorqueBox

  class InjectionError < StandardError
  end

  def self.fetch(something,options={})
    timeout = options[:timeout] || 45
    unless TorqueBox::Registry.has_key?(something.to_s)
      handler_registry = TorqueBox::ServiceRegistry['torquebox.core.injection.injectable-handler-registry']
      # handler_registry should only be nil when running outside of
      # TorqueBox so we just return the nil value and skip everything
      # else to facilitate testing outside the container
      return nil if handler_registry.nil?
      handler = handler_registry.get_handler(something)
      raise InjectionError.new("Invalid injection - #{something}") if handler.nil?
      injectable = handler.handle(something, true)
      service_name = injectable.get_service_name(TorqueBox::Registry['service-target'],
                                                 TorqueBox::Registry['deployment-unit'])
      service = TorqueBox::ServiceRegistry.registry.getService(service_name)
      raise InjectionError.new("Service not found for injection - #{something}") if service.nil?
      state = TorqueBox::MSC.wait_for_service_to_start(service, timeout)
      raise InjectionError.new("Injected service failed to start - #{service_name}") if state != 'UP'
      value = service.value
      raise InjectionError.new("Injected service had no value - #{service_name}") if value.nil?
      value = value.convert(JRuby.runtime) if value.respond_to?(:convert)
      if something.to_s.start_with?("service:")
        # don't cache services to support zero-downtime deploys (TORQUE-1217)
        return value
      else
        TorqueBox::Registry.merge!(something.to_s => value)
      end
    end
    TorqueBox::Registry[something.to_s]
  end

  module Injectors

    def fetch(something,options={})
      TorqueBox.fetch(something,options)
    end
    alias_method :__inject__, :fetch

    %w{ msc service cdi jndi queue topic }.each do |type|
      define_method("fetch_#{type}".to_sym) do |key|
        fetch(key)
      end
    end

  end
end

# Temporarily workaround TorqueSpec needing an inject(...) method
# @api private
module TorqueSpec
{'fraction_non_alphanumeric': 0.08385947871134855, 'fraction_numerical': 0.008742107819329771, 'mean_word_length': 3.6277153558052433, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4211358', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1665, 'n_words': 595}