private void refresh() { final Enumeration<SaslClientFactory> factories = Sasl.getSaslClientFactories(); final Map<String, List<SaslClientFactory>> map = Maps.newHashMap(); while (factories.hasMoreElements()) { final SaslClientFactory factory = factories.nextElement(); // Passing null so factory is populated with all possibilities. Properties passed when // instantiating a client are what really matter. See createSaslClient. for (final String mechanismName : factory.getMechanismNames(null)) { if (!map.containsKey(mechanismName)) { map.put(mechanismName, new ArrayList<SaslClientFactory>()); } map.get(mechanismName).add(factory); } } clientFactories = ImmutableMap.copyOf(map); if (logger.isDebugEnabled()) { logger.debug("Registered sasl client factories: {}", clientFactories.keySet()); } }
@Override public SaslClient createSaslClient(String[] mechanisms, String authorizationId, String protocol, String serverName, Map<String, ?> props, CallbackHandler cbh) throws SaslException { for (final String mechanism : mechanisms) { final List<SaslClientFactory> factories = clientFactories.get(mechanism); if (factories != null) { for (final SaslClientFactory factory : factories) { final SaslClient saslClient = factory.createSaslClient(new String[]{mechanism}, authorizationId, protocol, serverName, props, cbh); if (saslClient != null) { return saslClient; } } } } return null; }
public SaslClientContext(final SaslClientFactory saslClientFactory, final String mech, final String server_name, final CallbackHandler callback_handler, final Map<String, String> props, final Subject subject) throws SaslException { this.subject = subject; if (this.subject != null) { try { client = Subject.doAs(this.subject, new PrivilegedExceptionAction<SaslClient>() { @Override public SaslClient run() throws Exception { return saslClientFactory.createSaslClient(new String[] { mech }, null, SASL.SASL_PROTOCOL_NAME, server_name, props, callback_handler); } }); } catch (PrivilegedActionException e) { throw (SaslException)e.getCause(); // The createSaslServer will only throw this type of exception } } else { client = saslClientFactory.createSaslClient(new String[] { mech }, null, SASL.SASL_PROTOCOL_NAME, server_name, props, callback_handler); } }
public static SaslClientFactory getSaslClientFactory(String mech, Map<String, ?> props) { Iterator<SaslClientFactory> saslFactories = SaslUtils.getSaslClientFactories(SaslUtils.class.getClassLoader(), true); while (saslFactories.hasNext()) { SaslClientFactory saslFactory = saslFactories.next(); for (String supportedMech : saslFactory.getMechanismNames(props)) { if (mech.equals(supportedMech)) { return saslFactory; } } } throw new IllegalArgumentException("No SASL client factory for mech " + mech); }
/** * Test for <code>getSaslClientFactories()</code> method * * Assertion: returns enumeration of factories for producing SaslClient. * * Enumeration consists of 4 elements. * * 4 different providers define different mechanism and refer to the same * SaslClientFactory class */ public void testGetClient02() throws SaslException { mProv = new Provider[] { (new SpiEngUtils()).new MyProvider("MySaslClientProvider1", "Testing provider SaslClientFactory - 1", CLNTSRV .concat(mech[0]), fClientClass01), (new SpiEngUtils()).new MyProvider("MySaslClientProvider2", "Testing provider SaslClientFactory - 2", CLNTSRV .concat(mech[1]), fClientClass01), (new SpiEngUtils()).new MyProvider("MySaslClientProvider3", "Testing provider SaslClientFactory - 3", CLNTSRV .concat(mech[2]), fClientClass01), (new SpiEngUtils()).new MyProvider("MySaslClientProvider4", "Testing provider SaslClientFactory - 4", CLNTSRV .concat(mech[3]), fClientClass01) }; addProviders(); Enumeration<SaslClientFactory> en = Sasl.getSaslClientFactories(); assertNotNull("List of SaslClientFactories should not be null", en); assertTrue("List of SaslClientFactories should have elements", en .hasMoreElements()); myClientFactory01 mm = new myClientFactory01(); String[] mech01 = mm.getMechanismNames(null); int l = 0; while (en.hasMoreElements()) { SaslClientFactory f = en.nextElement(); if (f instanceof myClientFactory01) { l++; assertNull("Incorect SaslClient", f.createSaslClient(null, null, null, null, null, null)); String[] mech00 = f.getMechanismNames(null); assertEquals("Wrong length", mech00.length, mech01.length); for (int i = 0; i < mech00.length; i++) { assertEquals("Wrong mechanism name", mech00[i], mech01[i]); } } } assertEquals("Incorrect number of enumeration elements", l, mProv.length); }
/** * Creates the security provider with a map from SASL mechanisms to implementing factories. * * @param providerMap The map from SASL mechanims to implementing factory classes. */ public JCAProvider(Map<String, Class<? extends SaslClientFactory>> providerMap) { super("AMQSASLProvider", 1.0, "A JCA provider that registers all " + "AMQ SASL providers that want to be registered"); register(providerMap); }
/** * Registers client factory classes for a map of mechanism names to client factory classes. * * @param providerMap The map from SASL mechanims to implementing factory classes. */ private void register(Map<String, Class<? extends SaslClientFactory>> providerMap) { for (Map.Entry<String, Class<? extends SaslClientFactory>> me : providerMap.entrySet()) { put("SaslClientFactory." + me.getKey(), me.getValue().getName()); } }
/** * Creates the security provider with a map from SASL mechanisms to implementing factories. * * @param providerMap The map from SASL mechanims to implementing factory classes. */ public JCAProvider(Map<String, Class<? extends SaslClientFactory>> providerMap) { super("AMQSASLProvider-Client", 1.0, "A JCA provider that registers all " + "AMQ SASL providers that want to be registered"); register(providerMap); // Security.addProvider(this); }
/** * Registers client factory classes for a map of mechanism names to client factory classes. * * @param providerMap The map from SASL mechanims to implementing factory classes. */ private void register(Map<String, Class<? extends SaslClientFactory>> providerMap) { for (Map.Entry<String, Class<? extends SaslClientFactory>> me : providerMap.entrySet()) { put( "SaslClientFactory."+me.getKey(), me.getValue().getName()); log.debug("Registered SASL Client factory for " + me.getKey() + " as " + me.getValue().getName()); } }
/** * Parses the specified properties as a mapping from IANA registered SASL mechanism names to implementing client * factories. If the client factories cannot be instantiated or do not implement SaslClientFactory then the * properties refering to them are ignored. * * @param props The properties to scan for Sasl client factory implementations. * * @return A map from SASL mechanism names to implementing client factory classes. * * @todo Why tree map here? Do really want mechanisms in alphabetical order? Seems more likely that the declared * order of the mechanisms is intended to be preserved, so that they are registered in the declared order of * preference. Consider LinkedHashMap instead. */ private static Map<String, Class<? extends SaslClientFactory>> parseProperties(Properties props) { Enumeration e = props.propertyNames(); TreeMap<String, Class<? extends SaslClientFactory>> factoriesToRegister = new TreeMap<String, Class<? extends SaslClientFactory>>(); while (e.hasMoreElements()) { String mechanism = (String) e.nextElement(); String className = props.getProperty(mechanism); try { Class<?> clazz = Class.forName(className); if (!(SaslClientFactory.class.isAssignableFrom(clazz))) { _logger.error("Class " + clazz + " does not implement " + SaslClientFactory.class + " - skipping"); continue; } _logger.debug("Registering class "+ clazz.getName() +" for mechanism "+mechanism); factoriesToRegister.put(mechanism, (Class<? extends SaslClientFactory>) clazz); } catch (Exception ex) { _logger.error("Error instantiating SaslClientFactory calss " + className + " - skipping"); } } return factoriesToRegister; }
/** * Test for <code>getSaslClientFactories()</code> method * * Assertion: returns enumeration of factories for producing SaslClient. * * Enumeration consists of 4 elements. * * 4 different providers define the same mechanism and refer to the same * SaslClientFactory class */ public void testGetClient01() throws SaslException { mProv = new Provider[] { (new SpiEngUtils()).new MyProvider("MySaslClientProvider1", "Testing provider SaslClientFactory - 1", CLNTSRV .concat(mech[0]), fClientClass01), (new SpiEngUtils()).new MyProvider("MySaslClientProvider2", "Testing provider SaslClientFactory - 2", CLNTSRV .concat(mech[0]), fClientClass01), (new SpiEngUtils()).new MyProvider("MySaslClientProvider3", "Testing provider SaslClientFactory - 3", CLNTSRV .concat(mech[0]), fClientClass01), (new SpiEngUtils()).new MyProvider("MySaslClientProvider4", "Testing provider SaslClientFactory - 4", CLNTSRV .concat(mech[0]), fClientClass01) }; addProviders(); Enumeration<SaslClientFactory> en = Sasl.getSaslClientFactories(); assertNotNull("List of SaslClientFactories should not be null", en); assertTrue("List of SaslClientFactories should have elements", en .hasMoreElements()); myClientFactory01 mm = new myClientFactory01(); String[] mech01 = mm.getMechanismNames(null); int l = 0; while (en.hasMoreElements()) { SaslClientFactory f = en.nextElement(); if (f instanceof myClientFactory01) { l++; assertNull("Incorect SaslClient", f.createSaslClient(null, null, null, null, null, null)); String[] mech00 = f.getMechanismNames(null); assertEquals("Wrong length", mech00.length, mech01.length); for (int i = 0; i < mech00.length; i++) { assertEquals("Wrong mechanism name", mech00[i], mech01[i]); } } } assertEquals("Incorrect number of enumeration elements", l, mProv.length); }
public SaslClientFactoryWrapper(com.sun.security.sasl.preview.SaslClientFactory factory) { this.factory = factory; }
/** * Test for <code>getSaslClientFactories()</code> method * * Assertion: * returns enumeration without any element * * All providers are previously removed. */ public void testGetClient() { Enumeration<SaslClientFactory> en = Sasl.getSaslClientFactories(); assertNotNull("List of SaslClientFactories should not be null", en); assertFalse("List of SaslClientFactories should not haves elements", en .hasMoreElements()); }
/** * Returns an iterator of all of the registered {@code SaslClientFactory}s where the order is * based on the order of the Provider registration and/or class path order. Class path providers * are listed before global providers; in the event of a name conflict, the class path provider * is preferred. * * @param classLoader * the class loader to use * @param includeGlobal * {@code true} to include globally registered providers, {@code false} to exclude * them * @return the {@code Iterator} of {@code SaslClientFactory}s */ public static Iterator<SaslClientFactory> getSaslClientFactories(ClassLoader classLoader, boolean includeGlobal) { return getFactories(SaslClientFactory.class, classLoader, includeGlobal); }
/** * Returns an iterator of all of the registered {@code SaslClientFactory}s where the order is * based on the order of the Provider registration and/or class path order. * * @return the {@code Iterator} of {@code SaslClientFactory}s */ public static Iterator<SaslClientFactory> getSaslClientFactories() { return getFactories(SaslClientFactory.class, null, true); }