1

I am pushing my own test event queue over the System eventqueue. And in TestEQueue I have over loaded the dispatchEvent method with one call to super.dispatchEvent

      TestEQueue mytestqueue = new TestEQueue();
      Toolkit.getDefaultToolkit().getSystemEventQueue().push(TestEQueue);

But for some reason dispatching in the new TestQueue fails with AccessControlException. Where as the same event is successfully dispatched in the main program without TestEQueue.

How can this be possible as both the queues will running in the same thread group? How can I debug this issue? This is part of a very large test codebase, so I am not able to copy the functional code. Can this be related to security manager?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sirish
  • 9,183
  • 22
  • 72
  • 107
  • the api doc is for reading :-) from getSystemEventQueue(): _throws if a security manager exists and its java.lang.SecurityManager.checkAwtEventQueueAccess method denies access to the EventQueue_ – kleopatra Sep 16 '11 at 07:30
  • this call succeeds without exception – Sirish Sep 16 '11 at 18:12

2 Answers2

1

nobody know how do you built your own test event queue over the System eventqueue, maybe you miss there invokeAndWait,

useful infos and here

just my curiosity, if your test ends with success, then please test that with SwingUtilities.invokeAndWait, if there some differencies (awaiting nothing), and I marked your thread for notifying any changes :-)

this code should be works for Testing purposes,

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;

public class QueueTest {

    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
        eventQueue.push(new MyEventQueue());

        EventQueue.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                System.out.println("Run");
            }
        });
    }

    private static class MyEventQueue extends EventQueue {

        @Override
        public void postEvent(AWTEvent theEvent) {
            System.out.println("Event Posted");
            super.postEvent(theEvent);
        }
    }

    private QueueTest() {
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • In my case I am not triggering any UI processing from main thread, I am adding a new menu and simulated menu key press and release.My concern whether the events triggered because of this actions which are processed in EDT of new queue is having les permissions than original EDT – Sirish Sep 16 '11 at 03:41
  • I have tested printing the access permissions for both main dispatch thread and my test event queue EDT permissions. They differ.Why do they differ, they belong same thread group – Sirish Sep 16 '11 at 05:31
  • @siri no idea, could you edit your post with rellated code :-), maybe will be lots of fun – mKorbel Sep 16 '11 at 06:02
  • i was wrong permissions for both original EDT and TestEQueue are same... but still checkPermission call fails – Sirish Sep 18 '11 at 08:24
  • I (still) ensure that you cann't multiplayed EventQueue, one EDT Queue that's came from Swing package(s), just replace/clear/add/waiting only one Thread, not sure but from Java5 not possilble to somehow to playing with single Swing Threading, there are lots of promises that in Java7 will be implemented multi Queue, :-) I can't interesting somehow about that until Java7_010 -15upd, then will be Java7 stable and interesting for me, :-) – mKorbel Sep 18 '11 at 09:00
1

Note that push() replaces the existing EventQueue; it doesn't add a new queue. I think the premise of your question may be incorrect. See also this Q&A.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • yes you are right, but my doubt was related to whether events dispatched from this new queue's event dispatch thread have different access permissions from that of original queue's EDT. – Sirish Sep 16 '11 at 03:38
  • Sorry, I can't reproduce the effect you describe. – trashgod Sep 16 '11 at 04:45
  • I think that's not possible, @trashgod thanks for final result, my empty head, +1 – mKorbel Sep 16 '11 at 04:57
  • @trashgod I have checked the permissions they are same.. but still AccessControlException is thrown only from new pushed queue... I am not to debug the reason for this?Any Help? – Sirish Sep 16 '11 at 09:15
  • You could look for [Methods in `java.security` that throw `AccessControlException`](http://download.oracle.com/javase/7/docs/api/java/security/class-use/AccessControlException.html) being called in your code. – trashgod Sep 16 '11 at 14:29
  • @trashgo it is calling checkpermission of AccessControlContext not directly but by one of the API's which is trying to access free space on disk. I know that it requires permissions for this. But I am not able to understand reason why default event queue EDT has access but not newly pushed event queue, they both should have same access permissions isnt it? – Sirish Sep 17 '11 at 10:50
  • One would think so. Is this an unsigned applet or [tag:java-web-start] `` problem? – trashgod Sep 17 '11 at 12:02
  • @trashgod they are signed apps. – Sirish Sep 17 '11 at 18:09
  • A signed [tag:java-web-start] application with ` `? – trashgod Sep 17 '11 at 18:39
  • sorry i didn't give full info...its a signed jar file which is loaded at runtime... – Sirish Sep 18 '11 at 07:55
  • Sorry, I still don't understand how `TestEQueue` is being loaded; see also [`getSystemEventQueue()`](http://download.oracle.com/javase/7/docs/api/java/awt/Toolkit.html#getSystemEventQueue%28%29). – trashgod Sep 18 '11 at 08:07
  • Toolkit.getDefaultToolkit().getSystemEventQueue().push(TestEQueue) replaces the current queue with new queue. It is not exactly replacing, but any event that occurs on the original queue is forwarded to the new TestEQueue .This allows developers filtering certain events.Only thing we have to do is override dispatchEvent in the new TestEQueue – Sirish Sep 18 '11 at 08:14
  • I'm guessing that `getSystemEventQueue()` throws a `SecurityException`. Is `TestEQueue` loaded by different class loader? – trashgod Sep 18 '11 at 08:42
  • pushing event queue does not throw any exception it works fine. Exception is thrown while accessing file system attributes in checkpermission function call. I'm not much of the class loader, but the permission is checked for all methods present in current call stack, I dint any class related to class loader. Anyway will check once – Sirish Sep 18 '11 at 09:23