• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.12
  • kdelibs
  • khtml
  • java
kjavaprocess.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2000 Richard Moore <rich@kde.org>
4  * 2000 Wynn Wilkes <wynnw@caldera.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "kjavaprocess.h"
23 
24 #include <kdebug.h>
25 #include <kshell.h>
26 #include <kio/kprotocolmanager.h>
27 
28 #include <QtCore/QTextStream>
29 #include <QtCore/QMap>
30 
31 #include <config.h>
32 
33 class KJavaProcessPrivate
34 {
35 friend class KJavaProcess;
36 private:
37  QString jvmPath;
38  QString classPath;
39  QString mainClass;
40  QString extraArgs;
41  QString classArgs;
42  QMap<QString, QString> systemProps;
43 };
44 
45 KJavaProcess::KJavaProcess( QObject* parent )
46  : KProcess( parent ),
47  d(new KJavaProcessPrivate)
48 
49 {
50  connect( this, SIGNAL(readyReadStandardOutput()),
51  this, SLOT(slotReceivedData()) );
52  connect( this, SIGNAL(finished(int,QProcess::ExitStatus)),
53  this, SLOT(slotExited()) );
54  connect( this, SIGNAL(error(QProcess::ProcessError)),
55  this, SLOT(slotExited()) );
56 
57  d->jvmPath = "java";
58  d->mainClass = "-help";
59 }
60 
61 KJavaProcess::~KJavaProcess()
62 {
63  if ( state() != NotRunning )
64  {
65  kDebug(6100) << "stopping java process";
66  stopJava();
67  }
68  delete d;
69 }
70 
71 bool KJavaProcess::isRunning()
72 {
73  return state() != NotRunning;
74 }
75 
76 bool KJavaProcess::startJava()
77 {
78  return invokeJVM();
79 }
80 
81 void KJavaProcess::stopJava()
82 {
83  killJVM();
84 }
85 
86 void KJavaProcess::setJVMPath( const QString& path )
87 {
88  d->jvmPath = path;
89 }
90 
91 void KJavaProcess::setClasspath( const QString& classpath )
92 {
93  d->classPath = classpath;
94 }
95 
96 void KJavaProcess::setSystemProperty( const QString& name,
97  const QString& value )
98 {
99  d->systemProps.insert( name, value );
100 }
101 
102 void KJavaProcess::setMainClass( const QString& className )
103 {
104  d->mainClass = className;
105 }
106 
107 void KJavaProcess::setExtraArgs( const QString& args )
108 {
109  d->extraArgs = args;
110 }
111 
112 void KJavaProcess::setClassArgs( const QString& args )
113 {
114  d->classArgs = args;
115 }
116 
117 //Private Utility Functions used by the two send() methods
118 QByteArray KJavaProcess::addArgs( char cmd_code, const QStringList& args )
119 {
120  //the buffer to store stuff, etc.
121  QByteArray buff;
122  QTextStream output( &buff, QIODevice::ReadWrite );
123  const char sep = 0;
124 
125  //make space for the command size: 8 characters...
126  const QByteArray space( " " );
127  output << space;
128 
129  //write command code
130  output << cmd_code;
131 
132  //store the arguments...
133  if( args.isEmpty() )
134  {
135  output << sep;
136  }
137  else
138  {
139  QStringList::ConstIterator it = args.begin();
140  const QStringList::ConstIterator itEnd = args.end();
141  for( ; it != itEnd; ++it )
142  {
143  if( !(*it).isEmpty() )
144  {
145  output << (*it).toLocal8Bit();
146  }
147  output << sep;
148  }
149  }
150 
151  return buff;
152 }
153 
154 void KJavaProcess::storeSize( QByteArray* buff )
155 {
156  const int size = buff->size() - 8; //subtract out the length of the size_str
157  const QString size_str = QString("%1").arg( size, 8 );
158  kDebug(6100) << "KJavaProcess::storeSize, size = " << size_str;
159 
160  for( int i = 0; i < 8; ++i )
161  buff->data()[ i ] = size_str[i].toLatin1();
162 }
163 
164 void KJavaProcess::send( char cmd_code, const QStringList& args )
165 {
166  if( isRunning() )
167  {
168  QByteArray buff = addArgs( cmd_code, args );
169  storeSize( &buff );
170  kDebug(6100) << "<KJavaProcess::send " << (int)cmd_code;
171  write( buff );
172  }
173 }
174 
175 void KJavaProcess::send( char cmd_code, const QStringList& args,
176  const QByteArray& data )
177 {
178  if( isRunning() )
179  {
180  kDebug(6100) << "KJavaProcess::send, qbytearray is size = " << data.size();
181 
182  QByteArray buff = addArgs( cmd_code, args );
183  buff += data;
184 
185  storeSize( &buff );
186  write( buff );
187  }
188 }
189 
190 bool KJavaProcess::invokeJVM()
191 {
192  QStringList args;
193 
194  if( !d->classPath.isEmpty() )
195  {
196  args << "-classpath";
197  args << d->classPath;
198  }
199 
200  //set the system properties, iterate through the qmap of system properties
201  QMap<QString,QString>::ConstIterator it = d->systemProps.constBegin();
202  const QMap<QString,QString>::ConstIterator itEnd = d->systemProps.constEnd();
203 
204  for( ; it != itEnd; ++it )
205  {
206  if( !it.key().isEmpty() )
207  {
208  QString currarg = "-D" + it.key();
209  if( !it.value().isEmpty() )
210  currarg += '=' + it.value();
211  args << currarg;
212  }
213  }
214 
215  //load the extra user-defined arguments
216  if( !d->extraArgs.isEmpty() )
217  {
218  KShell::Errors err;
219  args += KShell::splitArgs( d->extraArgs, KShell::AbortOnMeta, &err );
220  if( err != KShell::NoError )
221  kWarning(6100) << "Extra args for JVM cannot be parsed, arguments = " << d->extraArgs;
222 
223  }
224 
225  args << d->mainClass;
226 
227  if ( !d->classArgs.isNull() )
228  args << d->classArgs;
229 
230  kDebug(6100) << "Invoking JVM" << d->jvmPath << "now...with arguments = " << KShell::joinArgs(args);
231 
232  setOutputChannelMode(KProcess::SeparateChannels);
233  setProgram( d->jvmPath, args );
234  start();
235 
236  return waitForStarted();
237 }
238 
239 void KJavaProcess::killJVM()
240 {
241  closeReadChannel( StandardOutput );
242  terminate();
243 }
244 
245 /* In this method, read one command and send it to the d->appletServer
246  * then return, so we don't block the event handling
247  */
248 void KJavaProcess::slotReceivedData()
249 {
250  //read out the length of the message,
251  //read the message and send it to the applet server
252  char length[9] = { 0 };
253  const int num_bytes = read( length, 8 );
254  if( num_bytes == -1 )
255  {
256  kError(6100) << "could not read 8 characters for the message length!!!!" << endl;
257  return;
258  }
259 
260  const QString lengthstr( length );
261  bool ok;
262  const int num_len = lengthstr.toInt( &ok );
263  if( !ok )
264  {
265  kError(6100) << "could not parse length out of: " << lengthstr << endl;
266  return;
267  }
268 
269  //now parse out the rest of the message.
270  char* const msg = new char[num_len];
271  const int num_bytes_msg = read( msg, num_len );
272  if( num_bytes_msg == -1 || num_bytes_msg != num_len )
273  {
274  kError(6100) << "could not read the msg, num_bytes_msg = " << num_bytes_msg << endl;
275  delete[] msg;
276  return;
277  }
278 
279  emit received( QByteArray( msg, num_len ) );
280  delete[] msg;
281 }
282 
283 void KJavaProcess::slotExited()
284 {
285  int status = -1;
286  if ( exitStatus() == NormalExit ) {
287  status = exitCode();
288  }
289  kDebug(6100) << "jvm exited with status " << status;
290  emit exited(status);
291 }
292 
293 #include "kjavaprocess.moc"
KJavaProcess::stopJava
void stopJava()
Stop the JVM (if it's running).
Definition: kjavaprocess.cpp:81
KProcess
kdebug.h
KJavaProcess::startJava
bool startJava()
Invoke the JVM with the parameters that have been set.
Definition: kjavaprocess.cpp:76
d
#define d
Definition: khtmlfind.cpp:42
kshell.h
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KProcess::setOutputChannelMode
void setOutputChannelMode(OutputChannelMode mode)
QString
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KJavaProcess::setMainClass
void setMainClass(const QString &clazzName)
The class to be called when startJava() is called.
Definition: kjavaprocess.cpp:102
KJavaProcess::storeSize
void storeSize(QByteArray *buff)
Definition: kjavaprocess.cpp:154
KJavaProcess::KJavaProcess
KJavaProcess(QObject *parent=0)
Creates a process object, the process is NOT invoked at this point.
Definition: kjavaprocess.cpp:45
kprotocolmanager.h
KJavaProcess::received
void received(const QByteArray &)
KShell::splitArgs
QStringList splitArgs(const QString &cmd, Options flags=NoOptions, Errors *err=0)
KJavaProcess::slotReceivedData
void slotReceivedData()
This slot is called when the Java Process writes to standard out.
Definition: kjavaprocess.cpp:248
KJavaProcess::setSystemProperty
void setSystemProperty(const QString &name, const QString &value)
Set a property on the java command line as -Dname=value, or -Dname if value is QString().
Definition: kjavaprocess.cpp:96
KJavaProcess::addArgs
QByteArray addArgs(char cmd_code, const QStringList &args)
Definition: kjavaprocess.cpp:118
KJavaProcess::send
void send(char cmd_code, const QStringList &args)
Sends a command to the KJAS Applet Server by building a QByteArray out of the data, and then writes it standard out.
Definition: kjavaprocess.cpp:164
kjavaprocess.h
output
void output(QList< Action > actions, QHash< QString, QString > domain)
QStringList
KShell::Errors
Errors
KJavaProcess::~KJavaProcess
virtual ~KJavaProcess()
Definition: kjavaprocess.cpp:61
KJavaProcess::exited
void exited(int status)
KProcess::start
void start()
KJavaProcess::setClasspath
void setClasspath(const QString &classpath)
This will set the classpath the Java process will use.
Definition: kjavaprocess.cpp:91
ok
KGuiItem ok()
KJavaProcess::isRunning
bool isRunning()
Returns the status of the java Process- true if it's ok, false if it has died.
Definition: kjavaprocess.cpp:71
KJavaProcess::setJVMPath
void setJVMPath(const QString &path)
Used to specify the path to the Java executable to be run.
Definition: kjavaprocess.cpp:86
KProcess::SeparateChannels
KShell::NoError
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KShell::joinArgs
QString joinArgs(const QStringList &args)
KJavaProcess::setExtraArgs
void setExtraArgs(const QString &args)
Extra flags passed to the JVM.
Definition: kjavaprocess.cpp:107
KJavaProcess
Definition: kjavaprocess.h:41
KJavaProcess::setClassArgs
void setClassArgs(const QString &classArgs)
Arguments passed to the main class.
Definition: kjavaprocess.cpp:112
KShell::AbortOnMeta
QMap< QString, QString >
KJavaProcess::killJVM
virtual void killJVM()
Definition: kjavaprocess.cpp:239
KJavaProcess::invokeJVM
virtual bool invokeJVM()
Definition: kjavaprocess.cpp:190
KProcess::setProgram
void setProgram(const QString &exe, const QStringList &args=QStringList())
KJavaProcess::slotExited
void slotExited()
This slot is called when the Java Process exited.
Definition: kjavaprocess.cpp:283
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:51:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal