Implementing Interfaces and Declaring Classes at Runtime

One of the fundamental benefits of an object-oriented language such as Java is the ability to design applications around Interfaces. Interaction with a class is done only through the known set of methods in the interface. Calling functions do not need to know the implementation, just the method signature. Interfaces are often used within a software application or framework to allow for flexibility and re-use, but the use of Interfaces can be even more powerful when you allow the implementations to be defined at runtime. FlexSnap:SI’s “Content Handler API” is an example of such a case.

Snowbound Software’s FlexSnap:SI Applet and Servlet based product comes straight out of the box with functionality to view images stored in a folder on your server. However, often times these images sit on another server, or in a content repository. FlexSnap:SI allows you to customize the retrieval of documents via a Java Interface named FlexSnapSIContentHandler .

The Content Handler class is defined in the applications web.xml as follows:

<init-param> 

<param-name> 

contentHandlerClass 

</param-name> 

<param-value> 

com.mycompany.MyContentHandler 

</param-value> 

</init-param> 

The FlexSnap code will instantiate your class as follows:

String contentClassName = config .getInitParameter( PARAM_CONTENT_CLASS ); 

Class contentClass = Class.forName( contentClassName ); 

FlexSnapSIContentHandlerInterface gContentRetriever = (FlexSnapSIContentHandlerInterface) contentClass 

.getConstructors()[0].newInstance( new Object[] {}); 

As long as your class implements the following set of methods, you are all set to go.

Method Summary

void

deleteAnnotation (String clientInstanceId, String documentKey, String annotationKey)

Called when the client has requested to delete the specified annotation layer.

byte[]

getAnnotationContent (String clientInstanceId, String documentKey, String annotationKey)

Returns the content for the specified annotation key in the form of a byte array.

String[]

getAnnotationNames (String clientInstanceId, String documentKey)

Returns an array of annotation object names for the specified clientInstance and documentKey array.

byte[]

getBookmarkContent (String clientInstanceId, String documentKey)

Returns the bookmark xml content for the specified documentKey in the form of a byte array.

byte[]

getDocumentContent (String clientInstanceId, String documentKey)

Returns the content for the specified content key in the form of a byte array.

boolean

hasAnnotations (String clientInstanceId, String documentKey)

Returns true if there is annotation content associated with the specified document.

void

init (ServletConfig config)

Performs any necessary configuration tasks

 

ContentHandlerResult

publishDocument(HttpServletRequest request, String clientInstanceId, String documentKey, byte[] data, int status)

This method is used when the File>Publish menu choise is invoked.

ContentHandlerResult

saveAnnotationContent(HttpServletRequest request, String clientInstanceId,String documentKey, String annotationKey, byte[] data)

This method gets called when updated annotation data for the specified annotation file is ready to be saved.

 

ContentHandlerResult

saveBookmarkContent(HttpServletRequest request, String clientInstanceId,String documentKey, byte[] data)

This method is called when bookmark data is saved.

 

ContentHandlerResult

saveDocumentComponents(HttpServletRequest request, String clientInstanceId,String documentKey, byte[] data, com.snowbound.snapserv.servlet.AnnotationLayer[] annotations, byte[] bookmark)

This method saves the document as well as the annotations and bookmarks associated with the document.

 

ContentHandlerResult

saveDocumentContent(HttpServletRequest request, String clientInstanceId,String documentKey, byte[] data)

This method is called when document data is saved.

 

ContentHandlerResult

sendCocumentContent(HttpServletRequest request, String clientInstanceId,String documentKey, byte[] data)

This method is called when document data is sent.

 
   

 

Category:

Online Demo