~zorba-coders/zorba/bug955040

« 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-03-06 08:11:49 UTC
  • Revision ID: cezar.lp@cezarandrei.com-20120306081149-ln2h00vpefiirezq
Integrate data-formating module into the same paradigm of java modules.
This fixes starting the JVM only once, and stating it right with the entire classpath of all the modules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 * limitations under the License.
15
15
 */
16
16
 
 
17
#include <fstream>
17
18
#include <iostream>
 
19
#include <istream>
18
20
#include <string>
19
21
#include <cstdlib>
20
22
#include <cstring>
21
23
#include <sstream>
22
24
 
23
25
#include "JavaVMSingelton.h"
24
 
 
25
 
namespace zorba { namespace xslfo {
 
26
#include <zorba/util/path.h>
 
27
#include <zorba/util/file.h>
 
28
#include <zorba/zorba.h>
 
29
 
 
30
 
 
31
namespace zorba { namespace jvm {
26
32
JavaVMSingelton* JavaVMSingelton::instance = NULL;
27
33
 
28
34
JavaVMSingelton::JavaVMSingelton(const char* classPath)
29
35
{
 
36
  std::cout << "JavaVMSingelton::JavaVMSingelton classPath: " << classPath << "\n"; std::cout.flush();
 
37
 
30
38
  memset(&args, 0, sizeof(args));
31
39
  jint r;
32
40
  jint nOptions = 2;
62
70
JavaVMSingelton::~JavaVMSingelton()
63
71
{
64
72
  if (instance) {
 
73
    delete instance;
65
74
    instance = NULL;
66
75
  }
67
 
  m_vm->DestroyJavaVM();
68
 
  delete[] awtOption;
69
 
  delete[] classPathOption;
 
76
  //m_vm->DestroyJavaVM();
 
77
  if (awtOption)
 
78
    delete[] awtOption;
 
79
  if (classPathOption)
 
80
    delete[] classPathOption;
70
81
}
71
82
 
72
83
JavaVMSingelton* JavaVMSingelton::getInstance(const char* classPath)
73
84
{
74
 
  if (instance == NULL) {
75
 
    instance = new JavaVMSingelton(classPath);
 
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
    }
76
122
  }
 
123
 
77
124
  return instance;
78
125
}
79
126
 
80
 
void JavaVMSingelton::destroyInstance() {
81
 
  delete instance;
 
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;
82
136
}
83
137
 
84
138
JavaVM* JavaVMSingelton::getVM()
91
145
  return m_env;
92
146
}
93
147
 
94
 
}} // namespace zorba, xslfo
 
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)->getProperties();
 
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