Search in sources :

Example 1 with NoProviderFoundException

use of com.att.cdp.exceptions.NoProviderFoundException in project AJSC by att.

the class TestContextFactory method testMultipleContexts.

/**
 * This test ensures that we can create multiple contexts from the same provider
 */
@Test
public void testMultipleContexts() {
    try {
        Context context1 = ContextFactory.getContext(ProviderName, null);
        assertNotNull(context1);
        assertTrue(context1 instanceof DummyProviderContext);
        Provider provider = context1.getProvider();
        assertNotNull(provider);
        assertEquals(ProviderName, provider.getName());
        Context context2 = ContextFactory.getContext(ProviderName, null);
        assertNotNull(context2);
        assertFalse(context1 == context2);
        assertFalse(context1.equals(context2));
    } catch (NoProviderFoundException e) {
        fail("We were supposed to find the TestProvider!");
    }
}
Also used : DummyProviderContext(com.att.cdp.zones.test.DummyProviderContext) NoProviderFoundException(com.att.cdp.exceptions.NoProviderFoundException) DummyProviderContext(com.att.cdp.zones.test.DummyProviderContext) Test(org.junit.Test)

Example 2 with NoProviderFoundException

use of com.att.cdp.exceptions.NoProviderFoundException in project AJSC by att.

the class TestContextFactory method testNotLoggedIn.

/**
 * Validate that the provider is initially not logged in
 */
@Test
public void testNotLoggedIn() {
    try {
        Context context = ContextFactory.getContext(ProviderName, null);
        assertNotNull(context);
        assertTrue(context instanceof DummyProviderContext);
        assertFalse(context.isLoggedIn());
        IdentityService identity = context.getIdentityService();
        assertNotNull(identity);
    } catch (NoProviderFoundException e) {
        fail("We were supposed to find the TestProvider!");
    }
}
Also used : DummyProviderContext(com.att.cdp.zones.test.DummyProviderContext) NoProviderFoundException(com.att.cdp.exceptions.NoProviderFoundException) DummyProviderContext(com.att.cdp.zones.test.DummyProviderContext) Test(org.junit.Test)

Example 3 with NoProviderFoundException

use of com.att.cdp.exceptions.NoProviderFoundException in project AJSC by att.

the class TestContextFactory method testTestProvider.

/**
 * This test ensures that we can load the test provider and that we get a valid context object from it.
 */
@Test
public void testTestProvider() {
    try {
        Context context = ContextFactory.getContext(ProviderName, null);
        assertNotNull(context);
        assertTrue(context instanceof DummyProviderContext);
        Provider provider = context.getProvider();
        assertNotNull(provider);
        assertEquals(ProviderName, provider.getName());
    } catch (NoProviderFoundException e) {
        fail("We were supposed to find the TestProvider!");
    }
}
Also used : DummyProviderContext(com.att.cdp.zones.test.DummyProviderContext) NoProviderFoundException(com.att.cdp.exceptions.NoProviderFoundException) DummyProviderContext(com.att.cdp.zones.test.DummyProviderContext) Test(org.junit.Test)

Example 4 with NoProviderFoundException

use of com.att.cdp.exceptions.NoProviderFoundException in project AJSC by att.

the class ContextFactory method getContext.

/**
 * This method is a special case that allows the caller to instantiate the provider directly by supplying the class
 * of the provider to be used. This can allow a caller that knows the implementation class, and which has that class
 * available to it on the class path, direct instantiation and avoiding the use of the service loader mechanism.
 *
 * @param providerClass
 *            The class of the provider to be used
 * @param properties
 *            The configuration properties to establish a context
 * @return The context, or null
 * @throws ZoneException
 *             If the provider cannot be used for some reason.
 */
public static Context getContext(Class<? extends Provider> providerClass, Properties properties) throws ZoneException {
    if (providerClass == null) {
        throw new NoProviderFoundException("No provider class was supplied, provider " + "context cannot be instantiated");
    }
    try {
        Provider provider = (Provider) providerClass.newInstance();
        Context ctx = provider.openContext(properties);
        logger.info(EELFResourceManager.format(Msg.ZONE_CONTEXT_CREATED, provider.getName()));
        return ctx;
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
        throw new NoProviderFoundException(String.format("Provider class %s cannot be instantiated", providerClass.getSimpleName()), e);
    }
}
Also used : NoProviderFoundException(com.att.cdp.exceptions.NoProviderFoundException)

Example 5 with NoProviderFoundException

use of com.att.cdp.exceptions.NoProviderFoundException in project AJSC by att.

the class ContextFactory method getContext.

/**
 * Attempt to locate the service provider bootstrap class. When we locate the provider bootstrap, ask it to return a
 * new context object for the caller.
 *
 * @param providerName
 *            The name of the provider that we are interested in using
 * @param properties
 *            An optional <code>Properties</code> object that can be used to configure the context, if needed. It
 *            can be null if no configuration is needed. The use of a configuration object is specific to the
 *            provider, some providers may require it while others may not.
 * @return The context object that allows access to that provider
 * @throws NoProviderFoundException
 *             If the provider cannot be loaded
 */
public static Context getContext(String providerName, Properties properties) throws NoProviderFoundException {
    Iterator<Provider> providers = loader.iterator();
    String msg = String.format("Attempting to locate provider named [%s]", providerName);
    logger.debug(msg);
    try {
        while (providers.hasNext()) {
            Provider provider = providers.next();
            logger.debug(String.format("Examining provider [%s]", provider.getName()));
            if (provider.getName().equals(providerName)) {
                Context ctx = provider.openContext(properties);
                logger.info(EELFResourceManager.format(Msg.ZONE_CONTEXT_CREATED, providerName));
                return ctx;
            }
        }
    } catch (ServiceConfigurationError e) {
        msg = EELFResourceManager.format(Msg.ZONE_PROVIDER_NOT_FOUND, providerName);
        throw new NoProviderFoundException(msg, e);
    }
    msg = EELFResourceManager.format(Msg.ZONE_PROVIDER_NOT_FOUND, providerName);
    logger.error(msg);
    throw new NoProviderFoundException(msg);
}
Also used : ServiceConfigurationError(java.util.ServiceConfigurationError) NoProviderFoundException(com.att.cdp.exceptions.NoProviderFoundException)

Aggregations

NoProviderFoundException (com.att.cdp.exceptions.NoProviderFoundException)6 DummyProviderContext (com.att.cdp.zones.test.DummyProviderContext)4 Test (org.junit.Test)4 Properties (java.util.Properties)1 ServiceConfigurationError (java.util.ServiceConfigurationError)1