KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > internal > ser > ServiceSerializationHelper


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.internal.ser;
16
17 import java.lang.ref.WeakReference JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20
21 /**
22  * Class used to hold a <em>global</em> instance of
23  * {@link org.apache.hivemind.internal.ser.ServiceSerializationSupport}, so that
24  * {@link org.apache.hivemind.internal.ser.ServiceToken}s may locate them.
25  *
26  * @author Howard M. Lewis Ship
27  * @since 1.1
28  */

29 public class ServiceSerializationHelper
30 {
31     private static final ThreadLocal JavaDoc _threadLocal = new ThreadLocal JavaDoc();
32
33     /**
34      * Returns the previously stored SSS.
35      *
36      * @throws ApplicationRuntimeException
37      * if no SSS has been stored.
38      */

39     public static ServiceSerializationSupport getServiceSerializationSupport()
40     {
41         ServiceSerializationSupport result = null;
42
43         WeakReference JavaDoc reference = (WeakReference JavaDoc) _threadLocal.get();
44         if (reference != null)
45             result = (ServiceSerializationSupport) reference.get();
46
47         if (result == null)
48             throw new ApplicationRuntimeException(SerMessages.noSupportSet());
49
50         return result;
51     }
52
53     /**
54      * Stores the SSS instance for later access; if an existing SSS is already stored, then an error
55      * is logged (should be just one SSS per class loader).
56      */

57
58     public static void setServiceSerializationSupport(
59             ServiceSerializationSupport serviceSerializationSupport)
60     {
61         WeakReference JavaDoc reference = new WeakReference JavaDoc(serviceSerializationSupport);
62
63         _threadLocal.set(reference);
64     }
65 }
Popular Tags