1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| public class ServiceTrackerPerso extends ServiceTracker
{
private static final int ADDED = 1;
private static final int MODIFIED = 2;
private static final int REMOVED = 3;
private BundleContext m_context;
public ServiceTrackerPerso (BundleContext context)
{
super(context, SimpleShape.class.getName(), null);
m_context = context;
}
public Object addingService(ServiceReference ref)
{
SimpleShape shape = new DefaultShape(m_context, ref);
processShapeOnEventThread(ADDED, ref, shape);
return shape;
}
public void modifiedService(ServiceReference ref, Object svc)
{
processShapeOnEventThread(MODIFIED, ref, (SimpleShape) svc);
}
public void removedService(ServiceReference ref, Object svc)
{
processShapeOnEventThread(REMOVED, ref, (SimpleShape) svc);
((DefaultShape) svc).dispose();
}
private void processShapeOnEventThread(
int action, ServiceReference ref, SimpleShape shape)
{
try
{
if (SwingUtilities.isEventDispatchThread())
{
processShape(action, ref, shape);
}
else
{
SwingUtilities.invokeAndWait(new ShapeRunnable(action, ref, shape));
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private void processShape(int action, ServiceReference ref, SimpleShape shape)
{
String name = (String) ref.getProperty(SimpleShape.NAME_PROPERTY);
switch (action)
{
case MODIFIED:
break;
case ADDED:
break;
case REMOVED:
break;
}
}
private class ShapeRunnable implements Runnable
{
private int m_action;
private ServiceReference m_ref;
private SimpleShape m_shape;
public ShapeRunnable(int action, ServiceReference ref, SimpleShape shape)
{
m_action = action;
m_ref = ref;
m_shape = shape;
}
public void run()
{
processShape(m_action, m_ref, m_shape);
}
} |
Partager