Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslewis2009-06-09 23:16:44 +0000
committerslewis2009-06-09 23:16:44 +0000
commit2d8789b7fda77bbf7d64aa8c38979ae692034ec7 (patch)
tree4d93513d3bc0c594893f786af67766c18ca09de3 /examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin
parent8a11bc994e5597695e881d6db1685c7673050b7f (diff)
downloadorg.eclipse.ecf-2d8789b7fda77bbf7d64aa8c38979ae692034ec7.tar.gz
org.eclipse.ecf-2d8789b7fda77bbf7d64aa8c38979ae692034ec7.tar.xz
org.eclipse.ecf-2d8789b7fda77bbf7d64aa8c38979ae692034ec7.zip
Initial checkin of eventadmin implementation that knows about remote EventHandlers
Diffstat (limited to 'examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin')
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.classpath7
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.project28
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.jdt.core.prefs69
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.pde.core.prefs4
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/META-INF/MANIFEST.MF17
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/build.properties4
-rw-r--r--examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/src/org/eclipse/ecf/examples/remoteservices/eventadmin/EventAdminImpl.java212
7 files changed, 341 insertions, 0 deletions
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.classpath b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.classpath
new file mode 100644
index 000000000..6f3b481ac
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/CDC-1.1%Foundation-1.1"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.project b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.project
new file mode 100644
index 000000000..0bbd8fb3f
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.ecf.examples.remoteservices.eventadmin</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.jdt.core.prefs b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 000000000..7e7a20f35
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,69 @@
+#Tue Jun 09 16:09:49 PDT 2009
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2
+org.eclipse.jdt.core.compiler.compliance=1.4
+org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning
+org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
+org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
+org.eclipse.jdt.core.compiler.problem.deadCode=warning
+org.eclipse.jdt.core.compiler.problem.deprecation=warning
+org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
+org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
+org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore
+org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning
+org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
+org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
+org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
+org.eclipse.jdt.core.compiler.problem.finalParameterBound=ignore
+org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
+org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
+org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
+org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore
+org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
+org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore
+org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
+org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
+org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
+org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
+org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
+org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
+org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
+org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
+org.eclipse.jdt.core.compiler.problem.nullReference=warning
+org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
+org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
+org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
+org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
+org.eclipse.jdt.core.compiler.problem.rawTypeReference=ignore
+org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
+org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
+org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
+org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
+org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
+org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
+org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
+org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=ignore
+org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
+org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
+org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore
+org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
+org.eclipse.jdt.core.compiler.problem.unusedImport=warning
+org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
+org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
+org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
+org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
+org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
+org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
+org.eclipse.jdt.core.compiler.source=1.3
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.pde.core.prefs b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.pde.core.prefs
new file mode 100644
index 000000000..7d46bdb76
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/.settings/org.eclipse.pde.core.prefs
@@ -0,0 +1,4 @@
+#Thu May 21 22:07:05 PDT 2009
+eclipse.preferences.version=1
+pluginProject.extensions=false
+resolve.requirebundle=false
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/META-INF/MANIFEST.MF b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/META-INF/MANIFEST.MF
new file mode 100644
index 000000000..94f1be110
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/META-INF/MANIFEST.MF
@@ -0,0 +1,17 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: ECF Eventadmin Example
+Bundle-SymbolicName: org.eclipse.ecf.examples.remoteservices.eventadmin
+Bundle-Version: 1.0.0.qualifier
+Bundle-Vendor: Eclipse.org
+Bundle-RequiredExecutionEnvironment: CDC-1.1/Foundation-1.1,
+ J2SE-1.4
+Import-Package: org.eclipse.ecf.core.util,
+ org.eclipse.ecf.osgi.services.distribution;version="1.0.0",
+ org.eclipse.ecf.remoteservice,
+ org.eclipse.equinox.concurrent.future;version="1.0.0",
+ org.osgi.framework;version="1.3.0",
+ org.osgi.service.event;version="1.2.0",
+ org.osgi.util.tracker;version="1.4.2"
+Require-Bundle: org.eclipse.equinox.common;bundle-version="3.5.0"
+Export-Package: org.eclipse.ecf.examples.remoteservices.eventadmin
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/build.properties b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/build.properties
new file mode 100644
index 000000000..34d2e4d2d
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/src/org/eclipse/ecf/examples/remoteservices/eventadmin/EventAdminImpl.java b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/src/org/eclipse/ecf/examples/remoteservices/eventadmin/EventAdminImpl.java
new file mode 100644
index 000000000..37960c9b2
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.eventadmin/src/org/eclipse/ecf/examples/remoteservices/eventadmin/EventAdminImpl.java
@@ -0,0 +1,212 @@
+package org.eclipse.ecf.examples.remoteservices.eventadmin;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.ISafeRunnable;
+import org.eclipse.core.runtime.SafeRunner;
+import org.eclipse.ecf.osgi.services.distribution.IDistributionConstants;
+import org.eclipse.ecf.remoteservice.IRemoteCall;
+import org.eclipse.ecf.remoteservice.IRemoteService;
+import org.eclipse.equinox.concurrent.future.IExecutor;
+import org.eclipse.equinox.concurrent.future.IProgressRunnable;
+import org.eclipse.equinox.concurrent.future.ThreadsExecutor;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class EventAdminImpl implements EventAdmin {
+
+ private BundleContext context;
+ private IExecutor executor;
+ private Object executorLock = new Object();
+ private ServiceTracker eventHandlerServiceTracker;
+ private Object eventHandlerSTLock = new Object();
+ private long defaultTimeout;
+
+ public EventAdminImpl(BundleContext context, IExecutor executor,
+ long defaultTimeout) {
+ this.context = context;
+ this.executor = executor;
+ this.defaultTimeout = defaultTimeout;
+ }
+
+ private IRemoteService getRemoteServiceForServiceReference(
+ ServiceReference ref) {
+ if (ref == null)
+ return null;
+ Object remoteProp = ref.getProperty(IDistributionConstants.REMOTE);
+ if (remoteProp != null && remoteProp instanceof IRemoteService)
+ return (IRemoteService) remoteProp;
+ return null;
+ }
+
+ private ServiceReference[] getEventHandlerServiceReferences(Event event) {
+ synchronized (eventHandlerSTLock) {
+ if (eventHandlerServiceTracker == null) {
+ eventHandlerServiceTracker = new ServiceTracker(context,
+ EventHandler.class.getName(), null);
+ eventHandlerServiceTracker.open();
+ }
+ ServiceReference[] refs = eventHandlerServiceTracker
+ .getServiceReferences();
+ List results = new ArrayList();
+ if (refs == null)
+ return null;
+ for (int i = 0; i < refs.length; i++) {
+ String eventFilter = (String) refs[i]
+ .getProperty(EventConstants.EVENT_FILTER);
+ Filter filter = null;
+ try {
+ filter = context.createFilter(eventFilter);
+ } catch (InvalidSyntaxException e) {
+ logError("getEventHandlers eventFilter=" + eventFilter, e);
+ continue;
+ }
+ if (event.matches(filter))
+ results.add(refs[i]);
+ }
+ return (ServiceReference[]) results
+ .toArray(new ServiceReference[] {});
+ }
+ }
+
+ private void logError(String string, Throwable e) {
+ System.err.println(string);
+ if (e != null)
+ e.printStackTrace(System.err);
+ }
+
+ public void postEvent(final Event event) {
+ ServiceReference[] refs = getEventHandlerServiceReferences(event);
+ if (refs == null) {
+ logWarning("postEvent event=" + event
+ + ". No service references found to post to.");
+ return;
+ }
+ for (int i = 0; i < refs.length; i++) {
+ IRemoteService remoteService = getRemoteServiceForServiceReference(refs[i]);
+ // If this is a remote service, then fire the EventHandler
+ // asynchronously
+ if (remoteService != null)
+ fireAsync(refs[i], remoteService, createRemoteCall(event));
+ // Else call the EventHandler asynchronously
+ else
+ fireAsync(refs[i], (EventHandler) context.getService(refs[i]),
+ event);
+ }
+ }
+
+ private void logWarning(String string) {
+ System.out.println(string);
+ }
+
+ private IRemoteCall createRemoteCall(final Event event) {
+ return new IRemoteCall() {
+
+ public String getMethod() {
+ return "handleEvent";
+ }
+
+ public Object[] getParameters() {
+ return new Object[] { event };
+ }
+
+ public long getTimeout() {
+ return defaultTimeout;
+ }
+ };
+ }
+
+ public void sendEvent(Event event) {
+ ServiceReference[] eventHandlerRefs = getEventHandlerServiceReferences(event);
+ if (eventHandlerRefs == null) {
+ logWarning("sendEvent event=" + event
+ + ". No service references found to post to.");
+ return;
+ }
+ // Now synchronously call every eventhandler
+ for (int i = 0; i < eventHandlerRefs.length; i++)
+ callSync(eventHandlerRefs[i], (EventHandler) context
+ .getService(eventHandlerRefs[i]), event);
+ }
+
+ private void callSync(final ServiceReference serviceReference,
+ final EventHandler eventHandler, final Event event) {
+ SafeRunner.run(new ISafeRunnable() {
+ public void handleException(Throwable exception) {
+ logCallException(
+ "Exception in EventHandler.handleEvent. eventHandler="
+ + eventHandler + ". serviceReference="
+ + serviceReference + ". event=" + event,
+ exception);
+ }
+
+ public void run() throws Exception {
+ eventHandler.handleEvent(event);
+ }
+ });
+ }
+
+ private void fireAsync(final ServiceReference serviceReference,
+ final EventHandler eventHandler, final Event event) {
+ getExecutor().execute(new IProgressRunnable() {
+ public Object run(IProgressMonitor arg0) throws Exception {
+ eventHandler.handleEvent(event);
+ return null;
+ }
+ }, null);
+ }
+
+ private IExecutor getExecutor() {
+ synchronized (executorLock) {
+ if (executor == null) {
+ executor = new ThreadsExecutor();
+ }
+ return executor;
+ }
+ }
+
+ private void fireAsync(final ServiceReference serviceReference,
+ final IRemoteService remoteService, final IRemoteCall call) {
+ SafeRunner.run(new ISafeRunnable() {
+ public void handleException(Throwable exception) {
+ logCallException(
+ "Exception in IRemoteService.fireAsync. IRemoteService="
+ + remoteService + ". serviceReference="
+ + serviceReference + ". remoteCall=" + call,
+ exception);
+ }
+
+ public void run() throws Exception {
+ remoteService.fireAsync(call);
+ }
+ });
+ }
+
+ private void logCallException(String string, Throwable t) {
+ System.err.println(string);
+ if (t != null)
+ t.printStackTrace(System.err);
+ }
+
+ public void dispose() {
+ synchronized (eventHandlerSTLock) {
+ if (eventHandlerServiceTracker != null) {
+ eventHandlerServiceTracker.close();
+ eventHandlerServiceTracker = null;
+ }
+ }
+ synchronized (executorLock) {
+ executor = null;
+ }
+ context = null;
+ }
+}

Back to the top