Search in sources :

Example 6 with ITransportFactory

use of org.eclipse.epp.mpc.core.service.ITransportFactory in project epp.mpc by eclipse.

the class TransportFactory method createTransport.

public static org.eclipse.epp.mpc.core.service.ITransport createTransport() {
    // search for registered factory service
    BundleContext context = MarketplaceClientCorePlugin.getBundle().getBundleContext();
    ServiceReference<ITransportFactory> serviceReference = getTransportServiceReference(context);
    if (serviceReference != null) {
        ITransportFactory transportService = context.getService(serviceReference);
        if (transportService != null) {
            try {
                return transportService.getTransport();
            } finally {
                transportService = null;
                context.ungetService(serviceReference);
            }
        }
    }
    IStatus serviceError = diagnoseTransportServiceRegistration(context, serviceReference);
    throw new IllegalStateException(new CoreException(serviceError));
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) BundleContext(org.osgi.framework.BundleContext)

Example 7 with ITransportFactory

use of org.eclipse.epp.mpc.core.service.ITransportFactory in project epp.mpc by eclipse.

the class TransportFactory method diagnoseTransportServiceRegistration.

private static IStatus diagnoseTransportServiceRegistration(BundleContext context, ServiceReference<ITransportFactory> serviceReference) {
    MultiStatus serviceError = null;
    if (serviceReference != null) {
        serviceError = new MultiStatus(MarketplaceClientCore.BUNDLE_ID, 0, Messages.TransportFactory_ServiceErrorUnregistered, null);
        serviceError.add(new Status(IStatus.ERROR, MarketplaceClientCore.BUNDLE_ID, Messages.TransportFactory_ServiceErrorServiceReference + serviceReference));
    } else {
        serviceError = new MultiStatus(MarketplaceClientCore.BUNDLE_ID, 0, Messages.TransportFactory_ServiceErrorNotFound, null);
        try {
            Collection<ServiceReference<ITransportFactory>> allServiceReferences = context.getServiceReferences(ITransportFactory.class, null);
            if (allServiceReferences.isEmpty()) {
                serviceError.add(new Status(IStatus.ERROR, MarketplaceClientCore.BUNDLE_ID, Messages.TransportFactory_ServiceErrorNoneAvailable));
            } else {
                String filter = computeDisabledTransportsFilter();
                serviceError.add(new Status(IStatus.ERROR, MarketplaceClientCore.BUNDLE_ID, Messages.TransportFactory_ServiceErrorAppliedFilter + filter));
            }
            for (ServiceReference<ITransportFactory> availableReference : allServiceReferences) {
                serviceError.add(new Status(IStatus.INFO, MarketplaceClientCore.BUNDLE_ID, Messages.TransportFactory_ServiceErrorFilteredService + availableReference.toString()));
            }
        } catch (InvalidSyntaxException e) {
            // Unreachable
            serviceError.add(new Status(IStatus.ERROR, MarketplaceClientCore.BUNDLE_ID, Messages.TransportFactory_ServiceErrorDetails, e));
        }
    }
    return serviceError;
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) MultiStatus(org.eclipse.core.runtime.MultiStatus) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 8 with ITransportFactory

use of org.eclipse.epp.mpc.core.service.ITransportFactory in project epp.mpc by eclipse.

the class MarketplaceClientCorePlugin method registerServices.

