Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Taming Tiger, Part 3

Decorate your code with Java annotations

  • Print
  • Feedback

Page 3 of 4

Finally, to see everything in action, let's add a main() method to the AnnotationsTest class. The main() method creates two threads. Each thread simulates a different user with a different role and calls all four public instance methods in the AnnotationsTest class. The main method is shown below:

public static void main(String[] args)
    {
        try
        {
            Thread t1 = new Thread(new Test1());
            t1.start();
            t1.join();
            Thread t2 = new Thread(new Test2());
            t2.start();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


And here are the Test1 and Test2 classes used by the main method:

class Test1 implements Runnable
{
    public void run() 
    {
         // Add the "HR" role for this user 
        SecurityBlanket.addPermission("HR");
        try
        {
            AnnotationsTest test = new AnnotationsTest();
            try
            {
                test.unRestrictedMethod("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
            try
            {
                test.fullyRestrictedMethod("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
            try
            {
                test.partiallyRestrictedMethod("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
            try
            {
                test.partiallyRestrictedMethod2("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }
        finally
        {
            SecurityBlanket.removePermission();
        }
    }
}
class Test2 implements Runnable
{
    public void run() 
    {        
        // Add the "Manager" role for this user
        SecurityBlanket.addPermission("Manager");
        try
        {
            AnnotationsTest test = new AnnotationsTest();
            try
            {
                test.unRestrictedMethod("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
            try
            {
                test.fullyRestrictedMethod("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
            try
            {
                test.partiallyRestrictedMethod("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
            try
            {
                test.partiallyRestrictedMethod2("Hi");
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }
        finally
        {
            SecurityBlanket.removePermission();
        }
    }
}


When you run the AnnotationsTest class, you will see the following output:

Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::<init>
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::unRestrictedMethod
Message from unRestrictedMethod: Hi
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::fullyRestrictedMethod
Unauthorized access to AnnotationsTest::fullyRestrictedMethod
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::partiallyRestrictedMethod
Unauthorized access to AnnotationsTest::partiallyRestrictedMethod
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::partiallyRestrictedMethod2
Message from partiallyRestrictedMethod2: Hi
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::<init>
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::unRestrictedMethod
Message from unRestrictedMethod: Hi
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::fullyRestrictedMethod
Unauthorized access to AnnotationsTest::fullyRestrictedMethod
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::partiallyRestrictedMethod
Message from partiallyRestrictedMethod: Hi
Mar 20, 2004 4:46:55 PM SecurityBlanket checkPermission
INFO: Checking security access to AnnotationsTest::partiallyRestrictedMethod2
Message from partiallyRestrictedMethod2: Hi


Observe the following in the output:

  • Print
  • Feedback

Resources