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

Toolkit - Performance

This post if part of a series about the File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents performance data that was captured for each major functionality with and without caching Linking, CP Assembler, Component Presentation Factory, Dynamic Content Queries, Model Factory. The data was captured on a 2014 Macbook Pro 15", 16 GB RAM, 2.6 GHz Intel Core i7 running OS X El Capitan. Test methodology: each test was run for 3 minutes and the total number of successful Toolkit API calls was measured. Then the number of calls per second was computed 'with cache' and 'without cache' test runs. Then a cache boost factor was calculated by diving (the number of API calls with cache) / (number of API calls without cache). Each cache test was executed 3 times, with different cache time-to-live values of 1 second, 5 seconds and 0 seconds (eternal cache, no expiration). The rationale is to see what impact different cache expiration/evi...

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...

A Core Service Java Client

I have seen many times the creation of a .NET client for SDL Tridion's Core Service, but not once a Java client. In .NET / Visual Studio is extremely easy to create the proxy classes based on the service's WSDL. So how hard can it be in Java? The entire code in this example is available in my Google Code project . Which Tool? Doing a bit of research on the multiple ways of creating Java proxy classes for web services left me quite confused as to which framework / utility to use. They are quite a few (AXIS, JAXB, Apache CXF, XFire, NetBeans plugin, IDEA plugin, Eclipse plugin, etc). They seemed to be cumbersome to use, too specific, limited, quirky, buggy, etc. What I wanted was a simple generic too that worked. Enter wsimport.exe ... Generating the Proxies Note: Check out the update post available here  http://yatb.mitza.net/2012/12/java-core-service-trouble-with-datetime.html Wsimport.exe is part of the JDK (mine is located in C:\Progrm Files\Java\jdk1.6.0_33\bin ),...