blob: 09d7dd1610a3a2b84c15c5244b74072f401bff9c [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.itest.api;
20
21import java.net.URL;
22import java.util.Hashtable;
23
24import org.apache.felix.dm.Component;
25import org.apache.felix.dm.ComponentState;
26import org.apache.felix.dm.ComponentStateListener;
27import org.apache.felix.dm.Dependency;
28import org.apache.felix.dm.DependencyManager;
29import org.apache.felix.dm.ResourceHandler;
30import org.apache.felix.dm.itest.util.Ensure;
31import org.apache.felix.dm.itest.util.TestBase;
32
33/**
34 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
35 */
36public class ResourceAdapterDependencyAddAndRemoveTest2 extends TestBase {
37 public void testBasicResourceAdapter() throws Exception {
38 DependencyManager m = getDM();
39 // helper class that ensures certain steps get executed in sequence
40 Ensure e = new Ensure();
41 // create a resource provider
42 ResourceProvider provider = new ResourceProvider(context, new URL("file://localhost/path/to/file1.txt"));
43 // activate it
44 Hashtable<String, String> props = new Hashtable<String, String>();
45 props.put("id", "1");
46 m.add(m.createComponent()
47 .setInterface(ServiceInterface.class.getName(), props)
48 .setImplementation(new ServiceProvider(e))
49 );
50
51 props = new Hashtable<String, String>();
52 props.put("id", "2");
53 m.add(m.createComponent()
54 .setInterface(ServiceInterface.class.getName(), props)
55 .setImplementation(new ServiceProvider(e))
56 );
57
58 m.add(m.createComponent()
59 .setImplementation(provider)
60 .add(m.createServiceDependency()
61 .setService(ResourceHandler.class)
62 .setCallbacks("add", "remove")
63 )
64 );
65
66 // create a resource adapter for our single resource
67 // note that we can provide an actual implementation instance here because there will be only one
68 // adapter, normally you'd want to specify a Class here
69 Dependency d = m.createServiceDependency().setService(ServiceInterface.class, "(id=1)").setRequired(true);
70 ResourceAdapter service = new ResourceAdapter(e, d);
71
72 CallbackInstance callbackInstance = new CallbackInstance(e, d);
73 Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed")
74 .setImplementation(service)
75 .setCallbacks(callbackInstance, "init", "start", "stop", "destroy");
76 component.add(new ComponentStateListenerImpl(e));
77 m.add(component);
78 // wait until the single resource is available
79 e.waitForStep(1, 5000);
80 // trigger a 'change' in our resource
81 provider.change();
82 // wait until the changed callback is invoked
83 e.waitForStep(2, 5000);
84
85 System.out.println("Done!");
86 m.clear();
87 }
88
89 static class ResourceAdapter {
90 protected URL m_resource; // injected by reflection.
91 final Dependency m_dependency;
92
93 ResourceAdapter(Ensure e, Dependency d) {
94 m_dependency = d;
95 }
96 }
97
98 static interface ServiceInterface {
99 public void invoke();
100 }
101
102 static class ServiceProvider implements ServiceInterface {
103 public ServiceProvider(Ensure e) {
104 }
105 public void invoke() {
106 }
107 }
108
109 static class CallbackInstance {
110 private final Ensure m_ensure;
111 private final Dependency m_dependency;
112
113 public CallbackInstance(Ensure e, Dependency d) {
114 m_ensure = e;
115 m_dependency = d;
116 }
117
118 void init(Component c) {
119 c.add(m_dependency);
120 System.out.println("init");
121 m_ensure.step(1);
122 }
123
124 void start() {
125 System.out.println("start");
126 }
127
128 void stop() {
129 System.out.println("stop");
130 }
131
132 void destroy() {
133 System.out.println("destroy");
134 }
135
136 void changed(Component component) {
137 m_ensure.step(2);
138 System.out.println("Changing the dependencies");
139 Dependency oldDependency = m_dependency;
140
141 // and add a new dependency
142 component.add(component.getDependencyManager().createServiceDependency().setService(ServiceInterface.class, "(id=2)").setRequired(true));
143 // remove the old dependency
144 component.remove(oldDependency);
145 System.out.println("Changed the dependencies");
146 }
147 }
148
149 static class ComponentStateListenerImpl implements ComponentStateListener {
150 public ComponentStateListenerImpl(Ensure e) {
151 }
152
153 @Override
154 public void changed(Component c, ComponentState state) {
155 switch (state) {
156 case INACTIVE:
157 System.out.println("INACTIVE");
158 break;
159 case INSTANTIATED_AND_WAITING_FOR_REQUIRED:
160 System.out.println("INSTANTIATED_AND_WAITING_FOR_REQUIRED");
161 break;
162 case WAITING_FOR_REQUIRED:
163 System.out.println("WAITING_FOR_REQUIRED");
164 break;
165 case TRACKING_OPTIONAL:
166 System.out.println("TRACKING_OPTIONAL");
167 break;
168
169 }
170 }
171 }
172}