This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Posts tagged: Java

Adding new paths for native libraries at runtime in Java

If you want to add some native libraries at runtime, it was tricky with Java 8+:

This was a valid solution:

final Field fldUsrPaths = ClassLoader.class.getDeclaredField("usr_paths");
fldUsrPaths.setAccessible(true);
 
//get array of paths
final String[] saPath = (String[])fldUsrPaths.get(null);
 
//check if the path to add is already present
for (String path : saPath)
{
    if (path.equals(pPath))
    {
        return;
    }
}
 
//add the new path
final String[] saNewPaths = Arrays.copyOf(saPath, saPath.length + 1);
saNewPaths[saNewPaths.length - 1] = pPath;
fldUsrPaths.set(null, saNewPaths);

Since Java 10+ it's not possible anymore, because of: JDK-8210522
Some details from Stackoverflow

If someone still needs a solution for Java 10+, here it is:

Lookup cl = MethodHandles.privateLookupIn(ClassLoader.class, MethodHandles.lookup());
VarHandle usr_paths = cl.findStaticVarHandle(ClassLoader.class, "usr_paths", String[].class);
String[] path = (String[])usr_paths.get();

...

usr_paths.set(new String[] {"A", "B"});

This code won't work if you are using Java 8. If you want support for e.g. Java < 10, simply use reflection :)
Sounds simple, but it is not simple.....

Here's a working solution: toPDF project (search for addLibraryPath(String pPath)).

The code in detail:

Class<?> clsMHandles = Class.forName("java.lang.invoke.MethodHandles");
       
Method mStaticLookup = clsMHandles.getMethod("lookup");
Object oStaticLookup = mStaticLookup.invoke(null);

Method mLookup = clsMHandles.getMethod("privateLookupIn", Class.class, Class.forName("java.lang.invoke.MethodHandles$Lookup"));
Object oLookup = mLookup.invoke(null, ClassLoader.class, oStaticLookup);

Method mFindStatic = oLookup.getClass().getMethod("findStaticVarHandle", Class.class, String.class, Class.class);

Object oVarHandle = mFindStatic.invoke(oLookup, ClassLoader.class, "usr_paths", String[].class);

//MethodHandle mh = MethodHandles.lookup().findVirtual(VarHandle.class, "get", MethodType.methodType(Object.class));
//mh.invoke(oVarHandle);

Method mFindVirtual = oStaticLookup.getClass().getMethod("findVirtual", Class.class, String.class, Class.forName("java.lang.invoke.MethodType"));

Class<?> clsMethodType = Class.forName("java.lang.invoke.MethodType");
Method mMethodType = clsMethodType.getMethod("methodType", Class.class);

Object oMethodHandleGet = mFindVirtual.invoke(oStaticLookup, Class.forName("java.lang.invoke.VarHandle"), "get", mMethodType.invoke(null, Object.class));

Method mMethodHandleGet = oMethodHandleGet.getClass().getMethod("invokeWithArguments", Object[].class);

String[] saPath = (String[])mMethodHandleGet.invoke(oMethodHandleGet, new Object[] {new Object[] {oVarHandle}});

//check if the path to add is already present
for (String path : saPath)
{
    if (path.equals(pPath))
    {
        return;
    }
}

//add the new path
final String[] saNewPaths = Arrays.copyOf(saPath, saPath.length + 1);
saNewPaths[saNewPaths.length - 1] = pPath;

//MethodHandle mh = MethodHandles.lookup().findVirtual(VarHandle.class, "set", MethodType.methodType(Void.class, Object[].class));
//mh.invoke(oVarHandle, new String[] {"GEHT"});

mMethodType = clsMethodType.getMethod("methodType", Class.class, Class.class);

Object oMethodHandleSet = mFindVirtual.invoke(oStaticLookup, Class.forName("java.lang.invoke.VarHandle"), "set", mMethodType.invoke(null, Void.class, Object[].class));

Method mMethodHandleSet = oMethodHandleSet.getClass().getMethod("invokeWithArguments", Object[].class);

