KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > locking > LockManagerFactory


1 package org.apache.ojb.odmg.locking;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
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  * This factory class creates LockManager instances according
29  * to the setting in the OJB properties file.
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 JavaDoc lockMapClass = conf.getClass("LockMapClass", Object JavaDoc.class);
46         Class JavaDoc lockManagerClass = conf.getClass("LockManagerClass", null);
47         if(lockManagerClass == null)
48         {
49             throw new OJBRuntimeException(buildErrorMsg(lockMapClass, lockManagerClass));
50         }
51         if(!lockMapClass.equals(Object JavaDoc.class))
52         {
53             // use the deprecated old odmg locking stuff
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             // use the kernel locking api
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 JavaDoc 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 JavaDoc e)
87         {
88             throw new OJBRuntimeException("Can't setup odmg lock manager instance", e);
89         }
90     }
91
92     private void setupLockManager(Class JavaDoc lockManagerClass)
93     {
94         try
95         {
96             lockManager = (org.apache.ojb.odmg.locking.LockManager) ClassHelper.newInstance(lockManagerClass);
97
98         }
99         catch(Exception JavaDoc e)
100         {
101             throw new OJBRuntimeException("Can't setup odmg lock manager instance", e);
102         }
103     }
104
105
106     private String JavaDoc buildErrorMsg(Class JavaDoc lockMap, Class JavaDoc lockManager)
107     {
108         String JavaDoc eol = SystemUtils.LINE_SEPARATOR;
109         StringBuffer JavaDoc msg = new StringBuffer JavaDoc("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      * Get a {@link org.apache.ojb.odmg.locking.LockManager} instance. The implementation class is
128      * configured in the OJB properties file.
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 }
139
Popular Tags