source: trunk/projects/jar2lib/src/main/resources/project-files/jtools/source/javaTools.cxx @ 7734

Revision 7734, 7.1 KB checked in by hinerm, 8 years ago (diff)

More control allowed in JavaTools

The following options were added to JavaTools:

  • specification of one folder containing jars to add to the classpath
  • specification of a list of files/directories to add to the classpath
  • specification of files to add to the java.library.path
  • specifications of commands to add as jace::CustomOptions

The header and method signature options were updated to reflect these changes
Note that with these changes the overall structure of jtools was simplified,
so a top-level directory from which other paths are relative can no longer be specified.

Line 
1//
2// javaTools.cxx
3//
4
5/*
6OME Bio-Formats ITK plugin for calling Bio-Formats from the Insight Toolkit.
7Copyright (c) 2008-@year@, UW-Madison LOCI.
8All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12    * Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17    * Neither the name of the UW-Madison LOCI nor the
18      names of its contributors may be used to endorse or promote products
19      derived from this software without specific prior written permission.
20
21THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY
22EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY
25DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32
33/*
34IMPORTANT NOTE: Although this software is distributed according to a
35"BSD-style" license, it requires the OME Bio-Formats Java library to do
36anything useful, which is licensed under the GPL v2 or later.
37As such, if you wish to distribute this software with Bio-Formats itself,
38your combined work must be distributed under the terms of the GPL.
39*/
40#include <iostream>
41#include <string>
42#include <sstream>
43#include "javaTools.h"
44
45#if defined (_WIN32)
46#define PATHSTEP ';'
47#define SLASH '\\'
48#else
49#define PATHSTEP ':'
50#define SLASH '/'
51#endif
52
53#ifdef __APPLE__
54#define PATH "DYLD_LIBRARY_PATH"
55#endif
56#ifdef __linux__
57#define PATH "LD_LIBRARY_PATH"
58#endif
59#ifdef _WIN32
60#define PATH "PATH"
61#endif
62#ifdef _WIN64
63#define PATH "PATH"
64#endif
65
66using namespace std;
67
68void JavaTools::createJVM()
69{
70  JavaTools::createJVM("jar", true, 256, vector<string>(), vector<string>(), vector<string>());
71}
72
73void JavaTools::createJVM(string jarFolder)
74{
75  JavaTools::createJVM(jarFolder, true, 256, vector<string>(), vector<string>(), vector<string>());
76}
77
78void JavaTools::createJVM(int memory)
79{
80  JavaTools::createJVM("jar", true, memory, vector<string>(), vector<string>(), vector<string>());
81}
82
83void JavaTools::createJVM(bool headless)
84{
85  JavaTools::createJVM("jar", headless, 256, vector<string>(), vector<string>(), vector<string>());
86}
87
88void JavaTools::createJVM(bool headless, int memory)
89{
90  JavaTools::createJVM("jar", headless, memory, vector<string>(), vector<string>(), vector<string>());
91}
92
93void JavaTools::createJVM(string jarFolder, bool headless, int memory)
94{
95  JavaTools::createJVM(jarFolder, headless, memory, vector<string>(), vector<string>(), vector<string>());
96}
97
98/**
99 * jarFolder is the name of the path to a directory of jars to be added to the classpath (default: jar)
100 * headless turns headless on or off for JVM instantiation (default: on)
101 * memory controls memory for instantiation of JVM in MB (default: 256)
102 * extraClasspath is a list of additional classpath entries to include
103 * extraJavaLibraryPath is a list of path entries to be appended to the JVM's java.library.path. ( default: .)
104 * extraOptions is a list of any other desired arguments to the JVM, added to the jace::OptionList (e.g., "-verbose:jni")
105 */
106void JavaTools::createJVM(string jarFolder, bool headless, int memory,
107  vector<string> extraClasspath, vector<string> extraJavaLibraryPath, vector<string> extraOptions)
108{
109  try {
110    jace::StaticVmLoader* tmpLoader = (jace::StaticVmLoader*)jace::helper::getVmLoader();
111    if(tmpLoader == NULL) {
112
113      // initialize the Java virtual machine
114      jace::OptionList list;
115      jace::StaticVmLoader loader(JNI_VERSION_1_4);
116
117      string classpath ("");
118
119      DIR *d;
120      struct dirent *dir;
121
122//TODO: test jar/*
123      // All .jar files in the jarFolder are added to the JVM's classpath
124      d = opendir(jarFolder.c_str());
125
126      if(d)
127      {
128        while ((dir = readdir(d)) != NULL)
129        {
130          string tmpName(dir->d_name);
131
132          if(tmpName.find(".jar") != string::npos)
133          {
134            if(classpath.length() >= 1)
135            {
136              classpath += PATHSTEP;
137            }
138            classpath += jarFolder + SLASH + dir->d_name;
139          }
140        }
141
142        closedir(d);
143      }
144
145      // All items in the extraClasspath are added to the JVM's classpath
146      if(!extraClasspath.empty())
147      {
148        for(vector<string>::iterator it = extraClasspath.begin(); it != extraClasspath.end(); it++)
149        {
150          classpath += PATHSTEP;
151          classpath += *it;
152        }
153      }
154
155      //std::cout << "extraClasspath : " << extraClasspath << std::endl;
156      //std::cout << "Classpath for JVM: " << classpath << std::endl;
157
158      // Gets the system-specific path environment variable's contents (which will eventually be passed to java.library.path in this JVM)
159      std::string pName = PATH;
160      char * libPath;
161      libPath = getenv(pName.c_str());
162      std::string jLibPath("");
163      if(libPath != NULL)
164      {
165        jLibPath = libPath;
166      }
167
168      // If no extra Java library path is specified, the current working directory is appended and passed to java.library.path.
169      // Otherwise, each provided path is appended.
170      if(extraJavaLibraryPath.empty())
171      {
172        if(jLibPath.length() >= 1)
173        {
174          jLibPath += PATHSTEP;
175        }
176        jLibPath += '.';
177      }
178      else
179      {
180        for(vector<string>::iterator it = extraJavaLibraryPath.begin(); it != extraJavaLibraryPath.end(); it++)
181        {
182          if(jLibPath.length() >= 1)
183          {
184           jLibPath += PATHSTEP;
185          }
186          jLibPath += *it;
187        }
188      }
189
190      list.push_back(jace::ClassPath(classpath));
191      list.push_back(jace::CustomOption("-Xcheck:jni"));
192
193      // Int to string conversion
194      std::string mem;
195      std::stringstream out;
196      out << memory;
197      mem = out.str();
198
199      list.push_back(jace::CustomOption("-Xmx" + mem + "m"));
200
201      if (headless)
202      {
203        list.push_back(jace::CustomOption("-Djava.awt.headless=true"));
204      }
205
206      list.push_back(jace::CustomOption("-Djava.library.path=" + jLibPath));
207
208      // All extra options are added as CustomOptions for JVM instantiation
209      if (!extraOptions.empty()) {
210        for (vector<string>::iterator it = extraOptions.begin(); it != extraOptions.end(); it++)
211        {
212          list.push_back(jace::CustomOption(*it));
213        }
214      }
215
216      jace::helper::createVm(loader, list, false);
217    }
218  }
219  catch (jace::JNIException& jniException) {
220    std::cerr << "Exception creating JVM: " << jniException.what() << std::endl;
221  }
222}
Note: See TracBrowser for help on using the repository browser.