Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +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.example.extenderbased.host.launch; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.ServiceLoader; |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 23 | import org.apache.felix.example.extenderbased.host.Activator; |
| 24 | import org.osgi.framework.Bundle; |
| 25 | import org.osgi.framework.BundleContext; |
| 26 | import org.osgi.framework.BundleException; |
| 27 | import org.osgi.framework.launch.Framework; |
| 28 | import org.osgi.framework.launch.FrameworkFactory; |
| 29 | |
| 30 | /** |
| 31 | * This class provides a static {@code main()} method so that the bundle can be |
| 32 | * run as a stand-alone host application. In such a scenario, the application |
| 33 | * creates its own embedded OSGi framework instance and interacts with the |
| 34 | * internal extensions to providing drawing functionality. To successfully |
| 35 | * launch the stand-alone application, it must be run from this bundle's |
| 36 | * installation directory using "{@code java -jar}". |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 37 | * The locations of any additional extensions that have to be started, have to |
| 38 | * be passed as command line arguments to this method. |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 39 | */ |
| 40 | public class Application |
| 41 | { |
| 42 | private static Framework m_framework = null; |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 43 | |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Enables the bundle to run as a stand-alone application. When this |
| 46 | * static {@code main()} method is invoked, the application creates |
| 47 | * its own embedded OSGi framework instance and interacts with the |
| 48 | * internal extensions to provide drawing functionality. To successfully |
| 49 | * launch as a stand-alone application, this method should be invoked from |
| 50 | * the bundle's installation directory using "{@code java -jar}". |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 51 | * The location of any extension that shall be installed can be passed |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 52 | * as parameters. |
| 53 | * <p> |
| 54 | * For example if you build the bundles inside your workspace, maven will |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 55 | * create a target directory in every project. To start the application |
| 56 | * from within your IDE you should pass: |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 57 | * <p> |
| 58 | * <pre> |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 59 | * {@code file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar |
| 60 | * file:../extenderbased.square/target/extenderbased.square-1.0.0.jar |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 61 | * file:../extenderbased.triangle/target/extenderbased.triangle-1.0.0.jar} |
| 62 | * </pre> |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 63 | * |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 64 | * @param args The locations of additional bundles to start. |
| 65 | **/ |
| 66 | public static void main(String[] args) |
| 67 | { |
| 68 | // args should never be null if the application is run from the command line. Check it anyway. |
| 69 | String[] locations = args != null ? args : new String[0]; |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 70 | |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 71 | // Print welcome banner. |
| 72 | System.out.println("\nWelcome to My Launcher"); |
| 73 | System.out.println("======================\n"); |
| 74 | |
| 75 | try |
| 76 | { |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 77 | Map<String, String> config = ConfigUtil.createConfig(); |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 78 | m_framework = createFramework(config); |
| 79 | m_framework.init(); |
| 80 | m_framework.start(); |
| 81 | installAndStartBundles(locations); |
| 82 | m_framework.waitForStop(0); |
| 83 | System.exit(0); |
| 84 | } |
| 85 | catch (Exception ex) |
| 86 | { |
| 87 | System.err.println("Could not create framework: " + ex); |
| 88 | ex.printStackTrace(); |
| 89 | System.exit(-1); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 94 | * Util method for creating an embedded Framework. Tries to create a {@link FrameworkFactory} |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 95 | * which is then be used to create the framework. |
| 96 | * |
| 97 | * @param config the configuration to create the framework with |
| 98 | * @return a Framework with the given configuration |
| 99 | */ |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 100 | private static Framework createFramework(Map<String, String> config) |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 101 | { |
| 102 | ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader.load(FrameworkFactory.class); |
| 103 | for(FrameworkFactory factory : factoryLoader){ |
| 104 | return factory.newFramework(config); |
| 105 | } |
| 106 | throw new IllegalStateException("Unable to load FrameworkFactory service."); |
| 107 | } |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 108 | |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 109 | /** |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 110 | * Installs and starts all bundles used by the application. Therefore the host bundle will be started. The locations |
| 111 | * of extensions for the host bundle can be passed in as parameters. |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 112 | * |
| 113 | * @param bundleLocations the locations where extension for the host bundle are located. Must not be {@code null}! |
| 114 | * @throws BundleException if something went wrong while installing or starting the bundles. |
| 115 | */ |
| 116 | private static void installAndStartBundles(String... bundleLocations) throws BundleException |
| 117 | { |
| 118 | BundleContext bundleContext = m_framework.getBundleContext(); |
| 119 | Activator hostActivator = new Activator(); |
| 120 | hostActivator.start(bundleContext); |
| 121 | for (String location : bundleLocations) |
| 122 | { |
| 123 | Bundle addition = bundleContext.installBundle(location); |
| 124 | addition.start(); |
| 125 | } |
| 126 | } |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 127 | } |