openhab-jrubyscripting 5.0.0.rc2 → 5.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/items/generic_item.rb +13 -5
  3. data/lib/openhab/core/items/metadata/hash.rb +17 -34
  4. data/lib/openhab/core/items/persistence.rb +2 -0
  5. data/lib/openhab/core/items/semantics/enumerable.rb +6 -4
  6. data/lib/openhab/core/profile_factory.rb +2 -0
  7. data/lib/openhab/core/provider.rb +8 -1
  8. data/lib/openhab/core/rules/provider.rb +25 -0
  9. data/lib/openhab/core/rules/registry.rb +76 -0
  10. data/lib/openhab/core/rules/rule.rb +150 -0
  11. data/lib/openhab/core/rules.rb +25 -0
  12. data/lib/openhab/core/timer.rb +5 -7
  13. data/lib/openhab/core/types.rb +1 -1
  14. data/lib/openhab/core.rb +0 -16
  15. data/lib/openhab/core_ext/java/list.rb +436 -0
  16. data/lib/openhab/core_ext/java/map.rb +66 -0
  17. data/lib/openhab/core_ext/java/zoned_date_time.rb +1 -2
  18. data/lib/openhab/core_ext/ruby/date.rb +2 -0
  19. data/lib/openhab/core_ext/ruby/date_time.rb +53 -0
  20. data/lib/openhab/core_ext/ruby/time.rb +88 -86
  21. data/lib/openhab/dsl/events/watch_event.rb +1 -1
  22. data/lib/openhab/dsl/items/builder.rb +8 -3
  23. data/lib/openhab/dsl/items/ensure.rb +6 -2
  24. data/lib/openhab/dsl/items/timed_command.rb +10 -11
  25. data/lib/openhab/dsl/rules/automation_rule.rb +36 -13
  26. data/lib/openhab/dsl/rules/builder.rb +99 -8
  27. data/lib/openhab/dsl/rules/name_inference.rb +0 -5
  28. data/lib/openhab/dsl/rules/terse.rb +1 -2
  29. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +17 -53
  30. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +0 -3
  31. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +1 -1
  32. data/lib/openhab/dsl/rules.rb +0 -21
  33. data/lib/openhab/dsl/thread_local.rb +2 -2
  34. data/lib/openhab/dsl/timer_manager.rb +3 -1
  35. data/lib/openhab/dsl/version.rb +1 -1
  36. data/lib/openhab/dsl.rb +12 -105
  37. data/lib/openhab/log.rb +2 -2
  38. data/lib/openhab/rspec/example_group.rb +42 -0
  39. data/lib/openhab/rspec/helpers.rb +31 -8
  40. data/lib/openhab/rspec/hooks.rb +3 -6
  41. data/lib/openhab/rspec/karaf.rb +45 -27
  42. data/lib/openhab/rspec/mocks/synchronous_executor.rb +11 -4
  43. data/lib/openhab/rspec/mocks/timer.rb +2 -1
  44. data/lib/openhab/rspec/suspend_rules.rb +4 -2
  45. metadata +23 -2
@@ -77,7 +77,12 @@ module OpenHAB
77
77
 
78
78
  return if service.get_instance("rspec")
79
79
 
80
- service.clone_instance(root_instance.name, "rspec", settings, false)
80
+ begin
81
+ service.clone_instance(root_instance.name, "rspec", settings, false)
82
+ rescue java.lang.NullPointerException
83
+ retry if fix_rmi_registry_npe
84
+ raise
85
+ end
81
86
  ensure
82
87
  extra_loaders = ::JRuby.runtime.instance_config.extra_loaders
83
88
  loader = extra_loaders.find { |l| l.class_loader == @karaf_instance_loader }
@@ -99,6 +104,7 @@ module OpenHAB
99
104
  java.lang.System.set_property("felix.cm.dir", felix_cm)
100
105
  # not handled by karaf instances
101
106
  java.lang.System.set_property("openhab.userdata", path)
107
+ @oh_userdata = nil
102
108
  java.lang.System.set_property("openhab.logdir", "#{path}/logs")
103
109
  end
104
110
  cleanup_instance
@@ -129,6 +135,7 @@ module OpenHAB
129
135
 
130
136
  set_up_bundle_listener
131
137
  wait_for_start
138
+ Mocks::SynchronousExecutor.instance.main_thread = Thread.current
132
139
  set_jruby_script_presets
133
140
  @main
134
141
  end
@@ -258,8 +265,6 @@ module OpenHAB
258
265
  props.remove("transformation")
259
266
  props.remove("ui")
260
267
  props.remove("binding") unless include_bindings
261
- # except we need jrubyscripting
262
- props.put("automation", "jrubyscripting")
263
268
  cfg.update(props)
264
269
 
265
270
  # configure persistence to use the mock service
@@ -302,7 +307,6 @@ module OpenHAB
302
307
  private_constant :BLOCKED_BUNDLE_TREES
303
308
 
304
309
  ALLOWED_BUNDLES = %w[
305
- org.openhab.automation.jrubyscripting
306
310
  org.openhab.core.io.monitor
307
311
  ].freeze
