blob: dbb3c3c6e7fc7b0ac794ac99b423a1ecdc5c08ce [file] [log] [blame]
Pierre De Ropfaca2892016-01-31 23:27:05 +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.lambda.samples.compositefactory;
20
21import static java.lang.System.out;
22
Pierre De Rop11527502016-02-18 21:07:16 +000023import org.apache.felix.dm.DependencyManager;
Pierre De Ropfaca2892016-01-31 23:27:05 +000024import org.apache.felix.dm.lambda.DependencyManagerActivator;
Pierre De Rop11527502016-02-18 21:07:16 +000025import org.osgi.framework.BundleContext;
Pierre De Ropfaca2892016-01-31 23:27:05 +000026import org.osgi.service.cm.ConfigurationAdmin;
27import 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 */
42public class Activator extends DependencyManagerActivator {
43 @Override
Pierre De Rop11527502016-02-18 21:07:16 +000044 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 Ropfaca2892016-01-31 23:27:05 +000046
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 Rop11527502016-02-18 21:07:16 +000056 .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 Ropfaca2892016-01-31 23:27:05 +000059
60 // Creates a configuration with pid name = "org.apache.felix.dependencymanager.lambda.samples.compositefactory.ProviderFactory"
Pierre De Rop11527502016-02-18 21:07:16 +000061 component(comp -> comp.impl(Configurator.class).withSvc(ConfigurationAdmin.class));
62 }
Pierre De Ropfaca2892016-01-31 23:27:05 +000063}