private void registerServices(BundleContext context) throws InvalidSyntaxException {
    List<ServiceRegistration<?>> serviceRegistrations = new ArrayList<ServiceRegistration<?>>();
    this.serviceRegistrations = serviceRegistrations;
    // highest-prio factory comes first
    List<ITransportFactory> factories = TransportFactory.listAvailableFactories();
    if (factories.isEmpty()) {
        return;
    }
    Collection<ServiceReference<ITransportFactory>> serviceReferences = context.getServiceReferences(ITransportFactory.class, null);
    int lowestPriority = Integer.MAX_VALUE;
    for (ServiceReference<ITransportFactory> serviceReference : serviceReferences) {
        Object legacyProperty = serviceReference.getProperty(TransportFactory.LEGACY_TRANSPORT_KEY);
        if (legacyProperty != null) {
            continue;
        }
        Integer ranking = (Integer) serviceReference.getProperty(Constants.SERVICE_RANKING);
        lowestPriority = Math.min(lowestPriority, ranking == null ? 0 : ranking.intValue());
    }
    int maxLegacyPriority, step;
    if (lowestPriority >= 0) {
        maxLegacyPriority = -100;
        step = 100;
    } else {
        int available = lowestPriority - Integer.MIN_VALUE;
        step = Math.min(100, available / factories.size());
        if (step == 0) {
            step = 1;
            maxLegacyPriority = Integer.MIN_VALUE + factories.size();
        } else {
            maxLegacyPriority = lowestPriority - step;
        }
    }
    // prio counts down from 0 in steps of 100
    int prio = maxLegacyPriority;
    for (ITransportFactory factory : factories) {
        Hashtable<String, Object> properties = new Hashtable<String, Object>();
        properties.put(Constants.SERVICE_RANKING, prio);
        properties.put(ComponentConstants.COMPONENT_NAME, "legacy:" + factory.getClass().getName());
        properties.put(TransportFactory.LEGACY_TRANSPORT_KEY, true);
        ServiceRegistration<ITransportFactory> registration = context.registerService(ITransportFactory.class, factory, properties);
        serviceRegistrations.add(registration);
        prio -= step;
    }
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 9 with ITransportFactory

use of org.eclipse.epp.mpc.core.service.ITransportFactory in project epp.mpc by eclipse.

the class TransportFactoryTest method testHttpClientTransportFallback.

@Test
public void testHttpClientTransportFallback() throws Exception {
    HttpClientTransport httpClientTransport = createFailingHttpClientTransport();
    HttpClientTransportFactory httpClientFactory = new HttpClientTransportFactory();
    httpClientFactory.setTransport(httpClientTransport);
    ITransportFactory secondaryFactory = Mockito.mock(ITransportFactory.class);
    ITransport secondaryTransport = Mockito.mock(ITransport.class);
    InputStream expectedResultStream = new ByteArrayInputStream("Secondary transport".getBytes("UTF-8"));
    Mockito.when(secondaryFactory.getTransport()).thenReturn(secondaryTransport);
    Mockito.when(secondaryTransport.stream(Matchers.<URI>any(), Matchers.<IProgressMonitor>any())).thenReturn(expectedResultStream);
    FallbackTransportFactory fallbackTransportFactory = new FallbackTransportFactory();
    fallbackTransportFactory.setPrimaryFactory(httpClientFactory);
    fallbackTransportFactory.setSecondaryFactory(secondaryFactory);
    InputStream stream = fallbackTransportFactory.getTransport().stream(URI.create("http://127.0.0.1:54321"), null);
    assertSame(expectedResultStream, stream);
}
Also used : HttpClientTransportFactory(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) ITransport(org.eclipse.epp.mpc.core.service.ITransport) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FallbackTransportFactory(org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) Test(org.junit.Test)

Example 10 with ITransportFactory

use of org.eclipse.epp.mpc.core.service.ITransportFactory in project epp.mpc by eclipse.

the class MappedTransportFactory method getTransport.

public ITransport getTransport() {
    return new ITransport() {

        private final ITransport delegateTransport = delegate.getTransport();

        public InputStream stream(URI location, IProgressMonitor monitor) throws FileNotFoundException, ServiceUnavailableException, CoreException {
            URI mapped = mapping.get(location);
            if (mapped == null) {
                if (location.getQuery() != null || location.getFragment() != null) {
                    try {
                        URI stripped = new URI(location.getScheme(), location.getHost(), location.getPath(), null);
                        mapped = mapping.get(stripped);
                    } catch (URISyntaxException e) {
                    }
                }
            }
            if (mapped == null) {
                mapped = location;
            } else if (!mapped.getScheme().toLowerCase().startsWith("http")) {
                ITransport nonHttpTransport = getNonHttpTransport(delegate, delegateTransport);
                if (nonHttpTransport != null) {
                    return nonHttpTransport.stream(mapped, monitor);
                }
            }
            return delegateTransport.stream(mapped, monitor);
        }

        private ITransport getNonHttpTransport(ITransportFactory delegate, ITransport delegateTransport) {
            ITransport nonHttpTransport = delegateTransport;
            if (delegate instanceof HttpClientTransportFactory) {
                FallbackTransportFactory fallbackFactory = new FallbackTransportFactory();
                fallbackFactory.setPrimaryFactory(delegate);
                delegate = fallbackFactory;
            }
            if (delegate instanceof FallbackTransportFactory) {
                FallbackTransportFactory fallbackFactory = (FallbackTransportFactory) delegate;
                ITransportFactory primaryFactory = fallbackFactory.getPrimaryFactory();
                if (primaryFactory instanceof HttpClientTransportFactory) {
                    ITransportFactory secondaryFactory = fallbackFactory.getFallbackFactory();
                    if (secondaryFactory != null) {
                        nonHttpTransport = secondaryFactory.getTransport();
                    }
                }
            }
            if (nonHttpTransport instanceof HttpClientTransport) {
                nonHttpTransport = new JavaPlatformTransportFactory().getTransport();
            }
            return nonHttpTransport;
        }
    };
}
Also used : HttpClientTransportFactory(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) ITransport(org.eclipse.epp.mpc.core.service.ITransport) FallbackTransportFactory(org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory) URISyntaxException(java.net.URISyntaxException) ITransportFactory(org.eclipse.epp.mpc.core.service.ITransportFactory) URI(java.net.URI) JavaPlatformTransportFactory(org.eclipse.epp.internal.mpc.core.util.JavaPlatformTransportFactory)

Aggregations

ITransportFactory (org.eclipse.epp.mpc.core.service.ITransportFactory)11 BundleContext (org.osgi.framework.BundleContext)5 HttpClientTransportFactory (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransportFactory)4 FallbackTransportFactory (org.eclipse.epp.internal.mpc.core.util.FallbackTransportFactory)4 ServiceReference (org.osgi.framework.ServiceReference)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Hashtable (java.util.Hashtable)2 LinkedHashSet (java.util.LinkedHashSet)2 IStatus (org.eclipse.core.runtime.IStatus)2 HttpClientTransport (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport)2 TransportFactory (org.eclipse.epp.internal.mpc.core.util.TransportFactory)2 ITransport (org.eclipse.epp.mpc.core.service.ITransport)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1