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.future; |
| 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.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 | */ |
| 38 | public class Activator extends DependencyManagerActivator { |
| 39 | /** |
| 40 | * Initialize our components using new DM-lambda activator base. |
| 41 | */ |
| 42 | @Override |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 43 | 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 Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 45 | |
| 46 | // System.setProperty("http.proxyHost","your.http.proxy.host"); |
| 47 | // System.setProperty("http.proxyPort", "your.http.proxy.port"); |
| 48 | |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 49 | // Create the PageLinks service, which asynchronously downloads the content of the Felix web page. |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 50 | // 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 54 | .withSvc(LogService.class, log -> log.add(PageLinksImpl::bind))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 55 | |
| 56 | // Just wait for the PageLinks service and display all links found from the Felix web site. |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 57 | component(comp -> comp.impl(this).withSvc(PageLinks.class, page -> page.add(this::setPageLinks))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 58 | } |
| 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 | } |