blob: a1e9a9eaceaf5a45d3932bbdf21f9037f9f207e5 [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.future;
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.log.LogService;
27
28/**
29 * This examples show how to use the new "Future" dependency available from the dependencymanager-lambda library.
30 * The PageLink component provides the list of available hrefs found from the Felix web site.
31 * The page is downloaded asynchronously using a CompletableFuture, and the component of the PageLinkImpl class
32 * will wait for the completion of the future before start.
33 *
34 * The interesting thing to look at is located in the PageLinkImpl.init() method.
35 *
36 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
37 */
38public class Activator extends DependencyManagerActivator {
39 /**
40 * Initialize our components using new DM-lambda activator base.
41 */
42 @Override
Pierre De Rop11527502016-02-18 21:07:16 +000043 public void init(BundleContext ctx, DependencyManager dm) throws Exception {
44 out.println("type \"log warn\" to see the logs emitted by this test.");
Pierre De Ropfaca2892016-01-31 23:27:05 +000045
46 // System.setProperty("http.proxyHost","your.http.proxy.host");
47 // System.setProperty("http.proxyPort", "your.http.proxy.port");
48
Pierre De Rop11527502016-02-18 21:07:16 +000049 // Create the PageLinks service, which asynchronously downloads the content of the Felix web page.
Pierre De Ropfaca2892016-01-31 23:27:05 +000050 // The PageLink service will be started once the page has been downloaded (using a CompletableFuture).
51 component(comp -> comp
52 .factory(() -> new PageLinksImpl("http://felix.apache.org/"))
53 .provides(PageLinks.class)
Pierre De Rop11527502016-02-18 21:07:16 +000054 .withSvc(LogService.class, log -> log.add(PageLinksImpl::bind)));
Pierre De Ropfaca2892016-01-31 23:27:05 +000055
56 // Just wait for the PageLinks service and display all links found from the Felix web site.
Pierre De Rop11527502016-02-18 21:07:16 +000057 component(comp -> comp.impl(this).withSvc(PageLinks.class, page -> page.add(this::setPageLinks)));
Pierre De Ropfaca2892016-01-31 23:27:05 +000058 }
59
60 /**
61 * display all the hrefs (links) found from the Felix web site.
62 */
63 void setPageLinks(PageLinks page) {
64 out.println("Felix site links: " + page.getLinks());
65 }
66}