- Code: Select all
public class DataFactory
{
static {
setProperties();
initialize();
}
private static Server serv = null;
public static boolean initialize() {
/*
* RMI Plug-in created a security.policy file in the
* project root that grants ALL permissions.
*/
if( System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = ApplicationContext.DbServerName();
String URL = ApplicationContext.DbServerURL();
try {
serv = (Server)Naming.lookup( URL+name );
consoleOut("DataFactory is connected to DbServer.");
return true;
}
catch( MalformedURLException ex ) { // fm Naming
ex.printStackTrace();
return false;
}
catch( NotBoundException ex ) { // fm Naming
ex.printStackTrace();
return false;
}
catch( RemoteException ex ) { // fm Naming or call to Server
ex.printStackTrace();
return false;
}
}
public static final Timestamp getCurrentTime ( ) {
try {
return serv.getCurrentTime ();
}
catch( RemoteException ex ) {
ex.printStackTrace();
return null;
}
}
...
}
This class was designed to isolate the 100 or so JUnits and clients from knowing anything about remote access or security. Here's a typical one:
- Code: Select all
public class UnitTest {
static DataFactory Fac = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
/* Class reference runs static initializer connecting to remote */
Fac = ApplicationContext.getDataFactory();
}
Data data = null;
@Before
public void setUp() throws Exception {
this.data = Fac.getData( key );
}
}
The problem is that I can't run any of them now without declaring it a RMI Application or RMI JUnit. If I don't I get this exception
- Code: Select all
java.security.AccessControlException: access denied (java.lang.RuntimePermission accessDeclaredMembers)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:264)
at java.security.AccessController.checkPermission(AccessController.java:427)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkMemberAccess(SecurityManager.java:1662)
at java.lang.Class.checkMemberAccess(Class.java:2125)
at java.lang.Class.getDeclaredMethods(Class.java:1762)
at org.junit.internal.runners.TestIntrospector.getTestMethods(TestIntrospector.java:26)
at org.junit.internal.runners.BeforeAndAfterRunner.runAfters(BeforeAndAfterRunner.java:62)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:37)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
apparently caused by ignorance of the security.policy file that would be specified on the RMI VM Properties tab in the Run Dialog.
Surely there must be a way to do whatever is needed programmatically. What do I need to do? Modify the default security.policy file? Or is there a way to load the replacement from within DataFactory or ApplicationContext?
Edit: fixed the -Djava.security.policy problem. It had embedded spaces.
So what should I do, define that System property at runtime? What about codebase?