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 | |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 21 | import javax.swing.Icon; |
| 22 | import javax.swing.SwingUtilities; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 23 | import org.apache.felix.example.servicebased.host.service.SimpleShape; |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 24 | import org.osgi.framework.BundleContext; |
| 25 | import org.osgi.framework.ServiceReference; |
| 26 | import org.osgi.util.tracker.ServiceTracker; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Extends the <tt>ServiceTracker</tt> to create a tracker for |
| 30 | * <tt>SimpleShape</tt> services. The tracker is responsible for |
| 31 | * listener for the arrival/departure of <tt>SimpleShape</tt> |
| 32 | * services and informing the application about the availability |
| 33 | * of shapes. This tracker forces all notifications to be processed |
| 34 | * on the Swing event thread to avoid synchronization and redraw |
| 35 | * issues. |
| 36 | **/ |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 37 | public class ShapeTracker extends ServiceTracker<SimpleShape, SimpleShape> |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 38 | { |
Richard S. Hall | b0eb11c | 2007-07-25 18:57:27 +0000 | [diff] [blame] | 39 | // The bundle context used for tracking. |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 40 | private final BundleContext m_context; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 41 | // The application object to notify. |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 42 | private final DrawingFrame m_frame; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Constructs a tracker that uses the specified bundle context to |
| 46 | * track services and notifies the specified application object about |
| 47 | * changes. |
| 48 | * @param context The bundle context to be used by the tracker. |
| 49 | * @param frame The application object to notify about service changes. |
| 50 | **/ |
| 51 | public ShapeTracker(BundleContext context, DrawingFrame frame) |
| 52 | { |
| 53 | super(context, SimpleShape.class.getName(), null); |
Richard S. Hall | b0eb11c | 2007-07-25 18:57:27 +0000 | [diff] [blame] | 54 | m_context = context; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 55 | m_frame = frame; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Overrides the <tt>ServiceTracker</tt> functionality to inform |
| 60 | * the application object about the added service. |
| 61 | * @param ref The service reference of the added service. |
| 62 | * @return The service object to be used by the tracker. |
| 63 | **/ |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 64 | @Override |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 65 | public SimpleShape addingService(ServiceReference<SimpleShape> ref) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 66 | { |
Richard S. Hall | b0eb11c | 2007-07-25 18:57:27 +0000 | [diff] [blame] | 67 | SimpleShape shape = new DefaultShape(m_context, ref); |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 68 | processShapeOnEventThread(ShapeEvent.ADDED, ref, shape); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 69 | return shape; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Overrides the <tt>ServiceTracker</tt> functionality to inform |
| 74 | * the application object about the modified service. |
| 75 | * @param ref The service reference of the modified service. |
| 76 | * @param svc The service object of the modified service. |
| 77 | **/ |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 78 | @Override |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 79 | public void modifiedService(ServiceReference<SimpleShape> ref, SimpleShape svc) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 80 | { |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 81 | processShapeOnEventThread(ShapeEvent.MODIFIED, ref, svc); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Overrides the <tt>ServiceTracker</tt> functionality to inform |
| 86 | * the application object about the removed service. |
| 87 | * @param ref The service reference of the removed service. |
| 88 | * @param svc The service object of the removed service. |
| 89 | **/ |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 90 | @Override |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 91 | public void removedService(ServiceReference<SimpleShape> ref, SimpleShape svc) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 92 | { |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 93 | processShapeOnEventThread(ShapeEvent.REMOVED, ref, svc); |
Richard S. Hall | b0eb11c | 2007-07-25 18:57:27 +0000 | [diff] [blame] | 94 | ((DefaultShape) svc).dispose(); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Processes a received service notification from the <tt>ServiceTracker</tt>, |
| 99 | * forcing the processing of the notification onto the Swing event thread |
| 100 | * if it is not already on it. |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 101 | * @param event The type of action associated with the notification. |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 102 | * @param ref The service reference of the corresponding service. |
| 103 | * @param shape The service object of the corresponding service. |
| 104 | **/ |
| 105 | private void processShapeOnEventThread( |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 106 | ShapeEvent event, ServiceReference<SimpleShape> ref, SimpleShape shape) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 107 | { |
| 108 | try |
| 109 | { |
| 110 | if (SwingUtilities.isEventDispatchThread()) |
| 111 | { |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 112 | processShape(event, ref, shape); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 113 | } |
| 114 | else |
| 115 | { |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 116 | SwingUtilities.invokeAndWait(new ShapeRunnable(event, ref, shape)); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | catch (Exception ex) |
| 120 | { |
| 121 | ex.printStackTrace(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Actually performs the processing of the service notification. Invokes |
| 127 | * the appropriate callback method on the application object depending on |
| 128 | * the action type of the notification. |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 129 | * @param event The type of action associated with the notification. |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 130 | * @param ref The service reference of the corresponding service. |
| 131 | * @param shape The service object of the corresponding service. |
| 132 | **/ |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 133 | private void processShape(ShapeEvent event, ServiceReference<SimpleShape> ref, SimpleShape shape) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 134 | { |
| 135 | String name = (String) ref.getProperty(SimpleShape.NAME_PROPERTY); |
| 136 | |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 137 | switch (event) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 138 | { |
| 139 | case MODIFIED: |
| 140 | m_frame.removeShape(name); |
| 141 | // Purposely let this fall through to the 'add' case to |
| 142 | // reload the service. |
| 143 | |
| 144 | case ADDED: |
| 145 | Icon icon = (Icon) ref.getProperty(SimpleShape.ICON_PROPERTY); |
| 146 | m_frame.addShape(name, icon, shape); |
| 147 | break; |
| 148 | |
| 149 | case REMOVED: |
| 150 | m_frame.removeShape(name); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Simple class used to process service notification handling on the |
| 157 | * Swing event thread. |
| 158 | **/ |
| 159 | private class ShapeRunnable implements Runnable |
| 160 | { |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 161 | private final ShapeEvent m_event; |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 162 | private final ServiceReference<SimpleShape> m_ref; |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 163 | private final SimpleShape m_shape; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Constructs an object with the specified action, service reference, |
| 167 | * and service object for processing on the Swing event thread. |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 168 | * @param event The type of action associated with the notification. |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 169 | * @param ref The service reference of the corresponding service. |
| 170 | * @param shape The service object of the corresponding service. |
| 171 | **/ |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 172 | public ShapeRunnable(ShapeEvent event, ServiceReference<SimpleShape> ref, SimpleShape shape) |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 173 | { |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 174 | m_event = event; |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 175 | m_ref = ref; |
| 176 | m_shape = shape; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Calls the <tt>processShape()</tt> method. |
| 181 | **/ |
Richard S. Hall | 3c1c088 | 2012-03-13 17:32:01 +0000 | [diff] [blame^] | 182 | @Override |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 183 | public void run() |
| 184 | { |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 185 | processShape(m_event, m_ref, m_shape); |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Richard S. Hall | 57376bf | 2012-03-06 19:19:04 +0000 | [diff] [blame] | 188 | |
| 189 | private static enum ShapeEvent |
| 190 | { |
| 191 | ADDED, |
| 192 | MODIFIED, |
| 193 | REMOVED |
| 194 | } |
Richard S. Hall | bad49dc | 2007-07-13 16:08:06 +0000 | [diff] [blame] | 195 | } |