Brian's Waste of Time

Thu, 13 May 2004

LazyWeb: Advice on Started Threads

Code shows the problem best, so... Given the following sample code:

    public void testSubjectFollowsSpawnedThreads()
    {
        // Never allow access to teacher
        rp.addRules(Teacher.class, new Rules()
        {
            public boolean allow(Object subject, Object object)
            {
                return false;
            }
        });
        Teacher teacher = new Teacher("Mr. Brown");
        final ClassRoom english = new ClassRoom(teacher);

        filter.setSubject(new Object());
        // Verify subject set on current thread
        assertNull(english.getTeacher());

        final boolean [] blocked = {false};
        final boolean [] finished = {false};
        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                blocked[0] = (english.getTeacher() == null);
                finished[0] = true;
            }
        });
        thread.start();
        while (!finished[0])
        {
            Thread.yield();
        }
        assertTrue(blocked[0]);
    }

I am trying to define a pointcut in AspectJ to match the beginning of the execution of the Runnable associated with thread. I need to be able to access some static context in the spawning thread and pass it into the advice (the current security subject). I have tried a couple things and failed to get it right =( I am seriously thinking that a static thread-local subject may not be the best way of specifying the subject, but I don't have a better one. Any thoughts on pointcut definition?

1 writebacks [/src/java/aop] permanent link