blob: 77524c65dcee80047cadb0a361059105eb85b967 [file] [log] [blame]
Richard S. Hallbad49dc2007-07-13 16:08:06 +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.servicebased.host;
20
21import java.awt.event.WindowAdapter;
22import java.awt.event.WindowEvent;
23import java.io.File;
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Map;
27
28import javax.swing.*;
29
30import org.osgi.framework.*;
31import org.apache.felix.framework.Felix;
32import org.apache.felix.framework.util.FelixConstants;
33import org.apache.felix.framework.util.StringMap;
34import org.apache.felix.framework.cache.BundleCache;
Richard S. Hall4ccc6d62008-02-04 16:31:44 +000035import org.apache.felix.main.AutoActivator;
Richard S. Hallbad49dc2007-07-13 16:08:06 +000036
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. Hallef4e7702009-06-10 19:54:02 +000052public class Activator implements BundleActivator
Richard S. Hallbad49dc2007-07-13 16:08:06 +000053{
Richard S. Hallbad49dc2007-07-13 16:08:06 +000054 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. Hallef4e7702009-06-10 19:54:02 +000063 public void start(final BundleContext context)
Richard S. Hallbad49dc2007-07-13 16:08:06 +000064 {
Richard S. Hallef4e7702009-06-10 19:54:02 +000065 javax.swing.SwingUtilities.invokeLater(new Runnable() {
66 // This creates of the application window.
67 public void run()
Richard S. Hallbad49dc2007-07-13 16:08:06 +000068 {
Richard S. Hallef4e7702009-06-10 19:54:02 +000069 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. Hallbad49dc2007-07-13 16:08:06 +000089 }
Richard S. Hallef4e7702009-06-10 19:54:02 +000090 });
Richard S. Hallbad49dc2007-07-13 16:08:06 +000091 }
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. Hallef4e7702009-06-10 19:54:02 +000099 Runnable runner = new Runnable() {
100 // This disposes of the application window.
101 public void run()
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000102 {
Richard S. Hallef4e7702009-06-10 19:54:02 +0000103 m_shapetracker.close();
104 m_frame.setVisible(false);
105 m_frame.dispose();
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000106 }
Richard S. Hallef4e7702009-06-10 19:54:02 +0000107 };
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000108
Richard S. Hallef4e7702009-06-10 19:54:02 +0000109 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. Hallbad49dc2007-07-13 16:08:06 +0000124 }
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. Hallcfd65102007-07-16 16:31:48 +0000130 * internal services to provide drawing functionality. To successfully
131 * launch as a stand-alone application, this method should be invoked from
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000132 * 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. Hall66b60a82007-07-16 16:26:11 +0000140 final File cachedir = File.createTempFile("felix.example.servicebased", null);
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000141 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. Hallef4e7702009-06-10 19:54:02 +0000150 configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
151 "org.apache.felix.example.servicebased.host.service; version=1.0.0");
Richard S. Hall4ccc6d62008-02-04 16:31:44 +0000152 configMap.put(AutoActivator.AUTO_START_PROP + ".1",
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000153 "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. Hallef4e7702009-06-10 19:54:02 +0000156 configMap.put(FelixConstants.LOG_LEVEL_PROP, "4");
157 configMap.put(Constants.FRAMEWORK_STORAGE, cachedir.getAbsolutePath());
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000158
Richard S. Hall4ccc6d62008-02-04 16:31:44 +0000159 // Create list to hold custom framework activators.
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000160 List list = new ArrayList();
Richard S. Hall4ccc6d62008-02-04 16:31:44 +0000161 // Add activator to process auto-start/install properties.
162 list.add(new AutoActivator(configMap));
163 // Add our own activator.
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000164 list.add(new Activator());
Richard S. Hallef4e7702009-06-10 19:54:02 +0000165 // Add our custom framework activators to the configuration map.
166 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000167
168 try
169 {
170 // Now create an instance of the framework.
Richard S. Hallef4e7702009-06-10 19:54:02 +0000171 Felix felix = new Felix(configMap);
Richard S. Hallbad49dc2007-07-13 16:08:06 +0000172 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. Hallef4e7702009-06-10 19:54:02 +0000199}