KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > rmi > registry > NamingServiceImpl


1 /***
2  * Fractal RMI: a binder for remote method calls between Fractal components.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Bruno Dillenseger
22  */

23
24 package org.objectweb.fractal.rmi.registry;
25
26 import org.objectweb.fractal.api.Component;
27
28 import java.io.File JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Vanilla implementation of a naming service component.
34  * Persistence is not implemented.
35  */

36
37 public class NamingServiceImpl
38   implements NamingService, NamingServiceAttributes
39 {
40
41   static final String JavaDoc defaultPersistenceFile = "namingServiceState.bytes";
42
43   private boolean isPersistent = false;
44
45   private String JavaDoc persistenceFileName = null;
46
47   private Map JavaDoc bindings = new HashMap JavaDoc();
48
49   // --------------------------------------------------------------------------
50
// Implementation of the NamingService interface
51
// --------------------------------------------------------------------------
52

53   public synchronized String JavaDoc[] list () {
54     return (String JavaDoc[])bindings.keySet().toArray(new String JavaDoc[bindings.size()]);
55   }
56
57   public synchronized Component lookup (final String JavaDoc name) {
58     return (Component)bindings.get(name);
59   }
60
61   public synchronized boolean bind (final String JavaDoc name, final Component comp) {
62     if (! bindings.containsKey(name)) {
63       bindings.put(name, comp);
64       return true;
65     } else {
66       return false;
67     }
68   }
69
70   public synchronized Component rebind (final String JavaDoc name, final Component comp) {
71     Component old = (Component)bindings.remove(name);
72     bindings.put(name, comp);
73     return old;
74   }
75
76   public synchronized Component unbind (final String JavaDoc name) {
77     return (Component)bindings.remove(name);
78   }
79
80   // --------------------------------------------------------------------------
81
// Implementation of the NamingServiceAttributes interface
82
// --------------------------------------------------------------------------
83

84   public boolean getPersistenceMode () {
85     return isPersistent;
86   }
87
88   public synchronized void setPersistenceMode (final boolean on) {
89     if (on) {
90       if (!isPersistent) {
91         isPersistent = true;
92         if (persistenceFileName == null) {
93           persistenceFileName = defaultPersistenceFile;
94         }
95       }
96     }
97     else if (isPersistent) {
98       isPersistent = false;
99       new File JavaDoc(persistenceFileName).delete();
100     }
101   }
102
103   public String JavaDoc getPersistenceFileName () {
104     //System.out.println("getPersistenceFile");
105
return persistenceFileName;
106   }
107
108   public synchronized void setPersistenceFileName (final String JavaDoc fileName) {
109     //System.out.println("setPersistenceFile");
110
if (isPersistent && !fileName.equals(persistenceFileName)) {
111       new File JavaDoc(persistenceFileName).renameTo(new File JavaDoc(fileName));
112     }
113     persistenceFileName = fileName;
114   }
115 }
116
Popular Tags