mMethodHandleSet.invoke(oMethodHandleSet, new Object[] {new Object[] {oVarHandle, saNewPaths}});

Not simple!!!

Summarized: It's not easy but it's still possible. So.... why are things getting more and more complex?

JVx and Java 8, Events and Lambdas

With version 8, Java has received support for lambda expressions and of course also JVx and its users directly profit from this new feature.

In case you do not know what a lambda expression is, it is basically the possibility to inline functions, sometimes also called "anonymous functions" and should not be confused with "anonymous classes", which Java has supported for quite some time now. Let's look at a simple example of anonymous classes:

new Thread(new Runnable()
{
    public void run()
    {
        // your code
    }
}).start();

This launches a thread that does something. The Runnable in our example is the anonymous class. Now with the new lambda support we don't need to write the interface implementation, but can directly tell it to run a function:

new Thread(() -> { /* your code */ }).start();

The empty parentheses and the arrow indicate a new anonymous function. The parentheses contain the list of parameters of the function that is going to be invoked, in our case it is empty because Runnable.run() does not have any parameters. So the anatomy of a lambda looks like this:

new Thread((optional explicit cast to target interface)(parameters) ->
{
    function body
});

Internally this is still compiled into an anonymous class, but the code becomes a lot smoother and easier to read.

Additionally it is now possible to reference functions directly, like this:

// instance
new Thread(this::worker).start();
// static
new Thread(YourWorkClass::worker).start();

Especially the last possibility is very exciting, as our event system has a very similar mechanism but until recently did not have compile-time safety and checks.

The new lambda feature is backwards compatible and can easily be used in JVx. Here is a simple test window that shows the new lambda expressions in combination with JVx events:

import javax.rad.genui.component.UIButton;
import javax.rad.genui.container.UIFrame;
import javax.rad.genui.layout.UIBorderLayout;
import javax.rad.ui.event.UIActionEvent;

public class LambdaShowcaseFrame extends UIFrame
{
    public LambdaShowcaseFrame()  
    {
        initializeUI();
    }

    public void doJVxAction(UIActionEvent pActionEvent)
    {
        System.out.println("JVx");
    }

    public void doLambda2(UIActionEvent pActionEvent)
    {
        System.out.println("Lambda 2");
    }

    private void initializeUI()
    {
        //before Java 8
        UIButton buttonJVx = new UIButton("JVx Style");
        buttonJVx.eventAction().addListener(this, "doJVxAction");

        // with Java 8

        UIButton buttonLambda1 = new UIButton("Lambda 1");
        buttonLambda1.eventAction().
                addListener(pActionEvent -> System.out.println("Lambda 1"));

        UIButton buttonLambda2 = new UIButton("Lambda 2");
        buttonLambda2.eventAction().addListener(this::doLambda2);

        setLayout(new UIBorderLayout());
        add(buttonJVx, UIBorderLayout.NORTH);
        add(buttonLambda1, UIBorderLayout.WEST);
        add(buttonLambda2, UIBorderLayout.EAST);
    }
}

New project: embedded NFC

The source code for NFC reading/writing (mifare cards, chip PN532) is available.

See http://sourceforge.net/projects/enfc.

If you use Eclipse, simply check out and open the included project. It should work without problems. A build.xml for ant is included and creates an enfc.jar under <project>/build/install/.

How to use the library?

First, test if your hardware works. Start the JUnit test cases:

<TestNFC>.testDiagnose
<TestNFC>.testCommunication

Both should be green.

If you have problems, check if your authentication Key is 0xFF_FF_FF_FF_FF_FF and change the port for the serial interface (default: COM6). Add the system property "-Dserialport=xxx" to your launch configuration or change AbstractNFCTest.

The library has one very important class and one important class :)

  • The very important class is NFC. It handles the communication with your hardware (supports read and write operations).
  • The important class is NFCReader

