KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > server > EmbeddedManager


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.server;
27
28 import java.lang.ref.WeakReference JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.WeakHashMap JavaDoc;
31
32 import org.objectweb.easybeans.api.EZBServer;
33
34 /**
35  * This class manages the Embedded instance that have been created. The Embedded
36  * object self register to this manager. Also, the list of the embedded server
37  * is a weak hashmap. So when an object is deleted, reference can be removed.
38  * @author Florent Benoit
39  */

40 public final class EmbeddedManager {
41
42     /**
43      * Utility class, no public constructor.
44      */

45     private EmbeddedManager() {
46
47     }
48
49     /**
50      * Map of embedded servers for some id.
51      */

52     private static Map JavaDoc<Integer JavaDoc, WeakReference JavaDoc<EZBServer>> servers = new WeakHashMap JavaDoc<Integer JavaDoc, WeakReference JavaDoc<EZBServer>>();
53
54
55     /**
56      * Gets the embedded server with the given id.
57      * @param id the identifier of the embedded server.
58      * @return the instance found or null.
59      */

60     public static EZBServer getEmbedded(final Integer JavaDoc id) {
61         WeakReference JavaDoc<EZBServer> weakRef = servers.get(id);
62         if (weakRef != null) {
63             return weakRef.get();
64         }
65         // not found
66
return null;
67     }
68
69     /**
70      * Add a new embedded server to the managed list.
71      * @param embedded a given server to add.
72      */

73     protected static void addEmbedded(final EZBServer embedded) {
74         // get ID
75
Integer JavaDoc id = embedded.getID();
76
77         // build reference (weak)
78
WeakReference JavaDoc<EZBServer> weakRef = new WeakReference JavaDoc<EZBServer>(embedded);
79
80         // add reference with given id
81
servers.put(id, weakRef);
82     }
83
84 }
85
Popular Tags