File: kjavaprocess.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,316 kB
  • sloc: cpp: 761,810; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (293 lines) | stat: -rw-r--r-- 7,377 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* This file is part of the KDE project
 *
 * Copyright (C) 2000 Richard Moore <rich@kde.org>
 *               2000 Wynn Wilkes <wynnw@caldera.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kjavaprocess.h"

#include <kdebug.h>
#include <kshell.h>
#include <kio/kprotocolmanager.h>

#include <QtCore/QTextStream>
#include <QtCore/QMap>

#include <config.h>

class KJavaProcessPrivate
{
friend class KJavaProcess;
private:
    QString jvmPath;
    QString classPath;
    QString mainClass;
    QString extraArgs;
    QString classArgs;
    QMap<QString, QString> systemProps;
};

KJavaProcess::KJavaProcess( QObject* parent )
	: KProcess( parent ),
	d(new KJavaProcessPrivate)

{
    connect( this, SIGNAL(readyReadStandardOutput()),
             this, SLOT(slotReceivedData()) );
    connect( this, SIGNAL(finished(int,QProcess::ExitStatus)),
             this, SLOT(slotExited()) );
    connect( this, SIGNAL(error(QProcess::ProcessError)),
             this, SLOT(slotExited()) );

    d->jvmPath = "java";
    d->mainClass = "-help";
}

KJavaProcess::~KJavaProcess()
{
    if ( state() != NotRunning )
    {
        kDebug(6100) << "stopping java process";
        stopJava();
    }
    delete d;
}

bool KJavaProcess::isRunning()
{
   return state() != NotRunning;
}

bool KJavaProcess::startJava()
{
   return invokeJVM();
}

void KJavaProcess::stopJava()
{
   killJVM();
}

void KJavaProcess::setJVMPath( const QString& path )
{
   d->jvmPath = path;
}

void KJavaProcess::setClasspath( const QString& classpath )
{
    d->classPath = classpath;
}

void KJavaProcess::setSystemProperty( const QString& name,
                                      const QString& value )
{
   d->systemProps.insert( name, value );
}

void KJavaProcess::setMainClass( const QString& className )
{
   d->mainClass = className;
}

void KJavaProcess::setExtraArgs( const QString& args )
{
   d->extraArgs = args;
}

void KJavaProcess::setClassArgs( const QString& args )
{
   d->classArgs = args;
}

//Private Utility Functions used by the two send() methods
QByteArray KJavaProcess::addArgs( char cmd_code, const QStringList& args )
{
    //the buffer to store stuff, etc.
    QByteArray buff;
    QTextStream output( &buff, QIODevice::ReadWrite );
    const char sep = 0;

    //make space for the command size: 8 characters...
    const QByteArray space( "        " );
    output << space;

    //write command code
    output << cmd_code;

    //store the arguments...
    if( args.isEmpty() )
    {
        output << sep;
    }
    else
    {
        QStringList::ConstIterator it = args.begin();
        const QStringList::ConstIterator itEnd = args.end();
        for( ; it != itEnd; ++it )
        {
            if( !(*it).isEmpty() )
            {
                output << (*it).toLocal8Bit();
            }
            output << sep;
        }
    }

    return buff;
}

void KJavaProcess::storeSize( QByteArray* buff )
{
    const int size = buff->size() - 8;  //subtract out the length of the size_str
    const QString size_str = QString("%1").arg( size, 8 );
    kDebug(6100) << "KJavaProcess::storeSize, size = " << size_str;

    for( int i = 0; i < 8; ++i )
        buff->data()[ i ] = size_str[i].toLatin1();
}

void KJavaProcess::send( char cmd_code, const QStringList& args )
{
    if( isRunning() )
    {
        QByteArray buff = addArgs( cmd_code, args );
        storeSize( &buff );
        kDebug(6100) << "<KJavaProcess::send " << (int)cmd_code;
        write( buff );
    }
}

void KJavaProcess::send( char cmd_code, const QStringList& args,
                         const QByteArray& data )
{
    if( isRunning() )
    {
        kDebug(6100) << "KJavaProcess::send, qbytearray is size = " << data.size();

        QByteArray buff = addArgs( cmd_code, args );
        buff += data;

        storeSize( &buff );
        write( buff );
    }
}

bool KJavaProcess::invokeJVM()
{
    QStringList args;

    if( !d->classPath.isEmpty() )
    {
        args << "-classpath";
        args << d->classPath;
    }

    //set the system properties, iterate through the qmap of system properties
    QMap<QString,QString>::ConstIterator it = d->systemProps.constBegin();
    const QMap<QString,QString>::ConstIterator itEnd = d->systemProps.constEnd();

    for( ; it != itEnd; ++it )
    {
        if( !it.key().isEmpty() )
        {
            QString currarg = "-D" + it.key();
            if( !it.value().isEmpty() )
                currarg += '=' + it.value();
            args << currarg;
        }
    }

    //load the extra user-defined arguments
    if( !d->extraArgs.isEmpty() )
    {
        KShell::Errors err;
        args += KShell::splitArgs( d->extraArgs, KShell::AbortOnMeta, &err );
        if( err != KShell::NoError )
            kWarning(6100) << "Extra args for JVM cannot be parsed, arguments = " << d->extraArgs;

    }

    args << d->mainClass;

    if ( !d->classArgs.isNull() )
        args << d->classArgs;

    kDebug(6100) << "Invoking JVM" << d->jvmPath << "now...with arguments = " << KShell::joinArgs(args);

    setOutputChannelMode(KProcess::SeparateChannels);
    setProgram( d->jvmPath, args );
    start();

    return waitForStarted();
}

void KJavaProcess::killJVM()
{
   closeReadChannel( StandardOutput );
   terminate();
}

/*  In this method, read one command and send it to the d->appletServer
 *  then return, so we don't block the event handling
 */
void KJavaProcess::slotReceivedData()
{
    //read out the length of the message,
    //read the message and send it to the applet server
    char length[9] = { 0 };
    const int num_bytes = read( length, 8 );
    if( num_bytes == -1 )
    {
        kError(6100) << "could not read 8 characters for the message length!!!!" << endl;
        return;
    }

    const QString lengthstr( length );
    bool ok;
    const int num_len = lengthstr.toInt( &ok );
    if( !ok )
    {
        kError(6100) << "could not parse length out of: " << lengthstr << endl;
        return;
    }

    //now parse out the rest of the message.
    char* const msg = new char[num_len];
    const int num_bytes_msg = read( msg, num_len );
    if( num_bytes_msg == -1 || num_bytes_msg != num_len )
    {
        kError(6100) << "could not read the msg, num_bytes_msg = " << num_bytes_msg << endl;
        delete[] msg;
        return;
    }

    emit received( QByteArray( msg, num_len ) );
    delete[] msg;
}

void KJavaProcess::slotExited()
{
    int status = -1;
    if ( exitStatus() == NormalExit ) {
     status = exitCode();
    }
    kDebug(6100) << "jvm exited with status " << status; 
    emit exited(status);
}

#include "kjavaprocess.moc"