The NFCReader reads two sectors (á 3 Blocks á 16bytes). The first sector contains a name (usually your first and last name). The second sector contains an authentication key (whatever). If you manage different data or want save read operations, check the source of NFCReader and create your own reader - it's easy.

Before you can read data, you have to write data to your card. Use following test case to write data:

<TestNFC>.testWrite

but don't forget to change the username and auth key. The defaults are René Jahn and 010203 ;-)

A simple test application:

public static void main(String[] pArgs)
{
  nfcr = new NFCReader();
  nfcr.setPort(System.getProperty("serialport"));
  nfcr.setAuthenticationKey(new byte[] {(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF});
  nfcr.addListener(new INFCListener()
  {
    public void nearFieldCommunication(final NFCEvent pEvent)
    {
      switch (pEvent.getType())
      {
        case BlockRead:
                byte[] byData = new byte[48];
                                                       
                List<?> liBytes = (List<?>)pEvent.getObject();
                                                       
                for (int i = 0; i < 3; i++)
                {
                  System.arraycopy((byte[])liBytes.get(i), 0, byData, i * 16, 16);     
                }
                                                       
                String sName = NFC.toString(byData);

                byData = new byte[48];

                for (int i = 3, j = 0; i < 6; i++, j++)
                {
                  System.arraycopy((byte[])liBytes.get(i), 0, byData, j * 16, 16);     
                }
                                                       
                String sKey = NFC.toString(byData);
                                                       
                System.out.println("Welcome: " + sName + " [" + sKey + "]");
                break;
        case Error:
                ((Exception)pEvent.getObject()).printStackTrace();
                break;
      }
    }
  });
       
  nfcr.searchCards(false);
}

Source Code modification with Eclipse AST

We heavily use Eclipse AST and JDT. The problem with both APIs is that you don't find a lot of documentation or examples. The best documentation was and is the source code. It shows all secrets.

Last friday at Eclipse DemoCamp Vienna, I discussed with CodeCop about AST and JDT and the missing documentation :)

I wrote a simple class for class/superclass/package/javadoc manipulation some months ago. It is included in VisionX and is not free, but no matter. Here is the source code:

/*
(c) SIB Visions 2012

http://www.sibvisions.com
*/
public class SimpleSourceFile {
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Class members
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /** the source parser. */
    private ASTParser parser;

    /** the parsed tokens of the source file. */
    private CompilationUnit cuSource;

    /** the original document. */
    private Document docOrig;

    /** the class type info. */
    private TypeDeclaration tdClass;

    /** the javadoc of the class. */
    private JavadocInfo jdoc;

    /** the full qualified name of the class. */
    private String sClassName;

    /** the full qualified name of the super class after loading. */
    private String sLoadedSuperClass;

    /** the full qualified name of the parent class. */
    private String sSuperClass;

    /** whether the source is changed. */
    private boolean bChanged = false;

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Initialization
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /**
     * Creates a new instance of <code>SimpleSourceFile</code> without source
     * information.
     */

    public SimpleSourceFile() {
        load("");
    }

    /**
     * Creates a new instance of <code>SimpleSourceFile</code> with a given
     * source file.
     *  
     * @param pFile the java source file
     * @throws IOException if an error occurs during reading the file
     */

    public SimpleSourceFile(File pFile) throws IOException {
        if (pFile.exists()) {
            JavaSource source = JavaSource.get(pFile.getAbsolutePath());

            if (source != null) {
                synchronized(source) {
                    load(new String(FileUtil.getContent(pFile)));
                }
            } else {
                load(new String(FileUtil.getContent(pFile)));
            }
        } else {
            load("");
        }
    }

    /**
     * Creates a new instance of <code>SimpleSourceFile</code> with a given
     * source stream. The stream is closed after reading
     *  
     * @param pStream the java source stream
     * @throws IOException if an error occurs during reading the stream
     */

    public SimpleSourceFile(InputStream pStream) throws IOException {
        this(pStream, true);
    }