308
312
  private_constant :ALLOWED_BUNDLES
@@ -420,12 +424,18 @@ module OpenHAB
420
424
  java.util.Hashtable.new(org.osgi.framework.Constants::SERVICE_RANKING => 1.to_java(:int))
421
425
  )
422
426
 
423
- require_relative "mocks/thing_handler"
424
- thf = Mocks::ThingHandlerFactory.instance
425
- bundle = org.osgi.framework.FrameworkUtil.get_bundle(org.openhab.core.thing.Thing)
426
- Mocks::BundleResolver.instance.register_class(thf.class, bundle)
427
- bundle.bundle_context.register_service(org.openhab.core.thing.binding.ThingHandlerFactory.java_class, thf,
428
- nil)
427
+ wait_for_service("org.openhab.core.thing.ThingManager") do |tm|
428
+ tm.class.field_accessor :bundleResolver
429
+
430
+ tm.bundleResolver = Mocks::BundleResolver.instance
431
+
432
+ require_relative "mocks/thing_handler"
433
+ thf = Mocks::ThingHandlerFactory.instance
434
+ bundle = org.osgi.framework.FrameworkUtil.get_bundle(org.openhab.core.thing.Thing)
435
+ Mocks::BundleResolver.instance.register_class(thf.class, bundle)
436
+ bundle.bundle_context.register_service(org.openhab.core.thing.binding.ThingHandlerFactory.java_class, thf,
437
+ nil)
438
+ end
429
439
  end
430
440
  if bundle_name == "org.openhab.core.automation"
431
441
  org.openhab.core.automation.internal.TriggerHandlerCallbackImpl.field_accessor :executor
@@ -549,31 +559,22 @@ module OpenHAB
549
559
  # import global variables and constants that the DSL expects,
550
560
  # since we're going to be running it in this same VM
551
561
  def set_jruby_script_presets
552
- wait_for_service("org.openhab.core.automation.module.script.ScriptEngineFactory",
553
- filter: "(service.config.description.uri=automation:jruby)") do |jrubyscripting|
554
- # "org.openhab.core.automation.module.script.internal.ScriptExtensionManager") do |sem|
555
- sem = OSGi.service(
556
- "org.openhab.core.automation.module.script.internal.ScriptExtensionManager"
557
- )
562
+ wait_for_service("org.openhab.core.automation.module.script.internal.ScriptExtensionManager") do |sem|
558
563
  # since we're not created by the ScriptEngineManager, this never gets set; manually set it
559
564
  $se = $scriptExtension = ScriptExtensionManagerWrapper.new(sem)
560
565
  scope_values = sem.find_default_presets("rspec")
561
- scope_values = scope_values.entry_set
562
-
563
- %w[mapInstancePresets mapGlobalPresets].each do |method_name|
564
- method = jrubyscripting.class.java_class.get_declared_method(method_name, java.util.Map::Entry.java_class)
565
-
566
- method.accessible = true
567
- scope_values = scope_values.map { |e| method.invoke(nil, e) }
568
- end
566
+ scope_values = scope_values.entry_set.to_a
569
567
 
570
568
  scope_values.each do |entry|
571
569
  key = entry.key
572
570
  value = entry.value
573
571
  # convert Java classes to Ruby classes
574
572
  value = value.ruby_class if value.is_a?(java.lang.Class) # rubocop:disable Lint/UselessAssignment
575
- # constants need to go into the global namespace
576
- key = "::#{key}" if ("A".."Z").cover?(key[0])
573
+ # variables are globals; constants go into the global namespace
574
+ key = case key[0]
575
+ when "a".."z" then "$#{key}"
576
+ when "A".."Z" then "::#{key}"
577
+ end
577
578
  eval("#{key} = value unless defined?(#{key})", nil, __FILE__, __LINE__) # rubocop:disable Security/Eval
578
579
  end
579
580
  end
@@ -796,7 +797,6 @@ module OpenHAB
796
797
  <f:feature>openhab-core-model-sitemap</f:feature>
797
798
  <f:feature>openhab-core-model-thing</f:feature>
798
799
  <f:feature>openhab-core-storage-json</f:feature>
799
- <f:feature>openhab-automation-jrubyscripting</f:feature>
800
800
  <f:feature>openhab-transport-http</f:feature>
801
801
  <f:feature prerequisite="true">wrapper</f:feature>
802
802
  <f:bundle>mvn:org.openhab.core.bundles/org.openhab.core.karaf/#{version}</f:bundle>
@@ -806,6 +806,24 @@ module OpenHAB
806
806
  </featuresProcessing>
807
807
  XML
808
808
  end
