1 | |
package org.apache.ojb.odmg.locking; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import org.apache.commons.lang.SystemUtils; |
19 | |
import org.apache.ojb.broker.OJBRuntimeException; |
20 | |
import org.apache.ojb.broker.locking.LockManager; |
21 | |
import org.apache.ojb.broker.util.ClassHelper; |
22 | |
import org.apache.ojb.broker.util.configuration.Configuration; |
23 | |
import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator; |
24 | |
import org.apache.ojb.broker.util.logging.Logger; |
25 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class LockManagerFactory |
32 | |
{ |
33 | |
private static LockManagerFactory lockManagerFactory = null; |
34 | |
private Logger log = LoggerFactory.getLogger(LockManagerFactory.class); |
35 | |
private org.apache.ojb.odmg.locking.LockManager lockManager; |
36 | |
|
37 | |
private LockManagerFactory() |
38 | |
{ |
39 | |
init(); |
40 | |
} |
41 | |
|
42 | |
private void init() |
43 | |
{ |
44 | |
Configuration conf = OjbConfigurator.getInstance().getConfigurationFor(null); |
45 | |
Class lockMapClass = conf.getClass("LockMapClass", Object.class); |
46 | |
Class lockManagerClass = conf.getClass("LockManagerClass", null); |
47 | |
if(lockManagerClass == null) |
48 | |
{ |
49 | |
throw new OJBRuntimeException(buildErrorMsg(lockMapClass, lockManagerClass)); |
50 | |
} |
51 | |
if(!lockMapClass.equals(Object.class)) |
52 | |
{ |
53 | |
|
54 | |
log.info("Setup *deprecated* odmg-locking api."); |
55 | |
log.info("Used LockManagerClass=" + lockManagerClass); |
56 | |
log.info("Used LockMapClass=" + lockMapClass); |
57 | |
if(!org.apache.ojb.odmg.locking.LockManager.class.isAssignableFrom(lockManagerClass)) |
58 | |
{ |
59 | |
throw new OJBRuntimeException(buildErrorMsg(lockMapClass, lockManagerClass)); |
60 | |
} |
61 | |
setupLockManager(lockManagerClass); |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | |
|
66 | |
log.info("Setup odmg-locking api."); |
67 | |
log.info("Used LockManagerClass=" + lockManagerClass); |
68 | |
if(org.apache.ojb.odmg.locking.LockManager.class.isAssignableFrom(lockManagerClass)) |
69 | |
{ |
70 | |
throw new OJBRuntimeException(buildErrorMsg(lockMapClass, lockManagerClass)); |
71 | |
} |
72 | |
setupLockManager(conf, lockManagerClass); |
73 | |
} |
74 | |
} |
75 | |
|
76 | |
private void setupLockManager(Configuration conf, Class lockManagerClass) |
77 | |
{ |
78 | |
long timeout = conf.getInteger("LockTimeout", 60000); |
79 | |
log.info("LockTimeout=" + timeout); |
80 | |
try |
81 | |
{ |
82 | |
LockManager lm = (LockManager) ClassHelper.newInstance(lockManagerClass); |
83 | |
lm.setLockTimeout(timeout); |
84 | |
lockManager = new LockManagerOdmgImpl(lm); |
85 | |
} |
86 | |
catch(Exception e) |
87 | |
{ |
88 | |
throw new OJBRuntimeException("Can't setup odmg lock manager instance", e); |
89 | |
} |
90 | |
} |
91 | |
|
92 | |
private void setupLockManager(Class lockManagerClass) |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | |
lockManager = (org.apache.ojb.odmg.locking.LockManager) ClassHelper.newInstance(lockManagerClass); |
97 | |
|
98 | |
} |
99 | |
catch(Exception e) |
100 | |
{ |
101 | |
throw new OJBRuntimeException("Can't setup odmg lock manager instance", e); |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
|
106 | |
private String buildErrorMsg(Class lockMap, Class lockManager) |
107 | |
{ |
108 | |
String eol = SystemUtils.LINE_SEPARATOR; |
109 | |
StringBuffer msg = new StringBuffer("Can't setup LockManager. Current used properties are:" + eol); |
110 | |
msg.append("LockMapClass=").append(lockMap != null ? lockMap.getName() : null) |
111 | |
.append(eol) |
112 | |
.append("LockManagerClass=").append(lockManager != null ? lockManager.getName() : null).append(eol) |
113 | |
.append("For correct setup of the lock manager, please enable the 'LockManagerClass' property") |
114 | |
.append(" in OJB configuration, OJB expects an 'org.apache.ojb.broker.locking.LockManager' implementation class.") |
115 | |
.append(eol) |
116 | |
.append("Or to enable the *deprecated* odmg-locking api enable the 'LockMapClass' AND the 'LockManager' properties") |
117 | |
.append(", in this case OJB expects an 'org.apache.ojb.odmg.locking.LockManager' implementation class."); |
118 | |
return msg.toString(); |
119 | |
} |
120 | |
|
121 | |
private org.apache.ojb.odmg.locking.LockManager getManager() |
122 | |
{ |
123 | |
return lockManager; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public static synchronized org.apache.ojb.odmg.locking.LockManager getLockManager() |
131 | |
{ |
132 | |
if(lockManagerFactory == null) |
133 | |
{ |
134 | |
lockManagerFactory = new LockManagerFactory(); |
135 | |
} |
136 | |
return lockManagerFactory.getManager(); |
137 | |
} |
138 | |
} |