    /**
     * Creates a new instance of <code>SimpleSourceFile</code> with a given
     * source stream.
     *  
     * @param pStream the java source stream
     * @param pAutoClose whether the stream should be closed after reading
     * @throws IOException if an error occurs during reading the stream
     */

    public SimpleSourceFile(InputStream pStream, boolean pAutoClose) throws IOException {
        load(new String(FileUtil.getContent(pStream, pAutoClose)));
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // User-defined methods
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /**
     * Loads the properties from the source file.
     *
     * @param pSource the java source
     */
@SuppressWarnings({
        "unchecked", "rawtypes"
    })
    private void load(String pSource) {
        jdoc = new JavadocInfo();

        if (parser == null) {
            parser = ASTUtil.createParser();
        }

        parser.setSource(pSource.toCharArray());

        cuSource = (CompilationUnit) parser.createAST(null);

        if (docOrig != null) {
            docOrig.set(pSource);
        } else {
            docOrig = new Document(pSource);
        }

        //we support only one class per file
        List types = cuSource.types();

        if (types.size() > 0 && types.get(0) instanceof TypeDeclaration) {
            tdClass = (TypeDeclaration) types.get(0);

            String sPackage;

            //we have to use the current package, if set
            if (cuSource.getPackage() != null) {
                sPackage = cuSource.getPackage().getName().getFullyQualifiedName() + ".";
            } else {
                sPackage = "";
            }

            sClassName = sPackage + tdClass.getName().getFullyQualifiedName();

            if (tdClass.getSuperclassType() != null) {
                String sSuperName = ((SimpleType)(tdClass.getSuperclassType())).getName().getFullyQualifiedName();

                if (sSuperName.indexOf('.') > 0) {
                    sSuperClass = sSuperName;
                } else {
                    //try to detect the full qualified name of the super class through the imports

                    List < ImportDeclaration > liImports = cuSource.imports();

                    boolean bFound = false;

                    if (liImports != null) {
                        String sTempName = "." + sSuperName;
                        String sImport;

                        for (int i = 0, anz = liImports.size(); !bFound && i < anz; i++) {
                            sImport = liImports.get(i).getName().getFullyQualifiedName();
                            if (sImport.endsWith(sTempName)) {
                                bFound = true;

                                sSuperClass = liImports.get(i).getName().getFullyQualifiedName();
                            }
                        }
                    }

                    if (!bFound) {
                        //we use the current package
                       sSuperClass = sPackage + sSuperName;
                    }
                }

                //cache for optimizing imports in apply
                sLoadedSuperClass = sSuperClass;
            }

            jdoc = new JavadocInfo(tdClass.getJavadoc(), pSource);
        } else {
            tdClass = null;
        }

        //currently loaded file can not be changed!
        bChanged = false;
    }

    /**
     * Sets the comment for the class.
     *
     * @param pText the comment
     */

    public void setClassComment(String pText) {
        bChanged |= !CommonUtil.equals(pText, jdoc.getDescription());

        jdoc.setDescription(pText);
    }

    /**
     * Gets the comment for the class.
     *
     * @return the comment
     */

    public String getClassComment() {
        return jdoc.getDescription();
    }

    /**
     * Sets the full qualified name of the class.
     *
     * @param pName the class name
     */

    public void setClassName(String pName) {
        if (pName == null) {
            throw new IllegalArgumentException("It is not allowed to remove the class definition (class name = null)!");
        }

        bChanged |= !CommonUtil.equals(pName, sClassName);

        sClassName = pName;
    }

    /**
     * Gets the full qualified name of the class.
     *
     * @return the class name
     */

    public String getClassName() {
        return sClassName;
    }

    /**
     * Sets the full qualified class name of the parent class.
     *
     * @param pSuperClass the full qualified class name of the parent class
     */

    public void setSuperClass(String pSuperClass) {
        bChanged |= !CommonUtil.equals(pSuperClass, sSuperClass);

        sSuperClass = pSuperClass;
    }

    /**
     * Gets the full qualified class name of the parent class.
     *
     * @return the class name
     */

    public String getSuperClass() {
        return sSuperClass;
    }

    /**
     * Replaces the relevant information with the specific values
     * and saves the source file.
     *
     * @throws Exception if the source modification fails
     */
@SuppressWarnings("unchecked")
    protected void apply() throws Exception {
        if (!bChanged) {
            return;
        }

        AST ast = cuSource.getAST();

        ASTRewrite arw = ASTRewrite.create(ast);

        //----------------------------------------------------
        // Change Package, Classname and Constructor
        //----------------------------------------------------

        String sCurrentPackage;

        //set package
        String[] sClassInfo = ClassUtil.splitName(sClassName);

        if (sClassInfo[0] != null) {
            if (cuSource.getPackage() != null) {
                arw.set(cuSource.getPackage(), PackageDeclaration.NAME_PROPERTY, ast.newName(sClassInfo[0]), null);

                sCurrentPackage = sClassInfo[0];
            } else {
                PackageDeclaration pdcl = ast.newPackageDeclaration();
                pdcl.setName(ast.newName(sClassInfo[0]));

                arw.set(cuSource, CompilationUnit.PACKAGE_PROPERTY, pdcl, null);

                sCurrentPackage = sClassInfo[0];
            }
        } else {
            arw.set(cuSource, CompilationUnit.PACKAGE_PROPERTY, null, null);

            sCurrentPackage = null;
        }

        if (tdClass == null) {
            //new class
            tdClass = ast.newTypeDeclaration();
            tdClass.setName(ast.newSimpleName(sClassInfo[1]));

            ListRewrite lrwImp = arw.getListRewrite(cuSource, CompilationUnit.TYPES_PROPERTY);
            lrwImp.insertFirst(tdClass, null);
        } else {
            //change class name
            SimpleName snClass = ast.newSimpleName(sClassInfo[1]);

            arw.set(tdClass, TypeDeclaration.NAME_PROPERTY, snClass, null);

            //chage constructors

            MethodDeclaration[] methods = tdClass.getMethods();

            if (methods != null) {
                for (MethodDeclaration method: methods) {
                    if (method.isConstructor()) {
                        arw.set(method, MethodDeclaration.NAME_PROPERTY, snClass, null);
                    }
                }
            }
        }

        //change Superclass

        List < ImportDeclaration > liImports = cuSource.imports();

        //remove "old" import
       if (sLoadedSuperClass != null) {
            if (liImports != null) {
                boolean bFound = false;

                for (int i = liImports.size() - 1; !bFound && i >= 0; i--) {
                    if (sLoadedSuperClass.equals(liImports.get(i).getName().getFullyQualifiedName())) {
                        bFound = true;

                        arw.remove(liImports.get(i), null);

                        liImports.remove(i);
                    }
                }
            }
        }

        if (sSuperClass == null) {
            arw.set(tdClass, TypeDeclaration.SUPERCLASS_TYPE_PROPERTY, null, null);
        } else {
            //check new super class import
            String[] sSuperClassInfo = ClassUtil.splitName(sSuperClass);

            if (sSuperClassInfo[0] != null) {
                boolean bFound = false;

                if (sCurrentPackage != null) {
                    bFound = sSuperClassInfo[0].equals(sCurrentPackage);
                }

                if (!bFound) {
                    if (liImports != null) {
                        for (int i = 0, anz = liImports.size(); !bFound && i < anz; i++) {
                            bFound = sSuperClass.equals(liImports.get(i).getName().getFullyQualifiedName());
                        }
                    }
                }

                //class not already importet -> add import
               if (!bFound) {
                    ImportDeclaration idSuper = ast.newImportDeclaration();
                    idSuper.setName(ast.newName(sSuperClass));

                    ListRewrite lrwImp = arw.getListRewrite(cuSource, CompilationUnit.IMPORTS_PROPERTY);
                    lrwImp.insertLast(idSuper, null);
                }
            }

            arw.set(tdClass, TypeDeclaration.SUPERCLASS_TYPE_PROPERTY, ast.newSimpleType(ast.newName(sSuperClassInfo[1])), null);
        }

        //----------------------------------------------------
        // Change Javadoc of the class
        //----------------------------------------------------

        if ("".equals(jdoc.toString())) {
            //no javadoc
            arw.set(tdClass, TypeDeclaration.JAVADOC_PROPERTY, null, null);
        } else {
            arw.set(tdClass, TypeDeclaration.JAVADOC_PROPERTY, jdoc.createJavadoc(tdClass.getAST()), null);
        }

        //----------------------------------------------------
        // Modify AST
        //----------------------------------------------------

        TextEdit edit = arw.rewriteAST(docOrig, null);
        edit.apply(docOrig);

        ASTUtil.applyFormat(docOrig);

        //simple reparse (important for the comments)
        parser.setSource(docOrig.get().toCharArray());

        cuSource = (CompilationUnit) parser.createAST(null);

        //----------------------------------------------------
        // Modify comments
        //----------------------------------------------------

        //Class comment after the last bracket

        int iCorr = 0;

        List < Comment > liComments = cuSource.getCommentList();

        if (liComments != null) {
            String sOldName = tdClass.getName().getFullyQualifiedName();
            String sText;

            for (Comment com: liComments) {
                sText = docOrig.get(com.getStartPosition() - iCorr, com.getLength());

                int iPos = 0;
                int iClassLen = sClassInfo[1].length();
                int iLenDiff = sOldName.length() - iClassLen;

                int iNextPos;

                CharacterType ctypePrev;
                CharacterType ctypeNext;

                while ((iPos = sText.indexOf(sOldName, iPos)) >= 0) {
                    if (iPos > 0) {
                        ctypePrev = StringUtil.getCharacterType("" + sText.charAt(iPos - 1));
                    } else {
                        ctypePrev = CharacterType.OnlySpecial;
                    }

                    iNextPos = iPos + sOldName.length();

                    if (iNextPos <= sText.length() - 1) {
                        ctypeNext = StringUtil.getCharacterType("" + sText.charAt(iNextPos));
                    } else {
                        ctypeNext = CharacterType.OnlySpecial;
                    }

                    //don't replace word parts e.g. ContactsWorkScreen should not be replaced with Contacts
                    if ((ctypePrev == CharacterType.OnlySpecial || ctypePrev == CharacterType.OnlyWhitespace) && (ctypeNext == CharacterType.OnlySpecial || ctypeNext == CharacterType.OnlyWhitespace)) {
                        docOrig.replace(com.getStartPosition() + iPos - iCorr, sOldName.length(), sClassInfo[1]);

                        iCorr += iLenDiff;
                        iPos += iClassLen;
                    } else {
                        iPos = iNextPos;
                    }
                }
            }
        }

        //----------------------------------------------------
        // Update
        //----------------------------------------------------

        load(docOrig.get());

        bChanged = false;
    }

    /**
     * Stores the changed source into the given file.
     *
     * @param pFile the target file for the source
     * @throws Exception if an error occurs during saving
     */

    public void saveAs(File pFile) throws Exception {
        apply();

        JavaSource source = JavaSource.get(pFile.getAbsolutePath());

        if (source != null) {
            synchronized(source) {
                FileUtil.save(pFile, docOrig.get().getBytes());

                source.reload();
            }
        } else {
            FileUtil.save(pFile, docOrig.get().getBytes());
        }
    }

    /**
     * Stores the changed source into the given stream.
     *
     * @param pStream the target stream for the source
     * @throws Exception if an error occurs during saving
     */

    public void saveAs(OutputStream pStream) throws Exception {
        apply();

        pStream.write(docOrig.get().getBytes());
        pStream.flush();
    }

    /**
     * Returns the modified source code.
     *  
     * @return the modified source code
     * @throws Exception if the source modification fails
     */

    protected String getSource() throws Exception {
        apply();

        return docOrig.get();
    }

} // SimpleSourceFile

Important imports:

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;

The class contains references to other classes like CommonUtil, FileUtil, StringUtil. These classes are included in JVx. There are two classes (JavaSource, ClassUtil) that are not free, but you should find out how it works without these classes.

If you'll set a new superclass, the superclass and its import, if available, will be updated. If you'll set a new classname, all constructors will be updated.

And once again: The best documentation is the source code :)

