1 16 package org.pentaho.core.admin.datasources.jboss; 17 18 import java.io.File ; 19 import java.io.FileOutputStream ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import org.dom4j.Document; 24 import org.dom4j.DocumentFactory; 25 import org.dom4j.Element; 26 import org.dom4j.Node; 27 import org.dom4j.io.OutputFormat; 28 import org.dom4j.io.XMLWriter; 29 import org.pentaho.core.admin.datasources.DataSourceInfo; 30 import org.pentaho.core.admin.datasources.ServerDatasourceAdmin; 31 import org.pentaho.core.system.PentahoSystem; 32 import org.pentaho.core.util.XmlHelper; 33 import org.pentaho.messages.Messages; 34 import org.pentaho.util.logging.Logger; 35 36 public class JBossDatasourceAdmin extends ServerDatasourceAdmin { 37 private static final String JBOSS_JNDI_NAME = "jndi-name"; private Map dsFileMap = new HashMap (); 39 40 public Map listContainerDataSources() { 41 Map jBossDSList = new HashMap (); 42 File jbossWeb = getJBossWebXmlFile(); 44 if (jbossWeb == null) { 45 return jBossDSList; 46 } 47 Document doc = getWebXmlDoc(jbossWeb); 48 List resourceList = doc.selectNodes("jboss-web/resource-ref"); if (dsFileMap.size() == 0) { 50 File deployDir = jbossWeb.getParentFile().getParentFile().getParentFile(); 52 getDSMap(deployDir); 53 } 54 for (int n = 0; n < resourceList.size(); n++) { 55 Element dataSourceNode = (Element) resourceList.get(n); 57 String description = ""; String name = ""; String type = null; 60 Node node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_TYPE); 62 if (node != null) { 63 type = node.getText(); 64 } 65 if (JAVAX_SQL_DATASOURCE.equals(type)) { 67 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_NAME); 68 if (node != null) { 69 name = node.getText(); 70 if (name.startsWith(JDBC_PREFIX)) { 71 name = name.substring(5); 72 } else { 73 continue; 75 } 76 } 77 node = dataSourceNode.selectSingleNode(JBOSS_JNDI_NAME); 78 if (node != null) { 79 JBossDataSourceInfo info = new JBossDataSourceInfo(name, description, type); 81 info.setJndiName(node.getText()); 82 JBossDataSourceInfo dsInfo = (JBossDataSourceInfo) dsFileMap.get(name); 84 if (dsInfo == null) { 85 info.setStatus(Messages.getErrorString("JBossDSAdmin.ERROR_0001_DS_FILE_MISSING", name)); jBossDSList.put(name, info); 91 } else { 92 jBossDSList.put(name, dsInfo); 94 } 95 } 96 } 97 } 98 return jBossDSList; 99 } 100 101 public DataSourceInfo getContainerDataSourceInfo(String id, DataSourceInfo info) { 102 Map dsList = listContainerDataSources(); 103 JBossDataSourceInfo jbossInfo = (JBossDataSourceInfo) dsList.get(id); 104 if (jbossInfo == null) { 105 return null; 106 } 107 return jbossInfo; 108 } 109 110 private void getDSMap(File deployDir) { 111 dsFileMap.clear(); 113 JBossDataSourceFileFilter filter = new JBossDataSourceFileFilter(); 116 File dsFiles[] = deployDir.listFiles(filter); 117 for (int f = 0; f < dsFiles.length; f++) { 118 Document dsDoc = this.getDSXmlDoc(dsFiles[f]); 119 String dsName = null; 120 Node dsNode = dsDoc.selectSingleNode("datasources/local-tx-datasource/jndi-name"); if (dsNode != null) { 122 dsName = dsNode.getText(); 123 JBossDataSourceInfo dsInfo = new JBossDataSourceInfo(dsName, "", JAVAX_SQL_DATASOURCE); dsInfo.setDSFileName(dsFiles[f].getName()); 125 dsNode = dsDoc.selectSingleNode("datasources/local-tx-datasource/connection-url"); if (dsNode != null) { 127 dsInfo.setUrl(dsNode.getText()); 128 } 129 dsNode = dsDoc.selectSingleNode("datasources/local-tx-datasource/driver-class"); if (dsNode != null) { 131 dsInfo.setDriver(dsNode.getText()); 132 } 133 dsNode = dsDoc.selectSingleNode("datasources/local-tx-datasource/user-name"); if (dsNode != null) { 135 dsInfo.setUserId(dsNode.getText()); 136 } 137 dsNode = dsDoc.selectSingleNode("datasources/local-tx-datasource/password"); if (dsNode != null) { 139 dsInfo.setPassword(dsNode.getText()); 140 } 141 dsFileMap.put(dsName, dsInfo); 142 } 143 } 144 } 145 146 public int deleteContainerDataSource(String id) { 147 try { 148 File jbossWeb = getJBossWebXmlFile(); 149 if (jbossWeb == null) { 150 return DS_FILE_OPERATION_FAILED; 151 } 152 File deployDir = jbossWeb.getParentFile().getParentFile().getParentFile(); 153 if (dsFileMap.size() == 0) { 154 getDSMap(deployDir); 156 } 157 JBossDataSourceInfo dsInfo = (JBossDataSourceInfo) dsFileMap.get(id); 159 File dsFile = new File (deployDir, dsInfo.getDSFileName()); 160 try { 161 if (!dsFile.delete()) { 162 System.err.println(Messages.getString("JBossDSAdmin.ERROR_0002_COULD_NOT_DELETE", dsInfo.getDSFileName())); return DS_FILE_OPERATION_FAILED; 165 } 166 } catch (Throwable t) { 167 System.err.println(Messages.getString("JBossDSAdmin.ERROR_0002_COULD_NOT_DELETE", dsInfo.getDSFileName())); return DS_FILE_OPERATION_FAILED; 170 } 171 Document doc = getWebXmlDoc(jbossWeb); 173 int status = removeJBossWebXmlDataSourceNode(doc, id); 174 if (status == DS_DELETED) { 175 try { 177 FileOutputStream outStream = new FileOutputStream (jbossWeb); 178 OutputFormat format = OutputFormat.createPrettyPrint(); 179 XMLWriter writer = new XMLWriter(outStream, format); 180 writer.write(doc); 181 writer.flush(); 182 writer.close(); 183 } catch (Exception e) { 184 return DS_FILE_OPERATION_FAILED; 185 } 186 return DS_DELETED; 187 } else { 188 return status; 189 } 190 } catch (Throwable t) { 191 t.printStackTrace(); 193 } 194 return DS_OPERATION_FAILED; 195 } 196 197 private int removeJBossWebXmlDataSourceNode(Document doc, String id) { 198 String targetName = JDBC_PREFIX + id; 200 Element jbossWebNode = (Element) doc.selectSingleNode("jboss-web"); List dataSourceNodes = jbossWebNode.selectNodes("resource-ref"); for (int n = 0; n < dataSourceNodes.size(); n++) { 204 Element dataSourceNode = (Element) dataSourceNodes.get(n); 206 String type = null; 207 Node node = dataSourceNode.selectSingleNode("res-type"); if (node != null) { 209 type = node.getText(); 210 } 211 if (JAVAX_SQL_DATASOURCE.equals(type)) { 212 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_NAME); 215 if (node != null) { 216 String dataSourceName = node.getText(); 217 if (dataSourceName != null && dataSourceName.equals(targetName)) { 218 jbossWebNode.remove(dataSourceNode); 221 return DS_DELETED; 222 } 223 } 224 } 225 } 226 return DS_NOT_FOUND; 227 } 228 229 public int renameContainerDataSource(String id, String newId) { 230 return DS_OPERATION_FAILED; 231 } 232 233 public int saveContainerDataSource(DataSourceInfo info, boolean isEdit) { 234 if (isEdit) { 235 return saveJBossDSEdit(info); 237 } else { 238 return savenewJBossDS(info); 240 } 241 } 242 243 private int saveJBossDSEdit(DataSourceInfo info) { 244 File jbossWeb = getJBossWebXmlFile(); 245 if (jbossWeb == null) { 246 return DS_FILE_OPERATION_FAILED; 247 } 248 File deployDir = jbossWeb.getParentFile().getParentFile().getParentFile(); 249 File dsFile = new File (deployDir, info.getName() + "-ds.xml"); if (dsFile == null || !dsFile.exists() || !dsFile.isFile()) { 251 return DS_FILE_OPERATION_FAILED; 252 } 253 Document dsDoc = XmlHelper.getDocFromFile(dsFile); 254 if (dsDoc == null) { 255 } 258 try { 259 if (dsDoc != null) { 260 Node node = dsDoc.selectSingleNode("//*[jndi-name='" + info.getName() + "']"); if (node == null) { 262 return DS_OPERATION_FAILED; 263 } 264 Element dsNode = (Element) node; 265 if (info.getUrl() == null) { 266 dsNode.selectSingleNode("connection-url").setText(""); } else { 268 dsNode.selectSingleNode("connection-url").setText(info.getUrl()); } 270 if (info.getDriver() == null) { 271 dsNode.selectSingleNode("driver-class").setText(""); } else { 273 dsNode.selectSingleNode("driver-class").setText(info.getDriver()); } 275 if (info.getUserId() == null) { 276 dsNode.selectSingleNode("user-name").setText(""); } else { 278 dsNode.selectSingleNode("user-name").setText(info.getUserId()); } 280 if (info.getPassword() == null) { 281 dsNode.selectSingleNode("password").setText(""); } else { 283 dsNode.selectSingleNode("password").setText(info.getPassword()); } 285 286 try { 287 FileOutputStream outStream = new FileOutputStream (jbossWeb); 288 OutputFormat format = OutputFormat.createPrettyPrint(); 289 XMLWriter writer = new XMLWriter(outStream, format); 290 writer.write(dsDoc); 291 writer.flush(); 292 writer.close(); 293 } catch (Exception e) { 294 return DS_OPERATION_FAILED; 295 } 296 297 return DS_SAVED; 298 } 299 } catch (Throwable t) { 300 Logger.error( getClass().getName(), Messages.getString("JBossDSAdmin.ERROR_0006_ERRORSAVINGEDIT"), t ); } 302 return DS_OPERATION_FAILED; 303 } 304 305 private int savenewJBossDS(DataSourceInfo info) { 306 File jbossWeb = getJBossWebXmlFile(); 307 if (jbossWeb == null) { 308 return DS_FILE_OPERATION_FAILED; 309 } 310 File deployDir = jbossWeb.getParentFile().getParentFile().getParentFile(); 311 File dsFile = new File (deployDir, info.getName() + "-ds.xml"); try { 316 317 318 Document jbossWebXMLDoc = XmlHelper.getDocFromFile(jbossWeb); 319 320 int status = saveJBossWebXmlDataSource(jbossWebXMLDoc, info); 321 if (status == DS_SAVED) { 322 FileOutputStream outStream = new FileOutputStream (jbossWeb); 324 OutputFormat format = OutputFormat.createPrettyPrint(); 325 XMLWriter writer = new XMLWriter(outStream, format); 326 writer.write(jbossWebXMLDoc); 327 writer.flush(); 328 writer.close(); 329 } 330 331 Document dsDoc = DocumentFactory.getInstance().createDocument(); 332 Element datasources = dsDoc.addElement("datasources"); Element datasouce = datasources.addElement("local-tx-datasource"); Element jndiName = datasouce.addElement("jndi-name"); jndiName.setText(info.getName()); 336 337 Element connectionUrl = datasouce.addElement("connection-url"); connectionUrl.setText(info.getUrl()); 339 340 Element driver = datasouce.addElement("driver-class"); driver.setText(info.getDriver()); 342 343 Element user = datasouce.addElement("user-name"); user.setText(info.getUserId()); 345 346 Element password = datasouce.addElement("password"); password.setText(info.getPassword()); 348 349 FileOutputStream outStream = new FileOutputStream (dsFile); 350 OutputFormat format = OutputFormat.createPrettyPrint(); 351 XMLWriter writer = new XMLWriter(outStream, format); 352 writer.write(dsDoc); 353 writer.flush(); 354 writer.close(); 355 356 return DS_SAVED; 357 } catch (Throwable t) { 358 Logger.error( getClass().getName(), Messages.getString("JBossDSAdmin.ERROR_0007_ERRORCREATINGDS"), t ); } 360 return DS_OPERATION_FAILED; 361 } 362 363 private int saveJBossWebXmlDataSource(Document doc, DataSourceInfo info) { 364 removeJBossWebXmlDataSourceNode(doc, info.getName()); 367 Element webApp = (Element) doc.selectSingleNode("jboss-web"); try { 373 Element resourceNode = webApp.addElement(WEB_XML_RESOURCE_REF); 374 resourceNode.addElement(WEB_XML_RESOURCE_REF_NAME).setText(JDBC_PREFIX + info.getName()); 375 resourceNode.addElement(WEB_XML_RESOURCE_REF_TYPE).setText(JAVAX_SQL_DATASOURCE); 376 resourceNode.addElement(JBOSS_JNDI_NAME).setText(JNDI_PREFIX + info.getName()); 377 } catch (Exception e) { 378 e.printStackTrace(); 379 } 380 return DS_SAVED; 381 } 382 383 private File getJBossWebXmlFile() { 384 File webXml = new File (PentahoSystem.getApplicationContext().getApplicationPath("WEB-INF" + File.separator + "jboss-web.xml")); if (!webXml.exists() || !webXml.isFile()) { 387 File standaloneWebXml = new File (applicationPath + File.separator + webAppName + File.separator + "WEB-INF" + File.separator + "jboss-web.xml"); if (standaloneWebXml.exists()) { 390 return standaloneWebXml; 391 } 392 System.err.println(Messages.getString("JBossDSAdmin.ERROR_0003_JBOSS_WEB_XML_NOT_FOUND", webXml.getAbsolutePath())); return null; 394 } 395 return webXml; 396 } 397 398 private Document getWebXmlDoc(File webXmlFile) { 399 Document doc = XmlHelper.getDocFromFile(webXmlFile); 401 if (doc == null) { 402 System.err.println(Messages.getString("JBossDSAdmin.ERROR_0004_JBOSS_WEB_XML_INVALID", webXmlFile.getAbsolutePath())); } 405 return doc; 406 } 407 408 private Document getDSXmlDoc(File dsXmlFile) { 409 Document doc = XmlHelper.getDocFromFile(dsXmlFile); 411 if (doc == null) { 412 System.err.println(Messages.getString("JBossDSAdmin.ERROR_0005_DS_FILE_INVALID", dsXmlFile.getAbsolutePath())); } 415 return doc; 416 } 417 } 418 | Popular Tags |