4207 |
04 Apr 08 |
nicklas |
1 |
/** |
4207 |
04 Apr 08 |
nicklas |
$Id$ |
4207 |
04 Apr 08 |
nicklas |
3 |
|
4207 |
04 Apr 08 |
nicklas |
Copyright (C) Authors contributing to this file. |
4207 |
04 Apr 08 |
nicklas |
5 |
|
4207 |
04 Apr 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4207 |
04 Apr 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4207 |
04 Apr 08 |
nicklas |
8 |
|
4207 |
04 Apr 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4207 |
04 Apr 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
4207 |
04 Apr 08 |
nicklas |
of the License, or (at your option) any later version. |
4207 |
04 Apr 08 |
nicklas |
13 |
|
4207 |
04 Apr 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4207 |
04 Apr 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4207 |
04 Apr 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4207 |
04 Apr 08 |
nicklas |
GNU General Public License for more details. |
4207 |
04 Apr 08 |
nicklas |
18 |
|
4207 |
04 Apr 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4207 |
04 Apr 08 |
nicklas |
21 |
*/ |
4207 |
04 Apr 08 |
nicklas |
22 |
package net.sf.basedb.util.extensions; |
4207 |
04 Apr 08 |
nicklas |
23 |
|
4618 |
30 Oct 08 |
nicklas |
24 |
import net.sf.basedb.util.extensions.Registry.RegisteredExtension; |
4618 |
30 Oct 08 |
nicklas |
25 |
|
4207 |
04 Apr 08 |
nicklas |
26 |
/** |
4207 |
04 Apr 08 |
nicklas |
Invokation context for an extension. Most |
4207 |
04 Apr 08 |
nicklas |
calls are forwarded to the invokation context for the |
4207 |
04 Apr 08 |
nicklas |
extension point this extension belongs to. |
4207 |
04 Apr 08 |
nicklas |
30 |
|
4207 |
04 Apr 08 |
nicklas |
@author nicklas |
4207 |
04 Apr 08 |
nicklas |
@version 2.7 |
4207 |
04 Apr 08 |
nicklas |
@base.modified $Date$ |
4207 |
04 Apr 08 |
nicklas |
34 |
*/ |
4207 |
04 Apr 08 |
nicklas |
35 |
class ExtensionContext<A extends Action> |
4207 |
04 Apr 08 |
nicklas |
36 |
extends InvokationContext<A> |
4207 |
04 Apr 08 |
nicklas |
37 |
{ |
5486 |
12 Nov 10 |
nicklas |
38 |
private final ExtensionPointContext<A> mainContext; |
4618 |
30 Oct 08 |
nicklas |
39 |
private final RegisteredExtension<A> extension; |
4207 |
04 Apr 08 |
nicklas |
40 |
private Renderer<? super A> renderer; |
4207 |
04 Apr 08 |
nicklas |
41 |
private boolean hasCreatedRenderer; |
4207 |
04 Apr 08 |
nicklas |
42 |
|
5486 |
12 Nov 10 |
nicklas |
43 |
ExtensionContext(ExtensionPointContext<A> mainContext, RegisteredExtension<A> extension) |
4207 |
04 Apr 08 |
nicklas |
44 |
{ |
4207 |
04 Apr 08 |
nicklas |
45 |
this.mainContext = mainContext; |
4207 |
04 Apr 08 |
nicklas |
46 |
this.extension = extension; |
4207 |
04 Apr 08 |
nicklas |
47 |
} |
4207 |
04 Apr 08 |
nicklas |
48 |
/* |
4207 |
04 Apr 08 |
nicklas |
From the InvokationContext object |
4207 |
04 Apr 08 |
nicklas |
50 |
--------------------------------- |
4207 |
04 Apr 08 |
nicklas |
51 |
*/ |
4207 |
04 Apr 08 |
nicklas |
52 |
@Override |
4207 |
04 Apr 08 |
nicklas |
53 |
public ClientContext getClientContext() |
4207 |
04 Apr 08 |
nicklas |
54 |
{ |
4207 |
04 Apr 08 |
nicklas |
55 |
return mainContext.getClientContext(); |
4207 |
04 Apr 08 |
nicklas |
56 |
} |
4207 |
04 Apr 08 |
nicklas |
57 |
|
4207 |
04 Apr 08 |
nicklas |
58 |
@Override |
4207 |
04 Apr 08 |
nicklas |
59 |
public ExtensionPoint<? super A> getExtensionPoint() |
4207 |
04 Apr 08 |
nicklas |
60 |
{ |
4207 |
04 Apr 08 |
nicklas |
61 |
return mainContext.getExtensionPoint(); |
4207 |
04 Apr 08 |
nicklas |
62 |
} |
4207 |
04 Apr 08 |
nicklas |
63 |
|
4207 |
04 Apr 08 |
nicklas |
64 |
@Override |
4207 |
04 Apr 08 |
nicklas |
65 |
public Extension<A> getExtension() |
4207 |
04 Apr 08 |
nicklas |
66 |
{ |
4207 |
04 Apr 08 |
nicklas |
67 |
return extension; |
4207 |
04 Apr 08 |
nicklas |
68 |
} |
4207 |
04 Apr 08 |
nicklas |
69 |
|
6030 |
28 Mar 12 |
nicklas |
70 |
/** |
6030 |
28 Mar 12 |
nicklas |
The class loader is the class loader that loaded the action |
6030 |
28 Mar 12 |
nicklas |
factory of the extension. |
6030 |
28 Mar 12 |
nicklas |
73 |
*/ |
4207 |
04 Apr 08 |
nicklas |
74 |
@Override |
6030 |
28 Mar 12 |
nicklas |
75 |
public ClassLoader getClassLoader() |
6030 |
28 Mar 12 |
nicklas |
76 |
{ |
6030 |
28 Mar 12 |
nicklas |
77 |
return extension.getClassLoader(); |
6030 |
28 Mar 12 |
nicklas |
78 |
} |
6030 |
28 Mar 12 |
nicklas |
79 |
|
6030 |
28 Mar 12 |
nicklas |
80 |
@Override |
4207 |
04 Apr 08 |
nicklas |
81 |
public Object getAttribute(String name) |
4207 |
04 Apr 08 |
nicklas |
82 |
{ |
4208 |
07 Apr 08 |
nicklas |
83 |
return mainContext.getRegistry().getAttribute(extension.getId(), name); |
4207 |
04 Apr 08 |
nicklas |
84 |
} |
4207 |
04 Apr 08 |
nicklas |
85 |
|
4207 |
04 Apr 08 |
nicklas |
86 |
@Override |
4207 |
04 Apr 08 |
nicklas |
87 |
protected Registry getRegistry() |
4207 |
04 Apr 08 |
nicklas |
88 |
{ |
4207 |
04 Apr 08 |
nicklas |
89 |
return mainContext.getRegistry(); |
4207 |
04 Apr 08 |
nicklas |
90 |
} |
4207 |
04 Apr 08 |
nicklas |
91 |
|
4207 |
04 Apr 08 |
nicklas |
92 |
@Override |
4207 |
04 Apr 08 |
nicklas |
93 |
protected void prepareRendererFactory() |
4207 |
04 Apr 08 |
nicklas |
94 |
{ |
4207 |
04 Apr 08 |
nicklas |
95 |
RendererFactory<? super A> factory = extension.getRendererFactory(); |
4264 |
30 Apr 08 |
nicklas |
96 |
if (factory != null) |
4264 |
30 Apr 08 |
nicklas |
97 |
{ |
4264 |
30 Apr 08 |
nicklas |
98 |
try |
4264 |
30 Apr 08 |
nicklas |
99 |
{ |
4264 |
30 Apr 08 |
nicklas |
100 |
factory.prepareContext(this); |
4264 |
30 Apr 08 |
nicklas |
101 |
} |
4322 |
30 May 08 |
nicklas |
102 |
catch (Throwable t) |
4264 |
30 Apr 08 |
nicklas |
103 |
{ |
5486 |
12 Nov 10 |
nicklas |
104 |
handleError("Error preparing renderer factory for extension '" + |
5486 |
12 Nov 10 |
nicklas |
105 |
extension.getId() + "': " + factory, t); |
4264 |
30 Apr 08 |
nicklas |
106 |
} |
4264 |
30 Apr 08 |
nicklas |
107 |
} |
4207 |
04 Apr 08 |
nicklas |
108 |
} |
4207 |
04 Apr 08 |
nicklas |
109 |
|
4207 |
04 Apr 08 |
nicklas |
110 |
/** |
4207 |
04 Apr 08 |
nicklas |
If the extension point allows it and this extension |
4207 |
04 Apr 08 |
nicklas |
specifies a renderer factory, create a new renderer from |
4207 |
04 Apr 08 |
nicklas |
it. Otherwise, get the renderer from the extension point. |
4207 |
04 Apr 08 |
nicklas |
114 |
*/ |
4207 |
04 Apr 08 |
nicklas |
115 |
@Override |
4207 |
04 Apr 08 |
nicklas |
116 |
protected Renderer<? super A> getRenderer() |
4207 |
04 Apr 08 |
nicklas |
117 |
{ |
4207 |
04 Apr 08 |
nicklas |
118 |
if (!hasCreatedRenderer) |
4207 |
04 Apr 08 |
nicklas |
119 |
{ |
4207 |
04 Apr 08 |
nicklas |
120 |
ExtensionPoint<? super A> ep = mainContext.getExtensionPoint(); |
4207 |
04 Apr 08 |
nicklas |
121 |
if (ep.allowRendererOverride()) |
4207 |
04 Apr 08 |
nicklas |
122 |
{ |
4207 |
04 Apr 08 |
nicklas |
123 |
RendererFactory<? super A> factory = extension.getRendererFactory(); |
4207 |
04 Apr 08 |
nicklas |
124 |
if (factory != null) |
4207 |
04 Apr 08 |
nicklas |
125 |
{ |
5486 |
12 Nov 10 |
nicklas |
126 |
try |
5486 |
12 Nov 10 |
nicklas |
127 |
{ |
5486 |
12 Nov 10 |
nicklas |
128 |
renderer = factory.getRenderer(this); |
5486 |
12 Nov 10 |
nicklas |
129 |
} |
5486 |
12 Nov 10 |
nicklas |
130 |
catch (Throwable t) |
5486 |
12 Nov 10 |
nicklas |
131 |
{ |
5486 |
12 Nov 10 |
nicklas |
132 |
handleError("Error creating renderer for for extension '" + |
5486 |
12 Nov 10 |
nicklas |
133 |
extension.getId() + "': " + factory, t); |
5486 |
12 Nov 10 |
nicklas |
134 |
} |
4207 |
04 Apr 08 |
nicklas |
135 |
} |
4207 |
04 Apr 08 |
nicklas |
136 |
} |
4207 |
04 Apr 08 |
nicklas |
137 |
if (renderer == null) |
4207 |
04 Apr 08 |
nicklas |
138 |
{ |
4207 |
04 Apr 08 |
nicklas |
139 |
renderer = mainContext.getRenderer(); |
4207 |
04 Apr 08 |
nicklas |
140 |
} |
4207 |
04 Apr 08 |
nicklas |
141 |
hasCreatedRenderer = true; |
4207 |
04 Apr 08 |
nicklas |
142 |
} |
4207 |
04 Apr 08 |
nicklas |
143 |
return renderer; |
4207 |
04 Apr 08 |
nicklas |
144 |
} |
4207 |
04 Apr 08 |
nicklas |
145 |
|
4207 |
04 Apr 08 |
nicklas |
146 |
/** |
4207 |
04 Apr 08 |
nicklas |
Call {@link Extension#getActionFactory()} and then |
4207 |
04 Apr 08 |
nicklas |
{@link ActionFactory#getActions(InvokationContext)} on the |
4207 |
04 Apr 08 |
nicklas |
returned factory. |
4207 |
04 Apr 08 |
nicklas |
150 |
*/ |
4207 |
04 Apr 08 |
nicklas |
151 |
@Override |
4207 |
04 Apr 08 |
nicklas |
152 |
protected A[] getActions() |
4207 |
04 Apr 08 |
nicklas |
153 |
{ |
4264 |
30 Apr 08 |
nicklas |
154 |
A[] actions = null; |
4264 |
30 Apr 08 |
nicklas |
155 |
ActionFactory<? extends A> factory = extension.getActionFactory(); |
4264 |
30 Apr 08 |
nicklas |
156 |
try |
4264 |
30 Apr 08 |
nicklas |
157 |
{ |
4264 |
30 Apr 08 |
nicklas |
158 |
actions = factory.getActions(this); |
4264 |
30 Apr 08 |
nicklas |
159 |
} |
4322 |
30 May 08 |
nicklas |
160 |
catch (Throwable t) |
4264 |
30 Apr 08 |
nicklas |
161 |
{ |
5486 |
12 Nov 10 |
nicklas |
162 |
handleError("Error creating actions for extension '" + |
5486 |
12 Nov 10 |
nicklas |
163 |
extension.getId() + "' using factory: " + factory, t); |
4264 |
30 Apr 08 |
nicklas |
164 |
} |
4264 |
30 Apr 08 |
nicklas |
165 |
return actions; |
4207 |
04 Apr 08 |
nicklas |
166 |
} |
4618 |
30 Oct 08 |
nicklas |
167 |
|
4618 |
30 Oct 08 |
nicklas |
168 |
@Override |
4618 |
30 Oct 08 |
nicklas |
169 |
protected void setError(A action, Throwable t) |
4618 |
30 Oct 08 |
nicklas |
170 |
{ |
4618 |
30 Oct 08 |
nicklas |
171 |
extension.setError(action, t); |
4618 |
30 Oct 08 |
nicklas |
172 |
} |
4618 |
30 Oct 08 |
nicklas |
173 |
|
4618 |
30 Oct 08 |
nicklas |
174 |
@Override |
4618 |
30 Oct 08 |
nicklas |
175 |
protected void clearError() |
4618 |
30 Oct 08 |
nicklas |
176 |
{ |
4618 |
30 Oct 08 |
nicklas |
177 |
extension.clearError(); |
4618 |
30 Oct 08 |
nicklas |
178 |
} |
5486 |
12 Nov 10 |
nicklas |
179 |
|
5486 |
12 Nov 10 |
nicklas |
180 |
@Override |
5486 |
12 Nov 10 |
nicklas |
181 |
protected ErrorHandler<? super A> getErrorHandler() |
5486 |
12 Nov 10 |
nicklas |
182 |
{ |
5486 |
12 Nov 10 |
nicklas |
183 |
return mainContext.getErrorHandler(); |
5486 |
12 Nov 10 |
nicklas |
184 |
} |
4207 |
04 Apr 08 |
nicklas |
185 |
// ---------------------------------------- |
4207 |
04 Apr 08 |
nicklas |
186 |
|
4207 |
04 Apr 08 |
nicklas |
187 |
boolean prepareActionFactory() |
4207 |
04 Apr 08 |
nicklas |
188 |
{ |
4264 |
30 Apr 08 |
nicklas |
189 |
boolean prepared = false; |
4264 |
30 Apr 08 |
nicklas |
190 |
ActionFactory<? extends A> factory = extension.getActionFactory(); |
4264 |
30 Apr 08 |
nicklas |
191 |
try |
4264 |
30 Apr 08 |
nicklas |
192 |
{ |
4264 |
30 Apr 08 |
nicklas |
193 |
prepared = factory.prepareContext(this); |
4264 |
30 Apr 08 |
nicklas |
194 |
} |
4322 |
30 May 08 |
nicklas |
195 |
catch (Throwable t) |
4264 |
30 Apr 08 |
nicklas |
196 |
{ |
5486 |
12 Nov 10 |
nicklas |
197 |
handleError("Error preparing action factory for extension '" + |
4322 |
30 May 08 |
nicklas |
198 |
extension.getId() + "': " + factory, t); |
4264 |
30 Apr 08 |
nicklas |
199 |
} |
4264 |
30 Apr 08 |
nicklas |
200 |
return prepared; |
4207 |
04 Apr 08 |
nicklas |
201 |
} |
4207 |
04 Apr 08 |
nicklas |
202 |
|
5486 |
12 Nov 10 |
nicklas |
203 |
private void handleError(String message, Throwable t) |
5486 |
12 Nov 10 |
nicklas |
204 |
{ |
5486 |
12 Nov 10 |
nicklas |
205 |
getErrorHandler().handleError(this, null, message, t); |
5486 |
12 Nov 10 |
nicklas |
206 |
} |
4207 |
04 Apr 08 |
nicklas |
207 |
|
5486 |
12 Nov 10 |
nicklas |
208 |
void handleError(A action, String message, Throwable t) |
5486 |
12 Nov 10 |
nicklas |
209 |
{ |
5486 |
12 Nov 10 |
nicklas |
210 |
getErrorHandler().handleError(this, action, message, t); |
5486 |
12 Nov 10 |
nicklas |
211 |
} |
5486 |
12 Nov 10 |
nicklas |
212 |
|
5486 |
12 Nov 10 |
nicklas |
213 |
|
4207 |
04 Apr 08 |
nicklas |
214 |
} |