summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Liebig2008-02-12 09:22:56 (EST)
committerStefan Liebig2008-02-12 09:22:56 (EST)
commitbd77107d6cb34e53bdf22321c044d8db09462733 (patch)
tree169fea2d31fe96332a2b21c40d9acd79ea6332b2
parent9975a2f77c3aa03da15d459fe1858f421deed586 (diff)
downloadorg.eclipse.riena.old-bd77107d6cb34e53bdf22321c044d8db09462733.zip
org.eclipse.riena.old-bd77107d6cb34e53bdf22321c044d8db09462733.tar.gz
org.eclipse.riena.old-bd77107d6cb34e53bdf22321c044d8db09462733.tar.bz2
formatting?
-rw-r--r--org.eclipse.riena.communication.discovery/src/org/eclipse/riena/internal/communication/discovery/RemoteServiceDiscovery.java341
1 files changed, 175 insertions, 166 deletions
diff --git a/org.eclipse.riena.communication.discovery/src/org/eclipse/riena/internal/communication/discovery/RemoteServiceDiscovery.java b/org.eclipse.riena.communication.discovery/src/org/eclipse/riena/internal/communication/discovery/RemoteServiceDiscovery.java
index 1749322..a0cff86 100644
--- a/org.eclipse.riena.communication.discovery/src/org/eclipse/riena/internal/communication/discovery/RemoteServiceDiscovery.java
+++ b/org.eclipse.riena.communication.discovery/src/org/eclipse/riena/internal/communication/discovery/RemoteServiceDiscovery.java
@@ -33,170 +33,179 @@ import com.caucho.hessian.client.HessianRuntimeException;
*
*/
public class RemoteServiceDiscovery {
- private static final String HOST_ID = RemoteServiceDiscovery.class.getName();
- private static final RemoteServiceDescription[] EMPTY_SERVICE_ENTRY_ARRAY = new RemoteServiceDescription[0];
- private IRemoteServiceRegistry registry;
- private RemoteServiceFactory rsFactory;
- private Map<String, RemoteServiceDescription> unpublishedServices = new HashMap<String, RemoteServiceDescription>();
- private BundleContext context;
-
- RemoteServiceDiscovery(BundleContext context) {
- super();
- this.context = context;
- }
-
- /**
- * Returns all services this publisher has
- *
- * @return
- */
- private synchronized RemoteServiceDescription[] getAllServices() {
- ServiceReference refPublisher = context.getServiceReference(IServicePublishEventDispatcher.ID);
- if (refPublisher == null) {
- System.out.println("Riena::RemoteServiceDiscovery:: WARN no IServicePublishEventDispatcher service available [" + IServicePublishEventDispatcher.ID
- + "]");
- return EMPTY_SERVICE_ENTRY_ARRAY;
- }
- IServicePublishEventDispatcher servicePublisher = (IServicePublishEventDispatcher) context.getService(refPublisher);
- try {
- RemoteServiceDescription[] rsDescriptions = servicePublisher.getAllServices();
- if (rsDescriptions == null) {
- return EMPTY_SERVICE_ENTRY_ARRAY;
- }
- return rsDescriptions;
- } finally {
- context.ungetService(refPublisher);
- }
- }
-
- private synchronized void updateInRegistry(RemoteServiceDescription[] rsDescriptions) {
- // get all services not just the service created with discovery
- List<IRemoteServiceRegistration> registeredServices = registry.registeredServices(null);
- Map<String, IRemoteServiceRegistration> existingPathsMap = new HashMap<String, IRemoteServiceRegistration>();
- // copy array to Map
- for (IRemoteServiceRegistration registeredService : registeredServices) {
- String url = registeredService.getReference().getURL();
- existingPathsMap.put(url, registeredService);
- }
-
- // newServices contains those that did not exist previously
- List<RemoteServiceDescription> newServices = new ArrayList<RemoteServiceDescription>();
- // remove those that still exist in the new list from the list of
- // existing paths
- for (RemoteServiceDescription rsDescription : rsDescriptions) {
- if (existingPathsMap.get(rsDescription.getURL()) != null) {
- existingPathsMap.remove(rsDescription.getURL());
- } else {
- newServices.add(rsDescription);
- }
- }
-
- // all other paths are now longer in the current list rsDescriptions and
- // have to be unpublished if they were created with my HOST_ID
- for (IRemoteServiceRegistration serviceReg : existingPathsMap.values()) {
- if (serviceReg.getReference().getHostId().equals(HOST_ID)) {
- serviceReg.unregister();
- }
- }
-
- // go through the newService Descriptions and create a new proxy for each of them
- List<IRemoteServiceReference> rsReferences = new ArrayList<IRemoteServiceReference>();
- for (RemoteServiceDescription rsDesc : newServices) {
- Class<?> interfaceClass = loadClass(rsDesc);
- rsDesc.setServiceInterfaceClass(interfaceClass);
-
- System.out.println("creating service with uri=" + rsDesc.getURL());
- IRemoteServiceReference rsRef = createReference(rsDesc);
- if (rsRef != null) {
- rsReferences.add(rsRef);
- } else {
- System.out.println("*****************");
- addAsUnpublished(rsDesc);
- }
-
- }
- for (IRemoteServiceReference rsRef : rsReferences) {
- // publish the new remote service references in the registry
- registry.registerService(rsRef);
- }
- }
-
- private IRemoteServiceReference createReference(RemoteServiceDescription rsDesc) {
- IRemoteServiceReference rsRef = rsFactory.createProxy(rsDesc);
- rsRef.setHostId(HOST_ID);
- return rsRef;
- }
-
- private void addAsUnpublished(RemoteServiceDescription rsDesc) {
- unpublishedServices.put(rsDesc.getURL(), rsDesc);
- }
-
- protected void checkForUnpublishedServices(String protocol) {
- Set<Entry<String, RemoteServiceDescription>> set = unpublishedServices.entrySet();
- for (Map.Entry<String, RemoteServiceDescription> entry : set) {
- RemoteServiceDescription rsDesc = entry.getValue();
- if (rsDesc.getProtocol().equals(protocol)) {
- IRemoteServiceReference rsRef = createReference(rsDesc);
-
- if (rsRef != null) {
- registry.registerService(rsRef);
- set.remove(entry); // also removes the entry from the map
- }
- }
- }
-
- }
-
- private Class<?> loadClass(RemoteServiceDescription endpoint) {
- if (endpoint.getServiceInterfaceClass() != null) {
- // Maybe the interface was transfered from server
- return endpoint.getServiceInterfaceClass();
- }
- String interfaceClassName = endpoint.getServiceInterfaceClassName();
-
- try {
- // Class loading: Maybe the host bundle of the interface is a buddy
- // friend of the service registry
- return new RemoteServiceFactory().loadClass(interfaceClassName);
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException("Riena:Communication::RemoteServiceDiscovery:: ERROR could not create web service interface for [" + interfaceClassName + "]", e);
- }
- }
-
- public void setRemoteServiceRegistry(IRemoteServiceRegistry registry) {
- this.registry = registry;
-
- }
-
- void start() {
- update();
- }
-
- void update() {
- if (registry != null) {
- try {
- RemoteServiceDescription[] serviceDescriptions = getAllServices();
- if (serviceDescriptions.length > 0) {
- updateInRegistry(serviceDescriptions);
- }
- } catch (HessianRuntimeException ex) {
- System.out.println("System::Riena:Communication::update of services from server failed. " + ex.getLocalizedMessage());
- }
- }
- }
-
- void stop() {
- if (registry != null) {
- // unregister all services I registered
- List<IRemoteServiceRegistration> registeredServices = registry.registeredServices(HOST_ID);
- for (IRemoteServiceRegistration rsReg : registeredServices) {
- rsReg.unregister();
- }
- }
- }
-
- void setRemoteServiceFactory(RemoteServiceFactory remoteServiceFactory) {
- this.rsFactory = remoteServiceFactory;
- }
+ private static final String HOST_ID = RemoteServiceDiscovery.class.getName();
+ private static final RemoteServiceDescription[] EMPTY_SERVICE_ENTRY_ARRAY = new RemoteServiceDescription[0];
+ private IRemoteServiceRegistry registry;
+ private RemoteServiceFactory rsFactory;
+ private Map<String, RemoteServiceDescription> unpublishedServices = new HashMap<String, RemoteServiceDescription>();
+ private BundleContext context;
+
+ RemoteServiceDiscovery(BundleContext context) {
+ super();
+ this.context = context;
+ }
+
+ /**
+ * Returns all services this publisher has
+ *
+ * @return
+ */
+ private synchronized RemoteServiceDescription[] getAllServices() {
+ ServiceReference refPublisher = context.getServiceReference(IServicePublishEventDispatcher.ID);
+ if (refPublisher == null) {
+ System.out
+ .println("Riena::RemoteServiceDiscovery:: WARN no IServicePublishEventDispatcher service available ["
+ + IServicePublishEventDispatcher.ID + "]");
+ return EMPTY_SERVICE_ENTRY_ARRAY;
+ }
+ IServicePublishEventDispatcher servicePublisher = (IServicePublishEventDispatcher) context
+ .getService(refPublisher);
+ try {
+ RemoteServiceDescription[] rsDescriptions = servicePublisher.getAllServices();
+ if (rsDescriptions == null) {
+ return EMPTY_SERVICE_ENTRY_ARRAY;
+ }
+ return rsDescriptions;
+ } finally {
+ context.ungetService(refPublisher);
+ }
+ }
+
+ private synchronized void updateInRegistry(RemoteServiceDescription[] rsDescriptions) {
+ // get all services not just the service created with discovery
+ List<IRemoteServiceRegistration> registeredServices = registry.registeredServices(null);
+ Map<String, IRemoteServiceRegistration> existingPathsMap = new HashMap<String, IRemoteServiceRegistration>();
+ // copy array to Map
+ for (IRemoteServiceRegistration registeredService : registeredServices) {
+ String url = registeredService.getReference().getURL();
+ existingPathsMap.put(url, registeredService);
+ }
+
+ // newServices contains those that did not exist previously
+ List<RemoteServiceDescription> newServices = new ArrayList<RemoteServiceDescription>();
+ // remove those that still exist in the new list from the list of
+ // existing paths
+ for (RemoteServiceDescription rsDescription : rsDescriptions) {
+ if (existingPathsMap.get(rsDescription.getURL()) != null) {
+ existingPathsMap.remove(rsDescription.getURL());
+ } else {
+ newServices.add(rsDescription);
+ }
+ }
+
+ // all other paths are now longer in the current list rsDescriptions and
+ // have to be unpublished if they were created with my HOST_ID
+ for (IRemoteServiceRegistration serviceReg : existingPathsMap.values()) {
+ if (serviceReg.getReference().getHostId().equals(HOST_ID)) {
+ serviceReg.unregister();
+ }
+ }
+
+ // go through the newService Descriptions and create a new proxy for
+ // each of them
+ List<IRemoteServiceReference> rsReferences = new ArrayList<IRemoteServiceReference>();
+ for (RemoteServiceDescription rsDesc : newServices) {
+ Class<?> interfaceClass = loadClass(rsDesc);
+ rsDesc.setServiceInterfaceClass(interfaceClass);
+
+ System.out.println("creating service with uri=" + rsDesc.getURL());
+ IRemoteServiceReference rsRef = createReference(rsDesc);
+ if (rsRef != null) {
+ rsReferences.add(rsRef);
+ } else {
+ System.out.println("*****************");
+ addAsUnpublished(rsDesc);
+ }
+
+ }
+ for (IRemoteServiceReference rsRef : rsReferences) {
+ // publish the new remote service references in the registry
+ registry.registerService(rsRef);
+ }
+ }
+
+ private IRemoteServiceReference createReference(RemoteServiceDescription rsDesc) {
+ IRemoteServiceReference rsRef = rsFactory.createProxy(rsDesc);
+ rsRef.setHostId(HOST_ID);
+ return rsRef;
+ }
+
+ private void addAsUnpublished(RemoteServiceDescription rsDesc) {
+ unpublishedServices.put(rsDesc.getURL(), rsDesc);
+ }
+
+ protected void checkForUnpublishedServices(String protocol) {
+ Set<Entry<String, RemoteServiceDescription>> set = unpublishedServices.entrySet();
+ for (Map.Entry<String, RemoteServiceDescription> entry : set) {
+ RemoteServiceDescription rsDesc = entry.getValue();
+ if (rsDesc.getProtocol().equals(protocol)) {
+ IRemoteServiceReference rsRef = createReference(rsDesc);
+
+ if (rsRef != null) {
+ registry.registerService(rsRef);
+ set.remove(entry); // also removes the entry from the map
+ }
+ }
+ }
+
+ }
+
+ private Class<?> loadClass(RemoteServiceDescription endpoint) {
+ if (endpoint.getServiceInterfaceClass() != null) {
+ // Maybe the interface was transfered from server
+ return endpoint.getServiceInterfaceClass();
+ }
+ String interfaceClassName = endpoint.getServiceInterfaceClassName();
+
+ try {
+ // Class loading: Maybe the host bundle of the interface is a buddy
+ // friend of the service registry
+ return new RemoteServiceFactory().loadClass(interfaceClassName);
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException(
+ "Riena:Communication::RemoteServiceDiscovery:: ERROR could not create web service interface for ["
+ + interfaceClassName + "]", e);
+ }
+ }
+
+ public void bind(IRemoteServiceRegistry registry) {
+ this.registry = registry;
+ }
+
+ public void unbind(IRemoteServiceRegistry registry) {
+ this.registry = registry;
+ }
+
+ void start() {
+ update();
+ }
+
+ void update() {
+ if (registry != null) {
+ try {
+ RemoteServiceDescription[] serviceDescriptions = getAllServices();
+ if (serviceDescriptions.length > 0) {
+ updateInRegistry(serviceDescriptions);
+ }
+ } catch (HessianRuntimeException ex) {
+ System.out.println("System::Riena:Communication::update of services from server failed. "
+ + ex.getLocalizedMessage());
+ }
+ }
+ }
+
+ void stop() {
+ if (registry != null) {
+ // unregister all services I registered
+ List<IRemoteServiceRegistration> registeredServices = registry.registeredServices(HOST_ID);
+ for (IRemoteServiceRegistration rsReg : registeredServices) {
+ rsReg.unregister();
+ }
+ }
+ }
+
+ void setRemoteServiceFactory(RemoteServiceFactory remoteServiceFactory) {
+ this.rsFactory = remoteServiceFactory;
+ }
}