~zorba-coders/zorba/data-formatting-installer-fix

« back to all changes in this revision

Viewing changes to src/com/zorba-xquery/www/modules/xsl-fo.xq.src/JavaVMSingelton.cpp

  • Committer: Cezar Andrei
  • Date: 2012-04-11 13:45:56 UTC
  • Revision ID: cezar.lp@cezarandrei.com-20120411134556-4ygqd54mfeavt2np
Integrated dependency on util-jvm module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2006-2008 The FLWOR Foundation.
3
 
 * 
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 * 
8
 
 * http://www.apache.org/licenses/LICENSE-2.0
9
 
 * 
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
#include <fstream>
18
 
#include <iostream>
19
 
#include <istream>
20
 
#include <string>
21
 
#include <cstdlib>
22
 
#include <cstring>
23
 
#include <sstream>
24
 
 
25
 
#include "JavaVMSingelton.h"
26
 
#include <zorba/util/path.h>
27
 
#include <zorba/util/file.h>
28
 
#include <zorba/zorba.h>
29
 
 
30
 
 
31
 
namespace zorba { namespace jvm {
32
 
JavaVMSingelton* JavaVMSingelton::instance = NULL;
33
 
 
34
 
JavaVMSingelton::JavaVMSingelton(const char* classPath)
35
 
{
36
 
  //std::cout << "JavaVMSingelton::JavaVMSingelton classPath: " << classPath << "\n"; std::cout.flush();
37
 
 
38
 
  memset(&args, 0, sizeof(args));
39
 
  jint r;
40
 
  jint nOptions = 2;
41
 
 
42
 
  std::string classpathOption;
43
 
  std::ostringstream os;
44
 
  os << "-Djava.class.path=" << classPath;
45
 
  classpathOption = os.str();
46
 
  classPathOption = new char[classpathOption.size() + 1];
47
 
  memset(classPathOption, 0, sizeof(char) * (classpathOption.size() + 1));
48
 
  memcpy(classPathOption, classpathOption.c_str(), classpathOption.size() * sizeof(char));
49
 
  std::string lAwtArgStr = "-Djava.awt.headless=true";
50
 
  awtOption = new char[lAwtArgStr.size() + 1];
51
 
  memset(awtOption, 0, sizeof(char) * (lAwtArgStr.size() + 1));
52
 
  memcpy(awtOption, lAwtArgStr.c_str(), sizeof(char) * lAwtArgStr.size());
53
 
  awtOption[lAwtArgStr.size()] = 0;
54
 
  options[0].optionString = classPathOption;
55
 
  options[0].extraInfo = NULL;
56
 
  options[1].optionString = awtOption;
57
 
  options[1].extraInfo = NULL;
58
 
  memset(&args, 0, sizeof(args));
59
 
  args.version  = JNI_VERSION_1_2;
60
 
  args.nOptions = nOptions;
61
 
  args.options  = options;
62
 
  args.ignoreUnrecognized = JNI_FALSE;
63
 
 
64
 
  r = JNI_CreateJavaVM(&m_vm, (void **)&m_env, &args);
65
 
  if (r != JNI_OK) {
66
 
    throw VMOpenException();
67
 
  }
68
 
}
69
 
 
70
 
JavaVMSingelton::~JavaVMSingelton()
71
 
{
72
 
  if (instance) {
73
 
    delete instance;
74
 
    instance = NULL;
75
 
  }
76
 
  m_vm->DestroyJavaVM();
77
 
  if (awtOption)
78
 
    delete[] awtOption;
79
 
  if (classPathOption)
80
 
    delete[] classPathOption;
81
 
}
82
 
 
83
 
JavaVMSingelton* JavaVMSingelton::getInstance(const char* classPath)
84
 
{
85
 
//#ifdef WIN32
86
 
//  // If pointer to instance of JavaVMSingelton exists (true) then return instance pointer else look for
87
 
//  // instance pointer in memory mapped pointer. If the instance pointer does not exist in
88
 
//  // memory mapped pointer, return a newly created pointer to an instance of Abc.
89
 
 
90
 
//  return instance ?
91
 
//     instance : (instance = (JavaVMSingelton*) MemoryMappedPointers::getPointer("JavaVMSingelton")) ?
92
 
//     instance : (instance = (JavaVMSingelton*) MemoryMappedPointers::createEntry("JavaVMSingelton",(void*)new JavaVMSingelton(classPath)));
93
 
//#else
94
 
 
95
 
 
96
 
  // If pointer to instance of JavaVMSingelton exists (true) then return instance pointer
97
 
  // else return a newly created pointer to an instance of JavaVMSingelton.
98
 
  if (instance == NULL)
99
 
  {
100
 
    JavaVM *jvms;
101
 
    jsize nVMs;
102
 
    if ( JNI_GetCreatedJavaVMs(&jvms, 1, &nVMs)==0 )
103
 
    {
104
 
      //std::cout << "Got JVMs " << nVMs << "\n"; std::cout.flush();
105
 
      if (nVMs == 1)
106
 
      {
107
 
        JavaVM *jvm = jvms;
108
 
        JNIEnv *env;
109
 
        if( jvm->AttachCurrentThread((void **)&env, NULL) ==0 )
110
 
        {
111
 
          // if there is a jvm opened already by a diffrent dynamic lib
112
 
          // make a singleton for this lib with that jvm
113
 
          instance = new JavaVMSingelton(jvm, env);
114
 
        }
115
 
      }
116
 
    }
117
 
 
118
 
    if (instance == NULL)
119
 
    {
120
 
      instance = new JavaVMSingelton(classPath);
121
 
    }
122
 
  }
123
 
 
124
 
  return instance;
125
 
}
126
 
 
127
 
JavaVMSingelton* JavaVMSingelton::getInstance(const zorba::StaticContext* aStaticContext)
128
 
{
129
 
  if (instance == NULL)
130
 
  {
131
 
    String cp = computeClassPath(aStaticContext);
132
 
    return getInstance(cp.c_str());
133
 
  }
134
 
 
135
 
  return instance;
136
 
}
137
 
 
138
 
JavaVM* JavaVMSingelton::getVM()
139
 
{
140
 
  return m_vm;
141
 
}
142
 
 
143
 
JNIEnv* JavaVMSingelton::getEnv()
144
 
{
145
 
  return m_env;
146
 
}
147
 
 
148
 
 
149
 
String JavaVMSingelton::computeClassPath(const zorba::StaticContext* aStaticContext)
150
 
{
151
 
  String cp;
152
 
 
153
 
  // get classpath from global Properties
154
 
  PropertiesGlobal * properties = Zorba::getInstance(NULL)->getPropertiesGlobal();
155
 
  std::string globalClassPath;
156
 
  properties->getJVMClassPath(globalClassPath);
157
 
  cp += globalClassPath;
158
 
 
159
 
  std::vector<String> lCPV;
160
 
  aStaticContext->getFullLibPath(lCPV);
161
 
 
162
 
  String pathSeparator(filesystem_path::get_path_separator());
163
 
  String dirSeparator(filesystem_path::get_directory_separator());
164
 
 
165
 
  for (std::vector<String>::iterator lIter = lCPV.begin();
166
 
       lIter != lCPV.end(); ++lIter)
167
 
  {
168
 
    // verify it contains a jars dir
169
 
    const filesystem_path baseFsPath((*lIter).str());
170
 
    const filesystem_path jarsFsPath(std::string("jars"));
171
 
    filesystem_path jarsDirPath(baseFsPath, jarsFsPath);
172
 
 
173
 
    file jarsDir(jarsDirPath);
174
 
 
175
 
    if ( jarsDir.exists() && jarsDir.is_directory())
176
 
    {
177
 
      std::vector<std::string> list;
178
 
      jarsDir.lsdir(list);
179
 
 
180
 
      for (std::vector<std::string>::iterator itemIter = list.begin();
181
 
           itemIter != list.end(); ++itemIter)
182
 
      {
183
 
        filesystem_path itemLocalFS(*itemIter);
184
 
        filesystem_path itemFS(jarsDirPath, itemLocalFS);
185
 
        file itemFile(itemFS);
186
 
        if ( itemFile.exists() && itemFile.is_file() )
187
 
        {
188
 
          std::string itemName = itemFile.get_path();
189
 
          std::string suffix = "-classpath.txt";
190
 
          size_t found;
191
 
          found = itemName.rfind(suffix);
192
 
          if (found!=std::string::npos &&
193
 
              found + suffix.length() == itemName.length() )
194
 
          {
195
 
            std::auto_ptr<std::istream> pathFile;
196
 
            pathFile.reset(new std::ifstream (itemName.c_str ()));
197
 
            if (!pathFile->good() || pathFile->eof() )
198
 
            {
199
 
              std::cerr << "file {" << itemName << "} not found or not readable." << std::endl;
200
 
              throw itemName;
201
 
            }
202
 
 
203
 
            // read file
204
 
            char line[1024];
205
 
            while( !pathFile->eof() && !pathFile->bad() && !pathFile->fail())
206
 
            {
207
 
              pathFile->getline(line, sizeof(line));
208
 
              std::string lineStr(line);
209
 
 
210
 
              if ( lineStr.size() == 0 )
211
 
                continue;
212
 
 
213
 
              //std::cout << "line: '" << lineStr << "'" << std::endl; std::cout.flush();
214
 
 
215
 
              const std::string normalizedPath =
216
 
                  filesystem_path::normalize_path( lineStr, jarsDirPath.get_path());
217
 
 
218
 
              cp += pathSeparator + normalizedPath;
219
 
            }
220
 
          }
221
 
        }
222
 
      }
223
 
    }
224
 
  }
225
 
 
226
 
  properties->setJVMClassPath(cp.str());
227
 
 
228
 
  //std::cout << "JavaVMSingelton::computeClassPath: '" << cp << "'" << std::endl; std::cout.flush();
229
 
  return cp;
230
 
}
231
 
 
232
 
}} // namespace zorba, jvm