Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dm.lambda.itest; |
| 20 | |
| 21 | import static org.apache.felix.dm.lambda.DependencyManagerActivator.aspect; |
| 22 | import static org.apache.felix.dm.lambda.DependencyManagerActivator.component; |
| 23 | |
| 24 | import org.apache.felix.dm.Component; |
| 25 | import org.apache.felix.dm.DependencyManager; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public class AspectWithCallbacksServiceDependencyTest extends TestBase { |
| 32 | public void testServiceRegistrationAndConsumption() { |
| 33 | DependencyManager m = getDM(); |
| 34 | // helper class that ensures certain steps get executed in sequence |
| 35 | Ensure e = new Ensure(); |
| 36 | // create a service provider and consumer |
| 37 | Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build(); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 38 | Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s->s.add("add").remove("remove")).build(); |
| 39 | Component asp = aspect(m, ServiceInterface.class).rank(100).add("add").remove("remove").swap("swap").impl(ServiceProviderAspect.class).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 40 | |
| 41 | m.add(sp); |
| 42 | m.add(sc); |
| 43 | m.add(asp); |
| 44 | m.remove(asp); |
| 45 | m.remove(sc); |
| 46 | m.remove(sp); |
| 47 | |
| 48 | // ensure we executed all steps inside the component instance |
| 49 | e.step(8); |
| 50 | } |
| 51 | |
| 52 | public void testServiceRegistrationAndConsumptionRef() { |
| 53 | DependencyManager m = getDM(); |
| 54 | // helper class that ensures certain steps get executed in sequence |
| 55 | Ensure e = new Ensure(); |
| 56 | // create a service provider and consumer |
| 57 | Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build(); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 58 | Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s->s.add(ServiceConsumer::add).remove(ServiceConsumer::remove)).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 59 | Component asp = aspect(m, ServiceInterface.class) |
| 60 | .impl(ServiceProviderAspect.class).rank(100) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 61 | .add(ServiceProviderAspect::add).remove(ServiceProviderAspect::remove).swap(ServiceProviderAspect::swap) |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 62 | .build(); |
| 63 | |
| 64 | m.add(sp); |
| 65 | m.add(sc); |
| 66 | m.add(asp); |
| 67 | m.remove(asp); |
| 68 | m.remove(sc); |
| 69 | m.remove(sp); |
| 70 | |
| 71 | // ensure we executed all steps inside the component instance |
| 72 | e.step(8); |
| 73 | } |
| 74 | |
| 75 | public void testServiceRegistrationAndConsumptionWithAspectCallbackInstance() { |
| 76 | DependencyManager m = getDM(); |
| 77 | // helper class that ensures certain steps get executed in sequence |
| 78 | Ensure e = new Ensure(); |
| 79 | // create a service provider and consumer |
| 80 | Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build(); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 81 | Component sc = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, s->s.add("add").remove("remove")).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 82 | |
| 83 | ServiceProviderAspect providerAspect = new ServiceProviderAspect(); |
| 84 | ServiceProviderAspectCallbackInstance aspectCb = new ServiceProviderAspectCallbackInstance(providerAspect); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 85 | Component asp = aspect(m, ServiceInterface.class).rank(100).impl(providerAspect).add(aspectCb::add).remove(aspectCb::remove).swap(aspectCb::swap).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 86 | |
| 87 | m.add(sp); |
| 88 | m.add(sc); |
| 89 | m.add(asp); |
| 90 | m.remove(asp); |
| 91 | m.remove(sc); |
| 92 | m.remove(sp); |
| 93 | |
| 94 | // ensure we executed all steps inside the component instance |
| 95 | e.step(8); |
| 96 | } |
| 97 | |
| 98 | static interface ServiceInterface { |
| 99 | public void invoke(String caller); |
| 100 | } |
| 101 | |
| 102 | static class ServiceProvider implements ServiceInterface { |
| 103 | private final Ensure m_ensure; |
| 104 | public ServiceProvider(Ensure e) { |
| 105 | m_ensure = e; |
| 106 | } |
| 107 | public void invoke(String caller) { |
| 108 | if (caller.equals("consumer.init")) { |
| 109 | m_ensure.step(3); |
| 110 | } else if (caller.equals("aspect.consumer.add")) { |
| 111 | m_ensure.step(5); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public static class ServiceProviderAspectCallbackInstance { |
| 117 | private final ServiceProviderAspect m_aspect; |
| 118 | |
| 119 | ServiceProviderAspectCallbackInstance(ServiceProviderAspect aspect) { |
| 120 | m_aspect = aspect; |
| 121 | } |
| 122 | |
| 123 | public void add(ServiceInterface service) { |
| 124 | m_aspect.add(service); |
| 125 | } |
| 126 | |
| 127 | public void remove(ServiceInterface service) { |
| 128 | m_aspect.remove(service); |
| 129 | } |
| 130 | |
| 131 | public void swap(ServiceInterface previous, ServiceInterface current) { |
| 132 | m_aspect.swap(previous, current); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static class ServiceProviderAspect implements ServiceInterface { |
| 137 | private volatile ServiceInterface m_service; |
| 138 | |
| 139 | public ServiceProviderAspect() { |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void invoke(String caller) { |
| 144 | m_service.invoke("aspect." + caller); |
| 145 | } |
| 146 | |
| 147 | public void add(ServiceInterface service) { |
| 148 | m_service = service; |
| 149 | } |
| 150 | |
| 151 | public void remove(ServiceInterface service) { |
| 152 | m_service = null; |
| 153 | } |
| 154 | |
| 155 | public void swap(ServiceInterface previous, ServiceInterface current) { |
| 156 | m_service = current; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static class ServiceConsumer { |
| 161 | private volatile ServiceInterface m_service; |
| 162 | private final Ensure m_ensure; |
| 163 | private int addCount = 0; |
| 164 | |
| 165 | public ServiceConsumer(Ensure e) { |
| 166 | m_ensure = e; |
| 167 | } |
| 168 | |
| 169 | public void init() { |
| 170 | m_ensure.step(2); |
| 171 | m_service.invoke("consumer.init"); |
| 172 | } |
| 173 | |
| 174 | public void destroy() { |
| 175 | m_ensure.step(7); |
| 176 | } |
| 177 | |
| 178 | public void add(ServiceInterface service) { |
| 179 | m_service = service; |
| 180 | switch (addCount) { |
| 181 | case 0: m_ensure.step(1); |
| 182 | break; |
| 183 | case 1: m_ensure.step(4); |
| 184 | // aspect had been added |
| 185 | m_service.invoke("consumer.add"); |
| 186 | break; |
| 187 | case 2: m_ensure.step(6); |
| 188 | break; |
| 189 | default: |
| 190 | } |
| 191 | addCount ++; |
| 192 | } |
| 193 | public void remove(ServiceInterface service) { |
| 194 | m_service = null; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | } |