1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web;
24
25 import java.net.URL;
26
27 import net.smartlab.config.Configuration;
28 import net.smartlab.config.ConfigurationException;
29 import net.smartlab.web.config.DomainConfigurationStrategy;
30 import net.smartlab.web.config.FileDomainConfigurationStrategy;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.hibernate.HibernateException;
35 import org.hibernate.StaleObjectStateException;
36 import org.hibernate.Transaction;
37
38
39
40
41
42
43
44 public abstract class Domain implements ManageableDomain {
45
46
47
48
49
50 final static ThreadLocal context = new ThreadLocal();
51
52
53
54
55 protected final Log logger = LogFactory.getLog(this.getClass());
56
57
58
59
60 private Configuration config;
61
62
63
64
65 private static DomainConfigurationStrategy strategy = new FileDomainConfigurationStrategy();
66 static {
67 try {
68 String strategy = System.getProperty("smartweb.domain.strategy");
69 if (strategy != null) {
70 Domain.strategy = (DomainConfigurationStrategy)Class.forName(strategy).newInstance();
71 } else {
72 LogFactory.getLog(Domain.class).warn("No configuration found: falling back to default configuration");
73 }
74 } catch (Exception e) {
75 LogFactory.getLog(Domain.class).fatal("Error configuring SmartWeb", e);
76 }
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91 protected Transaction begin(BusinessObjectFactory factory) throws BusinessException {
92 try {
93 return factory.current().beginTransaction();
94 } catch (HibernateException he) {
95 logger.error("[ smartweb ] failed to begin transaction");
96 throw new BusinessException("persistence.error.begin", he);
97 } catch (DAOException boe) {
98 throw new BusinessException(boe);
99 }
100 }
101
102
103
104
105
106
107
108
109 protected void commit(Transaction transaction) throws BusinessException {
110 try {
111 transaction.commit();
112 } catch (StaleObjectStateException sose) {
113 logger.info("[ smartweb ] optimistical locking collision");
114 throw new BusinessException("persistence.locking.collision", sose);
115 } catch (HibernateException he) {
116 logger.error("[ smartweb ] failed to commit transaction");
117 throw new BusinessException("persistence.error.commit", he);
118 }
119 }
120
121
122
123
124
125
126
127
128 protected void rollback(Transaction transaction) throws BusinessException {
129 try {
130 transaction.rollback();
131 } catch (HibernateException he) {
132 logger.warn("[ smartweb ] failed to rollback transaction");
133 }
134 }
135
136
137
138
139
140
141
142 public static void setConfigurationStrategy(DomainConfigurationStrategy strategy) {
143 Domain.strategy = strategy;
144 }
145
146
147
148
149
150 protected Domain() {
151 if (logger.isDebugEnabled()) {
152 logger.debug(this.getClass().getName() + " instantiated.");
153 }
154 }
155
156
157
158
159
160
161
162
163
164 public Configuration getConfiguration() throws ConfigurationException {
165 if (config == null) {
166 this.config = strategy.getConfiguration(this);
167 }
168 return config;
169 }
170
171
172
173
174
175
176
177
178
179
180 public Configuration getConfiguration(String filename) throws ConfigurationException {
181 if (config == null) {
182 config = strategy.getConfiguration(this, filename);
183 }
184 return config;
185 }
186
187
188
189
190
191
192
193
194
195
196 public static URL getResource(Class context, String[] names) {
197 Log logger = LogFactory.getLog(context);
198 URL resource = null;
199 for (int i = 0; i < names.length; i++) {
200 resource = context.getResource(names[i]);
201 logger.trace(" trying `" + names[i] + "`");
202 if (resource != null) {
203 break;
204 }
205 }
206 logger.debug(" resource is `" + resource + "`");
207 if (resource == null) {
208 logger.warn("No resource found in " + names);
209 }
210 return resource;
211 }
212
213
214
215
216
217
218
219
220
221 public static String getLastArchiveName(Class type) {
222 Log logger = LogFactory.getLog(type);
223 String path = type.getProtectionDomain().getCodeSource().getLocation().getFile();
224 if (path.indexOf("/WEB-INF/classes") > -1) {
225
226 path.substring(0, path.indexOf("/WEB-INF/classes"));
227 }
228 logger.debug(" archive path is `" + path + "`");
229 return path.substring(path.lastIndexOf('/') + 1);
230 }
231 }