1 13 16 package org.pentaho.core.admin.datasources; 17 18 import java.io.FileOutputStream ; 19 import java.io.InputStream ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 import org.pentaho.core.repository.ISolutionRepository; 29 import org.pentaho.core.session.IPentahoSession; 30 import org.pentaho.core.system.PentahoSystem; 31 import org.pentaho.core.util.DatasourceHelper; 32 33 37 public class StandaloneSimpleJNDIDatasourceAdmin extends DatasourceAdminBase { 38 String jndiPath = null; 39 40 Properties p = new Properties (); 41 42 Map datasources = new HashMap (); 43 44 IPentahoSession session; 45 46 49 public StandaloneSimpleJNDIDatasourceAdmin(String path, IPentahoSession session) { 50 super(); 51 setSimpleJNDIPath(path); 52 this.session = session; 53 init(); 54 } 55 56 public void init() { 57 String path = getSimpleJNDIPath(); 58 InputStream is = null; 59 try { 60 p.clear(); 61 datasources.clear(); 62 DatasourceHelper.clearCache(); 63 ISolutionRepository repository = PentahoSystem.getSolutionRepository(getSession()); 64 is = repository.getResourceInputStream(path); 65 p.load(is); 66 datasources = listDataSources(); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 } finally { 70 try { 71 is.close(); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 } 77 78 83 public Map listDataSources() { 84 if (datasources.size() > 0) { 85 return datasources; 86 } else if (p != null) { 87 Enumeration keys = p.keys(); 89 while (keys.hasMoreElements()) { 90 String propertyKey = (String ) keys.nextElement(); 91 if (propertyKey.indexOf("/") != -1) { 92 String key = propertyKey.substring(0, propertyKey.indexOf("/")); String propertyKeyType = propertyKey.substring(propertyKey.indexOf("/") + 1); String propertyValue = p.getProperty(propertyKey); 96 DataSourceInfo dsi = null; 97 if (datasources.containsKey(key)) { 98 dsi = (DataSourceInfo) datasources.get(key); 99 } else { 100 dsi = new DataSourceInfo(key, "desc", "javax.sql.DataSource"); datasources.put(key, dsi); 102 } 103 if (propertyKeyType.equalsIgnoreCase("type")) { dsi.setType(propertyValue); 105 } else if (propertyKeyType.equalsIgnoreCase("driver")) { dsi.setDriver(propertyValue); 107 } else if (propertyKeyType.equalsIgnoreCase("url")) { dsi.setUrl(propertyValue); 109 } else if (propertyKeyType.equalsIgnoreCase("user")) { dsi.setUserId(propertyValue); 111 } else if (propertyKeyType.equalsIgnoreCase("password")) { dsi.setPassword(propertyValue); 113 } 114 } 115 } 116 } 117 return datasources; 118 } 119 120 public void setSimpleJNDIPath(String path) { 121 jndiPath = path; 122 } 123 124 127 private String getSimpleJNDIPath() { 128 return jndiPath; 129 } 130 131 136 public int deleteDataSource(String id) { 137 Iterator iterator = datasources.keySet().iterator(); 138 FileOutputStream fos = null; 139 try { 140 fos = new FileOutputStream (getSimpleJNDIPath()); 141 List removeKeys = new LinkedList (); 142 while (iterator.hasNext()) { 143 String key = (String ) iterator.next(); 144 if (!key.equals(id)) { 145 DataSourceInfo info = (DataSourceInfo) datasources.get(key); 146 fos.write((info.getName() + "/type=" + info.getType() + "\n").getBytes()); fos.write((info.getName() + "/driver=" + info.getDriver() + "\n").getBytes()); fos.write((info.getName() + "/url=" + info.getUrl() + "\n").getBytes()); fos.write((info.getName() + "/user=" + info.getUserId() + "\n").getBytes()); fos.write((info.getName() + "/password=" + info.getPassword() + "\n").getBytes()); } else { 152 removeKeys.add(key); 153 } 154 } 155 for (int i = 0; i < removeKeys.size(); i++) { 156 String key = (String ) removeKeys.get(i); 157 datasources.remove(key); 158 } 159 } catch (Exception e) { 160 e.printStackTrace(); 161 } finally { 162 try { 163 fos.close(); 164 } catch (Exception e) { 165 } 166 } 167 init(); 168 return 0; 169 } 170 171 176 public int renameDataSource(String id, String newId) { 177 return 0; 179 } 180 181 186 public int saveDataSource(DataSourceInfo info, boolean allowEdit) { 187 datasources.put(info.getName(), info); 188 Iterator iterator = datasources.keySet().iterator(); 189 FileOutputStream fos = null; 190 try { 191 fos = new FileOutputStream (getSimpleJNDIPath()); 192 while (iterator.hasNext()) { 193 String key = (String ) iterator.next(); 194 info = (DataSourceInfo) datasources.get(key); 195 fos.write((info.getName() + "/type=" + info.getType() + "\n").getBytes()); fos.write((info.getName() + "/driver=" + info.getDriver() + "\n").getBytes()); fos.write((info.getName() + "/url=" + info.getUrl() + "\n").getBytes()); fos.write((info.getName() + "/user=" + info.getUserId() + "\n").getBytes()); fos.write((info.getName() + "/password=" + info.getPassword() + "\n").getBytes()); } 201 } catch (Exception e) { 202 e.printStackTrace(); 203 } finally { 204 try { 205 fos.close(); 206 } catch (Exception e) { 207 } 208 } 209 init(); 210 return 0; 211 } 212 213 218 public DataSourceInfo getDataSourceInfo(String id) { 219 return (DataSourceInfo) datasources.get(id); 220 } 221 222 public IPentahoSession getSession() { 223 return session; 224 } 225 226 public void setSession(IPentahoSession session) { 227 this.session = session; 228 } 229 } | Popular Tags |