xref: /AOO41X/main/jurt/com/sun/star/lib/uno/environments/remote/ThreadPoolManager.java (revision 2be432768a66cc90838f6a32e76ec156f587e741)
1*2be43276SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2be43276SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2be43276SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2be43276SAndrew Rist  * distributed with this work for additional information
6*2be43276SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2be43276SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2be43276SAndrew Rist  * "License"); you may not use this file except in compliance
9*2be43276SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2be43276SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2be43276SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2be43276SAndrew Rist  * software distributed under the License is distributed on an
15*2be43276SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2be43276SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2be43276SAndrew Rist  * specific language governing permissions and limitations
18*2be43276SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2be43276SAndrew Rist  *************************************************************/
21*2be43276SAndrew Rist 
22*2be43276SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.lib.uno.environments.remote;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir /**
27cdf0e10cSrcweir  * Manages the UNO thread pool factory.
28cdf0e10cSrcweir  *
29cdf0e10cSrcweir  * <P>The thread pool factory is a process-wide resource.  It is important that
30cdf0e10cSrcweir  * all UNO environments within a process share the same thread pool mechanisms:
31cdf0e10cSrcweir  * if a synchronous UNO call is bridged out from one local UNO environment over
32cdf0e10cSrcweir  * one remote bridge, and recursively calls back into another local UNO
33cdf0e10cSrcweir  * environment over another remote bridge, the code in the second environment
34cdf0e10cSrcweir  * should be executed in the thread that did the original call from the first
35cdf0e10cSrcweir  * environment.</P>
36cdf0e10cSrcweir  *
37cdf0e10cSrcweir  * <P>There are both a Java and a native thread pool factory.  A pure Java
38cdf0e10cSrcweir  * process will always use the Java thread pool factory.  A mixed process uses
39cdf0e10cSrcweir  * the system property <CODE>org.openoffice.native</CODE> (to be set by the
40cdf0e10cSrcweir  * native code that starts the JVM) to determine which implementation
41cdf0e10cSrcweir  * to use.</P>
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir public final class ThreadPoolManager {
44cdf0e10cSrcweir     /**
45cdf0e10cSrcweir      * Creates a thread pool instance.
46cdf0e10cSrcweir      *
47cdf0e10cSrcweir      * @return a new thread pool instance; will never be <CODE>null</CODE>
48cdf0e10cSrcweir      */
create()49cdf0e10cSrcweir     public static synchronized IThreadPool create() {
50cdf0e10cSrcweir         if (useNative) {
51cdf0e10cSrcweir             return new NativeThreadPool();
52cdf0e10cSrcweir         } else {
53cdf0e10cSrcweir             if (javaFactory == null) {
54cdf0e10cSrcweir                 javaFactory = new JavaThreadPoolFactory();
55cdf0e10cSrcweir             }
56cdf0e10cSrcweir             return javaFactory.createThreadPool();
57cdf0e10cSrcweir         }
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /**
61cdf0e10cSrcweir      * Leads to using the native thread pool factory, unless a Java thread pool
62cdf0e10cSrcweir      * has already been created.
63cdf0e10cSrcweir      *
64cdf0e10cSrcweir      * @return <CODE>false</CODE> if a Java thread pool has already been created
65cdf0e10cSrcweir      */
useNative()66cdf0e10cSrcweir     public static synchronized boolean useNative() {
67cdf0e10cSrcweir         useNative = javaFactory == null;
68cdf0e10cSrcweir         return useNative;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     private static boolean useNative
72cdf0e10cSrcweir     = System.getProperty("org.openoffice.native") != null;
73cdf0e10cSrcweir     private static JavaThreadPoolFactory javaFactory = null;
74cdf0e10cSrcweir 
ThreadPoolManager()75cdf0e10cSrcweir     private ThreadPoolManager() {} // do not instantiate
76cdf0e10cSrcweir }
77