View Javadoc
1 /*
2 * Blues.java
3 * Created on July 4, 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.LinkedList;
29
30 import com.lonniepryor.blues.util.BluesException;
31
32 /***
33 * Static entry point into the framework. Once an instance of Session has been created
34 * for a specific application, the components of that application become available
35 * through this class's static interface until the Session is disposed.
36 *
37 * @author Lonnie Pryor
38 * @version $Revision: 1.1 $
39 */
40 public final class Blues {
41 /*** The all-powerful thread-local. */
42 private static final ThreadLocal sessionStack = new ThreadLocal();
43
44 /***
45 * Creates a new Blues object.
46 */
47 private Blues () {
48 throw new UnsupportedOperationException();
49 }
50
51 /***
52 * Looks up the specified singleton in the currently bound Session.
53 *
54 * @param singletonType The type of the singleton instance.
55 *
56 * @return The requested singleton instance.
57 */
58 public static Object lookupSingleton (Class singletonType) {
59 return currentSession().lookupSingleton(singletonType);
60 }
61
62 /***
63 * Looks up the specified service in the currently bound Session.
64 *
65 * @param fullName The fully-qualified name of the service instance.
66 *
67 * @return The requested service instance.
68 */
69 public static Object lookupService (String fullName) {
70 return currentSession().lookupService(fullName);
71 }
72
73 /***
74 * Looks up the specified event dispatcher in the currently bound Session.
75 *
76 * @param dispatcherInterface The type of the dispatcher interface.
77 *
78 * @return The requested dispatcher instance.
79 */
80 public static Object lookupDispatcher (Class dispatcherInterface) {
81 return currentSession().lookupDispatcher(dispatcherInterface);
82 }
83
84 /***
85 * Binds the supplied Session to the current thread.
86 *
87 * @param session The session to bind.
88 */
89 static void bindSession (Session session) {
90 LinkedList stack = (LinkedList)sessionStack.get();
91 if (stack == null)
92 sessionStack.set(stack = new LinkedList());
93 stack.addLast(session);
94 }
95
96 /***
97 * Returns the currently bound session for the calling thread.
98 *
99 * @return The currently bound session for the calling thread.
100 */
101 static Session currentSession () {
102 LinkedList stack = (LinkedList)sessionStack.get();
103 if (stack == null)
104 throw new BluesException("No session bound to the current Thread");
105 return (Session)stack.getLast();
106 }
107
108 /***
109 * Unbinds the supplied Session from the current thread.
110 *
111 * @param session The session to unbind.
112 */
113 static void unbindSession (Session session) {
114 LinkedList stack = (LinkedList)sessionStack.get();
115 if (stack == null)
116 throw new BluesException("No session bound to the current Thread");
117 Session old = (Session)stack.getLast();
118 if (old != session)
119 throw new IllegalArgumentException(
120 session + " not bound to the current Thread");
121 stack.removeLast();
122 if (stack.isEmpty())
123 sessionStack.set(null);
124 }
125 }
This page was automatically generated by Maven