Skip to main content

TOM.Java - Now with Generics Support

What kept me busy lately (apart from my insane work load)? Adding 'generics' support to the TOM.Java API.

I was blogging a few weeks ago about a pet project of mine -- the Java Mediator for SDL Tridion Templating. In that post I was very enthusiastic about JNI4NET bridge between Java and .NET and namely about ProxyGen -- the tool that actually generates both Java and C# proxy classes. For what it is, ProxyGen is a great tool, but it's really a Java 1.4 tool -- namely, it does not support generics. Luckily, it is open source, so anybody can modify it...

However, the problem for me was my own learning curve. ProxyGen is in fact a code generator written in C# using CodeDom, which builds a class model (from JARs and DLLs) using reflection, and finally generates C# and Java code from this model.

I won't go into the ProxyGen details here (if you want more details, I posted more details on jni4net Google group).

Anyway, with this (partial) support for generics in TOM.Java, I am able to use Java counterparts for the main generic collections from C# (and the other way around for that matter - from Java collections to C# collections). The following type mappings are available:

Java
C#
java.util.List<E>System.Collections.Generic.IList<T>
java.util.Map<K, V>System.Collections.Generic.IDictionary<K, V>
java.util.Set<E>System.Collections.Generic.ISet<T>
java.util.Collection<E>System.Collections.Generic.ICollection<T>
java.lang.Iterable<E>System.Collections.Generic.IEnumerable<T>

The methods using generics delegate their calls to the non-generic native counterparts (as originally designed by ProxyGen). What I did was basically deal with the conversion from generic to non-generic and back. The delegate methods are suffixed with the name of the Java collection they are a generic for.

Example: Package.GetEntries() returns a C# IList of KeyValuePairs of string and Item objects.
Original signature:

IList<KeyValuePair<string, Item>> GetEntries()

ProxyGen generates a Java native proxy method with signature:

public native system.collections.IList GetEntries()

If I were to use this method I would have to use the IEnumerator iterator like this (and that does not support Java for-each loops):

for (IEnumerator enumerator = _package.GetEntries().GetEnumerator(); enumerator.MoveNext();) {
    @SuppressWarnings("rawtypes")
    KeyValuePair kvp = (KeyValuePair) enumerator.getCurrent();
    system.String key = (system.String) kvp.getKey();
    Item value = (Item) kvp.getValue();
    System.out.println(String.format("\tKey: %s\tValue: %s", key, value.GetAsString()));
}

Next to this, my enhancement to ProxyGen now generates a delegate that accepts generics

java.util.List<mitza.jni4net.wrapper.KeyValuePair<system.String, tridion.contentmanager.templating.Item>> GetEntriesList()

So the loop-above can be rewritten Java-style with for-each:

for (KeyValuePair<system.String, Item> kvp : _package.GetEntriesList()) {
    System.out.println(
        String.format("Key: %s Value: %s", kvp.getKey(), kvp.getValue().GetAsString()));
}

Other examples:

for (ComponentPresentation cp : page.getComponentPresentationsList()) {
    Component component = cp.getComponent();
    ComponentTemplate componentTemplate = cp.getComponentTemplate();
    System.out.println(String.format("CP: Component: '%s' %s + Component Template: '%s' %s",
        component.getTitle(), component.getId(), componentTemplate.getTitle(), componentTemplate.getId()));
}

for (VersionedItem item : page.GetVersionsIterable()) {
    System.out.println(String.format("Version: %s User: %s Date: %s",
        item.getVersion(), item.getRevisor().getTitle(), item.getRevisionDate()));
}


Comments

Unknown said…
A great job. Could you upload your modified proxygen.exe with partial support Generics ? i want to try it on my dll. tks you
Mihai Cădariu said…
They are available at http://jni4net.googlecode.com/svn/branches/0-8-generics/distributable/
Unknown said…
This comment has been removed by the author.
Unknown said…
Hello, it's done with proxygen to generate many proxies file for jvm and clr folder. But the building after that does not work. As the proxygen generates many wrongs name file javas such as abc`1.java, etc.. and many errors in .cs files suchs as @__DiscreteMembershipFunction<>, etc.. Can you help me to fix it ?
Unknown said…
This comment has been removed by the author.

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven