Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.felix.dm.lambda.samples.compositefactory; |
| 20 | |
| 21 | import static java.lang.System.out; |
| 22 | |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 23 | import org.apache.felix.dm.DependencyManager; |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 24 | import org.apache.felix.dm.lambda.DependencyManagerActivator; |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 25 | import org.osgi.framework.BundleContext; |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 26 | import org.osgi.service.cm.ConfigurationAdmin; |
| 27 | import org.osgi.service.log.LogService; |
| 28 | |
| 29 | /** |
| 30 | * Creates a "Provider" service. The implementation for this service (ProviderImpl) is |
| 31 | * created using a factory class (ProviderFactory) that also creates some other helper classes |
| 32 | * (ProviderComposite1 and ProviderComposite2) that are internally used by ProviderImpl. |
| 33 | * |
| 34 | * The ProviderFactory is also injected with a Configuration that can be used by the Factory |
| 35 | * when creating the ProviderImpl, ProviderComposite1, and ProviderComposite2 classes. |
| 36 | * |
| 37 | * The LogService in only injected to the ProviderImpl and the ProviderComposite1 classes. |
| 38 | * Both composites are called in their "start" callbacks, when all required dependencies are available. |
| 39 | * |
| 40 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 41 | */ |
| 42 | public class Activator extends DependencyManagerActivator { |
| 43 | @Override |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 44 | public void init(BundleContext ctx, DependencyManager dm) throws Exception { |
| 45 | out.println("type \"log warn\" to see the logs emitted by this test."); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 46 | |
| 47 | // Create the Factory used to instantiate ProvuderImpl, ProviderComposite1 and ProviderComposite2 |
| 48 | ProviderFactory factory = new ProviderFactory(); |
| 49 | |
| 50 | // Define the component which implementation is instantiated by the ProviderFactory. |
| 51 | // a LogService is injected in the ProviderImpl, as well as to the ProviderComposite1 class. |
| 52 | // And a configuration is injected directly to the ProviderFactory so it can use some configurations |
| 53 | // before creating the composition of classes. |
| 54 | component(comp -> comp |
| 55 | .factory(factory::create, factory::getComposition) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 56 | .start(ProviderImpl::start) // only call start on ProviderImpl |
| 57 | .withSvc(LogService.class, srv -> srv.add(ProviderImpl::bind).add(ProviderComposite1::bind)) |
| 58 | .withCnf(conf -> conf.update(MyConfig.class, factory::updated))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 59 | |
| 60 | // Creates a configuration with pid name = "org.apache.felix.dependencymanager.lambda.samples.compositefactory.ProviderFactory" |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 61 | component(comp -> comp.impl(Configurator.class).withSvc(ConfigurationAdmin.class)); |
| 62 | } |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 63 | } |