2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.blueprint.ext;
10 import com.google.common.base.Strings;
11 import java.util.ArrayList;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
18 import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
19 import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
20 import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
21 import org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata;
22 import org.apache.aries.util.AriesFrameworkUtil;
23 import org.opendaylight.controller.blueprint.BlueprintContainerRestartService;
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.Constants;
26 import org.osgi.framework.ServiceRegistration;
27 import org.osgi.service.blueprint.reflect.BeanProperty;
28 import org.osgi.service.blueprint.reflect.ComponentMetadata;
29 import org.osgi.service.blueprint.reflect.ValueMetadata;
30 import org.osgi.service.cm.ManagedService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The singleton component processor that is invoked by the blueprint container to perform operations on
36 * various component definitions prior to component creation.
38 * @author Thomas Pantelis
40 public class ComponentProcessor implements ComponentDefinitionRegistryProcessor {
41 static final String DEFAULT_TYPE_FILTER = "(|(type=default)(!(type=*)))";
43 private static final Logger LOG = LoggerFactory.getLogger(ComponentProcessor.class);
44 private static final String CM_PERSISTENT_ID_PROPERTY = "persistentId";
46 private final List<ServiceRegistration<?>> managedServiceRegs = new ArrayList<>();
47 private Bundle bundle;
48 private BlueprintContainerRestartService blueprintContainerRestartService;
49 private boolean restartDependentsOnUpdates;
50 private boolean useDefaultForReferenceTypes;
52 public void setBundle(final Bundle bundle) {
56 public void setBlueprintContainerRestartService(final BlueprintContainerRestartService restartService) {
57 this.blueprintContainerRestartService = restartService;
60 public void setRestartDependentsOnUpdates(final boolean restartDependentsOnUpdates) {
61 this.restartDependentsOnUpdates = restartDependentsOnUpdates;
64 public void setUseDefaultForReferenceTypes(final boolean useDefaultForReferenceTypes) {
65 this.useDefaultForReferenceTypes = useDefaultForReferenceTypes;
68 public void destroy() {
69 for (ServiceRegistration<?> reg: managedServiceRegs) {
70 AriesFrameworkUtil.safeUnregisterService(reg);
75 public void process(final ComponentDefinitionRegistry registry) {
76 LOG.debug("{}: In process", logName());
78 for (String name : registry.getComponentDefinitionNames()) {
79 ComponentMetadata component = registry.getComponentDefinition(name);
80 if (component instanceof MutableBeanMetadata) {
81 processMutableBeanMetadata((MutableBeanMetadata) component);
82 } else if (component instanceof MutableServiceReferenceMetadata) {
83 processServiceReferenceMetadata((MutableServiceReferenceMetadata)component);
88 private void processServiceReferenceMetadata(final MutableServiceReferenceMetadata serviceRef) {
89 if (!useDefaultForReferenceTypes) {
93 String filter = serviceRef.getFilter();
94 String extFilter = serviceRef.getExtendedFilter() == null ? null :
95 serviceRef.getExtendedFilter().getStringValue();
97 LOG.debug("{}: processServiceReferenceMetadata for {}, filter: {}, ext filter: {}", logName(),
98 serviceRef.getId(), filter, extFilter);
100 if (Strings.isNullOrEmpty(filter) && Strings.isNullOrEmpty(extFilter)) {
101 serviceRef.setFilter(DEFAULT_TYPE_FILTER);
103 LOG.debug("{}: processServiceReferenceMetadata for {} set filter to {}", logName(),
104 serviceRef.getId(), serviceRef.getFilter());
108 private void processMutableBeanMetadata(final MutableBeanMetadata bean) {
109 if (restartDependentsOnUpdates && bean.getRuntimeClass() != null
110 && AbstractPropertyPlaceholder.class.isAssignableFrom(bean.getRuntimeClass())) {
111 LOG.debug("{}: Found PropertyPlaceholder bean: {}, runtime {}", logName(), bean.getId(),
112 bean.getRuntimeClass());
114 for (BeanProperty prop : bean.getProperties()) {
115 if (CM_PERSISTENT_ID_PROPERTY.equals(prop.getName())) {
116 if (prop.getValue() instanceof ValueMetadata) {
117 ValueMetadata persistentId = (ValueMetadata)prop.getValue();
119 LOG.debug("{}: Found {} property, value : {}", logName(),
120 CM_PERSISTENT_ID_PROPERTY, persistentId.getStringValue());
122 registerManagedService(persistentId.getStringValue());
124 LOG.debug("{}: {} property metadata {} is not instanceof ValueMetadata",
125 logName(), CM_PERSISTENT_ID_PROPERTY, prop.getValue());
134 private void registerManagedService(final String persistentId) {
135 // Register a ManagedService so we get updates from the ConfigAdmin when the cfg file corresponding
136 // to the persistentId changes.
137 final ManagedService managedService = new ManagedService() {
138 private final AtomicBoolean initialUpdate = new AtomicBoolean(true);
139 private volatile Dictionary<String, ?> previousProperties;
142 public void updated(final Dictionary<String, ?> properties) {
143 LOG.debug("{}: ManagedService updated for persistentId {}, properties: {}, initialUpdate: {}",
144 logName(), persistentId, properties, initialUpdate);
146 // The first update occurs when the service is registered so ignore it as we want subsequent
147 // updates when it changes. The ConfigAdmin will send an update even if the cfg file doesn't
149 if (!initialUpdate.compareAndSet(true, false) && !Objects.equals(previousProperties, properties)) {
150 blueprintContainerRestartService.restartContainerAndDependents(bundle);
153 previousProperties = properties;
157 Dictionary<String, Object> props = new Hashtable<>();
158 props.put(Constants.SERVICE_PID, persistentId);
159 props.put(Constants.BUNDLE_SYMBOLICNAME, bundle.getSymbolicName());
160 props.put(Constants.BUNDLE_VERSION, bundle.getHeaders().get(Constants.BUNDLE_VERSION));
161 managedServiceRegs.add(bundle.getBundleContext().registerService(ManagedService.class.getName(),
162 managedService, props));
165 private String logName() {
166 return bundle.getSymbolicName();