Connector

Runo モジュール用に柔軟なコネクタを書いてみた。

  # Connecting to OpenOffice.org.
  module Connector
    class << self
      def create_resolver
        local_ctx = Runo.component_context
        smgr = local_ctx.getServiceManager
        smgr.createInstanceWithContext(
          "com.sun.star.bridge.UnoUrlResolver", local_ctx)
      end
  
      def connect(uno_url, resolver=nil)
        if ! resolver
          resolver = create_resolver
        end
        ctx = resolver.resolve(uno_url)
        if ctx
          Runo.component_context = ctx
        end
        ctx
      end
  
      #  Start OpenOffice.org and connect to it.
      #  Remove host cannot be boot.
      # @param office 
      def bootstrap(office="soffice", connection_type="socket", args)
        if connection_type == 'socket'
          host = args.fetch('host', 'localhost') # 129.0.0.1
          port = args.fetch('port', '2083')
          tcpNoDelay = args.fetch('tcpNoDelay', '0')
          template = 'socket,host=%s,port=%s' % [host, port]
          template2 = template + ',tcpNoDelay=%s' % tcpNoDelay
        elsif connection_type == 'pipe'
          argument = args.fetch('name', 'rubypipe')
          template2 = template = 'pipe,name=%s&' % [name]
        else
          raise ArgumentError, "unknown connection type (%s)" % connection_type
        end
        uno_url = 'uno:' + template2 + ';urp;StarOffice.ComponentContext'
        argument = '-accept=' + template + ';urp;StarOffice.ServiceManager'
        interval = args.fetch('interval', 3.0).to_f
        multiply = args.fetch('multiply', 5).to_i
        
        ctx = nil
        resolver = create_resolver
        i = 0
        while i < multiply
          begin
            ctx = resolver.resolve(uno_url)
            if ctx
              break
            end
          rescue
              Open3.popen3(office + " '" + argument + "'")
              sleep(interval)
          end
          i += 1
        end
        if ctx
          Runo.component_context = ctx
        end
        ctx
      end
    end
  end