KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > requeststate > NameService


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

18 package org.apache.beehive.netui.pageflow.requeststate;
19
20 import javax.servlet.http.HttpSession JavaDoc;
21 import java.lang.ref.WeakReference JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 /**
25  *
26  */

27 public class NameService
28 {
29     private static final String JavaDoc NAME_SERVICE = "netui.nameService";
30
31     private HashMap JavaDoc/*<String,WeakReference>*/ _nameMap;
32     private int _nextValue;
33
34     /**
35      * private constructor allowing for a factory method to access NameService objects.
36      */

37     private NameService() {
38         _nameMap = new HashMap JavaDoc/*<String,WeakReference>*/();
39         _nextValue = 0;
40     }
41
42     /**
43      * This will return the session specific instance of a NameService. There
44      * will only be a single NameService per session.
45      * @param session the HttpSession that contains the NameService
46      * @return the NameService associated with the session.
47      */

48     public static NameService instance(HttpSession JavaDoc session)
49     {
50         // synchronize on the session so we only create a single NameService
51
// within the session.
52
synchronized (session) {
53             NameService nameService = (NameService) session.getAttribute(NAME_SERVICE);
54             if (nameService == null) {
55                 nameService = new NameService();
56                 session.setAttribute(NAME_SERVICE,nameService);
57             }
58             assert(nameService != null) : "Found invalid null name service";
59             return nameService;
60         }
61     }
62
63     /**
64      * This method will create a unique name for an INameable object. The name
65      * will be unque within the session. This will throw an IllegalStateException
66      * if INameable.setObjectName has previously been called on object.
67      * @param namePrefix The prefix of the generated name.
68      * @param object the INameable object.
69      * @throws IllegalStateException if this method is called more than once for an object
70      */

71     public synchronized void nameObject(String JavaDoc namePrefix, INameable object)
72     {
73         String JavaDoc name = namePrefix + Integer.toString(_nextValue++);
74         object.setObjectName(name);
75     }
76
77     /**
78      * This is a debug method that will set the next integer value. This is used
79      * so tests can force the name.
80      * @param val
81      */

82     public void debugSetNameIntValue(int val) {
83         _nextValue = val;
84     }
85
86     /**
87      *
88      * @param object
89      */

90     public void put(INameable object) {
91         if (object == null)
92             throw new IllegalStateException JavaDoc("object must not be null");
93         String JavaDoc name = object.getObjectName();
94         if (name == null)
95             throw new IllegalStateException JavaDoc("object has not been named");
96
97         _nameMap.put(name,new WeakReference JavaDoc(object));
98     }
99
100     /**
101      *
102      * @param name
103      * @return INameable
104      */

105     public INameable get(String JavaDoc name) {
106         if (name == null)
107             throw new IllegalStateException JavaDoc("name must not be null");
108         WeakReference JavaDoc wr = (WeakReference JavaDoc) _nameMap.get(name);
109         if (wr == null)
110             return null;
111         INameable object = (INameable) wr.get();
112         if (object == null) {
113             _nameMap.remove(name);
114         }
115         return object;
116     }
117 }
118
Popular Tags