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

Revision 7713, 5.3 KB checked in by hinerm, 9 years ago (diff)

Added jtools library to jar2lib

jtools will allow users of jar2lib access to generic utility methods.
Currently supported:

  • standardized JVM creation methods
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
53using namespace std;
54
55void JavaTools::createJVM()
56{
57  JavaTools::createJVM("", "", true, 256);
58}
59
60void JavaTools::createJVM(string classdir)
61{
62  JavaTools::createJVM(classdir, std::string(""), true, 256);
63}
64
65void JavaTools::createJVM(int memory)
66{
67  JavaTools::createJVM("", "", true, memory);
68}
69
70void JavaTools::createJVM(bool headless)
71{
72  JavaTools::createJVM("", "", headless, 256);
73}
74
75void JavaTools::createJVM(string classdir, int memory)
76{
77  JavaTools::createJVM(classdir, std::string(""), true, memory);
78}
79
80void JavaTools::createJVM(string classdir, bool headless)
81{
82  JavaTools::createJVM(classdir, std::string(""), headless, 256);
83}
84
85void JavaTools::createJVM(int memory, string jarlist)
86{
87  JavaTools::createJVM("", jarlist, true, memory);
88}
89
90void JavaTools::createJVM(bool headless, string jarlist)
91{
92  JavaTools::createJVM("",jarlist, headless, 256);
93}
94
95void JavaTools::createJVM(string classdir, string jarlist)
96{
97  JavaTools::createJVM(classdir, jarlist, true, 256);
98}
99
100void JavaTools::createJVM(string classdir, string jarlist, int memory)
101{
102  JavaTools::createJVM(classdir, jarlist, true, memory);
103}
104
105void JavaTools::createJVM(string classdir, string jarlist, bool headless)
106{
107  JavaTools::createJVM(classdir, jarlist, headless, 256);
108}
109
110void JavaTools::createJVM(string classdir, string jarlist, bool headless, int memory)
111{
112
113  try {
114    jace::StaticVmLoader* tmpLoader = (jace::StaticVmLoader*)jace::helper::getVmLoader();
115    if(tmpLoader == NULL) {
116
117      // initialize the Java virtual machine
118      jace::OptionList list;
119      jace::StaticVmLoader loader(JNI_VERSION_1_4);
120
121      if(classdir.length() >= 1 && classdir.at(classdir.length() - 1) != SLASH ) {
122        classdir.append(1,SLASH);
123      }
124
125      std::string classpath ("");
126
127      classpath += classdir + "jace-runtime.jar" + PATHSTEP;
128      classpath += classdir + "loci_tools.jar" + PATHSTEP;
129      classpath += classdir + "bio-formats.jar";
130
131      std::cout << "Classpath for JVM: " << classpath << std::endl;
132
133      if(jarlist.length() >= 1)
134      {
135        classpath += PATHSTEP;
136
137        size_t found;
138        found = jarlist.find_first_of(";");
139        while(found != string::npos)
140        {
141          classpath += classdir + jarlist.substr(0, found) + PATHSTEP;
142
143          jarlist = jarlist.substr(found + 1, jarlist.length());
144
145          found = jarlist.find_first_of(";");
146        }
147
148        classpath += classdir + jarlist.substr(0, found);
149      }
150
151      list.push_back(jace::ClassPath(
152      classpath
153      ));
154      list.push_back(jace::CustomOption("-Xcheck:jni"));
155
156      std::string mem;
157      std::stringstream out;
158      out << memory;
159      mem = out.str();
160
161      list.push_back(jace::CustomOption("-Xmx" + mem + "m"));
162
163      if (headless)
164        list.push_back(jace::CustomOption("-Djava.awt.headless=true"));
165      else
166        list.push_back(jace::CustomOption("-Djava.awt.headless=false"));
167
168      list.push_back(jace::CustomOption("-Djava.library.path=" + classpath));
169      //list.push_back(jace::CustomOption("-verbose"));
170      //list.push_back(jace::CustomOption("-verbose:jni"));
171      jace::helper::createVm(loader, list, false);
172    }
173  }
174  catch (jace::JNIException& jniException) {
175    std::cerr << "Exception creating JVM: " << jniException.what() << std::endl;
176  }
177}
Note: See TracBrowser for help on using the repository browser.