Search in sources :

Example 1 with IRemoteServiceContainer

use of org.eclipse.ecf.remoteservice.IRemoteServiceContainer in project ecf by eclipse.

the class GenericAuthConsumerContainerSelector method createContainer.

@Override
protected IRemoteServiceContainer createContainer(ContainerTypeDescription containerTypeDescription, String containerTypeDescriptionName, @SuppressWarnings("rawtypes") Map properties) throws SelectContainerException {
    IRemoteServiceContainer result = super.createContainer(containerTypeDescription, containerTypeDescriptionName, properties);
    ISharedObjectContainerClient client = (ISharedObjectContainerClient) result.getContainer().getAdapter(ISharedObjectContainerClient.class);
    if (client != null) {
        client.setConnectInitiatorPolicy(new IConnectInitiatorPolicy() {

            public void refresh() {
            }

            public Object createConnectData(IContainer container, ID targetID, IConnectContext context) {
                // to the server.
                return getConnectData();
            }

            public int getConnectTimeout() {
                return 30000;
            }
        });
    }
    return result;
}
Also used : IRemoteServiceContainer(org.eclipse.ecf.remoteservice.IRemoteServiceContainer) IConnectContext(org.eclipse.ecf.core.security.IConnectContext) ISharedObjectContainerClient(org.eclipse.ecf.core.sharedobject.ISharedObjectContainerClient) IConnectInitiatorPolicy(org.eclipse.ecf.core.security.IConnectInitiatorPolicy) ID(org.eclipse.ecf.core.identity.ID) IContainer(org.eclipse.ecf.core.IContainer)

Example 2 with IRemoteServiceContainer

use of org.eclipse.ecf.remoteservice.IRemoteServiceContainer in project ecf by eclipse.

the class RemoteServiceAdmin method exportService.

