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.annotation.api.AspectService; |
| 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.ServiceDependency; |
| 26 | import org.apache.felix.dm.annotation.api.Start; |
| 27 | import org.apache.felix.dm.annotation.api.Stop; |
| 28 | import org.apache.felix.dm.itest.util.Ensure; |
| 29 | |
| 30 | /** |
| 31 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 32 | */ |
| 33 | public class AspectLifecycleAnnotation { |
| 34 | public interface ServiceInterface { |
| 35 | public void run(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Tests an aspect service, and ensure that its lifecycle methods are properly invoked (init/start/stop/destroy) |
| 40 | */ |
| 41 | @Component |
| 42 | public static class AspectLifecycleTest { |
| 43 | @ServiceDependency |
| 44 | void bind(ServiceInterface service) { |
| 45 | service.run(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | @Component |
| 50 | public static class ServiceProvider implements ServiceInterface { |
| 51 | public final static String ENSURE = "AspectLifecycleAnnotation.ServiceProvider"; |
| 52 | |
| 53 | @ServiceDependency(filter = "(name=" + ENSURE + ")") |
| 54 | protected volatile Ensure m_sequencer; |
| 55 | |
| 56 | public void run() { |
| 57 | m_sequencer.step(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @AspectService(ranking = 10) |
| 62 | public static class ServiceProviderAspect implements ServiceInterface { |
| 63 | public final static String ENSURE = "AspectLifecycleAnnotation.ServiceProviderAspect"; |
| 64 | |
| 65 | protected volatile boolean m_initCalled; |
| 66 | protected volatile Ensure m_sequencer; |
| 67 | |
| 68 | @ServiceDependency(filter = "(name=" + ENSURE + ")") |
| 69 | protected void bind(Ensure sequencer) { |
| 70 | m_sequencer = sequencer; |
| 71 | m_sequencer.step(2); |
| 72 | } |
| 73 | |
| 74 | @Init |
| 75 | void init() { |
| 76 | m_initCalled = true; |
| 77 | } |
| 78 | |
| 79 | @Start |
| 80 | void start() { |
| 81 | if (!m_initCalled) { |
| 82 | throw new IllegalStateException("start method called, but init method was not called"); |
| 83 | } |
| 84 | m_sequencer.step(3); |
| 85 | } |
| 86 | |
| 87 | public void run() { |
| 88 | m_sequencer.step(4); |
| 89 | } |
| 90 | |
| 91 | @Stop() |
| 92 | void stop() { |
| 93 | // At this point, the AspectLifecycleTest class has been rebound to the original ServiceProvider. |
| 94 | m_sequencer.step(6); |
| 95 | } |
| 96 | |
| 97 | @Destroy |
| 98 | void destroy() { |
| 99 | m_sequencer.step(7); |
| 100 | } |
| 101 | } |
| 102 | } |