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 package com.sun.star.lib.uno.environments.remote;
25 
26 /**
27  * Manages the UNO thread pool factory.
28  *
29  * <P>The thread pool factory is a process-wide resource.  It is important that
30  * all UNO environments within a process share the same thread pool mechanisms:
31  * if a synchronous UNO call is bridged out from one local UNO environment over
32  * one remote bridge, and recursively calls back into another local UNO
33  * environment over another remote bridge, the code in the second environment
34  * should be executed in the thread that did the original call from the first
35  * environment.</P>
36  *
37  * <P>There are both a Java and a native thread pool factory.  A pure Java
38  * process will always use the Java thread pool factory.  A mixed process uses
39  * the system property <CODE>org.openoffice.native</CODE> (to be set by the
40  * native code that starts the JVM) to determine which implementation
41  * to use.</P>
42  */
43 public final class ThreadPoolManager {
44     /**
45      * Creates a thread pool instance.
46      *
47      * @return a new thread pool instance; will never be <CODE>null</CODE>
48      */
create()49     public static synchronized IThreadPool create() {
50         if (useNative) {
51             return new NativeThreadPool();
52         } else {
53             if (javaFactory == null) {
54                 javaFactory = new JavaThreadPoolFactory();
55             }
56             return javaFactory.createThreadPool();
57         }
58     }
59 
60     /**
61      * Leads to using the native thread pool factory, unless a Java thread pool
62      * has already been created.
63      *
64      * @return <CODE>false</CODE> if a Java thread pool has already been created
65      */
useNative()66     public static synchronized boolean useNative() {
67         useNative = javaFactory == null;
68         return useNative;
69     }
70 
71     private static boolean useNative
72     = System.getProperty("org.openoffice.native") != null;
73     private static JavaThreadPoolFactory javaFactory = null;
74 
ThreadPoolManager()75     private ThreadPoolManager() {} // do not instantiate
76 }
77