| 1 16 package org.pentaho.core.admin.datasources; 17 18 import java.io.File ; 19 import java.io.FileOutputStream ; 20 import java.io.FileWriter ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import org.dom4j.Document; 27 import org.dom4j.DocumentHelper; 28 import org.dom4j.Element; 29 import org.dom4j.Node; 30 import org.dom4j.io.OutputFormat; 31 import org.dom4j.io.XMLWriter; 32 import org.pentaho.core.system.PentahoSystem; 33 import org.pentaho.core.util.XmlHelper; 34 import org.pentaho.messages.Messages; 35 36 public abstract class ServerDatasourceAdmin extends DatasourceAdminBase { 37 public static final String WEB_XML_RESOURCE_REF = "resource-ref"; public static final String WEB_XML_RESOURCE_REF_NAME = "res-ref-name"; public static final String WEB_XML_RESOURCE_REF_DESCRIPTION = "description"; public static final String WEB_XML_RESOURCE_REF_AUTH = "res-auth"; public static final String WEB_XML_RESOURCE_REF_TYPE = "res-type"; public static final String WEB_XML_RESOURCE_REF_URL = "res-url"; public static final String WEB_XML_RESOURCE_REF_DRIVER = "res-driver"; public static final String WEB_XML_RESOURCE_REF_USER_ID = "res-user"; public static final String WEB_XML_RESOURCE_REF_PASSWORD = "res-password"; protected String applicationPath = null; 47 protected String webAppName = null; 48 49 55 public abstract Map listContainerDataSources(); 56 57 65 public abstract int deleteContainerDataSource(String id); 66 67 76 public abstract int renameContainerDataSource(String id, String newId); 77 78 87 public abstract int saveContainerDataSource(DataSourceInfo info, boolean isEdit); 88 89 public abstract DataSourceInfo getContainerDataSourceInfo(String id, DataSourceInfo info); 90 91 public Map listDataSources() { 92 Map webappList = listWebXmlDataSources(); 93 return webappList; 95 } 96 97 public Document getDataSourcesIndex() { 98 Map containerList = listContainerDataSources(); 99 Map webappList = listWebXmlDataSources(); 100 Set nameList = containerList.keySet(); 101 Iterator nameIterator = nameList.iterator(); 102 Document doc = DocumentHelper.createDocument(); 104 Element sourcesNode = doc.addElement("datasources"); Element containerNode = sourcesNode.addElement("container"); while (nameIterator.hasNext()) { 107 String name = (String ) nameIterator.next(); 108 Element dsNode = containerNode.addElement("datasource"); dsNode.addAttribute("name", name); DataSourceInfo dsInfo = (DataSourceInfo) containerList.get(name); 111 String attr = dsInfo.getUrl(); 112 if (attr != null) { 113 dsNode.addElement("url").setText(attr); } 115 attr = dsInfo.getDescription(); 116 if (attr != null) { 117 dsNode.addElement("description").setText(attr); } 119 attr = dsInfo.getAuthorization(); 120 if (attr != null) { 121 dsNode.addElement("authorization").setText(attr); } 123 attr = dsInfo.getDriver(); 124 if (attr != null) { 125 dsNode.addElement("driver").setText(attr); } 127 attr = dsInfo.getType(); 128 if (attr != null) { 129 dsNode.addElement("type").setText(attr); } 131 attr = dsInfo.getUserId(); 132 if (attr != null) { 133 dsNode.addElement("user").setText(attr); } 135 } 136 nameList = webappList.keySet(); 137 nameIterator = nameList.iterator(); 138 Element webappNode = sourcesNode.addElement("webapp"); while (nameIterator.hasNext()) { 141 String name = (String ) nameIterator.next(); 142 Element dsNode = webappNode.addElement("datasource"); dsNode.addAttribute("name", name); } 145 return doc; 148 } 149 150 public DataSourceInfo getDataSourceInfo(String id) { 151 DataSourceInfo webXmlInfo = getWebXmlDataSourceInfo(id); 152 if (webXmlInfo == null) { 153 return null; 154 } else 155 return getContainerDataSourceInfo(id, webXmlInfo); 156 } 157 158 public DataSourceInfo getWebXmlDataSourceInfo(String id) { 159 Map dataSources = listWebXmlDataSources(); 160 return (DataSourceInfo) dataSources.get(id); 161 } 162 163 public int deleteDataSource(String id) { 164 try { 165 int status = deleteContainerDataSource(id); 167 if (status == DS_DELETED) { 168 return deleteWebXmlDataSource(id); 170 } else { 171 return status; 172 } 173 } catch (Throwable t) { 174 System.err.println(Messages.getErrorString("ServerDSAdmin.ERROR_0005_CANNOT_DELETE", id)); } 176 return DS_OPERATION_FAILED; 177 } 178 179 public int renameDataSource(String id, String newId) { 180 try { 181 int status = renameContainerDataSource(id, newId); 183 if (status == DS_RENAMED) { 184 return renameWebXmlDataSource(id, newId); 186 } else { 187 return status; 188 } 189 } catch (Throwable t) { 190 System.err.println(Messages.getErrorString("ServerDSAdmin.ERROR_0004_CANNOT_RENAME", id)); } 192 return DS_OPERATION_FAILED; 193 } 194 195 public int saveDataSource(DataSourceInfo info, boolean isEdit) { 196 try { 197 int status = saveContainerDataSource(info, isEdit); 199 if (status == DS_SAVED) { 200 return saveWebXmlDataSource(info, isEdit); 202 } else { 203 return status; 204 } 205 } catch (Throwable t) { 206 System.err.println(Messages.getErrorString("ServerDSAdmin.ERROR_0003_CANNOT_SAVE", info.getName())); } 208 return DS_OPERATION_FAILED; 209 } 210 211 217 public Map listWebXmlDataSources() { 218 Map webXmlDataSources = new HashMap (); 219 try { 220 File webXml = getWebXmlFile(); 221 if (webXml == null) { 222 return webXmlDataSources; 223 } 224 Document doc = getWebXmlDoc(webXml); 226 if (doc == null) { 227 return webXmlDataSources; 228 } 229 List dataSourceNodes = doc.selectNodes("web-app/" + WEB_XML_RESOURCE_REF); for (int n = 0; n < dataSourceNodes.size(); n++) { 232 Element dataSourceNode = (Element) dataSourceNodes.get(n); 234 String description = ""; String name = ""; String type = null; 237 Node node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_TYPE); 239 if (node != null) { 240 type = node.getText(); 241 } 242 if (JAVAX_SQL_DATASOURCE.equals(type)) { 244 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_DESCRIPTION); 245 if (node != null) { 246 description = node.getText(); 247 } 248 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_NAME); 249 if (node != null) { 250 name = node.getText(); 251 if (name.startsWith(JDBC_PREFIX)) { 252 name = name.substring(5); 253 } else { 254 continue; 256 } 257 } 258 DataSourceInfo info = new DataSourceInfo(name, description, type); 260 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_DRIVER); 263 if (node != null) { 264 info.setDriver(node.getText()); 265 } 266 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_URL); 267 if (node != null) { 268 info.setUrl(node.getText()); 269 } 270 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_USER_ID); 271 if (node != null) { 272 info.setUserId(node.getText()); 273 } 274 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_PASSWORD); 275 if (node != null) { 276 info.setPassword(node.getText()); 277 } 278 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_AUTH); 279 if (node != null) { 280 info.setAuthorization(node.getText()); 281 } 282 webXmlDataSources.put(name, info); 284 } 285 } 286 } catch (Throwable t) { 287 t.printStackTrace(); 289 } 290 return webXmlDataSources; 291 } 292 293 302 private int removeWebXmlDataSourceNode(Document doc, String id) { 303 String targetName = JDBC_PREFIX + id; 305 Element webAppElement = (Element) doc.selectSingleNode("web-app"); List dataSourceNodes = webAppElement.selectNodes("resource-ref"); for (int n = 0; n < dataSourceNodes.size(); n++) { 309 Element dataSourceNode = (Element) dataSourceNodes.get(n); 311 String type = null; 312 Node node = dataSourceNode.selectSingleNode("res-type"); if (node != null) { 314 type = node.getText(); 315 } 316 if (JAVAX_SQL_DATASOURCE.equals(type)) { 317 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_NAME); 320 if (node != null) { 321 String dataSourceName = node.getText(); 322 if (dataSourceName != null && dataSourceName.equals(targetName)) { 323 webAppElement.remove(dataSourceNode); 326 return DS_DELETED; 327 } 328 } 329 } 330 } 331 return DS_NOT_FOUND; 332 } 333 334 342 public int deleteWebXmlDataSource(String id) { 343 try { 344 File webXml = getWebXmlFile(); 345 if (webXml == null) { 346 return DS_FILE_OPERATION_FAILED; 347 } 348 Document doc = getWebXmlDoc(webXml); 350 if (doc == null) { 351 return DS_FILE_OPERATION_FAILED; 352 } 353 int status = removeWebXmlDataSourceNode(doc, id); 354 if (status == DS_DELETED) { 355 try { 357 FileOutputStream outStream = new FileOutputStream (webXml); 358 OutputFormat format = OutputFormat.createPrettyPrint(); 359 XMLWriter writer = new XMLWriter(outStream, format); 360 writer.write(doc); 361 writer.flush(); 362 writer.close(); 363 } catch (Exception e) { 364 return DS_FILE_OPERATION_FAILED; 365 } 366 return DS_DELETED; 367 } 368 return status; 369 } catch (Throwable t) { 370 t.printStackTrace(); 372 } 373 return DS_OPERATION_FAILED; 374 } 375 376 385 public int renameWebXmlDataSource(String id, String newId) { 386 try { 387 String targetName = JDBC_PREFIX + id; 389 File webXml = getWebXmlFile(); 390 if (webXml == null) { 391 return DS_FILE_OPERATION_FAILED; 392 } 393 Document doc = getWebXmlDoc(webXml); 395 if (doc == null) { 396 return DS_FILE_OPERATION_FAILED; 397 } 398 List dataSourceNodes = doc.selectNodes("web-app/" + WEB_XML_RESOURCE_REF); for (int n = 0; n < dataSourceNodes.size(); n++) { 401 Element dataSourceNode = (Element) dataSourceNodes.get(n); 403 String type = null; 404 Node node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_TYPE); 405 if (node != null) { 406 type = node.getText(); 407 } 408 if (JAVAX_SQL_DATASOURCE.equals(type)) { 409 node = dataSourceNode.selectSingleNode(WEB_XML_RESOURCE_REF_NAME); 412 if (node != null) { 413 String dataSourceName = node.getText(); 414 if (dataSourceName.equals(targetName)) { 415 node.setText(JDBC_PREFIX + newId); 418 try { 420 String xml = doc.asXML(); 421 FileWriter writer = new FileWriter (webXml); 422 writer.write(xml); 423 } catch (Exception e) { 424 return DS_FILE_OPERATION_FAILED; 425 } 426 return DS_RENAMED; 427 } 428 } 429 } 430 } 431 return DS_NOT_FOUND; 432 } catch (Throwable t) { 433 t.printStackTrace(); 435 } 436 return DS_OPERATION_FAILED; 437 } 438 439 448 public int saveWebXmlDataSource(DataSourceInfo info, boolean isEdit) { 449 if (isEdit) { 450 return DS_SAVED; 453 } 454 try { 455 if (info == null) { 456 System.err.println(Messages.getErrorString("ServerDSAdmin.ERROR_0007_DATASOURCE_IS_NULL")); return DS_OPERATION_FAILED; 459 } 460 String targetName = JDBC_PREFIX + info.getName(); 462 File webXml = getWebXmlFile(); 463 if (webXml == null) { 464 return DS_FILE_OPERATION_FAILED; 465 } 466 Document doc = getWebXmlDoc(webXml); 468 if (doc == null) { 469 return DS_FILE_OPERATION_FAILED; 470 } 471 Element webApp = (Element) doc.selectSingleNode("web-app"); Element resourceNode = webApp.addElement(WEB_XML_RESOURCE_REF); 478 String property = info.getDescription(); 479 if (property != null) { 480 resourceNode.addElement(WEB_XML_RESOURCE_REF_DESCRIPTION).setText(property); 481 } 482 resourceNode.addElement(WEB_XML_RESOURCE_REF_NAME).setText(targetName); 483 resourceNode.addElement(WEB_XML_RESOURCE_REF_TYPE).setText(JAVAX_SQL_DATASOURCE); 484 property = info.getAuthorization(); 485 if (property != null) { 486 resourceNode.addElement(WEB_XML_RESOURCE_REF_AUTH).setText(property); 487 } else { 488 resourceNode.addElement(WEB_XML_RESOURCE_REF_AUTH).setText("Container"); } 490 try { 492 FileOutputStream outStream = new FileOutputStream (webXml); 493 OutputFormat format = OutputFormat.createPrettyPrint(); 494 XMLWriter writer = new XMLWriter(outStream, format); 495 writer.write(doc); 496 writer.flush(); 497 writer.close(); 498 } catch (Exception e) { 499 return DS_FILE_OPERATION_FAILED; 500 } 501 return DS_SAVED; 502 } catch (Throwable t) { 503 t.printStackTrace(); 505 } 506 return DS_OPERATION_FAILED; 507 } 508 509 public void setApplicationRoot(String applicationPath) { 510 this.applicationPath = applicationPath; 511 } 512 513 public void setWebApplicationName(String webAppName) { 514 if (!webAppName.endsWith(".war")) { webAppName = webAppName + ".war"; } 517 this.webAppName = webAppName; 518 } 519 520 private File getWebXmlFile() { 521 File webXml = new File (PentahoSystem.getApplicationContext().getApplicationPath("WEB-INF" + File.separator + "web.xml")); if (!webXml.exists() || !webXml.isFile()) { 524 File standaloneWebXml = new File (applicationPath + File.separator + webAppName + File.separator + "WEB-INF" + File.separator + "web.xml"); if (standaloneWebXml.exists()) { 526 return standaloneWebXml; 527 } 528 System.err.println(Messages.getErrorString("ServerDSAdmin.ERROR_0001_USER_WEB_XML_NOT_FOUND", webXml.getAbsolutePath())); return null; 531 } 532 return webXml; 533 } 534 535 private Document getWebXmlDoc(File webXmlFile) { 536 Document doc = XmlHelper.getDocFromFile(webXmlFile); 538 if (doc == null) { 539 System.err.println(Messages.getErrorString("ServerDSAdmin.ERROR_0002_WEB_XML_INVALID", webXmlFile.getAbsolutePath())); } 542 return doc; 543 } 544 } 545 | Popular Tags |