1
|
|
/*
|
2
|
|
* Angkor Web Framework
|
3
|
|
*
|
4
|
|
* Distributable under LGPL license.
|
5
|
|
* See terms of license at gnu.org.
|
6
|
|
*/
|
7
|
|
|
8
|
|
package com.tirsen.angkor;
|
9
|
|
|
10
|
|
import org.apache.commons.logging.Log;
|
11
|
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
|
13
|
|
import javax.servlet.ServletException;
|
14
|
|
import javax.servlet.http.HttpSession;
|
15
|
|
import java.io.IOException;
|
16
|
|
|
17
|
|
/**
|
18
|
|
* TODO document ApplicationFactory
|
19
|
|
*
|
20
|
|
* <!-- $Id: ApplicationFactory.java,v 1.3 2002/10/13 19:59:22 tirsen Exp $ -->
|
21
|
|
* <!-- $Author: tirsen $ -->
|
22
|
|
*
|
23
|
|
* @author Jon Tirs´n (tirsen@users.sourceforge.net)
|
24
|
|
* @version $Revision: 1.3 $
|
25
|
|
*/
|
26
|
|
public class ApplicationFactory
|
27
|
|
{
|
28
|
|
private static Log logger = LogFactory.getLog(ApplicationFactory.class);
|
29
|
|
|
30
|
|
private Class applicationClass;
|
31
|
|
private HttpSession session;
|
32
|
|
|
33
|
3
|
public ApplicationFactory(HttpSession session)
|
34
|
|
{
|
35
|
3
|
this.session = session;
|
36
|
|
}
|
37
|
|
|
38
|
2
|
public Application getApplication()
|
39
|
|
{
|
40
|
2
|
Application application = null;
|
41
|
2
|
try
|
42
|
|
{
|
43
|
2
|
application = (Application) getSessionAttribute(getApplicationSessionAttributeName());
|
44
|
|
}
|
45
|
|
catch (Exception e)
|
46
|
|
{
|
47
|
0
|
throw new RuntimeException("Failed to access application:" + e.getMessage());
|
48
|
|
}
|
49
|
2
|
if (application == null)
|
50
|
|
{
|
51
|
0
|
throw new IllegalStateException("Application has not been initialized for this request you need to deploy the ApplicationFilter correctly.");
|
52
|
|
}
|
53
|
2
|
return application;
|
54
|
|
}
|
55
|
|
|
56
|
7
|
private String getApplicationSessionAttributeName()
|
57
|
|
{
|
58
|
|
//return getApplicationPath() + "/application";
|
59
|
|
// TODO
|
60
|
7
|
return "application";
|
61
|
|
}
|
62
|
|
|
63
|
2
|
public void ensureApplicationInit(Class applicationClass) throws IOException, ServletException
|
64
|
|
{
|
65
|
2
|
Application application = (Application) getSessionAttribute(getApplicationSessionAttributeName());
|
66
|
2
|
if (application == null)
|
67
|
|
{
|
68
|
2
|
try
|
69
|
|
{
|
70
|
2
|
application = (Application) applicationClass.newInstance();
|
71
|
|
}
|
72
|
|
catch (Exception e)
|
73
|
|
{
|
74
|
0
|
logger.error("could not instantiate application");
|
75
|
0
|
throw new ServletException("could not instantiate application: " + e.getMessage());
|
76
|
|
}
|
77
|
2
|
setApplication(application);
|
78
|
|
}
|
79
|
|
}
|
80
|
|
|
81
|
3
|
public void setApplication(Application application)
|
82
|
|
{
|
83
|
3
|
setSessionAttribute(getApplicationSessionAttributeName(), application);
|
84
|
|
}
|
85
|
|
|
86
|
4
|
protected Object getSessionAttribute(String attribute)
|
87
|
|
{
|
88
|
4
|
return session.getAttribute(attribute);
|
89
|
|
}
|
90
|
|
|
91
|
3
|
protected void setSessionAttribute(String attribute, Object value)
|
92
|
|
{
|
93
|
3
|
session.setAttribute(attribute, value);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
|