question

anadgoud avatar image
anadgoud asked anadgoud posted

loading a tcl package kept in plug-in resource folder at runtime

Hi

 

In my sdk plug-in resouce folder I have created a folder called "Lib" (at the same level as that Icons) underwhich I have placed a tcl library. I want to load this package at runtime in open request handler, how can I achive this.

 

I am currently doing it as follows but some how not convinced that it is the right way, please let me know.

 

URL installURL = Platform.getBundle("myplugin").getEntry("/");
   URL url = null;
   try {
    url = new URL(installURL, "Lib/myTool.tcl");
   } catch (MalformedURLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   
   InputStream stream = null;
   try {
    stream = url.openStream();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }

   if (stream == null)
    logMessage(model, IssueSeverity.ERROR, "myTool.tcl failed loading", requestResponse);
   
   String byteString = new String();
   byte[] b = new byte[100];
   int count = 0;
   try {
    while ((count = stream.read(b, 0,100)) > 0 )
    { 
      byteString = byteString + new String(b,0,count);     
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   tclInterpreter.execute(byteString);
   tclInterpreter.execute("package require Network");  // I guess this may not be required.

 

Regards

anadgoud

iTestsdk
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
KumarS avatar image
KumarS answered KumarS posted

You do not have to read in the file. You should be able to simply do:

 

 tclInterpreter.execute("source path_to_your_file");

 

You should add "package require Network" to top of your myTool.tcl file.

8 comments
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

PaulD avatar image PaulD commented ·

True.  But I think the problem is that the file doesn't exist in the file system anywhere because it is inside the plugin's JAR file as a resource.

 

If you do want to read it out of a resource, then you use something like the following.  (This is just off the top of my head, so complete untested!)

 

InputStream istream = null;

String contents = null;

try {

    istream = getClass().getResourceAsStream(new URL("blah/blah"));

    InputStreamReader rdr = new InputStreamReader(istream);

    char[] buf = new char[8000];

    int size = rdr.read(buf, 0, 8000);

    = String.valueOf(buf, 0, size);

} finally {

   if (istream != null) {

       istream.close();

   }

}

 

if (contents != null) {

   tclInterp.execute(contents);

}

 

 

0 Likes 0 ·
AmeyaB avatar image AmeyaB ♦ PaulD commented ·

Another solution would be to copy the Tcl file to a temporary folder, and then source it from there.

0 Likes 0 ·
anadgoud avatar image anadgoud AmeyaB ♦ commented ·

Ameya,

 

If we follow your solutions then probably we also need to make sure that TCLLIBPATH enviroment variable has this path. How do we control it.

 

Regards

anadgouda

0 Likes 0 ·
KumarS avatar image KumarS anadgoud commented ·

You should be able to set the "auto_path" variable inside your tcl shell. "auto_path" variable is what contains the TCLLIBPATH value inside a tcl shell. Just add your path to this variable and everything should work.

0 Likes 0 ·
PaulD avatar image PaulD anadgoud commented ·

Note that if you provide a full path to the file you want to source, it doesn't have to be in the Tcl path.

0 Likes 0 ·
anadgoud avatar image anadgoud PaulD commented ·

Thank you all for the advise. Copying file to temp folder and giving full path in source makes sense to me.

0 Likes 0 ·
AmeyaB avatar image AmeyaB ♦ anadgoud commented ·

Just ensure you cleanup after yourself, by deleting the temp file once done.

0 Likes 0 ·
anadgoud avatar image anadgoud PaulD commented ·

Thanks.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.