Pierre De Rop | 453344a | 2016-02-09 21:48:32 +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.Hashtable; |
| 23 | |
| 24 | import org.apache.felix.dm.annotation.api.Component; |
| 25 | import org.apache.felix.dm.annotation.api.ConfigurationDependency; |
| 26 | import org.apache.felix.dm.annotation.api.ServiceDependency; |
| 27 | import org.apache.felix.dm.annotation.api.Start; |
| 28 | import org.apache.felix.dm.annotation.api.Stop; |
| 29 | import org.apache.felix.dm.itest.util.Ensure; |
| 30 | import org.junit.Assert; |
| 31 | import org.osgi.service.cm.Configuration; |
| 32 | import org.osgi.service.cm.ConfigurationAdmin; |
| 33 | |
| 34 | /** |
| 35 | * Tests for new Configuration Proxy Types (FELIX-5177) |
| 36 | */ |
| 37 | public class ConfigurationProxy { |
| 38 | // For ConfigurationDependency service |
| 39 | public final static String ENSURE_CONFIG_DEPENDENCY = "ConfigurationProxy.Ensure.Configuration"; |
| 40 | |
| 41 | public interface Config { |
| 42 | String getFoo(); |
| 43 | } |
| 44 | |
| 45 | // This component configures the Consumer component. |
| 46 | @Component |
| 47 | public static class ConsumerConfigurator { |
| 48 | @ServiceDependency(filter="(name=" + ENSURE_CONFIG_DEPENDENCY + ")") |
| 49 | Ensure m_ensure; |
| 50 | |
| 51 | @ServiceDependency |
| 52 | ConfigurationAdmin m_cm; |
| 53 | |
| 54 | private Configuration m_conf; |
| 55 | |
| 56 | @Start |
| 57 | void start() throws IOException { |
| 58 | m_conf = m_cm.getConfiguration(Config.class.getName()); |
| 59 | Hashtable<String, Object> props = new Hashtable<>(); |
| 60 | props.put("foo", "bar"); |
| 61 | m_conf.update(props); |
| 62 | } |
| 63 | |
| 64 | @Stop |
| 65 | void stop() throws IOException { |
| 66 | m_conf.delete(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // This consumer depends on the configuration and on the provider, using multiple method signatures. |
| 71 | @Component |
| 72 | public static class Consumer { |
| 73 | Config m_config; |
| 74 | Config m_config2; |
| 75 | |
| 76 | @ConfigurationDependency |
| 77 | void updated(Config cnf) { |
| 78 | if (cnf != null) { |
| 79 | Assert.assertNotNull(cnf); |
| 80 | m_config = cnf; |
| 81 | } else { |
| 82 | m_config = null; |
| 83 | m_ensure.step(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @ConfigurationDependency |
| 88 | void updated2(org.apache.felix.dm.Component comp, Config cnf) { |
| 89 | if (cnf != null) { |
| 90 | Assert.assertNotNull(comp); |
| 91 | Assert.assertNotNull(cnf); |
| 92 | m_config2 = cnf; |
| 93 | } else { |
| 94 | m_config2 = null; |
| 95 | m_ensure.step(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @ServiceDependency(filter="(name=" + ENSURE_CONFIG_DEPENDENCY + ")") |
| 100 | Ensure m_ensure; |
| 101 | |
| 102 | @Start |
| 103 | void start() { |
| 104 | Assert.assertNotNull(m_config); |
| 105 | Assert.assertNotNull(m_config2); |
| 106 | Assert.assertEquals("bar", m_config.getFoo()); |
| 107 | Assert.assertEquals("bar", m_config2.getFoo()); |
| 108 | m_ensure.step(1); |
| 109 | } |
| 110 | } |
| 111 | } |