01: /**
02: * EasyBeans
03: * Copyright (C) 2006 Bull S.A.S.
04: * Contact: easybeans@ow2.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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: EmbeddedManager.java 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.api;
25:
26: import java.lang.ref.WeakReference;
27: import java.util.Map;
28: import java.util.WeakHashMap;
29:
30: /**
31: * This class manages the Embedded instance that have been created. The Embedded
32: * object self register to this manager. Also, the list of the embedded server
33: * is a weak hashmap. So when an object is deleted, reference can be removed.
34: * @author Florent Benoit
35: */
36: public final class EmbeddedManager {
37:
38: /**
39: * Utility class, no public constructor.
40: */
41: private EmbeddedManager() {
42:
43: }
44:
45: /**
46: * Map of embedded servers for some id.
47: */
48: private static Map<Integer, WeakReference<EZBServer>> servers = new WeakHashMap<Integer, WeakReference<EZBServer>>();
49:
50: /**
51: * Gets the embedded server with the given id.
52: * @param id the identifier of the embedded server.
53: * @return the instance found or null.
54: */
55: public static EZBServer getEmbedded(final Integer id) {
56: WeakReference<EZBServer> weakRef = servers.get(id);
57: if (weakRef != null) {
58: return weakRef.get();
59: }
60: // not found
61: return null;
62: }
63:
64: /**
65: * Add a new embedded server to the managed list.
66: * @param embedded a given server to add.
67: */
68: public static void addEmbedded(final EZBServer embedded) {
69: // get ID
70: Integer id = embedded.getID();
71:
72: // build reference (weak)
73: WeakReference<EZBServer> weakRef = new WeakReference<EZBServer>(
74: embedded);
75:
76: // add reference with given id
77: servers.put(id, weakRef);
78: }
79:
80: }
|