3.3 Tips for Creating Task Java Classes

Be careful when using static variables. Static variables are shared across all sessions, so every logged-in user will use them. If you want to save information that can be shared by multiple tasks, use the authentication context. Authentication context attributes can be set using the setAuthAttribute() method, and retrieved using the getAuthAttribute() method on the task context. The task context can be retrieved from the HttpServletRequest() with the getContext() method. You can also store information in local variables on the task.

When using authentication context attributes, prepend your key with your module ID to avoid overwriting keys of other plug-ins. For example:

 request.getContext().setAuthAttribute("acme.CurrentCount", "5");
 

When building a string with variables included, use the message formatting methods instead of concatenating. There is functionality in Java and JavaScript to do this correctly. In JavaScript use formatMessage(), found in eMFrameScripts. In Java use eMFrameUtils.formatMessage().

If you want certain tasks to be performed whenever there is a switch-over from one running task to another, or if you want to do cleanup when your task is released for garbage collection, override the release() method of the task that is currently running.

When catching Exceptions, use eMFrameUtils.setErrorMessage(Throwable t, TaskContext context) to report them to the user. Currently, many plug-ins catch an exception, pull out the message, then return it to the user. This may work in some cases, but generally is not enough information. Instead, catch the exception, pass it to setErrorMessage(t, context), and return false. GenErr must be the error template for this to work properly. This shows one of two things: If it is an SPIException it shows the NDS Error message with a link to get more details about the error. If it is another type of exception, it shows a general error message with a details link to show the exception class, the exception message, and a stack trace. This is preferable to showing just the error message because, for Java exceptions, this often does not make sense without the other information. For pages, throwing new PageException(t) provides similar functionality.

If your plug-in manages classes and attributes in eDirectory that are not part of the base schema, provide a graphic and translated names for these classes and attributes. To provide a graphic that shows up in the object selector, place a GIF file in the SDK_HOME/tomcat/webapps/nps/portal/modules/moduleID/images/dir directory. The GIF file must have the same name as the object class with all non-alphanumeric characters converted to underscores. For example, the graphic for the NDPS:Printer object is SDK_HOME/tomcat/webapps/nps/portal/modules/dev/images/dir/NDPS_Server_Domain.gif.

To provide translated names, create an XML file in the plugins directory that specifies what resource bundle and what attributes and classes it translates, then put values in that resource bundle for those translations. The keys for the translated class and attributes should use underscores instead of non-alphanumeric characters and should begin with ObjectType or Attribute.

Example XML file:

 <dir-translator>
 <resource-properties-file>FwResources</resource-properties-file>
 <object-type-name>AFP Server</object-type-name>
 <object-type-name>Alias</object-type-name>
 <object-type-name>NLS:Product Container</object-type-name>
    <object-type-name>NLS:License Certificate</object-type-name>
    <object-type-name>NLS:License Server</object-type-name>
    <attribute-name>L</attribute-name>
    <attribute-name>CN</attribute-name>
 <attribute-name>Full Name</attribute-name>
 <attribute-name>Surname</attribute-name>
 </dir-translator>
 

Example resource file:

 ObjectType.AFP_Server=AFP Server
 ObjectType.Alias=Alias
 ObjectType.NLS_Product_Container=License Product Container
 ObjectType.NLS_License_Certificate=License Certificate
 ObjectType.NLS_License_Server=License Service Provider
 Attribute.L=Location
 Attribute.CN=Common name
 Attribute.Full_Name=Full name
 Attribute.Surname=Surname
 

If your plug-in manages classes and attributes in eDirectory that are not part of the base schema, plug in to the iManager creator, deletor, move, and rename tasks. These allow you to plug in to the general create, delete, move and rename tasks and also allow you to make tasks that just create, delete, move or rename your specific object or set of objects. If you do not plug in this way, users cannot create your objects from the general create object task. For delete, rename, and move, it gives you the ability to do additional operations when the general tasks are used on your type of object. See Section 3.9, Extending the Object Management Tasks.

3.3.1 Encoding Data

You do not need to unencode parameters that are fetched from the HttpServletRequest object using the getParameter method. This is true without regard to the source of the parameter, whether using <href>, <action>, or <input> tags. The com.novell.emframe.dev.eMFrameUtils class provides urlEncode, urlDecode, toDisplay, and toScript methods, but we discourage encoding the data in your Java class unless there is no other way. You should perform all encoding in your JSP, as explained in Section 3.4.1, Encoding Data Using Tag Libraries.