KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > admin > datasources > StandaloneSimpleJNDIDatasourceAdmin


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  */

13 /**
14  *
15  */

16 package org.pentaho.core.admin.datasources;
17
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
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 /**
34  * @author Michael D'Amour
35  *
36  */

37 public class StandaloneSimpleJNDIDatasourceAdmin extends DatasourceAdminBase {
38     String JavaDoc jndiPath = null;
39
40     Properties JavaDoc p = new Properties JavaDoc();
41
42     Map JavaDoc datasources = new HashMap JavaDoc();
43
44     IPentahoSession session;
45
46     /**
47      *
48      */

49     public StandaloneSimpleJNDIDatasourceAdmin(String JavaDoc path, IPentahoSession session) {
50         super();
51         setSimpleJNDIPath(path);
52         this.session = session;
53         init();
54     }
55
56     public void init() {
57         String JavaDoc path = getSimpleJNDIPath();
58         InputStream JavaDoc 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 JavaDoc e) {
68             e.printStackTrace();
69         } finally {
70             try {
71                 is.close();
72             } catch (Exception JavaDoc e) {
73                 e.printStackTrace();
74             }
75         }
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.pentaho.admin.datasources.IDatasourceAdmin#listDataSources()
82      */

83     public Map JavaDoc listDataSources() {
84         if (datasources.size() > 0) {
85             return datasources;
86         } else if (p != null) {
87             // find datasources
88
Enumeration JavaDoc keys = p.keys();
89             while (keys.hasMoreElements()) {
90                 String JavaDoc propertyKey = (String JavaDoc) keys.nextElement();
91                 if (propertyKey.indexOf("/") != -1) {
92                     String JavaDoc key = propertyKey.substring(0, propertyKey.indexOf("/")); //$NON-NLS-1$
93
// propertyKeyType = type/driver/url/user/password
94
String JavaDoc propertyKeyType = propertyKey.substring(propertyKey.indexOf("/") + 1); //$NON-NLS-1$
95
String JavaDoc 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"); //$NON-NLS-1$ //$NON-NLS-2$
101
datasources.put(key, dsi);
102                     }
103                     if (propertyKeyType.equalsIgnoreCase("type")) { //$NON-NLS-1$
104
dsi.setType(propertyValue);
105                     } else if (propertyKeyType.equalsIgnoreCase("driver")) { //$NON-NLS-1$
106
dsi.setDriver(propertyValue);
107                     } else if (propertyKeyType.equalsIgnoreCase("url")) { //$NON-NLS-1$
108
dsi.setUrl(propertyValue);
109                     } else if (propertyKeyType.equalsIgnoreCase("user")) { //$NON-NLS-1$
110
dsi.setUserId(propertyValue);
111                     } else if (propertyKeyType.equalsIgnoreCase("password")) { //$NON-NLS-1$
112
dsi.setPassword(propertyValue);
113                     }
114                 }
115             }
116         }
117         return datasources;
118     }
119
120     public void setSimpleJNDIPath(String JavaDoc path) {
121         jndiPath = path;
122     }
123
124     /**
125      * @return
126      */

127     private String JavaDoc getSimpleJNDIPath() {
128         return jndiPath;
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.pentaho.admin.datasources.IDatasourceAdmin#deleteDataSource(java.lang.String)
135      */

136     public int deleteDataSource(String JavaDoc id) {
137         Iterator JavaDoc iterator = datasources.keySet().iterator();
138         FileOutputStream JavaDoc fos = null;
139         try {
140             fos = new FileOutputStream JavaDoc(getSimpleJNDIPath());
141             List JavaDoc removeKeys = new LinkedList JavaDoc();
142             while (iterator.hasNext()) {
143                 String JavaDoc key = (String JavaDoc) iterator.next();
144                 if (!key.equals(id)) {
145                     DataSourceInfo info = (DataSourceInfo) datasources.get(key);
146                     fos.write((info.getName() + "/type=" + info.getType() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
147
fos.write((info.getName() + "/driver=" + info.getDriver() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
148
fos.write((info.getName() + "/url=" + info.getUrl() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
149
fos.write((info.getName() + "/user=" + info.getUserId() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
150
fos.write((info.getName() + "/password=" + info.getPassword() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
151
} else {
152                     removeKeys.add(key);
153                 }
154             }
155             for (int i = 0; i < removeKeys.size(); i++) {
156                 String JavaDoc key = (String JavaDoc) removeKeys.get(i);
157                 datasources.remove(key);
158             }
159         } catch (Exception JavaDoc e) {
160             e.printStackTrace();
161         } finally {
162             try {
163                 fos.close();
164             } catch (Exception JavaDoc e) {
165             }
166         }
167         init();
168         return 0;
169     }
170
171     /*
172      * (non-Javadoc)
173      *
174      * @see org.pentaho.admin.datasources.IDatasourceAdmin#renameDataSource(java.lang.String, java.lang.String)
175      */

176     public int renameDataSource(String JavaDoc id, String JavaDoc newId) {
177         // TODO Auto-generated method stub
178
return 0;
179     }
180
181     /*
182      * (non-Javadoc)
183      *
184      * @see org.pentaho.admin.datasources.IDatasourceAdmin#saveDataSource(org.pentaho.admin.datasources.DataSourceInfo, boolean)
185      */

186     public int saveDataSource(DataSourceInfo info, boolean allowEdit) {
187         datasources.put(info.getName(), info);
188         Iterator JavaDoc iterator = datasources.keySet().iterator();
189         FileOutputStream JavaDoc fos = null;
190         try {
191             fos = new FileOutputStream JavaDoc(getSimpleJNDIPath());
192             while (iterator.hasNext()) {
193                 String JavaDoc key = (String JavaDoc) iterator.next();
194                 info = (DataSourceInfo) datasources.get(key);
195                 fos.write((info.getName() + "/type=" + info.getType() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
196
fos.write((info.getName() + "/driver=" + info.getDriver() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
197
fos.write((info.getName() + "/url=" + info.getUrl() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
198
fos.write((info.getName() + "/user=" + info.getUserId() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
199
fos.write((info.getName() + "/password=" + info.getPassword() + "\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
200
}
201         } catch (Exception JavaDoc e) {
202             e.printStackTrace();
203         } finally {
204             try {
205                 fos.close();
206             } catch (Exception JavaDoc e) {
207             }
208         }
209         init();
210         return 0;
211     }
212
213     /*
214      * (non-Javadoc)
215      *
216      * @see org.pentaho.admin.datasources.IDatasourceAdmin#getDataSourceInfo(java.lang.String)
217      */

218     public DataSourceInfo getDataSourceInfo(String JavaDoc 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