blob: e4f6b25b330cd5cc2cf703e31043692121188da6 [file] [log] [blame]
Pierre De Rop34231582010-05-23 20:05:16 +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;
20import java.util.Enumeration;
21import java.util.List;
22import java.util.Properties;
23
Marcel Offermans8b93efa2010-07-02 18:27:21 +000024import org.apache.felix.dm.Dependency;
Pierre De Rop34231582010-05-23 20:05:16 +000025import org.apache.felix.dm.DependencyManager;
Marcel Offermans8b93efa2010-07-02 18:27:21 +000026import org.apache.felix.dm.Service;
27import org.apache.felix.dm.ServiceStateListener;
Pierre De Rop34231582010-05-23 20:05:16 +000028import org.osgi.framework.ServiceReference;
29
30/**
31 * Adapter Service implementation. This class extends the FilterService in order to catch
32 * some Service methods for configuring actual adapter service implementation.
33 */
34public class AdapterServiceImpl extends FilterService
35{
36 /**
37 * Creates a new Adapter Service implementation.
38 * @param dm the dependency manager used to create our internal adapter service
39 * @param adapteeInterface the service interface to apply the adapter to
40 * @param adapteeFilter the filter condition to use with the service interface
41 */
42 public AdapterServiceImpl(DependencyManager dm, Class adapteeInterface, String adapteeFilter)
43 {
44 super(dm.createService()); // This service will be filtered by our super class, allowing us to take control.
45 m_service.setImplementation(new AdapterImpl(adapteeInterface, adapteeFilter))
46 .add(dm.createServiceDependency()
Marcel Offermans77bcf442010-06-24 11:15:42 +000047 .setService(adapteeInterface, adapteeFilter)
Pierre De Rop34231582010-05-23 20:05:16 +000048 .setAutoConfig(false)
49 .setCallbacks("added", "removed"));
50 }
51
52 public class AdapterImpl extends AbstractDecorator {
53 private final Class m_adapteeInterface;
54 private final String m_adapteeFilter;
55
56 public AdapterImpl(Class adapteeInterface, String adapteeFilter) {
57 m_adapteeInterface = adapteeInterface;
58 m_adapteeFilter = adapteeFilter;
59 }
60
61 public Service createService(Object[] properties) {
62 ServiceReference ref = (ServiceReference) properties[0];
Pierre De Rop34231582010-05-23 20:05:16 +000063 Properties props = new Properties();
64 String[] keys = ref.getPropertyKeys();
65 for (int i = 0; i < keys.length; i++) {
66 props.put(keys[i], ref.getProperty(keys[i]));
67 }
68 if (m_serviceProperties != null) {
69 Enumeration e = m_serviceProperties.keys();
70 while (e.hasMoreElements()) {
71 Object key = e.nextElement();
72 props.put(key, m_serviceProperties.get(key));
73 }
74 }
75 List dependencies = m_service.getDependencies();
76 dependencies.remove(0);
Pierre De Ropc128b172010-05-23 21:28:15 +000077 Service service = m_manager.createService()
Pierre De Rop34231582010-05-23 20:05:16 +000078 .setInterface(m_serviceInterfaces, props)
79 .setImplementation(m_serviceImpl)
80 .setFactory(m_factory, m_factoryCreateMethod) // if not set, no effect
81 .setComposition(m_compositionInstance, m_compositionMethod) // if not set, no effect
82 .setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy) // if not set, no effect
Marcel Offermansb1959f42010-07-01 12:23:51 +000083// .add(dependencies)
Pierre De Rop34231582010-05-23 20:05:16 +000084 .add(m_manager.createServiceDependency()
85 .setService(m_adapteeInterface, ref)
86 .setRequired(true));
Marcel Offermansb1959f42010-07-01 12:23:51 +000087
Marcel Offermans3d921212010-08-09 13:37:02 +000088 configureAutoConfigState(service, m_service);
89
Marcel Offermanse9c13d92010-07-01 14:01:02 +000090 for (int i = 0; i < dependencies.size(); i++) {
91 service.add(((Dependency) dependencies.get(i)).createCopy());
Marcel Offermansb1959f42010-07-01 12:23:51 +000092 }
93
Pierre De Ropc128b172010-05-23 21:28:15 +000094 for (int i = 0; i < m_stateListeners.size(); i ++) {
95 service.addStateListener((ServiceStateListener) m_stateListeners.get(i));
96 }
97 return service;
Pierre De Rop34231582010-05-23 20:05:16 +000098 }
99 }
100}