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