Swing RETRO application - MP3Tool - update

In the Swing RETRO application article, I wrote about my personal MP3Tool. When the tool was developed, the ID3v2 Tag was not very important and the cover image was not relevant. Now, every modern mp3 file has a cover or front or band image. The problem is that the "original" MP3Tool does not support images.

Because it is important for me, to change files with covers too, I decided to implement a simple image support. And during my research I found other features that are often used: Lyrics Tag and APE Tag. I have never heard about them. So I decided to implement at least delete support, for those Tags.

After some hours, the implementation was done. I was positive surprised about the source code quality, because the code is really old. It was amazing good.

The update version is available here and the version is now 2.2.

Cover images

The tool is not a SIB Visions Tool.

Swing RETRO application

During christmas break, I checked my (very) old personal Java applications. One of the first comments was from 2001. A long time ago. I had no UI framework, no testing framework and no automatic build process. All was hand-made :) I was amazed that I could read and edit the source code without problems.

One of my biggest and first applications, that I wrote for my personal use, was "MP3Tool". It is a tool to manage (very) large MP3 inventories, it has cool automations, supports offline edit/merge via XML, supports database loading and has command line support. It has support for ID3v1 and ID3v2. I have implemented my own library to read and write ID3 TAG infos (not complete, but works still great).

