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.BundleAdapterService; |
| 23 | import org.apache.felix.dm.annotation.api.BundleDependency; |
| 24 | import org.apache.felix.dm.annotation.api.Component; |
| 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.ServiceDependency; |
| 28 | import org.apache.felix.dm.annotation.api.Start; |
| 29 | import org.apache.felix.dm.annotation.api.Stop; |
| 30 | import org.apache.felix.dm.itest.util.Ensure; |
| 31 | import org.osgi.framework.Bundle; |
| 32 | import org.osgi.framework.BundleContext; |
| 33 | |
| 34 | /** |
| 35 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 36 | */ |
| 37 | public class BundleDependencyAnnotation { |
| 38 | public static final String ENSURE_CONSUMER = "BundleDependencyAnnotation.consumer"; |
| 39 | public static final String ENSURE_ADAPTER = "BundleDependencyAnnotation.adapter"; |
| 40 | public static final String METATYPE_BSN = "org.apache.felix.metatype"; |
| 41 | |
| 42 | public interface ServiceInterface extends Runnable { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Simple Consumer which has a BundleDependency dependency. |
| 47 | */ |
| 48 | @Component |
| 49 | public static class Consumer { |
| 50 | @ServiceDependency(filter = "(name=" + ENSURE_CONSUMER + ")") |
| 51 | private volatile Ensure m_sequencer; |
| 52 | |
| 53 | @BundleDependency(required = true, removed = "removed", filter = "(Bundle-SymbolicName=" + METATYPE_BSN + ")", stateMask = Bundle.ACTIVE) |
| 54 | public void add(Bundle b) { |
| 55 | if (b != null && b.getSymbolicName().equals(METATYPE_BSN)) { |
| 56 | m_sequencer.step(1); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Start |
| 61 | public void start() { |
| 62 | m_sequencer.step(2); |
| 63 | } |
| 64 | |
| 65 | @Stop |
| 66 | public void stop() { |
| 67 | m_sequencer.step(3); |
| 68 | } |
| 69 | |
| 70 | protected void removed(Bundle b) { |
| 71 | if (b != null && b.getSymbolicName().equals(METATYPE_BSN)) { |
| 72 | m_sequencer.step(4); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * ServiceInterface Consumer. |
| 79 | */ |
| 80 | @Component |
| 81 | public static class ServiceConsumer { |
| 82 | @ServiceDependency(filter = "(name=" + ENSURE_ADAPTER + ")") |
| 83 | volatile Ensure m_sequencer; |
| 84 | |
| 85 | @ServiceDependency |
| 86 | volatile ServiceInterface m_service; |
| 87 | |
| 88 | @Start |
| 89 | void start() { |
| 90 | m_sequencer.step(2); |
| 91 | m_service.run(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * A BundleAdapter test, which adapts the dependency manager bundle to the ServiceInterface service. |
| 97 | */ |
| 98 | @BundleAdapterService(filter = "(Bundle-SymbolicName=" + METATYPE_BSN + ")", stateMask = Bundle.INSTALLED |
| 99 | | Bundle.RESOLVED | Bundle.ACTIVE, propagate = true, properties = {@Property(name = "foo", value = "bar")}) |
| 100 | public static class ServiceProvider implements ServiceInterface { |
| 101 | // Adapted bundle (injected by reflection). |
| 102 | protected volatile Bundle m_bundle; |
| 103 | |
| 104 | // Our Sequencer required dependency |
| 105 | @ServiceDependency(filter = "(name=" + ENSURE_ADAPTER + ")") |
| 106 | volatile Ensure m_sequencer; |
| 107 | |
| 108 | // Check auto config injections |
| 109 | @Inject |
| 110 | volatile BundleContext m_bc; |
| 111 | BundleContext m_bcNotInjected; |
| 112 | |
| 113 | @Inject |
| 114 | volatile DependencyManager m_dm; |
| 115 | DependencyManager m_dmNotInjected; |
| 116 | |
| 117 | @Inject |
| 118 | volatile org.apache.felix.dm.Component m_component; |
| 119 | org.apache.felix.dm.Component m_componentNotInjected; |
| 120 | |
| 121 | @Start |
| 122 | void start() { |
| 123 | checkInjectedFields(); |
| 124 | m_sequencer.step(1); |
| 125 | } |
| 126 | |
| 127 | public void run() { |
| 128 | if (m_bundle == null || !m_bundle.getSymbolicName().equals(METATYPE_BSN)) { |
| 129 | throw new IllegalStateException("ServiceProvider did not get proper bundle: " + m_bundle); |
| 130 | } |
| 131 | m_sequencer.step(3); |
| 132 | } |
| 133 | |
| 134 | private void checkInjectedFields() { |
| 135 | if (m_bc == null) { |
| 136 | m_sequencer.throwable(new Exception("Bundle Context not injected")); |
| 137 | return; |
| 138 | } |
| 139 | if (m_bcNotInjected != null) { |
| 140 | m_sequencer.throwable(new Exception("Bundle Context must not be injected")); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | if (m_dm == null) { |
| 145 | m_sequencer.throwable(new Exception("DependencyManager not injected")); |
| 146 | return; |
| 147 | } |
| 148 | if (m_dmNotInjected != null) { |
| 149 | m_sequencer.throwable(new Exception("DependencyManager must not be injected")); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (m_component == null) { |
| 154 | m_sequencer.throwable(new Exception("Component not injected")); |
| 155 | return; |
| 156 | } |
| 157 | if (m_componentNotInjected != null) { |
| 158 | m_sequencer.throwable(new Exception("Component must not be injected")); |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |