blob: 26196bbfc9ad10bc13032558545f3c4e9c79c669 [file] [log] [blame]
Pierre De Rop19476fe2010-05-23 08:13:58 +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.ArrayList;
22import java.util.Enumeration;
23import java.util.List;
24import java.util.Properties;
25
Marcel Offermans8b93efa2010-07-02 18:27:21 +000026import org.apache.felix.dm.Dependency;
Pierre De Rop19476fe2010-05-23 08:13:58 +000027import org.apache.felix.dm.DependencyManager;
Marcel Offermans8b93efa2010-07-02 18:27:21 +000028import org.apache.felix.dm.Service;
29import org.apache.felix.dm.ServiceDependency;
30import org.apache.felix.dm.ServiceStateListener;
Pierre De Rop19476fe2010-05-23 08:13:58 +000031import org.osgi.framework.Constants;
32import org.osgi.framework.ServiceReference;
33
34/**
35 * Aspect Service implementation. This class extends the FilterService in order to catch
36 * some Service methods for configuring actual aspect service implementation.
37 */
38public class AspectServiceImpl extends FilterService
39{
40 public AspectServiceImpl(DependencyManager dm, Class aspectInterface, String aspectFilter, int ranking, String autoConfig)
41 {
42 super(dm.createService()); // This service will be filtered by our super class, allowing us to take control.
43 m_service.setImplementation(new AspectImpl(aspectInterface, aspectFilter, ranking, autoConfig))
44 .add(dm.createServiceDependency()
45 .setService(aspectInterface, createAspectFilter(aspectFilter))
46 .setAutoConfig(false)
47 .setCallbacks("added", "removed"));
48 }
49
50 private String createAspectFilter(String filter) {
51 // we only want to match services which are not themselves aspects
52 if (filter == null || filter.length() == 0) {
53 return "(!(" + DependencyManager.ASPECT + "=*))";
54 }
55 else {
56 return "(&(!(" + DependencyManager.ASPECT + "=*))" + filter + ")";
57 }
58 }
59
60 /**
61 * This class is the Aspect Implementation. It will create the actual Aspect Service, and
62 * will use the Aspect Service parameters provided by our enclosing class.
63 */
64 class AspectImpl extends AbstractDecorator {
65 private final Class m_aspectInterface; // the service decorated by this aspect
66 private final String m_aspectFilter; // the service filter decorated by this aspect
67 private final int m_ranking; // the aspect ranking
68 private final String m_field; // the aspect impl field name where to inject decorated service
69
70 public AspectImpl(Class aspectInterface, String aspectFilter, int ranking, String field) {
71 m_aspectInterface = aspectInterface;
72 m_aspectFilter = aspectFilter;
73 m_ranking = ranking;
74 m_field = field;
75 }
76
77 public Service createService(Object[] params) {
78 List dependencies = m_service.getDependencies();
79 // remove our internal dependency
80 dependencies.remove(0);
81 Properties serviceProperties = getServiceProperties(params);
82 String[] serviceInterfaces = getServiceInterfaces();
83 Service service = m_manager.createService()
84 .setInterface(serviceInterfaces, serviceProperties)
85 .setImplementation(m_serviceImpl)
86 .setFactory(m_factory, m_factoryCreateMethod) // if not set, no effect
87 .setComposition(m_compositionInstance, m_compositionMethod) // if not set, no effect
88 .setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy) // if not set, no effect
Pierre De Rop19476fe2010-05-23 08:13:58 +000089 .add(getAspectDependency());
Marcel Offermansb1959f42010-07-01 12:23:51 +000090
Marcel Offermans3d921212010-08-09 13:37:02 +000091 configureAutoConfigState(service, m_service);
92
Marcel Offermanse9c13d92010-07-01 14:01:02 +000093 for (int i = 0; i < dependencies.size(); i++) {
94 service.add(((Dependency) dependencies.get(i)).createCopy());
Marcel Offermansb1959f42010-07-01 12:23:51 +000095 }
96
Pierre De Rop19476fe2010-05-23 08:13:58 +000097 for (int i = 0; i < m_stateListeners.size(); i ++) {
98 service.addStateListener((ServiceStateListener) m_stateListeners.get(i));
99 }
100 return service;
101 }
102
103 private Properties getServiceProperties(Object[] params) {
104 ServiceReference ref = (ServiceReference) params[0];
105 Object service = params[1];
106 Properties props = new Properties();
107 // first add our aspect property
108 props.put(DependencyManager.ASPECT, ref.getProperty(Constants.SERVICE_ID));
109 // and the ranking
110 props.put(Constants.SERVICE_RANKING, Integer.valueOf(m_ranking));
111 String[] keys = ref.getPropertyKeys();
112 for (int i = 0; i < keys.length; i++) {
113 props.put(keys[i], ref.getProperty(keys[i]));
114 }
115 if (m_serviceProperties != null) {
116 Enumeration e = m_serviceProperties.keys();
117 while (e.hasMoreElements()) {
118 Object key = e.nextElement();
119 props.put(key, m_serviceProperties.get(key));
120 }
121 }
122 return props;
123 }
124
125 private String[] getServiceInterfaces()
126 {
127 List serviceNames = new ArrayList();
128 // Of course, we provide the aspect interface.
129 serviceNames.add(m_aspectInterface.getName());
130 // But also append additional aspect implementation interfaces.
131 if (m_serviceInterfaces != null) {
132 for (int i = 0; i < m_serviceInterfaces.length; i ++) {
133 if (! m_serviceInterfaces[i].equals(m_aspectInterface.getName())) {
134 serviceNames.add(m_serviceInterfaces[i]);
135 }
136 }
137 }
138 return (String[]) serviceNames.toArray(new String[serviceNames.size()]);
139 }
140
141 private Dependency getAspectDependency() {
142 ServiceDependency sd =
143 m_manager.createServiceDependency()
144 .setService(m_aspectInterface, createAspectFilter(m_aspectFilter))
145 .setRequired(true);
146
147 if (m_field != null) {
148 sd.setAutoConfig(m_field);
149 }
150 return sd;
151 }
152
153 private String createAspectFilter(String filter) {
154 if (filter == null || filter.length() == 0) {
155 return "(|(!(" + Constants.SERVICE_RANKING + "=*))(" + Constants.SERVICE_RANKING + "<=" + (m_ranking - 1) + "))";
156 } else {
157 return "(&(|(!(" + Constants.SERVICE_RANKING + "=*))(" + Constants.SERVICE_RANKING + "<=" + (m_ranking - 1) + "))" + filter + ")";
158 }
159 }
160 }
161}