# HG changeset patch # User Jaroslav Tulach # Date 1213430008 -7200 # Node ID a9bd40166b6019f69a597819393d34ca7a4bb277 # Parent cba5d0a9e9f09af110cc123e71dfd77650cd7802 Fixed "Give creator more rights" diff -r cba5d0a9e9f0 -r a9bd40166b60 samples/privilegedcreator/nbproject/project.properties --- a/samples/privilegedcreator/nbproject/project.properties Sat Jun 14 09:53:25 2008 +0200 +++ b/samples/privilegedcreator/nbproject/project.properties Sat Jun 14 09:53:28 2008 +0200 @@ -1,3 +1,5 @@ +application.title=privilegedcreator +application.vendor=jarda build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form # This directory is removed when the project is cleaned: @@ -16,6 +18,7 @@ dist.jar=${dist.dir}/privilegedcreator.jar dist.javadoc.dir=${dist.dir}/javadoc excludes= +file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar includes=** jar.compress=false javac.classpath= @@ -27,8 +30,7 @@ javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ - ${libs.junit.classpath}:\ - ${libs.junit_4.classpath} + ${file.reference.junit-4.4.jar} javadoc.additionalparam= javadoc.author=false javadoc.encoding=${source.encoding} diff -r cba5d0a9e9f0 -r a9bd40166b60 samples/privilegedcreator/src/api/Executors.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/src/api/Executors.java Sat Jun 14 09:53:28 2008 +0200 @@ -0,0 +1,60 @@ + +package api; + +import java.util.concurrent.Executor; + +/** + * + * @author Jaroslav Tulach + */ +public final class Executors { + /** let's prefer factory methods */ + private Executors() { + } + + + public static Executor create() { + return new Simple(); + } + + public static Executor create(boolean fair) { + Configuration conf = new Configuration(); + conf.setFair(fair); + return new Fair(conf); + } + + // BEGIN: design.less.privileged + public static Executor create(Configuration config) { + return new Fair(config); + } + + public static final class Configuration { + boolean fair; + int maxWaiters = -1; + + public void setFair(boolean fair) { + this.fair = fair; + } + public void setMaxWaiters(int max) { + this.maxWaiters = max; + } + } + // END: design.less.privileged + + private static final class Simple implements Executor { + public synchronized void execute(Runnable command) { + command.run(); + } + } + private static final class Fair implements Executor { + private final Configuration conf; + + public Fair(Configuration conf) { + this.conf = conf; + } + + public void execute(Runnable command) { + // TBD + } + } +} diff -r cba5d0a9e9f0 -r a9bd40166b60 samples/privilegedcreator/src/api/Mutex.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/src/api/Mutex.java Sat Jun 14 09:53:28 2008 +0200 @@ -0,0 +1,51 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package api; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * + * @author Jaroslav Tulach + */ +public class Mutex { + Lock lock = new ReentrantLock(); + + public Mutex() { + } + + + public void readAccess(Runnable r) { + try { + lock.lock(); + r.run(); + } finally { + lock.unlock(); + } + } + + + + public Mutex(Privileged privileged) { + if (privileged.mutex != null) { + throw new IllegalStateException(); + } + privileged.mutex = this; + } + + public static final class Privileged { + private Mutex mutex; + + public void enterReadAccess() { + mutex.lock.lock(); + } + + public void exitReadAccess() { + mutex.lock.unlock(); + } + } +} diff -r cba5d0a9e9f0 -r a9bd40166b60 samples/privilegedcreator/test/accessprotectedmethod/AccessWriteLockTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/test/accessprotectedmethod/AccessWriteLockTest.java Sat Jun 14 09:53:28 2008 +0200 @@ -0,0 +1,17 @@ +package accessprotectedmethod; + + +public class AccessWriteLockTest { + + @org.junit.Test + public void testCallToMyDocument() { + MyDocument doc = new MyDocument(); + + // following line does not compile as + // writeLock() has protected access in AbstractDocument + // doc.writeLock(); + + doc.writeLockAccess(); + } + +} diff -r cba5d0a9e9f0 -r a9bd40166b60 samples/privilegedcreator/test/accessprotectedmethod/MyDocument.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/test/accessprotectedmethod/MyDocument.java Sat Jun 14 09:53:28 2008 +0200 @@ -0,0 +1,25 @@ +package accessprotectedmethod; + +import javax.swing.text.AbstractDocument; +import javax.swing.text.Element; +import javax.swing.text.StringContent; + +// BEGIN: public.accessor +public class MyDocument extends AbstractDocument { + public MyDocument() {super(new StringContent());} + + final void writeLockAccess() { + writeLock(); + } +// END: public.accessor + + @Override + public Element getDefaultRootElement() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Element getParagraphElement(int pos) { + throw new UnsupportedOperationException("Not supported yet."); + } +} diff -r cba5d0a9e9f0 -r a9bd40166b60 samples/privilegedcreator/test/api/MutexTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/test/api/MutexTest.java Sat Jun 14 09:53:28 2008 +0200 @@ -0,0 +1,69 @@ +package api; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +public class MutexTest { + // BEGIN: mutex.init + private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged(); + public static final Mutex MUTEX = new Mutex(PRIVILEGED); + // END: mutex.init + + public MutexTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of readAccess method, of class Mutex. + */ + @Test + public void readAccess() { + class R implements Runnable { + int cnt; + + public void run() { + cnt++; + } + } + R r = new R(); + Mutex instance = new Mutex(); + instance.readAccess(r); + assertEquals("One call to runnable", 1, r.cnt); + } + + @Test + public void usePrivileged() { + Mutex.Privileged lock = new Mutex.Privileged(); + Mutex mutex = new Mutex(lock); + + // BEGIN: mutex.privileged + lock.enterReadAccess(); + try { + // do the operation + } finally { + lock.exitReadAccess(); + } + // END: mutex.privileged + + } + +} \ No newline at end of file