Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +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.servicebased.host; |
| 20 | |
| 21 | import java.awt.event.WindowAdapter; |
| 22 | import java.awt.event.WindowEvent; |
| 23 | import java.io.File; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import javax.swing.*; |
| 29 | |
| 30 | import org.osgi.framework.*; |
| 31 | import org.apache.felix.framework.Felix; |
| 32 | import org.apache.felix.framework.util.FelixConstants; |
| 33 | import org.apache.felix.framework.util.StringMap; |
| 34 | import org.apache.felix.framework.cache.BundleCache; |
Richard S. Hall | 4ccc6d6 | 2008-02-04 16:31:44 +0000 | [diff] [blame] | 35 | import org.apache.felix.main.AutoActivator; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * The activator of the host application bundle. The activator creates the |
| 39 | * main application <tt>JFrame</tt> and starts tracking <tt>SimpleShape</tt> |
| 40 | * services. All activity is performed on the Swing event thread to avoid |
| 41 | * synchronization and repainting issues. Closing the application window |
| 42 | * will result in <tt>Bundle.stop()</tt> being called on the system bundle, |
| 43 | * which will cause the framework to shutdown and the JVM to exit. |
| 44 | * <p> |
| 45 | * This class also provides a static <tt>main()</tt> method so that it can be |
| 46 | * run as a stand-alone host application. In such a scenario, the application |
| 47 | * creates its own embedded Felix framework instance and interacts with the |
| 48 | * internal services to providing drawing functionality. To successfully |
| 49 | * launch the stand-alone application, it must be run from this bundle's |
| 50 | * installation directory using "<tt>java -jar</tt>". |
| 51 | **/ |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 52 | public class Activator implements BundleActivator |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 53 | { |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 54 | private DrawingFrame m_frame = null; |
| 55 | private ShapeTracker m_shapetracker = null; |
| 56 | |
| 57 | /** |
| 58 | * Displays the applications window and starts service tracking; |
| 59 | * everything is done on the Swing event thread to avoid synchronization |
| 60 | * and repainting issues. |
| 61 | * @param context The context of the bundle. |
| 62 | **/ |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 63 | public void start(final BundleContext context) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 64 | { |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 65 | javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 66 | // This creates of the application window. |
| 67 | public void run() |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 68 | { |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 69 | m_frame = new DrawingFrame(); |
| 70 | m_frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 71 | m_frame.addWindowListener(new WindowAdapter() { |
| 72 | public void windowClosing(WindowEvent evt) |
| 73 | { |
| 74 | try |
| 75 | { |
| 76 | context.getBundle(0).stop(); |
| 77 | } |
| 78 | catch (BundleException ex) |
| 79 | { |
| 80 | ex.printStackTrace(); |
| 81 | } |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | m_frame.setVisible(true); |
| 86 | |
| 87 | m_shapetracker = new ShapeTracker(context, m_frame); |
| 88 | m_shapetracker.open(); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 89 | } |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 90 | }); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Stops service tracking and disposes of the application window. |
| 95 | * @param context The context of the bundle. |
| 96 | **/ |
| 97 | public void stop(BundleContext context) |
| 98 | { |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 99 | Runnable runner = new Runnable() { |
| 100 | // This disposes of the application window. |
| 101 | public void run() |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 102 | { |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 103 | m_shapetracker.close(); |
| 104 | m_frame.setVisible(false); |
| 105 | m_frame.dispose(); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 106 | } |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 107 | }; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 108 | |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 109 | if (SwingUtilities.isEventDispatchThread()) |
| 110 | { |
| 111 | runner.run(); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | try |
| 116 | { |
| 117 | javax.swing.SwingUtilities.invokeAndWait(runner); |
| 118 | } |
| 119 | catch (Exception ex) |
| 120 | { |
| 121 | ex.printStackTrace(); |
| 122 | } |
| 123 | } |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Enables the bundle to run as a stand-alone application. When this |
| 128 | * static <tt>main()</tt> method is invoked, the application creates |
| 129 | * its own embedded Felix framework instance and interacts with the |
Richard S. Hall | cfd6510 | 2007-07-16 16:31:48 +0000 | [diff] [blame] | 130 | * internal services to provide drawing functionality. To successfully |
| 131 | * launch as a stand-alone application, this method should be invoked from |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 132 | * the bundle's installation directory using "<tt>java -jar</tt>". |
| 133 | * @param argv The command-line arguments. |
| 134 | * @throws Exception If anything goes wrong. |
| 135 | **/ |
| 136 | public static void main(String[] argv) throws Exception |
| 137 | { |
| 138 | // Create a temporary bundle cache directory and |
| 139 | // make sure to clean it up on exit. |
Richard S. Hall | 66b60a8 | 2007-07-16 16:26:11 +0000 | [diff] [blame] | 140 | final File cachedir = File.createTempFile("felix.example.servicebased", null); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 141 | cachedir.delete(); |
| 142 | Runtime.getRuntime().addShutdownHook(new Thread() { |
| 143 | public void run() |
| 144 | { |
| 145 | deleteFileOrDir(cachedir); |
| 146 | } |
| 147 | }); |
| 148 | |
| 149 | Map configMap = new StringMap(false); |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 150 | configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, |
| 151 | "org.apache.felix.example.servicebased.host.service; version=1.0.0"); |
Richard S. Hall | 4ccc6d6 | 2008-02-04 16:31:44 +0000 | [diff] [blame] | 152 | configMap.put(AutoActivator.AUTO_START_PROP + ".1", |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 153 | "file:../servicebased.circle/target/servicebased.circle-1.0.0.jar " + |
| 154 | "file:../servicebased.square/target/servicebased.square-1.0.0.jar " + |
| 155 | "file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar"); |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 156 | configMap.put(FelixConstants.LOG_LEVEL_PROP, "4"); |
| 157 | configMap.put(Constants.FRAMEWORK_STORAGE, cachedir.getAbsolutePath()); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 158 | |
Richard S. Hall | 4ccc6d6 | 2008-02-04 16:31:44 +0000 | [diff] [blame] | 159 | // Create list to hold custom framework activators. |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 160 | List list = new ArrayList(); |
Richard S. Hall | 4ccc6d6 | 2008-02-04 16:31:44 +0000 | [diff] [blame] | 161 | // Add activator to process auto-start/install properties. |
| 162 | list.add(new AutoActivator(configMap)); |
| 163 | // Add our own activator. |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 164 | list.add(new Activator()); |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 165 | // Add our custom framework activators to the configuration map. |
| 166 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 167 | |
| 168 | try |
| 169 | { |
| 170 | // Now create an instance of the framework. |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 171 | Felix felix = new Felix(configMap); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 172 | felix.start(); |
| 173 | } |
| 174 | catch (Exception ex) |
| 175 | { |
| 176 | System.err.println("Could not create framework: " + ex); |
| 177 | ex.printStackTrace(); |
| 178 | System.exit(-1); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Utility method used to delete the profile directory when run as |
| 184 | * a stand-alone application. |
| 185 | * @param file The file to recursively delete. |
| 186 | **/ |
| 187 | private static void deleteFileOrDir(File file) |
| 188 | { |
| 189 | if (file.isDirectory()) |
| 190 | { |
| 191 | File[] childs = file.listFiles(); |
| 192 | for (int i = 0;i < childs.length;i++) |
| 193 | { |
| 194 | deleteFileOrDir(childs[i]); |
| 195 | } |
| 196 | } |
| 197 | file.delete(); |
| 198 | } |
Richard S. Hall | ef4e770 | 2009-06-10 19:54:02 +0000 | [diff] [blame^] | 199 | } |