Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * 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: http://www.fsf.org.
 */
 package org.jboss.security.plugins.auth;
 
 import java.util.Set;
 
 
Common PrivilegedAction used by classes in this package.

Author(s):
Scott.Stark@jboss.org
Anil.Saldhana@redhat.com
Version:
$Revision: 65313 $
 
 {
    private static class ToStringSubjectAction implements PrivilegedAction<String>
    {
       Subject subject;
       ToStringSubjectAction(Subject subject)
       {
          this. = subject;
       }
       public String run()
       {
          StringBuffer tmp = new StringBuffer();
          tmp.append("Subject(");
          tmp.append(System.identityHashCode());
          tmp.append(").principals=");
          Iterator<Principalprincipals = .getPrincipals().iterator();
          whileprincipals.hasNext() )
          {
             Object p = principals.next();
             Class<?> c = p.getClass();
             tmp.append(c.getName());
             tmp.append('@');
             tmp.append(System.identityHashCode(c));
             tmp.append('(');
             tmp.append(p);
             tmp.append(')');
          }
          return tmp.toString();
       }
    }
 
    private static class GetSubjectAction implements PrivilegedExceptionAction<Subject>
    {
       static PrivilegedExceptionAction<SubjectACTION = new GetSubjectAction();
       public Subject run() throws PolicyContextException
       {
          return (Subject) PolicyContext.getContext(.);  
       }
    }
 
    private static class CopySubjectAction implements PrivilegedAction<Object>
    {
       Subject fromSubject;
       Subject toSubject;
       boolean setReadOnly;
       boolean deepCopy;
       
       CopySubjectAction(Subject fromSubjectSubject toSubjectboolean setReadOnly)
      {
         this. = fromSubject;
         this. = toSubject;
         this. = setReadOnly;
      }
      public void setDeepCopy(boolean flag)
      {
         this. = flag;
      }
      
      @SuppressWarnings("unchecked")
      public Object run()
      {
         Set principals = .getPrincipals();
         Set principals2 = .getPrincipals();
         Iterator<Principaliter = principals.iterator();
         whileiter.hasNext() )
            principals2.add(getCloneIfNeeded(iter.next()));  
         Set privateCreds = .getPrivateCredentials();
         Set privateCreds2 = .getPrivateCredentials();
         iter = privateCreds.iterator();
         whileiter.hasNext() )
            privateCreds2.add(getCloneIfNeeded(iter.next()));
         Set publicCreds = .getPublicCredentials();
         Set publicCreds2 = .getPublicCredentials();
         iter = publicCreds.iterator();
         whileiter.hasNext() )
            publicCreds2.add(getCloneIfNeeded(iter.next()));
         if == true )
            .setReadOnly();
         return null;
      }
      
      