// RemoteServiceAdmin service interface impl methods
public Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> exportService(final ServiceReference<?> serviceReference, Map<String, ?> op) {
    trace("exportService", // $NON-NLS-1$ //$NON-NLS-2$
    "serviceReference=" + serviceReference + ",properties=" + // $NON-NLS-1$
    op);
    final Map<String, ?> overridingProperties = PropertiesUtil.mergeProperties(serviceReference, op == null ? Collections.EMPTY_MAP : op);
    // get exported interfaces
    final String[] exportedInterfaces = PropertiesUtil.getExportedInterfaces(serviceReference, overridingProperties);
    if (exportedInterfaces == null)
        throw new IllegalArgumentException(// $NON-NLS-1$
        org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_INTERFACES + " not set");
    // verifyExportedInterfaces
    if (!validExportedInterfaces(serviceReference, exportedInterfaces))
        return Collections.EMPTY_LIST;
    // Get optional exported configs
    String[] ecs = PropertiesUtil.getStringArrayFromPropertyValue(overridingProperties.get(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS));
    if (ecs == null) {
        ecs = PropertiesUtil.getStringArrayFromPropertyValue(serviceReference.getProperty(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS));
    }
    final String[] exportedConfigs = ecs;
    // Get all intents (service.intents, service.exported.intents,
    // service.exported.intents.extra)
    final String[] serviceIntents = PropertiesUtil.getServiceIntents(serviceReference, overridingProperties);
    // Create result registrations. This collection will be returned
    Collection<ExportRegistration> resultRegistrations = new ArrayList<ExportRegistration>();
    // check for previously exported registration for the serviceReference
    synchronized (exportedRegistrations) {
        ExportEndpoint exportEndpoint = findExistingExportEndpoint(serviceReference, null);
        // If found then create a second ExportRegistration from endpoint
        if (exportEndpoint != null) {
            trace("exportService", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            "serviceReference=" + serviceReference + " export endpoint already exists=" + exportEndpoint + // $NON-NLS-1$
            ".  Returning new ExportRegistration for existing endpoint");
            ExportRegistration reg = new ExportRegistration(exportEndpoint);
            addExportRegistration(reg);
            resultRegistrations.add(reg);
        }
    }
    // If the serviceReference hasn't already been exported before (above)
    if (resultRegistrations.size() == 0) {
        // Get a host container selector
        final IHostContainerSelector hostContainerSelector = getHostContainerSelector();
        // and use it to select ECF remote service containers that match given exported
        // interfaces, configs, and intents
        IRemoteServiceContainer[] rsContainers = null;
        try {
            rsContainers = AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws SelectContainerException {
                    return hostContainerSelector.selectHostContainers(serviceReference, (Map<String, Object>) overridingProperties, exportedInterfaces, exportedConfigs, serviceIntents);
                }
            });
        } catch (PrivilegedActionException e) {
            Exception except = e.getException();
            // see discussion on osgi bug
            // https://www.osgi.org/members/bugzilla/show_bug.cgi?id=2591
            // $NON-NLS-1$
            String errorMessage = "Failed to select host container";
            if (except instanceof SelectContainerException) {
                SelectContainerException sce = (SelectContainerException) except;
                Throwable sceCause = sce.getCause();
                if (sceCause instanceof ContainerCreateException) {
                    // Some dummy props need to be set to allow the creation of a dummy export
                    // registration
                    Map<String, Object> props = new HashMap<String, Object>(overridingProperties);
                    // $NON-NLS-1$
                    props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID, "0");
                    props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS, // $NON-NLS-1$
                    "import.error.config");
                    // $NON-NLS-1$
                    props.put(RemoteConstants.ENDPOINT_ID, "export.error.id");
                    props.put(RemoteConstants.ENDPOINT_CONTAINER_ID_NAMESPACE, StringID.class.getName());
                    ExportRegistration errorRegistration = new RemoteServiceAdmin.ExportRegistration(sceCause, new EndpointDescription(serviceReference, props));
                    addExportRegistration(errorRegistration);
                    resultRegistrations.add(errorRegistration);
                } else
                    throw new IllegalArgumentException(errorMessage, except);
            } else
                throw new IllegalArgumentException(errorMessage, except);
        }
        // If no registration exist (no errorRegistration added above)
        if (resultRegistrations.size() == 0) {
            // If no containers found above, log warning and return
            if (rsContainers == null || rsContainers.length == 0) {
                String errorMessage = // $NON-NLS-1$
                "No containers found for serviceReference=" + serviceReference + " properties=" + // $NON-NLS-1$
                overridingProperties + // $NON-NLS-1$
                ". Remote service NOT EXPORTED";
                // $NON-NLS-1$
                logWarning("exportService", errorMessage);
                return Collections.EMPTY_LIST;
            }
            // actually do the export
            synchronized (exportedRegistrations) {
                // For all selected containers
                for (int i = 0; i < rsContainers.length; i++) {
                    Map endpointDescriptionProperties = createExportEndpointDescriptionProperties(serviceReference, (Map<String, Object>) overridingProperties, exportedInterfaces, serviceIntents, rsContainers[i]);
                    // otherwise, actually export the service to create
                    // a new ExportEndpoint and use it to create a new
                    // ExportRegistration
                    EndpointDescription endpointDescription = new EndpointDescription(endpointDescriptionProperties);
                    checkEndpointPermission(endpointDescription, EndpointPermission.EXPORT);
                    ExportRegistration exportRegistration = null;
                    try {
                        // Actually do the export and return export
                        // registration
                        exportRegistration = exportService(serviceReference, overridingProperties, exportedInterfaces, rsContainers[i], endpointDescriptionProperties);
                    } catch (Exception e) {
                        exportRegistration = new ExportRegistration(e, endpointDescription);
                    }
                    addExportRegistration(exportRegistration);
                    // We add it to the results in either success or error case
                    resultRegistrations.add(exportRegistration);
                }
            }
        }
    }
    // publish all activeExportRegistrations
    for (ExportRegistration exportReg : resultRegistrations) publishExportEvent(exportReg);
    // $NON-NLS-1$ //$NON-NLS-2$
    trace("exportService", "exported registrations=" + resultRegistrations);
    // and return
    return new ArrayList<org.osgi.service.remoteserviceadmin.ExportRegistration>(resultRegistrations);
}
Also used : ArrayList(java.util.ArrayList) ContainerCreateException(org.eclipse.ecf.core.ContainerCreateException) IRemoteServiceContainer(org.eclipse.ecf.remoteservice.IRemoteServiceContainer) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ECFException(org.eclipse.ecf.core.util.ECFException) ServiceException(org.osgi.framework.ServiceException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) PrivilegedActionException(java.security.PrivilegedActionException) ContainerCreateException(org.eclipse.ecf.core.ContainerCreateException) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 3 with IRemoteServiceContainer

