Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +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 org.apache.felix.dm.DependencyManager; |
| 22 | import org.apache.felix.dm.annotation.api.Component; |
| 23 | import org.apache.felix.dm.annotation.api.Destroy; |
| 24 | import org.apache.felix.dm.annotation.api.Init; |
| 25 | import org.apache.felix.dm.annotation.api.Inject; |
| 26 | import org.apache.felix.dm.annotation.api.Property; |
| 27 | import org.apache.felix.dm.annotation.api.Registered; |
| 28 | import org.apache.felix.dm.annotation.api.ServiceDependency; |
| 29 | import org.apache.felix.dm.annotation.api.Start; |
| 30 | import org.apache.felix.dm.annotation.api.Stop; |
| 31 | import org.apache.felix.dm.annotation.api.Unregistered; |
| 32 | import org.apache.felix.dm.itest.util.Ensure; |
| 33 | import org.osgi.framework.BundleContext; |
| 34 | import org.osgi.framework.ServiceRegistration; |
| 35 | import org.osgi.service.log.LogService; |
| 36 | |
| 37 | /** |
| 38 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 39 | */ |
| 40 | public class SimpleAnnotations { |
| 41 | /** |
| 42 | * Provides a <code>Runnable</code> service, which is required by the |
| 43 | * {@link Consumer} class. |
| 44 | */ |
| 45 | @Component(properties = {@Property(name = "foo", value = "bar"), @Property(name="type", value="SimpleAnnotations")}) |
| 46 | public static class Producer implements Runnable { |
| 47 | public final static String ENSURE = "SimpleAnnotations.Producer"; |
| 48 | |
| 49 | @ServiceDependency(filter = "(name=" + ENSURE + ")") |
| 50 | volatile Ensure _ensure; |
| 51 | |
| 52 | @ServiceDependency |
| 53 | volatile LogService _logService; |
| 54 | |
| 55 | @Inject |
| 56 | volatile BundleContext _ctx; |
| 57 | |
| 58 | @Init |
| 59 | protected void init() { |
| 60 | _logService.log(LogService.LOG_INFO, "producer.init"); |
| 61 | // Our component is initializing (at this point: all required |
| 62 | // dependencies are injected). |
| 63 | _ensure.step(1); |
| 64 | } |
| 65 | |
| 66 | @Start |
| 67 | protected void start() { |
| 68 | // We are about to be registered in the OSGi registry. |
| 69 | _ensure.step(2); |
| 70 | } |
| 71 | |
| 72 | @Registered |
| 73 | protected void registered(ServiceRegistration sr) { |
| 74 | _logService.log(LogService.LOG_INFO, "Registered"); |
| 75 | if (sr == null) { |
| 76 | _ensure.throwable(new Exception("ServiceRegistration is null")); |
| 77 | } |
| 78 | if (!"bar".equals(sr.getReference().getProperty("foo"))) { |
| 79 | _ensure.throwable(new Exception("Invalid Service Properties")); |
| 80 | } |
| 81 | _ensure.step(3); |
| 82 | } |
| 83 | |
| 84 | public void run() { |
| 85 | _ensure.step(5); |
| 86 | } |
| 87 | |
| 88 | @Stop |
| 89 | protected void stop() { |
| 90 | // We are about to be unregistered from the OSGi registry, and we |
| 91 | // must stop. |
| 92 | _ensure.step(8); |
| 93 | } |
| 94 | |
| 95 | @Unregistered |
| 96 | protected void stopped() { |
| 97 | // We are unregistered from the OSGi registry. |
| 98 | _ensure.step(9); |
| 99 | } |
| 100 | |
| 101 | @Destroy |
| 102 | public void destroy() { |
| 103 | // Our component is shutting down. |
| 104 | _ensure.step(10); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Consumes a service which is provided by the {@link Producer} class. |
| 110 | */ |
| 111 | @Component |
| 112 | public static class Consumer { |
| 113 | public final static String ENSURE = "SimpleAnnotations.Consumer"; |
| 114 | |
| 115 | @ServiceDependency |
| 116 | volatile LogService _logService; |
| 117 | |
| 118 | @ServiceDependency(filter="(type=SimpleAnnotations)") |
| 119 | volatile Runnable _runnable; |
| 120 | |
| 121 | @ServiceDependency(filter = "(name=" + ENSURE + ")") |
| 122 | volatile Ensure _ensure; |
| 123 | |
| 124 | @Inject |
| 125 | volatile BundleContext _bc; |
| 126 | BundleContext _bcNotInjected; |
| 127 | |
| 128 | @Inject |
| 129 | volatile DependencyManager _dm; |
| 130 | DependencyManager _dmNotInjected; |
| 131 | |
| 132 | @Inject |
| 133 | volatile org.apache.felix.dm.Component _component; |
| 134 | org.apache.felix.dm.Component _componentNotInjected; |
| 135 | |
| 136 | @Start |
| 137 | protected void start() { |
| 138 | _logService.log(LogService.LOG_INFO, "Consumer.START: "); |
| 139 | checkInjectedFields(); |
| 140 | _ensure.step(4); |
| 141 | _runnable.run(); |
| 142 | } |
| 143 | |
| 144 | private void checkInjectedFields() { |
| 145 | if (_bc == null) { |
| 146 | _ensure.throwable(new Exception("Bundle Context not injected")); |
| 147 | return; |
| 148 | } |
| 149 | if (_bcNotInjected != null) { |
| 150 | _ensure.throwable(new Exception("Bundle Context must not be injected")); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if (_dm == null) { |
| 155 | _ensure.throwable(new Exception("DependencyManager not injected")); |
| 156 | return; |
| 157 | } |
| 158 | if (_dmNotInjected != null) { |
| 159 | _ensure.throwable(new Exception("DependencyManager must not be injected")); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | if (_component == null) { |
| 164 | _ensure.throwable(new Exception("Component not injected")); |
| 165 | return; |
| 166 | } |
| 167 | if (_componentNotInjected != null) { |
| 168 | _ensure.throwable(new Exception("Component must not be injected")); |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Stop |
| 174 | protected void stop() { |
| 175 | _ensure.step(6); |
| 176 | } |
| 177 | |
| 178 | @Destroy |
| 179 | void destroy() { |
| 180 | _ensure.step(7); |
| 181 | } |
| 182 | } |
| 183 | } |