xref: /trunk/main/stoc/source/javavm/javavm.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #if !defined INCLUDED_STOC_JAVAVM_JAVAVM_HXX
25 #define INCLUDED_STOC_JAVAVM_JAVAVM_HXX
26 
27 #include "jvmargs.hxx"
28 
29 #include "com/sun/star/container/XContainerListener.hpp"
30 #include "com/sun/star/lang/XInitialization.hpp"
31 #include "com/sun/star/java/XJavaThreadRegister_11.hpp"
32 #include "com/sun/star/java/XJavaVM.hpp"
33 #include "com/sun/star/lang/XServiceInfo.hpp"
34 #include "com/sun/star/uno/Reference.hxx"
35 #include "cppuhelper/compbase5.hxx"
36 #include "osl/thread.hxx"
37 #include "rtl/ref.hxx"
38 #include <osl/mutex.hxx>
39 #include <rtl/ustring.hxx>
40 
41 namespace com { namespace sun { namespace star {
42     namespace container { class XContainer; }
43     namespace uno { class XComponentContext; }
44 } } }
45 namespace jvmaccess {
46     class UnoVirtualMachine;
47     class VirtualMachine;
48 }
49 
50 namespace stoc_javavm {
51 
52 bool configureJava(const com::sun::star::uno::Reference<
53                    com::sun::star::uno::XComponentContext>& xContext);
54 // The MS compiler needs a typedef here, so the JavaVirtualMachine ctor can call
55 // its base class ctor:
56 typedef
57 cppu::WeakComponentImplHelper5< com::sun::star::lang::XInitialization,
58                                 com::sun::star::lang::XServiceInfo,
59                                 com::sun::star::java::XJavaVM,
60                                 com::sun::star::java::XJavaThreadRegister_11,
61                                 com::sun::star::container::XContainerListener >
62 JavaVirtualMachine_Impl;
63 
64 class JavaVirtualMachine: private osl::Mutex, public JavaVirtualMachine_Impl
65 {
66 public:
67     explicit JavaVirtualMachine(
68         com::sun::star::uno::Reference<
69             com::sun::star::uno::XComponentContext > const & rContext);
70 
71     // XInitialization
72     virtual void SAL_CALL
73     initialize(com::sun::star::uno::Sequence< com::sun::star::uno::Any > const &
74                    rArguments);
75 
76     // XServiceInfo
77     virtual rtl::OUString SAL_CALL getImplementationName();
78 
79     virtual sal_Bool SAL_CALL
80     supportsService(rtl::OUString const & rServiceName);
81 
82     virtual com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL
83     getSupportedServiceNames();
84 
85     // XJavaVM
86     virtual com::sun::star::uno::Any SAL_CALL
87     getJavaVM(com::sun::star::uno::Sequence< sal_Int8 > const & rProcessId);
88 
89     virtual sal_Bool SAL_CALL isVMStarted();
90 
91     virtual sal_Bool SAL_CALL isVMEnabled();
92 
93     // XJavaThreadRegister_11
94     virtual sal_Bool SAL_CALL isThreadAttached();
95 
96     virtual void SAL_CALL registerThread();
97 
98     virtual void SAL_CALL revokeThread();
99 
100     // XContainerListener
101     virtual void SAL_CALL
102     disposing(com::sun::star::lang::EventObject const & rSource);
103 
104     virtual void SAL_CALL
105     elementInserted(com::sun::star::container::ContainerEvent const & rEvent);
106 
107     virtual void SAL_CALL
108     elementRemoved(com::sun::star::container::ContainerEvent const & rEvent);
109 
110     virtual void SAL_CALL
111     elementReplaced(com::sun::star::container::ContainerEvent const & rEvent);
112 
113 private:
114     JavaVirtualMachine(JavaVirtualMachine &); // not implemented
115     void operator =(JavaVirtualMachine); // not implemented
116 
117     virtual ~JavaVirtualMachine();
118 
119     virtual void SAL_CALL disposing();
120 
121     JavaVM * createJavaVM(JVM const & jvm, JNIEnv ** pMainThreadEnv);
122         // throws com::sun::star::uno::RuntimeException
123 
124     void registerConfigChangesListener();
125 
126     void setINetSettingsInVM(bool set_reset);
127 
128     void setUpUnoVirtualMachine(JNIEnv * environment);
129 
130     void handleJniException(JNIEnv * environment);
131 
132     com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
133         m_xContext;
134 
135     // the following are controlled by the 'this' mutex:
136     bool m_bDisposed;
137     rtl::Reference< jvmaccess::VirtualMachine > m_xVirtualMachine;
138     rtl::Reference< jvmaccess::UnoVirtualMachine > m_xUnoVirtualMachine;
139     JavaVM * m_pJavaVm;
140         // stored as an instance member for backwards compatibility in getJavaVM
141     bool m_bDontCreateJvm;
142         // If the first creation of Java failed and this flag is set then the
143         // next call to getJavaVM throws a RuntimException.  This is useful when
144         // the second attempt to create Java might cause a crash.
145     com::sun::star::uno::Reference< com::sun::star::container::XContainer >
146         m_xInetConfiguration;
147     com::sun::star::uno::Reference< com::sun::star::container::XContainer >
148         m_xJavaConfiguration; // for Java settings
149 
150     osl::ThreadData m_aAttachGuards;
151 };
152 
153 }
154 
155 #endif // INCLUDED_STOC_JAVAVM_JAVAVM_HXX
156