809
+
810
+ def fix_rmi_registry_npe
811
+ full_path = File.join(oh_userdata, "etc/org.apache.karaf.management.cfg")
812
+ stat = File.stat(full_path)
813
+ return false unless stat.file? && stat.size.zero?
814
+
815
+ contents = <<~TEXT
816
+ # This file was autogenerated by openhab-jrubyscripting.
817
+ # Feel free to customize.
818
+ rmiRegistryPort = 1099
819
+ rmiServerPort = 44444
820
+ TEXT
821
+ begin
822
+ File.write(full_path, contents)
823
+ rescue Errno::EACCESS
824
+ abort "Unable to write to `#{full_path}`. Please use sudo and set it to:\n\n#{contents}"
825
+ end
826
+ end
809
827
  end
810
828
  end
811
829
  end
@@ -16,19 +16,25 @@ module OpenHAB
16
16
  end
17
17
 
18
18
  class SynchronousExecutor < java.util.concurrent.ScheduledThreadPoolExecutor
19
- class << self
20
- def instance
21
- @instance ||= new(1)
22
- end
19
+ include Singleton
20
+
21
+ attr_accessor :main_thread
22
+
23
+ def initialize
24
+ super(1)
23
25
  end
24
26
 
25
27
  def submit(runnable)
28
+ return super unless Thread.current == main_thread
29
+
26
30
  runnable.respond_to?(:run) ? runnable.run : runnable.call
27
31
 
28
32
  java.util.concurrent.CompletableFuture.completed_future(nil)
29
33
  end
30
34
 
31
35
  def execute(runnable)
36
+ return super unless Thread.current == main_thread
37
+
32
38
  runnable.run
33
39
  end
34
40
 
@@ -37,6 +43,7 @@ module OpenHAB
37
43
  def shutdown_now
38
44
  []
39
45
  end
46
+ alias_method :shutdownNow, :shutdown_now
40
47
  end
41
48
 
42
49
  class SynchronousExecutorMap
@@ -50,6 +50,7 @@ module OpenHAB
50
50
  end
51
51
 
52
52
  def reschedule(time = nil)
53
+ Thread.current[:openhab_rescheduled_timer] = true if Thread.current[:openhab_rescheduled_timer] == self
53
54
  @execution_time = new_execution_time(time || @time)
54
55
  @executed = false
55
56
 
@@ -62,8 +63,8 @@ module OpenHAB
62
63
  raise "Timer already cancelled" if cancelled?
63
64
  raise "Timer already executed" if terminated?
64
65
 
65
- super
66
66
  @executed = true
67
+ super
67
68
  end
68
69
 
69
70
  def cancel
@@ -21,7 +21,9 @@ module OpenHAB
21
21
  process_queue(create_queue(inputs), mod, inputs)
22
22
  end
23
23
  rescue Exception => e
24
- @run_context.logger.log_exception(e)
24
+ raise if defined?(::RSpec) && ::RSpec.current_example.example_group.propagate_exceptions?
25
+
26
+ @run_context.send(:logger).log_exception(e)
25
27
  end
26
28
  end
27
29
  end
@@ -29,7 +31,7 @@ module OpenHAB
29
31
  # DSL::Rules::AutomationRule.prepend(AutomationRule)
30
32
 
31
33
  module DSL
32
- def after(*)
34
+ def after(*, **)
33
35
  return if SuspendRules.suspended?
34
36
 
35
37
  super
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-jrubyscripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.rc2
4
+ version: 5.0.0.rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-22 00:00:00.000000000 Z
11
+ date: 2022-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ruby2_keywords
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: cucumber
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -354,6 +368,10 @@ files:
354
368
  - lib/openhab/core/profile_factory.rb
355
369
  - lib/openhab/core/provider.rb
356
370
  - lib/openhab/core/registry.rb
371
+ - lib/openhab/core/rules.rb
372
+ - lib/openhab/core/rules/provider.rb
373
+ - lib/openhab/core/rules/registry.rb
374
+ - lib/openhab/core/rules/rule.rb
357
375
  - lib/openhab/core/script_handling.rb
358
376
  - lib/openhab/core/things.rb
359
377
  - lib/openhab/core/things/channel.rb
@@ -392,8 +410,10 @@ files:
392
410
  - lib/openhab/core_ext.rb
393
411
  - lib/openhab/core_ext/java/class.rb
394
412
  - lib/openhab/core_ext/java/duration.rb
413
+ - lib/openhab/core_ext/java/list.rb
395
414
  - lib/openhab/core_ext/java/local_date.rb
396
415
  - lib/openhab/core_ext/java/local_time.rb
416
+ - lib/openhab/core_ext/java/map.rb
397
417
  - lib/openhab/core_ext/java/month.rb
398
418
  - lib/openhab/core_ext/java/month_day.rb
399
419
  - lib/openhab/core_ext/java/period.rb
@@ -404,6 +424,7 @@ files:
404
424
  - lib/openhab/core_ext/ruby/array.rb
405
425
  - lib/openhab/core_ext/ruby/class.rb
406
426
  - lib/openhab/core_ext/ruby/date.rb
427
+ - lib/openhab/core_ext/ruby/date_time.rb
407
428
  - lib/openhab/core_ext/ruby/numeric.rb
408
429
  - lib/openhab/core_ext/ruby/range.rb
409
430
  - lib/openhab/core_ext/ruby/time.rb