use of org.eclipse.ecf.remoteservice.IRemoteServiceContainer in project ecf by eclipse.

the class ConsumerContainerSelector method selectConsumerContainer.

public IRemoteServiceContainer selectConsumerContainer(EndpointDescription endpointDescription) throws SelectContainerException {
    // $NON-NLS-1$ //$NON-NLS-2$
    trace("selectConsumerContainer", "endpointDescription=" + endpointDescription);
    // Get service.imported.configs
    List<String> sic = PropertiesUtil.getStringPlusProperty(endpointDescription.getProperties(), org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS);
    String[] serviceImportedConfigs = sic.toArray(new String[sic.size()]);
    // Get the endpointID
    ID endpointContainerID = endpointDescription.getContainerID();
    // Get connect targetID
    ID connectTargetID = endpointDescription.getConnectTargetID();
    IRemoteServiceContainer rsContainer = (reuseExistingContainers) ? selectExistingConsumerContainer(endpointContainerID, serviceImportedConfigs, connectTargetID) : null;
    // set to true
    if (rsContainer == null && autoCreateContainer)
        rsContainer = createAndConfigureConsumerContainer(serviceImportedConfigs, endpointDescription.getProperties());
    // Get the connect target ID from the endpointDescription
    // and connect the given containers to the connect targetID
    // This is only needed when when the endpointID is different from
    // the connect targetID, and the containers are not already
    // connected
    connectContainerToTarget(rsContainer, connectTargetID);
    // $NON-NLS-1$ //$NON-NLS-2$
    trace("selectConsumerContainer", "rsContainer selected=" + rsContainer);
    return rsContainer;
}
Also used : IRemoteServiceContainer(org.eclipse.ecf.remoteservice.IRemoteServiceContainer) ID(org.eclipse.ecf.core.identity.ID)

Example 4 with IRemoteServiceContainer

use of org.eclipse.ecf.remoteservice.IRemoteServiceContainer in project ecf by eclipse.

the class HostContainerSelector method selectHostContainers.

// Adding synchronized to make the host container finding
// thread safe to deal with bug
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=331836
/**
 * @see org.eclipse.ecf.osgi.services.remoteserviceadmin.IHostContainerSelector#selectHostContainers(org.osgi.framework.ServiceReference,
 *      java.util.Map, java.lang.String[], java.lang.String[],
 *      java.lang.String[])
 * @since 2.0
 */