The tool is a "developer" tool that is not very user-friendly, but it is 11 years old and has tons of features. It was developed from a developer for a developer :) If someone needs a Java tool for mass editing of MP3 Files - here it is. It is not Open Source because it was not relevant in 2001. It is Free for everyone and if someone needs the source code it should not be a problem to upload it somewhere. :)

The tool has a German and English language file. I don't know if the auto detection works but you can change the language file in the settings, via Toolbar. I have not tried it on MacOS, but used it very often with Linux. If you search documentation - I have never used one!

I use the tool from time to time and still love it because it just works. After 11 years, I think it is ok to share it with you.

To be sure - The tool is not a SIB Visions Tool - It is a retro style swing application developed long time ago ;-)

mp3uebersicht_eingelesen

File overview

mp3optimierung_ergebnis_mehrfach

File optimization

mp3konvertierung_konvert

TAG conversion

JVx Kommunikation im Vergleich

Die in JVx integrierte Kommunikation hätte durchaus potential für ein eigenständiges Open Source Projekt. Wir sind jedoch bemüht die Entwicklung immer so einfach als möglich zu halten. Das bedeutet nicht, daß in JVx alle möglichen Frameworks zusammengeführt werden und somit ein nicht überschaubares "etwas" entsteht, sondern JVx enthält alles was nötig ist um Business Anwendungen in einer 3 Schichten Architektur zu erstellen. Und dazu gehört nun mal eine effiziente Client/Server Kommunikation.

