Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * JBoss, Home of Professional Open Source.
   * Copyright 2010, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site:
 import java.util.Map;
Implementation of ManagedReferenceFactory that returns AuthenticationManagers

Marcus Moyses
     public String getInstanceClassName() {
         return getReference().getInstance().getClass().getName();
     public String getJndiViewInstanceValue() {
         return String.valueOf(getReference().getInstance());

This method returns a Context proxy that is only able to handle a lookup operation for an atomic name of a security domain.
     public ManagedReference getReference() {
         final ClassLoader loader;
         try {
             loader = SecurityActions.getModuleClassLoader();
         } catch (ModuleLoadException e) {
             throw ..unableToGetModuleClassLoader(e);
         Class<?>[] interfaces = { Context.class };
         return new ValueManagedReference(new ImmediateValue<Object>(Proxy.newProxyInstance(loaderinterfacesthis)));

This is the InvocationHandler callback for the Context interface that was created by our getObjectInstance() method. We handle the java:jboss/jaas/domain level operations here.
     public Object invoke(Object proxyMethod methodObject[] argsthrows Throwable {
         Context ctx = new InitialContext();
         NameParser parser = ctx.getNameParser("");
         String securityDomain = null;
         Name name = null;
         final JNDIBasedSecurityManagement securityManagement = JNDIBasedSecurityManagement.class.cast(
        final ConcurrentHashMap<StringSecurityDomainContextsecurityManagerMap = securityManagement.getSecurityManagerMap();
        String methodName = method.getName();
        if (methodName.equals("toString"))
            return . + " Context proxy";
        if (methodName.equals("list"))
            return new DomainEnumeration(securityManagerMap.keys(), securityManagerMap);
        if (methodName.equals("bind") || methodName.equals("rebind")) {
            if (args[0] instanceof String)
                name = parser.parse((Stringargs[0]);
                name = (Nameargs[0];
            securityDomain = name.get(0);
            SecurityDomainContext val = (SecurityDomainContextargs[1];
            return proxy;
        if (!methodName.equals("lookup"))
            throw ..operationNotSupported(method);
        if (args[0] instanceof String)
            name = parser.parse((Stringargs[0]);
            name = (Nameargs[0];
        securityDomain = name.get(0);
        SecurityDomainContext securityDomainCtx = lookupSecurityDomain(securityManagementsecurityManagerMapsecurityDomain);
        Object binding = securityDomainCtx.getAuthenticationManager();
        // Look for requests against the security domain context
        if (name.size() == 2) {
            String request = name.get(1);
            binding = securityDomainCtx.lookup(request);
        return binding;

Creates a SecurityDomainContext if one cannot be found in JNDI for a given security domain

securityManagement security management
securityManagerMap security manager map
securityDomain the name of the security domain
an instance of SecurityDomainContext
java.lang.Exception if an error occurs
    private SecurityDomainContext lookupSecurityDomain(final JNDIBasedSecurityManagement securityManagement,
            final ConcurrentHashMap<StringSecurityDomainContextsecurityManagerMapfinal String securityDomain)
            throws Exception {
        SecurityDomainContext sdc = securityManagerMap.get(securityDomain);
        if (sdc == null) {
            sdc = securityManagement.createSecurityDomainContext(securityDomainnew DefaultAuthenticationCacheFactory());
        return sdc;
        return ;

Enumeration for the bound security domains in JNDI

Marcus Moyses
    class DomainEnumeration implements NamingEnumeration<NameClassPair> {
        DomainEnumeration(Enumeration<StringdomainsMap<StringSecurityDomainContextctxMap) {
            this. = domains;
            this. = ctxMap;
        public void close() {
        public boolean hasMoreElements() {
            return .hasMoreElements();
        public boolean hasMore() {
            return .hasMoreElements();
        public NameClassPair next() {
            String name = (String.nextElement();
            Object value = .get(name);
            String className = value.getClass().getName();
            NameClassPair pair = new NameClassPair(nameclassName);
            return pair;
        public NameClassPair nextElement() {
            return next();
New to GrepCode? Check out our FAQ X