blob: 841ba87fe777de9ad74c0bee54c335f3dfc99e6c [file] [log] [blame]
Pierre De Rop3a00a212015-03-01 09:27:46 +00001/*
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 */
19package org.apache.felix.dm.runtime.itest.components;
20
21import org.apache.felix.dm.annotation.api.AspectService;
22import org.apache.felix.dm.annotation.api.Component;
23import org.apache.felix.dm.annotation.api.Destroy;
24import org.apache.felix.dm.annotation.api.Init;
25import org.apache.felix.dm.annotation.api.ServiceDependency;
26import org.apache.felix.dm.annotation.api.Start;
27import org.apache.felix.dm.annotation.api.Stop;
28import org.apache.felix.dm.itest.util.Ensure;
29
30/**
31 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
32 */
33public 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}