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

Revision 7731, 5.8 KB checked in by hinerm, 8 years ago (diff)

Fixes for ticket #298 and #654

Ticket #298 fixes:

  • search for a /jar subdirectory in working directory or provided classpath
  • all files in /jar are added to the classpath of the instantiated JVM

Ticket #654 fixes:

  • copying of project headers
  • copying of proxy headers
  • copying of jar dependencies to /jar
  • removal of loci_tool dependency
  • packaging improvements to remove full path references
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 jarlist)
61{
62  JavaTools::createJVM("", jarlist, 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, "", true, memory);
78}
79
80void JavaTools::createJVM(string classdir, bool headless)
81{
82  JavaTools::createJVM(classdir, "", 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
110//TODO: Add option override java library path
111/**
112* jarlist is a semi-colon-separated list of additional jars to include
113**/
114void JavaTools::createJVM(string classdir, string jarlist, bool headless, int memory)
115{
116
117  try {
118    jace::StaticVmLoader* tmpLoader = (jace::StaticVmLoader*)jace::helper::getVmLoader();
119    if(tmpLoader == NULL) {
120
121      // initialize the Java virtual machine
122      jace::OptionList list;
123      jace::StaticVmLoader loader(JNI_VERSION_1_4);
124
125      if(classdir.length() >= 1 && classdir.at(classdir.length() - 1) != SLASH ) {
126        classdir.append(1,SLASH);
127      }
128
129      std::string classpath ("");
130
131      classpath += classdir + "jace-runtime.jar";
132
133      DIR *d;
134      struct dirent *dir;
135
136      if(classdir.length() >= 1)
137      {
138        string tmp_dir(classdir);
139        tmp_dir += "jar";
140        d = opendir(tmp_dir.c_str());
141      }
142      else
143      {
144        d = opendir("./jar");
145      }
146
147      if(d)
148      {
149        while ((dir = readdir(d)) != NULL)
150        {
151          string tmp_name(dir->d_name);
152
153          if(tmp_name.compare(".") != 0 && tmp_name.compare("..") !=0)
154          {
155            classpath += PATHSTEP;
156            classpath += classdir + "jar" + SLASH + dir->d_name;
157          }
158        }
159
160        closedir(d);
161      }
162
163
164      if(jarlist.length() >= 1)
165      {
166        classpath += PATHSTEP;
167
168        size_t found;
169        found = jarlist.find_first_of(";");
170        while(found != string::npos)
171        {
172          classpath += classdir + jarlist.substr(0, found) + PATHSTEP;
173
174          jarlist = jarlist.substr(found + 1, jarlist.length());
175
176          found = jarlist.find_first_of(";");
177        }
178
179        classpath += classdir + jarlist.substr(0, found);
180      }
181
182      //std::cout << "jarlist : " << jarlist << std::endl;
183      //std::cout << "Classpath for JVM: " << classpath << std::endl;
184
185      list.push_back(jace::ClassPath(
186      classpath
187      ));
188      list.push_back(jace::CustomOption("-Xcheck:jni"));
189
190      std::string mem;
191      std::stringstream out;
192      out << memory;
193      mem = out.str();
194
195      list.push_back(jace::CustomOption("-Xmx" + mem + "m"));
196
197      if (headless)
198        list.push_back(jace::CustomOption("-Djava.awt.headless=true"));
199      //list.push_back(jace::CustomOption("-verbose"));
200      //list.push_back(jace::CustomOption("-verbose:jni"));
201      jace::helper::createVm(loader, list, false);
202    }
203  }
204  catch (jace::JNIException& jniException) {
205    std::cerr << "Exception creating JVM: " << jniException.what() << std::endl;
206  }
207}
Note: See TracBrowser for help on using the repository browser.