View Javadoc
1 /*
2 * Session.java
3 * Created on July 19, 2003
4 *
5 * The Blues Framework - A lightweight application framework
6 * Copyright (C) 2003 Lonnie Pryor
7 * http://blues.lonniepryor.com
8 *
9 * This library is free software; you can redistribute it and/or modify it under the
10 * terms of the GNU Lesser General Public License as published by the Free Software
11 * Foundation; either version 2.1 of the License, or (at your option) any later
12 * version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along
19 * with this library; if not, write to:
20 *
21 * The Free Software Foundation, Inc.
22 * 59 Temple Place, Suite 330
23 * Boston, MA 02111-1307 USA
24 *
25 */
26 package com.lonniepryor.blues;
27
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31
32 /***
33 * Represents a binding of an Engine to a Thread. An instance of Engine can be
34 * bound to the current Thread by creating an instance of this class. The Thread
35 * will be bound until dispose() is called on the Session instance. Instances of
36 * this class should NEVER be shared between Threads.
37 *
38 * @author Lonnie Pryor
39 * @version $Revision: 1.1 $
40 */
41 public final class Session {
42 /*** The engine we're bound to. */
43 private final Engine engine;
44 /*** The locals we're managing. */
45 private final Map locals = new HashMap();
46 /*** True if this session has been disposed. */
47 private boolean disposed = false;
48
49 /***
50 * Creates a new Session object.
51 *
52 * @param engine The Engine to bind to.
53 */
54 public Session (Engine engine) {
55 this.engine = engine;
56 Blues.bindSession(this);
57 }
58
59 /***
60 * Looks up the specified singleton in this Session.
61 *
62 * @param singletonInterface The type of the singleton instance.
63 *
64 * @return The requested singleton instance.
65 */
66 public Object lookupSingleton (Class singletonInterface) {
67 return disposed ? null : engine.getSingletonInstance(singletonInterface);
68 }
69
70 /***
71 * Looks up the specified service in this Session.
72 *
73 * @param fullName The name of the service instance.
74 *
75 * @return The requested service instance.
76 */
77 public Object lookupService (String fullName) {
78 return disposed ? null : engine.getServiceInstance(fullName);
79 }
80
81 /***
82 * Looks up the specified service in this Session.
83 *
84 * @param dispatcherInterface The name of the service instance.
85 *
86 * @return The requested service instance.
87 */
88 public Object lookupDispatcher (Class dispatcherInterface) {
89 return disposed ? null : engine.getDispatcherInstance(dispatcherInterface);
90 }
91
92 /***
93 * Destroys this Session and releases the current Thread.
94 */
95 public void dispose () {
96 if (disposed)
97 return;
98 try {
99 disposeLocals(locals.entrySet().iterator());
100 } finally {
101 disposed = true;
102 Blues.unbindSession(this);
103 }
104 }
105
106 /***
107 * Returns engine we're bound to.
108 *
109 * @return The engine we're bound to.
110 */
111 Engine getEngine () {
112 return engine;
113 }
114
115 /***
116 * Returns the value for the specified Local.
117 *
118 * @param local The Local to find the value for.
119 *
120 * @return The value for the specified Local.
121 */
122 Object getLocalValue (Local local) {
123 if (disposed)
124 return null;
125 if (locals.containsKey(local))
126 return locals.get(local);
127 Object value = local.createValue();
128 setLocalValue(local, value);
129 return value;
130 }
131
132 /***
133 * Sets the value for the specified Local.
134 *
135 * @param local The Local to set the value for.
136 * @param value The value for the specified Local.
137 */
138 void setLocalValue (Local local, Object value) {
139 if (disposed)
140 return;
141 locals.put(local, value);
142 }
143
144 /***
145 * Recursivly disposes every local in the supplied entry iterator.
146 *
147 * @param iter An iterator of Map.Entrys containing Locals and their values.
148 */
149 private void disposeLocals (Iterator iter) {
150 if (iter.hasNext()) {
151 Map.Entry entry = (Map.Entry)iter.next();
152 try {
153 if (entry.getValue() != null)
154 ((Local)entry.getKey()).disposeValue(entry.getValue());
155 } finally {
156 disposeLocals(iter);
157 }
158 }
159 }
160 }
This page was automatically generated by Maven