001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: // Created on Oct 11, 2006
017: package org.kuali.rice.ojb;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.log4j.Logger;
023: import org.apache.ojb.broker.PBKey;
024: import org.apache.ojb.broker.PersistenceBroker;
025: import org.apache.ojb.broker.PersistenceBrokerFactory;
026: import org.springframework.beans.factory.InitializingBean;
027:
028: /**
029: * Utility class/bean that sets the platform OJB JDBC Connection Descriptors programmatically
030: * at run time, so that it can be parameterized by Spring/Workflow configuration.
031: * A map of jcdalias/datasource bean names to OJB platforms is accepted as the 'platforms' propery.
032: * An empty key, or "DEFAULT" key is interpreted as specifying the default PersistenceBroker/jcdalias
033: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
034: */
035: public class OjbPlatformConfigurer implements InitializingBean {
036: private static final Logger LOG = Logger
037: .getLogger(OjbPlatformConfigurer.class);
038:
039: /**
040: * Literal to accept as specifying the "default" PersistenceBroker/jcdalias
041: */
042: private static final String DEFAULT = "DEFAULT";
043:
044: private Map<String, String> dsToPlatform;
045:
046: /**
047: * Sets jcdAlias->platform string map.
048: * @param platforms the map of jcdAlias->OJB platform string
049: */
050: public void setPlatforms(Map<String, String> platforms) {
051: this .dsToPlatform = platforms;
052: }
053:
054: public void afterPropertiesSet() throws Exception {
055: configureOJBConnectionDescriptors(this .dsToPlatform);
056: }
057:
058: /**
059: * Configures the default OJB connection descriptor to use the specified platform
060: * @param platform the OJB platform string
061: */
062: public static void configureDefaultOJBConnectionDescriptor(
063: String platform) {
064: configureOJBConnectionDescriptor("", platform);
065: }
066:
067: /**
068: * Configures the specified OJB connection descriptor to use the specified platform
069: * @param jcdAlias the jcd alias
070: * @param platform the OJB platform string
071: */
072: public static void configureOJBConnectionDescriptor(
073: String jcdAlias, String platform) {
074: Map<String, String> platforms = new HashMap<String, String>();
075: platforms.put(jcdAlias, platform);
076: configureOJBConnectionDescriptors(platforms);
077: }
078:
079: /**
080: * Configures the OJB connection descriptors with the platforms specified in the
081: * given Map<String, String> which is taken as a map of jcdAlias->platform string.
082: * The jcdAlias <code>""</code> or <code>"DEFAULT"</code> ({@link #DEFAULT}) is
083: * interpreted as the default connection descriptor.
084: * @param platforms the map of jcdAlias->OJB platform string
085: */
086: public static void configureOJBConnectionDescriptors(
087: Map<String, String> platforms) {
088: int numUpdated = 0;
089: if (platforms != null) {
090: // no way to obtain all possible PersistenceBroker/jcdalias keys from OJB? shame.
091: for (Map.Entry<String, String> entry : platforms.entrySet()) {
092: String key = entry.getKey();
093: PersistenceBroker pb;
094: // empty key means the default persistence broker/jcd alias
095: boolean deflt = key.length() == 0
096: || DEFAULT.equals(key);
097: if (deflt) {
098: pb = PersistenceBrokerFactory
099: .defaultPersistenceBroker();
100: } else {
101: pb = PersistenceBrokerFactory
102: .createPersistenceBroker(new PBKey(key));
103: }
104: // PersisenceBroker and JCD alias are one and the same I guess
105: LOG
106: .info("Setting "
107: + (deflt ? "<<<default>>>" : "'" + key
108: + "'")
109: + " OJB connection descriptor database platform to '"
110: + entry.getValue() + "'");
111: pb.serviceConnectionManager().getConnectionDescriptor()
112: .setDbms(entry.getValue());
113: numUpdated++;
114: }
115: }
116: if (numUpdated > 0) {
117: LOG.info("Updated " + numUpdated
118: + " OJB JDBC connection descriptors");
119: } else {
120: LOG.warn("No OJB JDBC connection descriptors updated");
121: }
122: }
123: }
|