public synchronized IRemoteServiceContainer[] selectHostContainers(ServiceReference serviceReference, Map<String, Object> overridingProperties, String[] serviceExportedInterfaces, String[] serviceExportedConfigs, String[] serviceIntents) throws SelectContainerException {
    trace(// $NON-NLS-1$
    "selectHostContainers", // $NON-NLS-1$ //$NON-NLS-2$
    "serviceReference=" + serviceReference + ",overridingProperties=" + overridingProperties + // $NON-NLS-1$
    ",exportedInterfaces=" + ((serviceExportedInterfaces == null) ? Collections.EMPTY_LIST : Arrays.asList(serviceExportedInterfaces)) + // $NON-NLS-1$
    ",serviceExportedConfigs=" + ((serviceExportedConfigs == null) ? Collections.EMPTY_LIST : Arrays.asList(serviceExportedConfigs)) + // $NON-NLS-1$
    ",serviceIntents=" + ((serviceIntents == null) ? Collections.EMPTY_LIST : Arrays.asList(serviceIntents)));
    // Find previously created containers that match the given
    // serviceExportedConfigs and serviceIntents
    Collection rsContainers = (reuseExistingContainers) ? selectExistingHostContainers(serviceReference, overridingProperties, serviceExportedInterfaces, serviceExportedConfigs, serviceIntents) : Collections.EMPTY_LIST;
    if (rsContainers.size() == 0 && autoCreateContainer) {
        // If no existing containers are found we'll go through
        // finding/creating/configuring/connecting
        rsContainers = createAndConfigureHostContainers(serviceReference, overridingProperties, serviceExportedInterfaces, serviceExportedConfigs, serviceIntents);
        // if SERVICE_EXPORTED_CONTAINER_CONNECT_TARGET service property is
        // specified, then
        // connect the host container(s)
        Object target = overridingProperties.get(RemoteConstants.ENDPOINT_CONNECTTARGET_ID);
        if (target != null) {
            for (Iterator i = rsContainers.iterator(); i.hasNext(); ) {
                IContainer container = ((IRemoteServiceContainer) i.next()).getContainer();
                try {
                    connectHostContainer(serviceReference, overridingProperties, container, target);
                } catch (Exception e) {
                    logException(// $NON-NLS-1$
                    "doConnectContainer failure containerID=" + container.getID() + " target=" + target, // $NON-NLS-1$
                    e);
                }
            }
        }
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    trace("selectHostContainers", "rsContainers selected=" + rsContainers);
    // return result
    return (IRemoteServiceContainer[]) rsContainers.toArray(new IRemoteServiceContainer[rsContainers.size()]);
}
Also used : IRemoteServiceContainer(org.eclipse.ecf.remoteservice.IRemoteServiceContainer) Iterator(java.util.Iterator) Collection(java.util.Collection) IContainer(org.eclipse.ecf.core.IContainer)

Example 5 with IRemoteServiceContainer

use of org.eclipse.ecf.remoteservice.IRemoteServiceContainer in project ecf by eclipse.

the class AbstractConsumerContainerSelector method createContainer.

/**
 * @param containerTypeDescription containerTypeDescription
 * @param containerTypeDescriptionName containerTypeDescriptionName
 * @param properties properties
 * @return IRemoteServiceContainer created container.  Should not be <code>null</code>
 * @throws SelectContainerException thrown if container cannot be created or configured
 * @since 2.0
 */
protected IRemoteServiceContainer createContainer(ContainerTypeDescription containerTypeDescription, String containerTypeDescriptionName, Map properties) throws SelectContainerException {
    try {
        IContainer container = (properties == null) ? getContainerFactory().createContainer(containerTypeDescriptionName) : getContainerFactory().createContainer(containerTypeDescriptionName, properties);
        IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class);
        if (adapter == null)
            throw new SelectContainerException("Container config id=" + containerTypeDescriptionName + " does not implement IRemoteServiceContainerAdapter", null, // $NON-NLS-1$ //$NON-NLS-2$
            containerTypeDescription);
        return new RemoteServiceContainer(container);
    } catch (ContainerCreateException e) {
        String message = // $NON-NLS-1$
        "Cannot create container config id=" + containerTypeDescriptionName;
        logException(message, e);
        throw new SelectContainerException(message, e, containerTypeDescription);
    }
}
Also used : RemoteServiceContainer(org.eclipse.ecf.remoteservice.RemoteServiceContainer) IRemoteServiceContainer(org.eclipse.ecf.remoteservice.IRemoteServiceContainer) IRemoteServiceContainerAdapter(org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter) ContainerCreateException(org.eclipse.ecf.core.ContainerCreateException) IContainer(org.eclipse.ecf.core.IContainer)

Aggregations

IRemoteServiceContainer (org.eclipse.ecf.remoteservice.IRemoteServiceContainer)9 IContainer (org.eclipse.ecf.core.IContainer)4 ContainerCreateException (org.eclipse.ecf.core.ContainerCreateException)3 PrivilegedActionException (java.security.PrivilegedActionException)2 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 ContainerConnectException (org.eclipse.ecf.core.ContainerConnectException)2 ContainerTypeDescription (org.eclipse.ecf.core.ContainerTypeDescription)2 ID (org.eclipse.ecf.core.identity.ID)2 ECFException (org.eclipse.ecf.core.util.ECFException)2 IRemoteServiceContainerAdapter (org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter)2 RemoteServiceContainer (org.eclipse.ecf.remoteservice.RemoteServiceContainer)2 BundleException (org.osgi.framework.BundleException)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ServiceException (org.osgi.framework.ServiceException)2 Collection (java.util.Collection)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1