Es gibt natürlich Frameworks für die Kommunikation zwischen Client und Server, wie z.B. Netty. Doch unabhängig von den vielen Möglichkeiten des Frameworks ist die Bibliothek mit knapp 700KB ein großer Brocken (nur für die Kommunikation!). Weiters ist ein Kommunikationsframework alleine noch nicht ausreichend, denn die Objekte müssen zwischen Client und Server auch noch serialisiert bzw. deserialisiert werden.

Für die Serialisierung könnten wiederum vorhandene Frameworks wie z.B.: Google Protocol Buffers oder Hessian eingesetzt werden. Doch die Google Protocol Buffers erfordern die Definition der zu übertragenden Objekte und ist daher für ein generisches Framework wie JVx nicht geeignet. Und Hessian unterstützt leider nicht alle Objekttypen und ist nicht out-of-the-box mit z.B. Android einsatzfähig.

Der in JVx integrierte Kommunikationsmechanismus erlaubt ohne weiteres die Integration von den bisher genannten Frameworks, da weder das Transportprotokoll noch die Serialisierung ausschlaggebend sind für eine Business Anwendung. Außerdem definiert JVx mit Interfaces, was die Kommunikation bieten muss. Üblicherweise ist eine eigene Implementierung durch die Ableitung von AbstractSerializedConnection bzw. die Implementierung von ISerializer rasch durchgeführt.

