JavaMachineManager::JavaMachineManager(ResourceManager& resman): m_resman(resman)
{
  DEBUG("Now searching the JVM installed on the system...");

    m_registryVms = JVMRegistryLookup::lookupJVM();
    m_javahomeVm = JVMEnvVarLookup::lookupJVM("JAVA_HOME");
    m_jrepathVm = JVMEnvVarLookup::lookupJVM("JRE_HOME");
    m_jdkpathVm = JVMEnvVarLookup::lookupJVM("JDK_HOME");
    m_exitCode = 0;
    m_useConsole = true;
    m_acceptExe = true;
    m_acceptDLL = true;
    m_preferDLL = false;

    if (resman.getProperty("bundledvm").length() > 0)
    {
        string bjvm = resman.getProperty("bundledvm");
        DEBUG("Found a vm bundled with the application: (" + bjvm + ")");
        m_localVMenabled = true;
	std::string home = FileUtils::concFile(resman.getCurrentDirectory(), bjvm);
        m_localVM.JavaHome = home;
    } else
    {
        m_localVMenabled = false;
    }
    DEBUG("Current directory is " + resman.getCurrentDirectory());
}
Exemple #2
0
bool SunJVMLauncher::setupVM(ResourceManager& resource, JVMBase* vm)
{
  //
  // create the properties array
  const vector<JavaProperty>& jprops = resource.getJavaProperties();
  for (int i=0; i<jprops.size(); i++)
    {
      vm->addProperty(jprops[i]);
    }

  if (resource.getProperty("maxheap") != "")
    vm->setMaxHeap( StringUtils::parseInt(resource.getProperty("maxheap") ));
  
  if (resource.getProperty("initialheap") != "")
    vm->setInitialHeap( StringUtils::parseInt(resource.getProperty("initialheap") ));
  
  if (resource.useEmbeddedJar())
    {
      std::string embj = resource.saveJarInTempFile();
      vm->addPathElement(embj);
    }
  
  std::string jnijar = resource.saveJnismoothInTempFile();
  if (jnijar != "")
    vm->addPathElement(jnijar);

  //
  // Define the classpath
  std::vector<std::string> classpath = resource.getNormalizedClassPathVector();
  for (int i=0; i<classpath.size(); i++)
    {
      vm->addPathElement(classpath[i]);
    }

  //
  // Defines the arguments passed to the java application
  //  vm->setArguments(resource.getProperty(ResourceManager::KEY_ARGUMENTS));
  std::vector<std::string> args = resource.getArguments();
  for (int i=0; i<args.size(); i++)
    {
      vm->addArgument(args[i]);
    }

}
bool SunJVMLauncher::runProc(ResourceManager& resource, bool useConsole, const string& origin)
{
    std::string classname = resource.getProperty(string(ResourceManager::KEY_MAINCLASSNAME));

    if (VmVersion.isValid() == false)
    {
        DEBUG("No version identified for " + toString());
        SunJVMExe exe(this->JavaHome);
        VmVersion = exe.guessVersion();
        DEBUG("Version found: " + VmVersion.toString());
    }

    if (VmVersion.isValid() == false)
        return false;

    if (origin != "bundled") {

        Version max(resource.getProperty(ResourceManager:: KEY_MAXVERSION));
        Version min(resource.getProperty(ResourceManager:: KEY_MINVERSION));

        if (min.isValid() && (VmVersion < min))
            return false;

        if (max.isValid() && (max < VmVersion))
            return false;
    }

    SunJVMExe exe(this->JavaHome, VmVersion);
    setupVM(resource, &exe);
    if (exe.run(classname, useConsole))
    {
        m_exitCode = exe.getExitCode();
        return true;
    }
    return false;
}
bool SunJVMLauncher::run(ResourceManager& resource, const string& origin, bool justInstanciate)
{
    DEBUG("Running now " + this->toString() + ", instanciate=" + (justInstanciate?"yes":"no"));

    Version max(resource.getProperty(ResourceManager:: KEY_MAXVERSION));
    Version min(resource.getProperty(ResourceManager:: KEY_MINVERSION));

    // patch proposed by zregvart: if you're using bundeled JVM, you
    // apriori know the version bundled and we can trust. The version
    // check is therefore unrequired.
    if (origin != "bundled") {

        if (VmVersion.isValid() == false)
        {
            DEBUG("No version identified for " + toString());
            SunJVMExe exe(this->JavaHome);
            VmVersion = exe.guessVersion();
            DEBUG("Version found: " + VmVersion.toString());
        }

        if (VmVersion.isValid() == false)
        {
            DEBUG("No version found, can't instanciate DLL without it");
            return false;
        }

        if (min.isValid() && (VmVersion < min))
            return false;

        if (max.isValid() && (max < VmVersion))
            return false;
    }

    DEBUG("Launching " + toString());

    //
    // search for the dll if it's not set in the registry, or if the
    // file doesn't exist
    //
    if ( (this->JavaHome.size()>0)
            && ((this->RuntimeLibPath.size() == 0) || (!FileUtils::fileExists(this->RuntimeLibPath))) )
    {
        std::string assump = FileUtils::concFile(this->JavaHome, "jre\\bin\\jvm.dll");
        std::string assump2 = FileUtils::concFile(this->JavaHome, "jre\\bin\\server\\jvm.dll"); // for JRE 1.5+
        std::string assump3 = FileUtils::concFile(this->JavaHome, "jre\\bin\\client\\jvm.dll"); // for JRE 1.5+
        std::string assump4 = FileUtils::concFile(this->JavaHome, "bin\\javai.dll"); // For JRE 1.1

        if (FileUtils::fileExists(assump))
            this->RuntimeLibPath = assump;
        else if (FileUtils::fileExists(assump2))
            this->RuntimeLibPath = assump2;
        else if (FileUtils::fileExists(assump3))
            this->RuntimeLibPath = assump3;
        else if (FileUtils::fileExists(assump4))
            this->RuntimeLibPath = assump4;
        else
        {
            vector<string> dlls = FileUtils::recursiveSearch(this->JavaHome, string("jvm.dll"));
            if (dlls.size() > 0)
                this->RuntimeLibPath = dlls[0];
        }
    }

    if (FileUtils::fileExists(this->RuntimeLibPath))
    {
        DEBUG("RuntimeLibPath used: " + this->RuntimeLibPath);
        Version v = this->VmVersion;
        if (!v.isValid())
        {
            v = min;
            if (!v.isValid())
                v = Version("1.2.0");
            DEBUG("No version, trying with " + v.toString());
        }

        m_dllrunner = new SunJVMDLL(this->RuntimeLibPath, v);
        // set up the vm parameters...
        setupVM(resource, m_dllrunner);

        if (justInstanciate)
            return m_dllrunner->instanciate();
        else
            return m_dllrunner->run(resource.getProperty(ResourceManager::KEY_MAINCLASSNAME),
                                    true);
    }

    return false;
}