blob: b298e68c23fc184656976fe547ce2c3cd3896879 [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
26import org.apache.felix.dm.DependencyManager;
27import org.apache.felix.dm.dependencies.Dependency;
28import org.apache.felix.dm.dependencies.ServiceDependency;
29import org.apache.felix.dm.service.Service;
30import org.apache.felix.dm.service.ServiceStateListener;
31import 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
89 .add(dependencies)
90 .add(getAspectDependency());
91 for (int i = 0; i < m_stateListeners.size(); i ++) {
92 service.addStateListener((ServiceStateListener) m_stateListeners.get(i));
93 }
94 return service;
95 }
96
97 private Properties getServiceProperties(Object[] params) {
98 ServiceReference ref = (ServiceReference) params[0];
99 Object service = params[1];
100 Properties props = new Properties();
101 // first add our aspect property
102 props.put(DependencyManager.ASPECT, ref.getProperty(Constants.SERVICE_ID));
103 // and the ranking
104 props.put(Constants.SERVICE_RANKING, Integer.valueOf(m_ranking));
105 String[] keys = ref.getPropertyKeys();
106 for (int i = 0; i < keys.length; i++) {
107 props.put(keys[i], ref.getProperty(keys[i]));
108 }
109 if (m_serviceProperties != null) {
110 Enumeration e = m_serviceProperties.keys();
111 while (e.hasMoreElements()) {
112 Object key = e.nextElement();
113 props.put(key, m_serviceProperties.get(key));
114 }
115 }
116 return props;
117 }
118
119 private String[] getServiceInterfaces()
120 {
121 List serviceNames = new ArrayList();
122 // Of course, we provide the aspect interface.
123 serviceNames.add(m_aspectInterface.getName());
124 // But also append additional aspect implementation interfaces.
125 if (m_serviceInterfaces != null) {
126 for (int i = 0; i < m_serviceInterfaces.length; i ++) {
127 if (! m_serviceInterfaces[i].equals(m_aspectInterface.getName())) {
128 serviceNames.add(m_serviceInterfaces[i]);
129 }
130 }
131 }
132 return (String[]) serviceNames.toArray(new String[serviceNames.size()]);
133 }
134
135 private Dependency getAspectDependency() {
136 ServiceDependency sd =
137 m_manager.createServiceDependency()
138 .setService(m_aspectInterface, createAspectFilter(m_aspectFilter))
139 .setRequired(true);
140
141 if (m_field != null) {
142 sd.setAutoConfig(m_field);
143 }
144 return sd;
145 }
146
147 private String createAspectFilter(String filter) {
148 if (filter == null || filter.length() == 0) {
149 return "(|(!(" + Constants.SERVICE_RANKING + "=*))(" + Constants.SERVICE_RANKING + "<=" + (m_ranking - 1) + "))";
150 } else {
151 return "(&(|(!(" + Constants.SERVICE_RANKING + "=*))(" + Constants.SERVICE_RANKING + "<=" + (m_ranking - 1) + "))" + filter + ")";
152 }
153 }
154 }
155}