/clipboard.monitor.component/src/clipboard/monitor/component/ClipboardMonitorComponent.java

https://github.com/mateuszszulc/clipboard.monitor · Java · 98 lines · 59 code · 11 blank · 28 comment · 11 complexity · 224789285322c3a71e09b195b1a5f416 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2010 Philipp Kursawe.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Philipp Kursawe (phil.kursawe@gmail.com) - initial API and implementation
  10. ******************************************************************************/
  11. package clipboard.monitor.component;
  12. import org.osgi.framework.BundleContext;
  13. import org.osgi.framework.InvalidSyntaxException;
  14. import org.osgi.framework.ServiceReference;
  15. import clipboard.monitor.ClipboardEvent;
  16. import clipboard.monitor.ClipboardListener;
  17. import clipboard.monitor.ClipboardMonitor;
  18. /**
  19. * Abstract base class for clipboard monitor OSGi components.
  20. *
  21. *
  22. * @author <a href="mailto:phil.kursawe@gmail.com">Philipp Kursawe</a>
  23. *
  24. */
  25. public abstract class ClipboardMonitorComponent implements ClipboardMonitor {
  26. private ClipboardMonitor monitor;
  27. private BundleContext bundleContext;
  28. /**
  29. * Subclasses must return the monitor to use with this component here. This
  30. * method is called only once. The implementation does not need to remember
  31. * what it returns. Must <em>not</em> return <code>null</code>.
  32. */
  33. protected abstract ClipboardMonitor getMonitor();
  34. private ClipboardMonitor _getMonitor() {
  35. if (null == monitor) {
  36. monitor = getMonitor();
  37. if (null == monitor) {
  38. throw new IllegalArgumentException("monitor must not be null"); //$NON-NLS-1$
  39. }
  40. }
  41. return monitor;
  42. }
  43. protected void activate(BundleContext context) {
  44. this.bundleContext = context;
  45. start();
  46. }
  47. protected void deactivate() {
  48. stop();
  49. }
  50. public void start() {
  51. _getMonitor().start();
  52. }
  53. public void stop() {
  54. if (monitor != null) {
  55. monitor.stop();
  56. monitor = null;
  57. }
  58. }
  59. /**
  60. * This method should be called by subclasses to send the event to
  61. * registered service listeners.
  62. *
  63. * @param event
  64. */
  65. protected void onChange(ClipboardEvent event) {
  66. ServiceReference[] references = null;
  67. try {
  68. references = bundleContext.getServiceReferences(
  69. ClipboardListener.class.getName(), null);
  70. } catch (InvalidSyntaxException e) {
  71. }
  72. if (references != null) {
  73. for (ServiceReference ref : references) {
  74. ClipboardListener listener = (ClipboardListener) bundleContext
  75. .getService(ref);
  76. try {
  77. if (listener != null) {
  78. listener.onEvent(event);
  79. }
  80. } catch (Throwable t) {
  81. } finally {
  82. bundleContext.ungetService(ref);
  83. }
  84. }
  85. }
  86. }
  87. }