blob: df52b2fe940e6aa499ef74b17164fed501d0bab6 [file] [log] [blame]
Richard S. Hallc9e91a22012-03-08 17:09:16 +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.example.extenderbased.host.launch;
20
21import java.util.Map;
22import java.util.ServiceLoader;
Richard S. Hallc9e91a22012-03-08 17:09:16 +000023import org.apache.felix.example.extenderbased.host.Activator;
24import org.osgi.framework.Bundle;
25import org.osgi.framework.BundleContext;
26import org.osgi.framework.BundleException;
27import org.osgi.framework.launch.Framework;
28import 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. Hallf48d7dc2012-03-26 15:23:41 +000037 * 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. Hallc9e91a22012-03-08 17:09:16 +000039 */
40public class Application
41{
42 private static Framework m_framework = null;
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000043
Richard S. Hallc9e91a22012-03-08 17:09:16 +000044 /**
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. Hallf48d7dc2012-03-26 15:23:41 +000051 * The location of any extension that shall be installed can be passed
Richard S. Hallc9e91a22012-03-08 17:09:16 +000052 * as parameters.
53 * <p>
54 * For example if you build the bundles inside your workspace, maven will
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000055 * create a target directory in every project. To start the application
56 * from within your IDE you should pass:
Richard S. Hallc9e91a22012-03-08 17:09:16 +000057 * <p>
58 * <pre>
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000059 * {@code file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar
60 * file:../extenderbased.square/target/extenderbased.square-1.0.0.jar
Richard S. Hallc9e91a22012-03-08 17:09:16 +000061 * file:../extenderbased.triangle/target/extenderbased.triangle-1.0.0.jar}
62 * </pre>
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000063 *
Richard S. Hallc9e91a22012-03-08 17:09:16 +000064 * @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. Hallf48d7dc2012-03-26 15:23:41 +000070
Richard S. Hallc9e91a22012-03-08 17:09:16 +000071 // Print welcome banner.
72 System.out.println("\nWelcome to My Launcher");
73 System.out.println("======================\n");
74
75 try
76 {
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000077 Map<String, String> config = ConfigUtil.createConfig();
Richard S. Hallc9e91a22012-03-08 17:09:16 +000078 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. Hallf48d7dc2012-03-26 15:23:41 +000094 * Util method for creating an embedded Framework. Tries to create a {@link FrameworkFactory}
Richard S. Hallc9e91a22012-03-08 17:09:16 +000095 * 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. Hallf48d7dc2012-03-26 15:23:41 +0000100 private static Framework createFramework(Map<String, String> config)
Richard S. Hallc9e91a22012-03-08 17:09:16 +0000101 {
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. Hallf48d7dc2012-03-26 15:23:41 +0000108
Richard S. Hallc9e91a22012-03-08 17:09:16 +0000109 /**
Richard S. Hallf48d7dc2012-03-26 15:23:41 +0000110 * 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. Hallc9e91a22012-03-08 17:09:16 +0000112 *
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. Hallf48d7dc2012-03-26 15:23:41 +0000127}