djna
djna

Reputation: 55957

Worklight adapters, Java Classpath

The simple problem is: how do I make a Java Resource bundle available to code in Java JAR that is accessed from a Worklight adapter? I think that this amounts to: what is the classpath used by the Java beneath a Worklight adapter?

Reason: we use a third-party Java library that communicates with a particular enterprise service, it has it's own configuration mechanism to determine that service's location, that mechanism uses resource bundles.

We put the library JARs in the server/lib folder and successfully can invoke the library code from the adapter:

    manager = com.third.party.manager.getInstance();
    // manager is correctly obtained
    token = manager.talkToServer();
    // this fails with an error indicating that the server location is unknown

We see that the third party code attempts to use exactly this pattern, in particular the3 varient of getBundle() is as shown here:

java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ThirdParty");
Enumeration<String> enumeration = bundle.getKeys();

while (enumeration.hasMoreElements()){
 String str = enumeration.nextElement();
 System.out.println("property " + str);
     //etc

Now, I was expecting to package my resource bundle in the base of a new JAR file and add it to the server/lib and have it picked up. I created that resource JAR file, containing

ThirdParty.properties

and as a test created a little Java application containing the above code, and added the resource JAR to that application's classpath; the bundle we loaded as expected.

I then converted that Java to adapter code:

var bundle = java.util.ResourceBundle.getBundle("ThirdParty");

var enumeration = bundle.getKeys();

while (enumeration.hasMoreElements())
{
  var str = enumeration.nextElement();  
  var val = bundle.getString(str);    
}

Add my property JAR file to server/lib and get an exception telling me the bundle cannot be found.

Suggestions for a better classpath?

Upvotes: 0

Views: 571

Answers (1)

Anton
Anton

Reputation: 3166

I've just tried it and everything worked just fine. Steps I tried to recreate the issue:

  1. Created new WL project with WL Studio 6.1
  2. Added props.properties file with a single name=anton property
  3. Right-clicked it in Eclipse, export -> Java -> JAR file. The only checkbox I had checked is Export generated class files and resources. I've named the file resources.jar
  4. Deleted props.properties and copied resources.jar to server/lib folder
  5. Created adapter, added following procedure

    function test(){
        return {
             data: com.anton.PropGetter.getProp("name")
        };
    }
    
  6. Created PropGetter.java class under com.anton package with following code

    package com.anton;
    
    import java.util.ResourceBundle;
    
    public class PropGetter {
        public static String getProp(String propName){
            ResourceBundle bundle = ResourceBundle.getBundle("props");
            return propName + " :: " + bundle.getString(propName);
        }
    }
    
  7. Tested adapter procedure, got following output:

    {
       "data": "name :: anton",
       "isSuccessful": true
    }
    

Upvotes: 2

Related Questions