Fixed "Give creator more rights"
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:28 +0200
changeset 71a9bd40166b60
parent 70 cba5d0a9e9f0
child 72 7b2a5d8f5e6d
Fixed "Give creator more rights"
samples/privilegedcreator/nbproject/project.properties
samples/privilegedcreator/src/api/Executors.java
samples/privilegedcreator/src/api/Mutex.java
samples/privilegedcreator/test/accessprotectedmethod/AccessWriteLockTest.java
samples/privilegedcreator/test/accessprotectedmethod/MyDocument.java
samples/privilegedcreator/test/api/MutexTest.java
     1.1 --- a/samples/privilegedcreator/nbproject/project.properties	Sat Jun 14 09:53:25 2008 +0200
     1.2 +++ b/samples/privilegedcreator/nbproject/project.properties	Sat Jun 14 09:53:28 2008 +0200
     1.3 @@ -1,3 +1,5 @@
     1.4 +application.title=privilegedcreator
     1.5 +application.vendor=jarda
     1.6  build.classes.dir=${build.dir}/classes
     1.7  build.classes.excludes=**/*.java,**/*.form
     1.8  # This directory is removed when the project is cleaned:
     1.9 @@ -16,6 +18,7 @@
    1.10  dist.jar=${dist.dir}/privilegedcreator.jar
    1.11  dist.javadoc.dir=${dist.dir}/javadoc
    1.12  excludes=
    1.13 +file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar
    1.14  includes=**
    1.15  jar.compress=false
    1.16  javac.classpath=
    1.17 @@ -27,8 +30,7 @@
    1.18  javac.test.classpath=\
    1.19      ${javac.classpath}:\
    1.20      ${build.classes.dir}:\
    1.21 -    ${libs.junit.classpath}:\
    1.22 -    ${libs.junit_4.classpath}
    1.23 +    ${file.reference.junit-4.4.jar}
    1.24  javadoc.additionalparam=
    1.25  javadoc.author=false
    1.26  javadoc.encoding=${source.encoding}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/privilegedcreator/src/api/Executors.java	Sat Jun 14 09:53:28 2008 +0200
     2.3 @@ -0,0 +1,60 @@
     2.4 +
     2.5 +package api;
     2.6 +
     2.7 +import java.util.concurrent.Executor;
     2.8 +
     2.9 +/**
    2.10 + *
    2.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.12 + */
    2.13 +public final class Executors {
    2.14 +    /** let's prefer factory methods */
    2.15 +    private Executors() {
    2.16 +    }
    2.17 +    
    2.18 +    
    2.19 +    public static Executor create() {
    2.20 +        return new Simple();
    2.21 +    }
    2.22 +    
    2.23 +    public static Executor create(boolean fair) {
    2.24 +        Configuration conf = new Configuration();
    2.25 +        conf.setFair(fair);
    2.26 +        return new Fair(conf);
    2.27 +    }
    2.28 +
    2.29 +    // BEGIN: design.less.privileged
    2.30 +    public static Executor create(Configuration config) {
    2.31 +        return new Fair(config);
    2.32 +    }
    2.33 +    
    2.34 +    public static final class Configuration {
    2.35 +        boolean fair;
    2.36 +        int maxWaiters = -1;
    2.37 +        
    2.38 +        public void setFair(boolean fair) {
    2.39 +            this.fair = fair;
    2.40 +        }
    2.41 +        public void setMaxWaiters(int max) {
    2.42 +            this.maxWaiters = max;
    2.43 +        }
    2.44 +    }
    2.45 +    // END: design.less.privileged
    2.46 +    
    2.47 +    private static final class Simple implements Executor {
    2.48 +        public synchronized void execute(Runnable command) {
    2.49 +            command.run();
    2.50 +        }
    2.51 +    }
    2.52 +    private static final class Fair implements Executor {
    2.53 +        private final Configuration conf;
    2.54 +        
    2.55 +        public Fair(Configuration conf) {
    2.56 +            this.conf = conf;
    2.57 +        }
    2.58 +        
    2.59 +        public void execute(Runnable command) {
    2.60 +            // TBD
    2.61 +        }
    2.62 +    }
    2.63 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/privilegedcreator/src/api/Mutex.java	Sat Jun 14 09:53:28 2008 +0200
     3.3 @@ -0,0 +1,51 @@
     3.4 +/*
     3.5 + * To change this template, choose Tools | Templates
     3.6 + * and open the template in the editor.
     3.7 + */
     3.8 +
     3.9 +package api;
    3.10 +
    3.11 +import java.util.concurrent.locks.Lock;
    3.12 +import java.util.concurrent.locks.ReentrantLock;
    3.13 +
    3.14 +/**
    3.15 + *
    3.16 + * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
    3.17 + */
    3.18 +public class Mutex {
    3.19 +    Lock lock = new ReentrantLock();
    3.20 +    
    3.21 +    public Mutex() {
    3.22 +    }
    3.23 +
    3.24 +    
    3.25 +    public void readAccess(Runnable r) {
    3.26 +        try {
    3.27 +            lock.lock();
    3.28 +            r.run();
    3.29 +        } finally {
    3.30 +            lock.unlock();
    3.31 +        }
    3.32 +    }
    3.33 +    
    3.34 +
    3.35 +    
    3.36 +    public Mutex(Privileged privileged) {
    3.37 +        if (privileged.mutex != null) {
    3.38 +            throw new IllegalStateException();
    3.39 +        }
    3.40 +        privileged.mutex = this;
    3.41 +    }
    3.42 +    
    3.43 +    public static final class Privileged {
    3.44 +        private Mutex mutex;
    3.45 +        
    3.46 +        public void enterReadAccess() {
    3.47 +            mutex.lock.lock();
    3.48 +        }
    3.49 +        
    3.50 +        public void exitReadAccess() {
    3.51 +            mutex.lock.unlock();
    3.52 +        }
    3.53 +    }
    3.54 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/privilegedcreator/test/accessprotectedmethod/AccessWriteLockTest.java	Sat Jun 14 09:53:28 2008 +0200
     4.3 @@ -0,0 +1,17 @@
     4.4 +package accessprotectedmethod;
     4.5 +
     4.6 +
     4.7 +public class AccessWriteLockTest {
     4.8 +    
     4.9 +    @org.junit.Test
    4.10 +    public void testCallToMyDocument() {
    4.11 +        MyDocument doc = new MyDocument();
    4.12 +        
    4.13 +        // following line does not compile as
    4.14 +        // writeLock() has protected access in AbstractDocument
    4.15 +        // doc.writeLock();
    4.16 +        
    4.17 +        doc.writeLockAccess();
    4.18 +    }
    4.19 +    
    4.20 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/privilegedcreator/test/accessprotectedmethod/MyDocument.java	Sat Jun 14 09:53:28 2008 +0200
     5.3 @@ -0,0 +1,25 @@
     5.4 +package accessprotectedmethod;
     5.5 +
     5.6 +import javax.swing.text.AbstractDocument;
     5.7 +import javax.swing.text.Element;
     5.8 +import javax.swing.text.StringContent;
     5.9 +
    5.10 +// BEGIN: public.accessor
    5.11 +public class MyDocument  extends AbstractDocument {
    5.12 +    public MyDocument() {super(new StringContent());}
    5.13 +    
    5.14 +    final void writeLockAccess() {
    5.15 +        writeLock();
    5.16 +    }
    5.17 +// END: public.accessor
    5.18 +
    5.19 +    @Override
    5.20 +    public Element getDefaultRootElement() {
    5.21 +        throw new UnsupportedOperationException("Not supported yet.");
    5.22 +    }
    5.23 +
    5.24 +    @Override
    5.25 +    public Element getParagraphElement(int pos) {
    5.26 +        throw new UnsupportedOperationException("Not supported yet.");
    5.27 +    }
    5.28 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/privilegedcreator/test/api/MutexTest.java	Sat Jun 14 09:53:28 2008 +0200
     6.3 @@ -0,0 +1,69 @@
     6.4 +package api;
     6.5 +
     6.6 +import org.junit.After;
     6.7 +import org.junit.AfterClass;
     6.8 +import org.junit.Before;
     6.9 +import org.junit.BeforeClass;
    6.10 +import org.junit.Test;
    6.11 +import static org.junit.Assert.*;
    6.12 +
    6.13 +public class MutexTest {
    6.14 +    // BEGIN: mutex.init
    6.15 +    private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged();
    6.16 +    public static final Mutex MUTEX = new Mutex(PRIVILEGED);
    6.17 +    // END: mutex.init
    6.18 +
    6.19 +    public MutexTest() {
    6.20 +    }
    6.21 +
    6.22 +    @BeforeClass
    6.23 +    public static void setUpClass() throws Exception {
    6.24 +    }
    6.25 +
    6.26 +    @AfterClass
    6.27 +    public static void tearDownClass() throws Exception {
    6.28 +    }
    6.29 +
    6.30 +    @Before
    6.31 +    public void setUp() {
    6.32 +    }
    6.33 +
    6.34 +    @After
    6.35 +    public void tearDown() {
    6.36 +    }
    6.37 +
    6.38 +    /**
    6.39 +     * Test of readAccess method, of class Mutex.
    6.40 +     */
    6.41 +    @Test
    6.42 +    public void readAccess() {
    6.43 +        class R implements Runnable {
    6.44 +            int cnt;
    6.45 +            
    6.46 +            public void run() {
    6.47 +                cnt++;
    6.48 +            }
    6.49 +        }
    6.50 +        R r = new R();
    6.51 +        Mutex instance = new Mutex();
    6.52 +        instance.readAccess(r);
    6.53 +        assertEquals("One call to runnable", 1, r.cnt);
    6.54 +    }
    6.55 +    
    6.56 +    @Test
    6.57 +    public void usePrivileged() {
    6.58 +        Mutex.Privileged lock = new Mutex.Privileged();
    6.59 +        Mutex mutex = new Mutex(lock);
    6.60 +
    6.61 +        // BEGIN: mutex.privileged
    6.62 +        lock.enterReadAccess();
    6.63 +        try {
    6.64 +          // do the operation
    6.65 +        } finally {
    6.66 +           lock.exitReadAccess();
    6.67 +        }
    6.68 +        // END: mutex.privileged
    6.69 +        
    6.70 +    }
    6.71 +
    6.72 +}
    6.73 \ No newline at end of file