Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +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.runtime.itest.components; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.Dictionary; |
| 23 | import java.util.Hashtable; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.apache.felix.dm.annotation.api.Component; |
| 27 | import org.apache.felix.dm.annotation.api.ConfigurationDependency; |
| 28 | import org.apache.felix.dm.annotation.api.FactoryConfigurationAdapterService; |
| 29 | import org.apache.felix.dm.annotation.api.ServiceDependency; |
| 30 | import org.apache.felix.dm.annotation.api.Start; |
| 31 | import org.apache.felix.dm.annotation.api.Stop; |
| 32 | import org.apache.felix.dm.itest.util.Ensure; |
| 33 | import org.junit.Assert; |
| 34 | import org.osgi.framework.ServiceReference; |
| 35 | import org.osgi.service.cm.Configuration; |
| 36 | import org.osgi.service.cm.ConfigurationAdmin; |
| 37 | |
| 38 | /** |
| 39 | * Tests various bind method signatures |
| 40 | */ |
| 41 | public class MethodSignatures { |
| 42 | // For Consumer service |
| 43 | public final static String ENSURE_SERVICE_DEPENDENCY = "MethodSignatures1"; |
| 44 | |
| 45 | // For FactoryPidComponent |
| 46 | public final static String ENSURE_FACTORYPID = "MethodSignatures2"; |
| 47 | |
| 48 | // This component configures the Consumer component. |
| 49 | @Component |
| 50 | public static class ConsumerConfigurator { |
| 51 | @ServiceDependency(filter="(name=" + ENSURE_SERVICE_DEPENDENCY + ")") |
| 52 | Ensure m_ensure; |
| 53 | |
| 54 | @ServiceDependency |
| 55 | ConfigurationAdmin m_cm; |
| 56 | |
| 57 | private Configuration m_conf; |
| 58 | |
| 59 | @Start |
| 60 | void start() throws IOException { |
| 61 | m_conf = m_cm.getConfiguration(Consumer.class.getName()); |
| 62 | Hashtable<String, Object> props = new Hashtable<>(); |
| 63 | props.put("foo", "bar"); |
| 64 | m_conf.update(props); |
| 65 | } |
| 66 | |
| 67 | @Stop |
| 68 | void stop() throws IOException { |
| 69 | m_conf.delete(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // A simple provider service |
| 74 | @Component(provides=Provider.class) |
| 75 | public static class Provider { |
| 76 | @ServiceDependency(filter="(name=" + ENSURE_SERVICE_DEPENDENCY + ")") |
| 77 | Ensure m_ensure; |
| 78 | } |
| 79 | |
| 80 | // This consumer depends on the configuration and on the provider, using multiple method signatures. |
| 81 | @Component |
| 82 | public static class Consumer { |
| 83 | Dictionary<String, Object> m_properties; |
| 84 | Dictionary<String, Object> m_properties2; |
| 85 | |
| 86 | @ConfigurationDependency |
| 87 | void updated(Dictionary<String, Object> properties) { |
| 88 | m_properties = properties; |
| 89 | } |
| 90 | |
| 91 | @ConfigurationDependency |
| 92 | void updated2(org.apache.felix.dm.Component component, Dictionary<String, Object> properties) { |
| 93 | Assert.assertNotNull(component); |
| 94 | m_properties2 = properties; |
| 95 | } |
| 96 | |
| 97 | @ServiceDependency(filter="(name=" + ENSURE_SERVICE_DEPENDENCY + ")") |
| 98 | Ensure m_ensure; |
| 99 | |
| 100 | @ServiceDependency |
| 101 | void bind(org.apache.felix.dm.Component component, ServiceReference ref, Provider provider) { |
| 102 | Assert.assertNotNull(component); |
| 103 | Assert.assertNotNull(ref); |
| 104 | Assert.assertNotNull(provider); |
| 105 | m_ensure.step(); |
| 106 | } |
| 107 | |
| 108 | @ServiceDependency |
| 109 | void bind(org.apache.felix.dm.Component component, Provider provider) { |
| 110 | Assert.assertNotNull(component); |
| 111 | Assert.assertNotNull(provider); |
| 112 | m_ensure.step(); |
| 113 | } |
| 114 | |
| 115 | @ServiceDependency |
| 116 | void bind(org.apache.felix.dm.Component component, Map<?, ?> properties, Provider provider) { |
| 117 | Assert.assertNotNull(component); |
| 118 | Assert.assertNotNull(properties); |
| 119 | Assert.assertNotNull(provider); |
| 120 | m_ensure.step(); |
| 121 | } |
| 122 | |
| 123 | @ServiceDependency |
| 124 | void bind(ServiceReference ref, Provider provider) { |
| 125 | Assert.assertNotNull(ref); |
| 126 | Assert.assertNotNull(provider); |
| 127 | m_ensure.step(); |
| 128 | } |
| 129 | |
| 130 | @ServiceDependency |
| 131 | void bind(Provider provider) { |
| 132 | Assert.assertNotNull(provider); |
| 133 | m_ensure.step(); |
| 134 | } |
| 135 | |
| 136 | @ServiceDependency |
| 137 | void bind(Provider provider, Map<?,?> properties) { |
| 138 | Assert.assertNotNull(provider); |
| 139 | Assert.assertNotNull(properties); |
| 140 | m_ensure.step(); |
| 141 | } |
| 142 | |
| 143 | @ServiceDependency |
| 144 | void bind(Map<?,?> properties, Provider provider) { |
| 145 | Assert.assertNotNull(properties); |
| 146 | Assert.assertNotNull(provider); |
| 147 | m_ensure.step(); |
| 148 | } |
| 149 | |
| 150 | @ServiceDependency |
| 151 | void bind(Provider provider, Dictionary<?,?> properties) { |
| 152 | Assert.assertNotNull(properties); |
| 153 | Assert.assertNotNull(provider); |
| 154 | m_ensure.step(); |
| 155 | } |
| 156 | |
| 157 | @ServiceDependency |
| 158 | void bind(Dictionary<?,?> properties, Provider provider) { |
| 159 | Assert.assertNotNull(properties); |
| 160 | Assert.assertNotNull(provider); |
| 161 | m_ensure.step(); |
| 162 | } |
| 163 | |
| 164 | @Start |
| 165 | void start() { |
| 166 | Assert.assertNotNull(m_properties); |
| 167 | Assert.assertNotNull(m_properties2); |
| 168 | Assert.assertEquals("bar", m_properties.get("foo")); |
| 169 | Assert.assertEquals("bar", m_properties2.get("foo")); |
| 170 | m_ensure.step(10); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // This component configures the FactoryPidComponent / FactoryPidComponent2 components. |
| 175 | @Component |
| 176 | public static class FactoryPidConfigurator { |
| 177 | @ServiceDependency(filter="(name=" + ENSURE_FACTORYPID + ")") |
| 178 | Ensure m_ensure; |
| 179 | |
| 180 | @ServiceDependency |
| 181 | ConfigurationAdmin m_cm; |
| 182 | |
| 183 | private Configuration m_conf1; |
| 184 | private Configuration m_conf2; |
| 185 | |
| 186 | @Start |
| 187 | void start() throws IOException { |
| 188 | m_conf1 = m_cm.createFactoryConfiguration(FactoryPidComponent.class.getName()); |
| 189 | Hashtable<String, Object> props = new Hashtable<>(); |
| 190 | props.put("foo", "bar"); |
| 191 | m_conf1.update(props); |
| 192 | |
| 193 | m_conf2 = m_cm.createFactoryConfiguration(FactoryPidComponent2.class.getName()); |
| 194 | props = new Hashtable<>(); |
| 195 | props.put("foo", "bar"); |
| 196 | m_conf2.update(props); |
| 197 | } |
| 198 | |
| 199 | @Stop |
| 200 | void stop() throws IOException { |
| 201 | m_conf1.delete(); |
| 202 | m_conf2.delete(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // This is a factory pid component with an updated callback having the "updated(Dictionary)" signature |
| 207 | @FactoryConfigurationAdapterService |
| 208 | public static class FactoryPidComponent { |
| 209 | Dictionary<String, Object> m_properties; |
| 210 | |
| 211 | void updated(Dictionary<String, Object> properties) { |
| 212 | m_properties = properties; |
| 213 | } |
| 214 | |
| 215 | @ServiceDependency(filter="(name=" + ENSURE_FACTORYPID + ")") |
| 216 | Ensure m_ensure; |
| 217 | |
| 218 | @Start |
| 219 | void start() { |
| 220 | Assert.assertNotNull(m_properties); |
| 221 | Assert.assertEquals("bar", m_properties.get("foo")); |
| 222 | m_ensure.step(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // This is a factory pid component with an updated callback having the "updated(Component, Dictionary)" signature |
| 227 | @FactoryConfigurationAdapterService |
| 228 | public static class FactoryPidComponent2 { |
| 229 | Dictionary<String, Object> m_properties; |
| 230 | |
| 231 | void updated(org.apache.felix.dm.Component component, Dictionary<String, Object> properties) { |
| 232 | Assert.assertNotNull(component); |
| 233 | m_properties = properties; |
| 234 | } |
| 235 | |
| 236 | @ServiceDependency(filter="(name=" + ENSURE_FACTORYPID + ")") |
| 237 | Ensure m_ensure; |
| 238 | |
| 239 | @Start |
| 240 | void start() { |
| 241 | Assert.assertNotNull(m_properties); |
| 242 | Assert.assertEquals("bar", m_properties.get("foo")); |
| 243 | m_ensure.step(); |
| 244 | } |
| 245 | } |
| 246 | } |