blob: dc60d789901b5eeb417680dc5d3529b7b484fead [file] [log] [blame]
Pierre De Rop445ddec2010-05-23 21:05:27 +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.impl;
20
21import java.util.Enumeration;
22import java.util.List;
23import java.util.Properties;
24
25import org.apache.felix.dm.DependencyManager;
Marcel Offermansb1959f42010-07-01 12:23:51 +000026import org.apache.felix.dm.dependencies.Dependency;
Pierre De Rop445ddec2010-05-23 21:05:27 +000027import org.apache.felix.dm.resources.Resource;
28import org.apache.felix.dm.service.Service;
Pierre De Ropc128b172010-05-23 21:28:15 +000029import org.apache.felix.dm.service.ServiceStateListener;
Pierre De Rop445ddec2010-05-23 21:05:27 +000030
31/**
32 * Resource adapter service implementation. This class extends the FilterService in order to catch
33 * some Service methods for configuring actual resource adapter service implementation.
34 */
35public class ResourceAdapterServiceImpl extends FilterService
36{
37 /**
38 * Creates a new Resource Adapter Service implementation.
39 * @param dm the dependency manager used to create our internal adapter service
40 */
41 public ResourceAdapterServiceImpl(DependencyManager dm, String resourceFilter, boolean propagate) {
42 super(dm.createService()); // This service will be filtered by our super class, allowing us to take control.
43 m_service.setImplementation(new ResourceAdapterImpl(resourceFilter, propagate))
44 .add(dm.createResourceDependency()
45 .setFilter(resourceFilter)
46 .setAutoConfig(false)
47 .setCallbacks("added", "removed"));
48 }
49
50 public class ResourceAdapterImpl extends AbstractDecorator {
51 private final String m_resourceFilter;
52 private final boolean m_propagate;
53
54 public ResourceAdapterImpl(String resourceFilter, boolean propagate) {
55 m_resourceFilter = resourceFilter;
56 m_propagate = propagate;
57 }
58
59 public Service createService(Object[] properties) {
60 Resource resource = (Resource) properties[0];
61 Properties props = new Properties();
62 if (m_serviceProperties != null) {
63 Enumeration e = m_serviceProperties.keys();
64 while (e.hasMoreElements()) {
65 Object key = e.nextElement();
66 props.put(key, m_serviceProperties.get(key));
67 }
68 }
69 List dependencies = m_service.getDependencies();
70 // the first dependency is always the dependency on the resource, which
71 // will be replaced with a more specific dependency below
72 dependencies.remove(0);
Pierre De Ropc128b172010-05-23 21:28:15 +000073 Service service = m_manager.createService()
74 .setInterface(m_serviceInterfaces, props)
75 .setImplementation(m_serviceImpl)
76 .setFactory(m_factory, m_factoryCreateMethod) // if not set, no effect
77 .setComposition(m_compositionInstance, m_compositionMethod) // if not set, no effect
78 .setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy) // if not set, no effect
Pierre De Ropc128b172010-05-23 21:28:15 +000079 .add(m_manager.createResourceDependency()
80 .setResource(resource)
81 .setPropagate(m_propagate)
82 .setCallbacks(null, "changed", null)
83 .setAutoConfig(true)
Marcel Offermansb1959f42010-07-01 12:23:51 +000084 .setRequired(true));
85
86 for (Object d : dependencies) {
87 service.add(((Dependency) d).createCopy());
88 }
89
Pierre De Ropc128b172010-05-23 21:28:15 +000090 for (int i = 0; i < m_stateListeners.size(); i ++) {
91 service.addStateListener((ServiceStateListener) m_stateListeners.get(i));
92 }
93 return service;
Pierre De Rop445ddec2010-05-23 21:05:27 +000094 }
95 }
96}