Deploying Java Applets - Dynamic Linking Using Reflection
(Page 5 of 5 )
As an alternative, you can also dynamically load the JNLP classes using reflection as shown in this code excerpt from classJnlpProxyin packagecom.croftsoft.core.jnlp.
private static Object getBasicServiceObject ( )
//////////////////////////////////////////////////////////////////////
{
try
{
Class serviceManagerClass
= Class.forName ( "javax.jnlp.ServiceManager" );
Method lookupMethod = serviceManagerClass.getMethod (
"lookup", new Class [ ] { String.class } );
return lookupMethod.invoke (
null, new Object [ ] { "javax.jnlp.BasicService" } );
}
catch ( Exception ex )
{
return null;
}
}
ClassMethodis from core packagejava.lang.reflect.
public static boolean showDocument ( URL url )
//////////////////////////////////////////////////////////////////////
{
if ( basicServiceObject == null )
{
return false;
}
try
{
Method method = basicServiceClass.getMethod (
"showDocument", new Class [ ] { URL.class } );
Boolean resultBoolean = ( Boolean ) method.invoke (
basicServiceObject, new Object [ ] { url } );
return resultBoolean.booleanValue ( );
}
catch ( Exception ex )
{
ex.printStackTrace ( );
throw new RuntimeException ( ex.getMessage ( ) );
}
}
By saving a singleton static reference to theBasicServiceinstance retrieved, we can then use it within our own staticshowDocument()method. The example method returnsfalseif the client platform does not support JNLP, allowing the calling code to respond with a substitute behavior.
In my opinion, dynamic linking using custom interfaces instead of reflection makes for more comprehensible code. I have found that you almost never need reflection techniques if you keep this in mind.
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter two of Advanced Java Game Programming, written by David Wallace Croft (Apress; ISBN: 1590591232). Check it out today at your favorite bookstore. Buy this book now.
|
|