|
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "Utils.h" |
|
17 #include <utf.h> |
|
18 #include <stdio.h> |
|
19 #include <stdlib.h> |
|
20 #include <errortranslator.h> |
|
21 |
|
22 |
|
23 JavaVM* gJVM; |
|
24 extern jclass gExcCls; |
|
25 |
|
26 jint JNI_OnLoad(JavaVM *vm, void *reserved) |
|
27 { |
|
28 gJVM = vm; |
|
29 return JNI_VERSION_1_4; |
|
30 } |
|
31 |
|
32 extern TPanicHandler gPanicHandler; |
|
33 |
|
34 /** |
|
35 * Constuctor taking a Java JNI string and converting it to an EPOC TPtrC. |
|
36 */ |
|
37 RJString::RJString( JNIEnv& aJni, jstring aString ) |
|
38 : iJni( aJni ), iString( aString ) |
|
39 { |
|
40 // Potential for a string to be NULL, but NULL cannot be passed into |
|
41 // JNI methods, so must check. If string is NULL, will result in empty string |
|
42 if ( iString != NULL ) |
|
43 { |
|
44 Set( aJni.GetStringChars( iString, NULL ), aJni.GetStringLength( iString ) ); |
|
45 } |
|
46 } |
|
47 |
|
48 |
|
49 |
|
50 /** |
|
51 * Frees up the JNI string resources, if they need to be freed. |
|
52 */ |
|
53 RJString::~RJString() |
|
54 { |
|
55 if ( iString != NULL ) |
|
56 { |
|
57 iJni.ReleaseStringChars( iString, this->Ptr() ); |
|
58 } |
|
59 } |
|
60 |
|
61 //---------------------------------------------------------------------------- |
|
62 /* Takes an EPOC string and returns a Java JNI string */ |
|
63 jstring CreateJavaString( JNIEnv* aJni, const TDesC& aString ) |
|
64 { |
|
65 const jchar* stringPtr = aString.Ptr(); |
|
66 const jsize stringLength = aString.Length(); |
|
67 jstring jniString = aJni->NewString( stringPtr, stringLength ); |
|
68 return jniString; |
|
69 } |
|
70 |
|
71 void ThrowRtExc(JNIEnv* aJni, const TDesC8& aMessage ) |
|
72 { |
|
73 if ( gExcCls ==NULL ) |
|
74 { |
|
75 printf("Could not create exception class java/lang/RuntimeException, unable to throw\n"); |
|
76 return; |
|
77 } |
|
78 aJni->ThrowNew(gExcCls, (const char*) aMessage.Ptr()); |
|
79 } |
|
80 |
|
81 void ThrowExc(JNIEnv* aJni, const TDesC8& aException, const TDesC8& aMessage ) |
|
82 { |
|
83 jclass jc = aJni->FindClass((const char *)aException.Ptr()); |
|
84 if ( jc == NULL ) { |
|
85 jc = gExcCls; |
|
86 } |
|
87 aJni->ThrowNew(jc, (const char*) aMessage.Ptr()); |
|
88 } |
|
89 |
|
90 void ThrowExc(JNIEnv* aJni, const TInt aErrorCode ) |
|
91 { |
|
92 TBuf8<256> exception; |
|
93 TBuf8<256> message; |
|
94 GetException(aErrorCode, exception, message); |
|
95 exception.ZeroTerminate(); |
|
96 message.ZeroTerminate(); |
|
97 jclass jc = aJni->FindClass((const char *)exception.Ptr()); |
|
98 if ( jc == NULL ) { |
|
99 jc = gExcCls; |
|
100 } |
|
101 aJni->ThrowNew(jc, (const char*) message.Ptr()); |
|
102 } |
|
103 |
|
104 |
|
105 void SosPanicHandler(const TDesC& aCat, TInt aReason) |
|
106 { |
|
107 _LIT8(KPanicString, " [SOS Panic]"); |
|
108 _LIT8(KSpace, " : "); |
|
109 TBuf8<512> exceptionMessage; |
|
110 exceptionMessage.Copy(aCat); |
|
111 exceptionMessage.Append(KSpace); |
|
112 exceptionMessage.AppendNum(aReason); |
|
113 exceptionMessage.Append(KPanicString); |
|
114 exceptionMessage.Append('\0'); |
|
115 // Get the JNIEnv |
|
116 JNIEnv* env; |
|
117 int res = gJVM->GetEnv((void**)&env, JNI_VERSION_1_4); |
|
118 |
|
119 if ( res == JNI_EDETACHED ) |
|
120 { |
|
121 printf("Attaching to the current thread...\n"); |
|
122 res = gJVM->AttachCurrentThread((void**)&env, NULL); |
|
123 } |
|
124 |
|
125 if ( res < 0 ) |
|
126 { |
|
127 printf("Could not attach current thread while handling panic!"); |
|
128 exit(-1); |
|
129 } |
|
130 ThrowRtExc(env, exceptionMessage); |
|
131 //printf("SOS panic handled by JNI : %s\n", (const char*)exceptionMessage.Ptr()); |
|
132 } |
|
133 |
|
134 |
|
135 |
|
136 const TInt KMaxExceptionClassNameLength = 200; |
|
137 static const TText8 KExceptionClassNames[][KMaxExceptionClassNameLength+1]= |
|
138 { |
|
139 #define EXCEPTIONNAME(s) #s |
|
140 #include "exceptionmappings.h" |
|
141 #undef EXCEPTIONNAME |
|
142 }; |
|
143 |
|
144 |
|
145 void GetException(TInt aErrorCode, TDes8& aException, TDes8& aMessage) |
|
146 { |
|
147 _LIT8(KRuntimeException, "java/lang/RuntimeException"); |
|
148 TranslateError(aErrorCode, aMessage); |
|
149 aErrorCode = - aErrorCode; |
|
150 if ( aErrorCode > 46 || aErrorCode < 0) |
|
151 { |
|
152 aException.Copy(KRuntimeException); |
|
153 } |
|
154 else |
|
155 { |
|
156 aException.Copy(KExceptionClassNames[aErrorCode]); |
|
157 } |
|
158 } |
|
159 |