KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > piratepete > dbpirate > Registry


1 package com.piratepete.dbpirate;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.jdom.Document;
10 import org.jdom.Element;
11 import org.jdom.JDOMException;
12 import org.jdom.input.SAXBuilder;
13 import com.piratepete.util.db.DBConnectionType;
14 import com.piratepete.util.db.DBConnection;
15
16
17 /**
18  * Registry Class
19  * Copyright (C) 2003 David L. Whitehurst<p>
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.<p>
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  * GNU General Public License for more details.<p>
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.<p>
34  *
35  * <a HREF="http://www.piratepetesoftware.com">piratepetesoftware.com</a><br>
36  * <a HREF="mailto:dlwhitehurst@comcast.net">dlwhitehurst@comcast.net</a>
37  *
38  * @version 1.0a
39  */

40 public class Registry {
41     private ArrayList JavaDoc dbtypes; // database name collection
42
private ArrayList JavaDoc dblogins; // database login collection
43

44     public Registry() {
45         dbtypes = new ArrayList JavaDoc();
46         dblogins = new ArrayList JavaDoc();
47         loadDBTypes();
48         loadDBLogins();
49         
50         ArrayList JavaDoc myarray;
51         myarray = getDatabaseLogins();
52         
53         int i = 999; // debugging stop
54

55     }
56     
57     public void loadDBTypes() {
58         try {
59             SAXBuilder builder = new SAXBuilder();
60             InputStream JavaDoc ins = ClassLoader.getSystemResourceAsStream("db.conf"); // was dbpirate.conf
61
Document doc = builder.build (ins);
62             Element rootElement = doc.getRootElement();
63             //Element dbElement = rootElement.getChild("database");
64
List JavaDoc children = rootElement.getChildren("database");
65             Iterator JavaDoc iterator = children.iterator();
66             while (iterator.hasNext()) {
67                 Element child = (Element) iterator.next();
68                 DBConnectionType dtype = new DBConnectionType();
69                 dtype.setName(child.getChildText("name"));
70                 dtype.setClassname(child.getChildText("classname"));
71                 dtype.setURL(child.getChildText("url"));
72                 dbtypes.add(dtype);
73             }
74             
75         } catch(JDOMException e) {
76             e.printStackTrace();
77         } catch(NullPointerException JavaDoc e) {
78             e.printStackTrace();
79         } catch (IOException JavaDoc ioe) {
80             ioe.printStackTrace();
81         }
82         
83     }
84     
85     public void loadDBLogins() {
86         try {
87             SAXBuilder builder = new SAXBuilder();
88             InputStream JavaDoc ins = ClassLoader.getSystemResourceAsStream("testlogins.conf"); // test file only
89
Document doc = builder.build (ins);
90             Element rootElement = doc.getRootElement();
91             Element dbElement = rootElement.getChild("connections");
92             List JavaDoc children = dbElement.getChildren("connection");
93             Iterator JavaDoc iterator = children.iterator();
94             while (iterator.hasNext()) {
95                 Element child = (Element) iterator.next();
96                 DBConnection dbconnection = new DBConnection();
97                 dbconnection.setName(child.getChildText("name"));
98                 dbconnection.setUserName(child.getChildText("user"));
99                 dbconnection.setPassword(child.getChildText("password"));
100                 dbconnection.setHostname(child.getChildText("hostname"));
101                 dbconnection.setSid(child.getChildText("sid"));
102                 dbconnection.setClassname(child.getChildText("classname"));
103                 dbconnection.setUrl(child.getChildText("url"));
104                 dbconnection.setPort(child.getChildText("port"));
105                 dbconnection.setTimestamp(child.getChildText("timestamp"));
106                 dblogins.add(dbconnection);
107             }
108             
109         } catch(JDOMException e) {
110             e.printStackTrace();
111         } catch(NullPointerException JavaDoc e) {
112             e.printStackTrace();
113         } catch (IOException JavaDoc ioe) {
114             ioe.printStackTrace();
115         }
116         
117     }
118     
119     public ArrayList JavaDoc getDatabaseTypes() {
120         if (dbtypes != null) {
121             return dbtypes;
122         } else {
123             return null;
124         }
125     }
126
127     public ArrayList JavaDoc getDatabaseLogins() {
128         if (dblogins != null) {
129             return dblogins;
130         } else {
131             return null;
132         }
133     }
134
135 }
136
Popular Tags