001: package org.apache.ojb.odmg.locking;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.lang.SystemUtils;
019: import org.apache.ojb.broker.OJBRuntimeException;
020: import org.apache.ojb.broker.locking.LockManager;
021: import org.apache.ojb.broker.util.ClassHelper;
022: import org.apache.ojb.broker.util.configuration.Configuration;
023: import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
024: import org.apache.ojb.broker.util.logging.Logger;
025: import org.apache.ojb.broker.util.logging.LoggerFactory;
026:
027: /**
028: * This factory class creates LockManager instances according
029: * to the setting in the OJB properties file.
030: */
031: public class LockManagerFactory {
032: private static LockManagerFactory lockManagerFactory = null;
033: private Logger log = LoggerFactory
034: .getLogger(LockManagerFactory.class);
035: private org.apache.ojb.odmg.locking.LockManager lockManager;
036:
037: private LockManagerFactory() {
038: init();
039: }
040:
041: private void init() {
042: Configuration conf = OjbConfigurator.getInstance()
043: .getConfigurationFor(null);
044: Class lockMapClass = conf
045: .getClass("LockMapClass", Object.class);
046: Class lockManagerClass = conf
047: .getClass("LockManagerClass", null);
048: if (lockManagerClass == null) {
049: throw new OJBRuntimeException(buildErrorMsg(lockMapClass,
050: lockManagerClass));
051: }
052: if (!lockMapClass.equals(Object.class)) {
053: // use the deprecated old odmg locking stuff
054: log.info("Setup *deprecated* odmg-locking api.");
055: log.info("Used LockManagerClass=" + lockManagerClass);
056: log.info("Used LockMapClass=" + lockMapClass);
057: if (!org.apache.ojb.odmg.locking.LockManager.class
058: .isAssignableFrom(lockManagerClass)) {
059: throw new OJBRuntimeException(buildErrorMsg(
060: lockMapClass, lockManagerClass));
061: }
062: setupLockManager(lockManagerClass);
063: } else {
064: // use the kernel locking api
065: log.info("Setup odmg-locking api.");
066: log.info("Used LockManagerClass=" + lockManagerClass);
067: if (org.apache.ojb.odmg.locking.LockManager.class
068: .isAssignableFrom(lockManagerClass)) {
069: throw new OJBRuntimeException(buildErrorMsg(
070: lockMapClass, lockManagerClass));
071: }
072: setupLockManager(conf, lockManagerClass);
073: }
074: }
075:
076: private void setupLockManager(Configuration conf,
077: Class lockManagerClass) {
078: long timeout = conf.getInteger("LockTimeout", 60000);
079: log.info("LockTimeout=" + timeout);
080: try {
081: LockManager lm = (LockManager) ClassHelper
082: .newInstance(lockManagerClass);
083: lm.setLockTimeout(timeout);
084: lockManager = new LockManagerOdmgImpl(lm);
085: } catch (Exception e) {
086: throw new OJBRuntimeException(
087: "Can't setup odmg lock manager instance", e);
088: }
089: }
090:
091: private void setupLockManager(Class lockManagerClass) {
092: try {
093: lockManager = (org.apache.ojb.odmg.locking.LockManager) ClassHelper
094: .newInstance(lockManagerClass);
095:
096: } catch (Exception e) {
097: throw new OJBRuntimeException(
098: "Can't setup odmg lock manager instance", e);
099: }
100: }
101:
102: private String buildErrorMsg(Class lockMap, Class lockManager) {
103: String eol = SystemUtils.LINE_SEPARATOR;
104: StringBuffer msg = new StringBuffer(
105: "Can't setup LockManager. Current used properties are:"
106: + eol);
107: msg
108: .append("LockMapClass=")
109: .append(lockMap != null ? lockMap.getName() : null)
110: .append(eol)
111: .append("LockManagerClass=")
112: .append(
113: lockManager != null ? lockManager.getName()
114: : null)
115: .append(eol)
116: .append(
117: "For correct setup of the lock manager, please enable the 'LockManagerClass' property")
118: .append(
119: " in OJB configuration, OJB expects an 'org.apache.ojb.broker.locking.LockManager' implementation class.")
120: .append(eol)
121: .append(
122: "Or to enable the *deprecated* odmg-locking api enable the 'LockMapClass' AND the 'LockManager' properties")
123: .append(
124: ", in this case OJB expects an 'org.apache.ojb.odmg.locking.LockManager' implementation class.");
125: return msg.toString();
126: }
127:
128: private org.apache.ojb.odmg.locking.LockManager getManager() {
129: return lockManager;
130: }
131:
132: /**
133: * Get a {@link org.apache.ojb.odmg.locking.LockManager} instance. The implementation class is
134: * configured in the OJB properties file.
135: */
136: public static synchronized org.apache.ojb.odmg.locking.LockManager getLockManager() {
137: if (lockManagerFactory == null) {
138: lockManagerFactory = new LockManagerFactory();
139: }
140: return lockManagerFactory.getManager();
141: }
142: }
|