Mit dem UniversalSerializer wird außerdem eine Lösung bereitgestellt mit der Objekte, zwischen unterschiedlichsten Technologien, ausgetauscht werden können. Und das ohne sich Gedanken über die Konfiguration zu machen bzw. Mappings zu erstellen. Der Einsatz des UniversalSerializer in Verbindung mit Netty ist ebenfalls ohne Probleme möglich, nur um hier auf den flexiblen Einsatz der JVx Kommunikation hinzuweisen!

Die Kommunikation wird im Moment mit Java und .NET erfolgreich eingesetzt. Weiters funktioniert die Kommunikation auch ohne jegliche Anpassung mit Android.

JVx Web User Interface

Wie bereits erwähnt können mit JVx, Technologie unabhängige User Interfaces erstellt werden. Der Desktop Bereich ist mit Swing und QT im Moment gut abgedeckt. Doch wie sieht es mit einer Implementierung für HTML/Ajax aus?

Wäre doch großartig, wenn eine Applikation nicht nur als RIA (Desktop, Applet) verwendet werden kann sondern auch als klassische Web Anwendung. Die Installation von Plugins wäre dadurch nicht mehr notwendig.

Unsere Entwicklungsabteilung arbeitet gerade mit Hochdruck an der WebUI Implementierung für JVx. Dabei setzen wir vor allem auf GWT und die Komponenten Bibliothek extGWT.

Die Implementierung wird ebenfalls unter der Apache License, Version 2.0, veröffentlicht. Die ersten Screenshots werden wir in einem unserer nächsten Postings veröffentlichen.

Technologie unabhängige User Interfaces mit JVx

Mit JVx können Technologie unabhängige User Interfaces erstellt werden. Die Implementierung für Swing ist bereits umgesetzt und in der Bibliothek enthalten. Die Implementierung für QT Jambi ist nahezu fertig und steht bereits in den Startlöchern.

Die folgenden Screenshots zeigen die Kontakte Maske aus der Showcase Anwendung. Die Anwendung wurde einmalig mit JVx in Java implementiert und wird ohne Code Änderung als Swing Applikation und als QT Applikation gestartet:

Swing UI Implementation

Swing UI Implementierung

QT UI Implementierung

QT UI Implementierung

 

Der Unterschied der beiden Technologien könnte größer nicht sein, ist in unserem Beispiel aber nur an Details zu erkennen:

  • Mit Swing ist maximal eine ToolBar pro Bereich (NORD, SÜD, WEST, OST) möglich.
    QT ermöglicht beliebig viele ToolBars.
  • Ein Swing Button hat breitere Margins als ein QT Button
  • Die Window Buttons von Internal Frames werden von QT detaillierter dargestellt
  • Die Tabellen reagieren beim Scrollen unterschiedlich

Durch die Swing Implementierung kann auf die Vielzahl an vorhandenen Swing Controls zurückgegriffen werden um die Applikation an spezielle Kundenwünsche anzupassen. Mit der QT Jambi Implementierung können QT Jambi Controls von anderen Anbietern eingebunden werden.

Fundtruhe - APIs

Zum Wochenausklang einige interessante Links für mobile Anwendungen:

  • JSON Serializer

    Ein noch recht junges Projekt für die Objektserialisierung mit JSON.

  • JFXtras

    Im Rahmen des Projektes werden Erweiterungen für Java FX entwickelt. Unter anderem neue Komponenten und Effekte.

  • AiCharts (Kommerziell)

    Eine mächtige Bibliothek für die Erstellung von Charts unter Android.