| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.core.framework.persistence.ojb; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.apache.log4j.Logger; |
| 21 | |
import org.apache.ojb.broker.metadata.ConnectionRepository; |
| 22 | |
import org.apache.ojb.broker.metadata.DescriptorRepository; |
| 23 | |
import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; |
| 24 | |
import org.apache.ojb.broker.metadata.MetadataManager; |
| 25 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
| 26 | |
import org.kuali.rice.core.api.config.property.Config; |
| 27 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 28 | |
import org.kuali.rice.core.api.lifecycle.BaseLifecycle; |
| 29 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
| 30 | |
import org.springframework.beans.factory.InitializingBean; |
| 31 | |
import org.springframework.core.io.DefaultResourceLoader; |
| 32 | |
import org.w3c.dom.Document; |
| 33 | |
import org.w3c.dom.Element; |
| 34 | |
import org.w3c.dom.NodeList; |
| 35 | |
import org.xml.sax.InputSource; |
| 36 | |
|
| 37 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 38 | |
import javax.xml.transform.Transformer; |
| 39 | |
import javax.xml.transform.TransformerFactory; |
| 40 | |
import javax.xml.transform.dom.DOMSource; |
| 41 | |
import javax.xml.transform.stream.StreamResult; |
| 42 | |
import javax.xml.xpath.XPath; |
| 43 | |
import javax.xml.xpath.XPathConstants; |
| 44 | |
import javax.xml.xpath.XPathFactory; |
| 45 | |
import java.io.BufferedInputStream; |
| 46 | |
import java.io.BufferedOutputStream; |
| 47 | |
import java.io.ByteArrayInputStream; |
| 48 | |
import java.io.ByteArrayOutputStream; |
| 49 | |
import java.io.InputStream; |
| 50 | |
import java.util.Iterator; |
| 51 | |
import java.util.List; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public class BaseOjbConfigurer extends BaseLifecycle implements InitializingBean { |
| 59 | |
|
| 60 | 0 | private static final Logger LOG = Logger.getLogger(BaseOjbConfigurer.class); |
| 61 | |
|
| 62 | |
public static final String RICE_OJB_PROPERTIES_PARAM = "rice.custom.ojb.properties"; |
| 63 | |
public static final String OJB_PROPERTIES_PROP = "OJB.properties"; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
protected String[] jcdAliases; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
protected String metadataLocation; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 0 | public BaseOjbConfigurer() { |
| 78 | |
|
| 79 | 0 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | 0 | public BaseOjbConfigurer(String moduleName) { |
| 89 | 0 | this.metadataLocation = "classpath:org/kuali/rice/" + moduleName.toLowerCase() + "/config/OJB-repository-" + moduleName.toLowerCase() + ".xml"; |
| 90 | 0 | this.jcdAliases = new String[] { moduleName.toLowerCase() + "DataSource" }; |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | 0 | public BaseOjbConfigurer(String[] jcdAliases, String metadataLocation) { |
| 100 | 0 | this.jcdAliases = jcdAliases; |
| 101 | 0 | this.metadataLocation = metadataLocation; |
| 102 | 0 | } |
| 103 | |
|
| 104 | |
@Override |
| 105 | |
public void start() throws Exception { |
| 106 | |
|
| 107 | 0 | String currentValue = System.getProperty(OJB_PROPERTIES_PROP); |
| 108 | |
try { |
| 109 | 0 | System.setProperty(OJB_PROPERTIES_PROP, getOjbPropertiesLocation()); |
| 110 | 0 | MetadataManager mm = MetadataManager.getInstance(); |
| 111 | 0 | establishConnectionMetaData(mm); |
| 112 | 0 | establishRepositoryMetaData(mm); |
| 113 | |
} finally { |
| 114 | 0 | if (currentValue == null) { |
| 115 | 0 | System.getProperties().remove(OJB_PROPERTIES_PROP); |
| 116 | |
} else { |
| 117 | 0 | System.setProperty(OJB_PROPERTIES_PROP, currentValue); |
| 118 | |
} |
| 119 | 0 | } |
| 120 | 0 | super.start(); |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public void stop() throws Exception { |
| 125 | 0 | super.stop(); |
| 126 | 0 | } |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
protected String getOjbPropertiesLocation() { |
| 131 | 0 | String ojbPropertiesLocation = ConfigContext.getCurrentContextConfig().getProperty(RICE_OJB_PROPERTIES_PARAM); |
| 132 | 0 | return ojbPropertiesLocation; |
| 133 | |
} |
| 134 | |
|
| 135 | |
protected void establishConnectionMetaData(MetadataManager mm) throws Exception { |
| 136 | 0 | String connMetadata = getMetadataLocation(); |
| 137 | 0 | if (StringUtils.isBlank(connMetadata)) { |
| 138 | 0 | LOG.info("No OJB connection metadata loaded."); |
| 139 | 0 | return; |
| 140 | |
} |
| 141 | 0 | if (!isConnectionAlreadyConfigured(mm)) { |
| 142 | 0 | LOG.info("Loading OJB Connection Metadata from " + connMetadata); |
| 143 | 0 | DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader()); |
| 144 | 0 | InputStream is = resourceLoader.getResource(connMetadata).getInputStream(); |
| 145 | 0 | is = preprocessConnectionMetadata(is); |
| 146 | 0 | ConnectionRepository cr = mm.readConnectionRepository(is); |
| 147 | 0 | mm.mergeConnectionRepository(cr); |
| 148 | |
try { |
| 149 | 0 | is.close(); |
| 150 | 0 | } catch (Exception e) { |
| 151 | 0 | LOG.warn("Failed to close stream to file " + connMetadata, e); |
| 152 | 0 | } |
| 153 | 0 | } else { |
| 154 | 0 | LOG.info("OJB Connections already configured for jcd aliases '" + StringUtils.join(getJcdAliases(), ", ") + "', skipping Metadata merge."); |
| 155 | |
} |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
protected InputStream preprocessConnectionMetadata(InputStream inputStream) throws Exception { |
| 159 | 0 | Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(inputStream)); |
| 160 | 0 | XPath xpath = XPathFactory.newInstance().newXPath(); |
| 161 | 0 | NodeList connectionDescriptors = (NodeList)xpath.evaluate("/descriptor-repository/jdbc-connection-descriptor", document, XPathConstants.NODESET); |
| 162 | 0 | for (int index = 0; index < connectionDescriptors.getLength(); index++) { |
| 163 | 0 | Element descriptor = (Element)connectionDescriptors.item(index); |
| 164 | 0 | String currentPlatform = descriptor.getAttribute("platform"); |
| 165 | 0 | if (StringUtils.isBlank(currentPlatform)) { |
| 166 | 0 | String ojbPlatform = ConfigContext.getCurrentContextConfig().getProperty(Config.OJB_PLATFORM); |
| 167 | 0 | if (StringUtils.isEmpty(ojbPlatform)) { |
| 168 | 0 | throw new ConfigurationException("Could not configure OJB, the '" + Config.OJB_PLATFORM + "' configuration property was not set."); |
| 169 | |
} |
| 170 | 0 | LOG.info("Setting OJB connection descriptor database platform to '" + ojbPlatform + "'"); |
| 171 | 0 | descriptor.setAttribute("platform", ojbPlatform); |
| 172 | |
} |
| 173 | |
} |
| 174 | 0 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
| 175 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 176 | 0 | transformer.transform(new DOMSource(document), new StreamResult(new BufferedOutputStream(baos))); |
| 177 | 0 | return new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())); |
| 178 | |
} |
| 179 | |
|
| 180 | |
@SuppressWarnings("unchecked") |
| 181 | |
protected boolean isConnectionAlreadyConfigured(MetadataManager mm) { |
| 182 | 0 | List descriptors = mm.connectionRepository().getAllDescriptor(); |
| 183 | 0 | for (Iterator iterator = descriptors.iterator(); iterator.hasNext();) { |
| 184 | 0 | JdbcConnectionDescriptor descriptor = (JdbcConnectionDescriptor) iterator.next(); |
| 185 | 0 | for (String jcdAlias : getJcdAliases()) { |
| 186 | 0 | if (descriptor.getJcdAlias().equals(jcdAlias)) { |
| 187 | 0 | return true; |
| 188 | |
} |
| 189 | |
} |
| 190 | 0 | } |
| 191 | 0 | return false; |
| 192 | |
} |
| 193 | |
|
| 194 | |
protected InputStream preprocessRepositoryMetadata(InputStream inputStream) throws Exception { |
| 195 | 0 | return inputStream; |
| 196 | |
} |
| 197 | |
|
| 198 | |
protected void establishRepositoryMetaData(MetadataManager mm) throws Exception { |
| 199 | 0 | String repoMetadata = getMetadataLocation(); |
| 200 | 0 | if (StringUtils.isBlank(repoMetadata)) { |
| 201 | 0 | LOG.info("No OJB repository metadata loaded."); |
| 202 | 0 | return; |
| 203 | |
} |
| 204 | 0 | LOG.info("Loading OJB Metadata from " + repoMetadata); |
| 205 | 0 | DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader()); |
| 206 | 0 | InputStream is = resourceLoader.getResource(repoMetadata).getInputStream(); |
| 207 | 0 | is = preprocessRepositoryMetadata(is); |
| 208 | 0 | DescriptorRepository dr = mm.readDescriptorRepository(is); |
| 209 | 0 | mm.mergeDescriptorRepository(dr); |
| 210 | |
try { |
| 211 | 0 | is.close(); |
| 212 | 0 | } catch (Exception e) { |
| 213 | 0 | LOG.warn("Failed to close stream to file " + repoMetadata, e); |
| 214 | 0 | } |
| 215 | 0 | } |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
protected String[] getJcdAliases() { |
| 223 | 0 | return jcdAliases; |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
protected String getMetadataLocation() { |
| 232 | 0 | return metadataLocation; |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public void afterPropertiesSet() throws Exception { |
| 239 | 0 | this.start(); |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
public void setJcdAliases(String[] jcdAliases) { |
| 246 | 0 | this.jcdAliases = jcdAliases; |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public void setMetadataLocation(String metadataLocation) { |
| 253 | 0 | this.metadataLocation = metadataLocation; |
| 254 | 0 | } |
| 255 | |
|
| 256 | |
} |