Because the content of a CodeModule object can consist of class or JAR files, the code tests for the type of Java module to set the ContentType property on the ContentTransfer object. You must check in the CodeModule object; if you do not, it is stored as a reservation (in progress) version of a CodeModule object, which results in an exception when you attempt to create the Action subobject.
Also included in the examples is optional code that demonstrates creating an Action subobject in the same procedure that creates the CodeModule object. Such a procedure might be useful in an event action creation wizard that gives users the option to create the code module. If you code a procedure that creates both objects, ensure that you trap for a potential exception in creating the Action subobject and delete the previously created CodeModule object in the catch block. If you do not, and if an exception occurs, then a copy of the first CodeModule object is created when the code is rerun. Potential errors that can throw a creation exception are failing to check in a CodeModule object or setting the Action subobject's ProgId property to the wrong value.
Java Example
...
// Create a File object for the JAR containing the event handlers.
File handlerJar = new File("C:\\EclipseWorkspace\\EventHandlers\\EventHandlers.jar");
// non-Windows: File handlerJar = new File("/EclipseWorkspace/EventHandlers/EventHandlers.jar");
// Get object for existing folder where JAR will be stored in object store.
Folder folder=Factory.Folder.fetchInstance(os, "/CodeModules", null);
// Create ContentTransfer object from JAR content.
ContentElementList contentList = Factory.ContentTransfer.createList();
ContentTransfer ctNew;
FileInputStream fileIS;
ctNew = Factory.ContentTransfer.createInstance();
fileIS = new FileInputStream(handlerJar.getAbsolutePath());
ctNew.setCaptureSource(fileIS);
// Set mime type based on whether the Java module is a class or JAR.
String fileName = handlerJar.getName();
if (fileName.endsWith(".class"))
{
ctNew.set_ContentType("application/java-byte-code");
}
else if (fileName.endsWith(".jar"))
{
ctNew.set_ContentType("application/java-archive");
}
// Add content element to list, which will be added to CodeModule object (below).
contentList.add(ctNew);
// Create CodeModule object.
CodeModule newCM = Factory.CodeModule.createInstance(os, "CodeModule");
// Set DocumentTitle property.
String propertyName = "DocumentTitle";
newCM.getProperties().putValue(propertyName, "Java Event Handlers");
// Add content element to CodeModule object.
newCM.set_ContentElements(contentList);
// Check in CodeModule object.
newCM.checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
newCM.save(RefreshMode.REFRESH);
// File CodeModule object and save.
DynamicReferentialContainmentRelationship drcr
= (DynamicReferentialContainmentRelationship)folder.file((IndependentlyPersistableObject)newCM,
AutoUniqueName.AUTO_UNIQUE,
"Java Event Handlers",
DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
drcr.save(RefreshMode.NO_REFRESH);
//////////////////////////////////////////////
// The following code is optional. It demonstrates creating an Action subobject,
// in this case, EventAction, with exception handling code.
try
{
EventAction eventAction = Factory.EventAction.createInstance(os, null);
eventAction.set_ProgId("RenameActionHandler");
eventAction.set_CodeModule(newCM);
eventAction.set_DisplayName("EventActionWithCodeModule");
eventAction.save(RefreshMode.REFRESH);
}
catch (Exception e)
{
System.out.println("EventAction creation failed: " + e.getMessage());
newCM.delete();
newCM.save(RefreshMode.REFRESH);
}
C# Example
...
// Create a FileInfo object for the JAR containing the event handlers.
FileInfo handlerJar = new FileInfo (@"C:\\EclipseWorkspace\\EventHandlers\\EventHandlers.jar");
// Get object for existing folder where JAR will be stored in object store.
IFolder folder = Factory.Folder.FetchInstance(os, "/CodeModules", null);
// Create ContentTransfer object from JAR content.
IContentElementList contentList = Factory.ContentTransfer.CreateList();
IContentTransfer ctNew;
FileStream fileS = handlerJar.OpenRead();
ctNew = Factory.ContentTransfer.CreateInstance();
ctNew.SetCaptureSource(fileS);
//Set mime type based on whether the Java module is a class or JAR.
string filename = handlerJar.Name;
if (filename.EndsWith(".class"))
{
ctNew.ContentType = "application/java-byte-code";
}
else if (filename.EndsWith(".jar"))
{
ctNew.ContentType = "application/java-archive";
}
// Add content element to list, which will be added to CodeModule object (below).
contentList.Add(ctNew);
// Create CodeModule object.
ICodeModule newCM = Factory.CodeModule.CreateInstance(os, "CodeModule");
// Set DocumentTitle property.
newCM.Properties["DocumentTitle"] = "Java Event Handlers");
// Add content element to CodeModule object.
newCM.ContentElements = contentList;
// Check in CodeModule object.
newCM.Checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
newCM.Save(RefreshMode.REFRESH);
// File CodeModule object and save.
IDynamicReferentialContainmentRelationship drcr
= (IDynamicReferentialContainmentRelationship) folder.File((IIndependentlyPersistableObject)newCM,
AutoUniqueName.AUTO_UNIQUE,
"Java Event Handlers",
DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
drcr.Save(RefreshMode.NO_REFRESH);
//////////////////////////////////////////////
// The following code is optional. It demonstrates creating an IAction subobject,
// in this case, IEventAction, with exception handling code.
try
{
IEventAction eventAction = Factory.EventAction.CreateInstance(os, null);
eventAction.ProgId = "RenameActionHandler";
eventAction.CodeModule = newCM;
eventAction.DisplayName = "EventActionWithCodeModule";
eventAction.Save(RefreshMode.REFRESH);
}
catch (Exception e)
{
System.Console.WriteLine("EventAction creation failed: " + e.Message );
newCM.Delete();
newCM.Save(RefreshMode.REFRESH);
}