Check if the deepCopy flag is ON && Object implements Cloneable and return cloned object
      private Object getCloneIfNeeded(Object obj)
      {
         Object clonedObject = null;
         if(this. && obj instanceof Cloneable)
         {
            Class<?> clazz = obj.getClass();
            try
            {
               Method cloneMethod = clazz.getMethod("clone", (Class[])null);
               clonedObject = cloneMethod.invoke(obj, (Object[])null);
   }
            catch (Exception e)
            {//Ignore non-cloneable issues 
            } 
         }
         if(clonedObject == null)
            clonedObject = obj;
         return clonedObject;
      }
   }
   private static class LoginContextAction implements PrivilegedExceptionAction<LoginContext>
   {
      Subject subject;
      LoginContextAction(String securityDomainSubject subject,
         CallbackHandler handler)
      {
         this. = securityDomain;
         this. = subject;
         this. = handler;
      }
      public LoginContext run() throws Exception
      {
         LoginContext lc = new LoginContext();
         return lc;
      }
   }
   private static class GetTCLAction implements PrivilegedAction<ClassLoader>
   {
      static PrivilegedAction<ClassLoaderACTION = new GetTCLAction();
      public ClassLoader run()
      {
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         return loader;
      }
   }
   private static class SetContextInfoAction implements PrivilegedAction<Object>
   {
      String key;
      Object value;
      SetContextInfoAction(String keyObject value)
      {
         this. = key;
         this. = value;
      }
      public Object run()
      {
         //Set it on the current security context also
         SecurityContext sc = SecurityContextAssociation.getSecurityContext();
         if(sc != null)
         {
            sc.getData().put(.toString(), );
         }
         return SecurityAssociation.setContextInfo();
      }
   }
   interface PrincipalInfoAction
   {
      {
         public void push(final Principal principalfinal Object credential,
            final Subject subjectfinal String securityDomain
         {
            AccessController.doPrivileged(
               new PrivilegedAction<Object>()
               {
                  public Object run()
                  {
                     SecurityContext sc = SecurityContextAssociation.getSecurityContext();
                     if(sc == null)
                     {
                        try
                        {
                           sc = SecurityContextFactory.createSecurityContext(principalcredential,
                                 subjectsecurityDomain);
                        }
                        catch (Exception e)
                        {
                           throw new RuntimeException(e);
                        }
                     }
                     SecurityContextAssociation.setSecurityContext(sc);
                     return null;
                  }
               }
            );
         }
         public void pop()
         {
            AccessController.doPrivileged(
               new PrivilegedAction<Object>()
               {
                  public Object run()
                  {
                     //SecurityAssociation.popSubjectContext();
                     SecurityContextAssociation.clearSecurityContext();
                     return null;
                  }
               }
            );
         }
      };
      {
         public void push(Principal principalObject credentialSubject subject,
               String securityDomain)
         {
            //SecurityAssociation.pushSubjectContext(subject, principal, credential);
            SecurityContext sc = SecurityContextAssociation.getSecurityContext();
            if(sc == null)
            {
               try
               {
                  sc = SecurityContextFactory.createSecurityContext(principalcredential,
                        subjectsecurityDomain);
               }
               catch (Exception e)
               {
                  throw new RuntimeException(e);
               }
            }
            else
            {
               sc.getUtil().createSubjectInfo(principalcredentialsubject); 
            }
            SecurityContextAssociation.setSecurityContext(sc); 
         }
         public void pop()
         {
            SecurityContextAssociation.clearSecurityContext();
         }
      };
      void push(Principal principalObject credentialSubject subjectString securityDomain);
      void pop();
   }
   {
      Subject subject = (Subject) AccessController.doPrivileged(.);
      return subject;
   }
   static void copySubject(Subject fromSubjectSubject toSubject)
   {
      copySubject(fromSubjecttoSubjectfalse);
   }
   static void copySubject(Subject fromSubjectSubject toSubjectboolean setReadOnly)
   {
      CopySubjectAction action = new CopySubjectAction(fromSubjecttoSubjectsetReadOnly);
      if( System.getSecurityManager() != null )
         AccessController.doPrivileged(action);
      else
         action.run();
   }
   static void copySubject(Subject fromSubjectSubject toSubjectboolean setReadOnly,
         boolean deepCopy)
   {
      CopySubjectAction action = new CopySubjectAction(fromSubjecttoSubjectsetReadOnly);
      action.setDeepCopy(deepCopy);
      if( System.getSecurityManager() != null )
         AccessController.doPrivileged(action);
      else
         action.run();
   }
   static LoginContext createLoginContext(String securityDomainSubject subject,
      CallbackHandler handler)
      throws LoginException
   {
      LoginContextAction action = new LoginContextAction(securityDomainsubjecthandler);
      try
      {
         LoginContext lc = (LoginContext) AccessController.doPrivileged(action);
         return lc;
      }
      catch(PrivilegedActionException e)
      {
         Exception ex = e.getException();
         ifex instanceof LoginException )
            throw (LoginExceptionex;
         else
            throw new LoginException(ex.getMessage());
      }
   } 
   
   {
      ClassLoader loader = (ClassLoader) AccessController.doPrivileged(.);
      return loader;
   }
   static Object setContextInfo(String keyObject value)
   {
      SetContextInfoAction action = new SetContextInfoAction(keyvalue);
      Object prevInfo = AccessController.doPrivileged(action);
      return prevInfo;
   }
   static String toString(Subject subject)
   {
      ToStringSubjectAction action = new ToStringSubjectAction(subject);
      String info = (String) AccessController.doPrivileged(action);
      return info;
   }
New to GrepCode? Check out our FAQ X