View Javadoc

1   /*
2    * Created on 15.09.2004
3    */
4   package myx;
5   
6   import java.sql.Connection;
7   import java.sql.DriverManager;
8   import java.sql.SQLException;
9   import java.util.logging.Level;
10  import java.util.logging.Logger;
11  
12  import org.xmldb.api.base.Collection;
13  import org.xmldb.api.base.Database;
14  import org.xmldb.api.base.XMLDBException;
15  import org.xmldb.api.base.ErrorCodes;
16  
17  /***
18   * @author Mladen Adamovic (adamm@blic.net) 
19   */
20  public class myxDatabase implements Database {
21      private static Logger logger = Logger.getLogger(myxDatabase.class.getName());
22      private Connection con;
23  
24      /***
25       * change this MySQL related data if neccesery
26       */
27      public static final String MySqlDriverName="com.mysql.jdbc.Driver";
28      public static final String MySqlDbURL="jdbc:mysql://"; 
29      public String beforeUsername="myx_data?user=";
30      public String username="myx_admin";
31      public String beforePass="&password=";
32      public String password="admin_pass"; 
33      public static String serverName, serverPort;
34  
35  
36      public myxDatabase() {
37          super();
38          //connect(); at this stage the host is not yet set
39      }
40  
41      public Connection setConnection(Connection newConnection) {
42          return con = newConnection;
43      }
44  
45      public Connection getConnection() throws XMLDBException {
46  	if (isConnected()) {
47  	    return con;
48  	}
49  	else {
50  	    return openConnection();
51  	}
52      }
53  
54      public boolean isConnected() {
55  	if (con==null) {
56  	    return false;
57  	}
58  	return true;
59      }
60  
61      public Connection openConnection() throws XMLDBException {
62          if(con==null) {
63              try {
64  		//make connection
65  		Class.forName(MySqlDriverName).newInstance();
66  		String sqlConnectionString = MySqlDbURL+serverName+':'+serverPort+'/'+beforeUsername+username+beforePass+password;
67  		logger.config("trying MySQL connection:"+sqlConnectionString);
68  		con = DriverManager.getConnection(sqlConnectionString);
69  		//logger.fine("con:"+con);
70              } 
71  	    catch(Exception e) {
72  		//logger.log(Level.SEVERE, "Error connecting to database",e); 
73  		throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
74  					 "Error connecting to database ("+e.getMessage()+")");
75              }
76          }
77  	return con;
78      }
79  
80      public void closeConnection() throws XMLDBException {
81  	logger.config("Closing connection");
82          if(con!=null) {
83  	    try {
84  		con.close();
85  		con = null;
86  	    }
87  	    catch (SQLException e) {
88  		//logger.log(Level.SEVERE,"Error closing collection :",e.getMessage());
89  		throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
90  					 e.getMessage());
91  	    }
92  	}
93  	else {
94  	    throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
95  				     "Connection already closed");
96  	}
97      }
98  
99      /***
100      * @see org.xmldb.api.base.Database#getName()
101      */
102     public String getName() throws XMLDBException {
103         return new String("myx");
104     }
105 
106     /***
107      * @see org.xmldb.api.base.Database#getNames()
108      */
109     public String[] getNames() throws XMLDBException {
110         return new String[]{"myx"};
111     }
112 
113     /***
114      * @see org.xmldb.api.base.Database#getCollection(java.lang.String, java.lang.String, java.lang.String)
115      */
116     public Collection getCollection(String uri, String username, String password)
117 	throws XMLDBException {
118 	if (isConnected()) {
119 	    closeConnection();
120 	}
121         this.username=username;
122         this.password=password;
123 	
124         String colName=uri.substring(uri.lastIndexOf('/')+1);
125         serverName=uri.substring(0,uri.lastIndexOf('/'));
126         if(serverName.startsWith(getName()+"://")) {
127 	    serverName=serverName.substring(6);
128 	}
129         if(serverName.endsWith("//")) {
130 	    serverName=serverName.substring(0,serverName.length()-1);
131 	}
132         if(serverName.length()<2) {
133 	    serverName="localhost";
134 	}
135 	// extract the port if one exists
136 	
137 	if (serverName.indexOf(":") > 0) {
138 	    serverPort = serverName.substring(serverName.indexOf(":")+1,serverName.length());
139 	    serverName = serverName.substring(0,serverName.indexOf(":"));
140 	}
141 	else {
142 	    serverPort = "3306";
143 	}
144 
145         logger.config("uri:"+uri);
146         logger.config("serverName="+serverName);
147         logger.config("serverPort="+serverPort);
148         logger.config("username="+username);
149         logger.config("password="+password);
150         logger.config("colName="+colName);
151 
152         openConnection();
153         myxCollection myxCol=new myxCollection(this,colName);
154         return myxCol;
155     }
156 
157     /***
158      * @param colName name of collection to open
159      * @return collection with given name
160      */
161     public Collection getCollection(String colName) throws XMLDBException{
162     	openConnection();
163     	myxCollection myxCol=new myxCollection(this,colName);
164         return myxCol;
165     }
166     /***
167      * @see org.xmldb.api.base.Database#acceptsURI(java.lang.String)
168      */
169     public boolean acceptsURI(String uri) throws XMLDBException {
170         return false;
171     }
172 
173     /***
174      * @see org.xmldb.api.base.Database#getConformanceLevel()
175      */
176     public String getConformanceLevel() throws XMLDBException {
177         return "1";
178     }
179 
180     /***
181      * @see org.xmldb.api.base.Configurable#getProperty(java.lang.String)
182      */
183     public String getProperty(String name) throws XMLDBException {
184         return System.getProperty(name);
185     }
186 
187     /***
188      * @see org.xmldb.api.base.Configurable#setProperty(java.lang.String, java.lang.String)
189      */
190     public void setProperty(String name, String value) throws XMLDBException {
191         System.setProperty(name, value);
192     }
193 }