KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > backupTool > dbDump > DbDumperFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.outerj.daisy.backupTool.dbDump;
17
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 public class DbDumperFactory {
22     public static DbDumper createDbDumper(String JavaDoc dburl, String JavaDoc username, String JavaDoc password) throws Exception JavaDoc {
23         Pattern JavaDoc urlPattern = Pattern.compile("jdbc:(.*)://(([^\\?:]+)(:(\\d*))?)/([^\\?:]+)(\\?.+)?");
24         Matcher JavaDoc urlMatcher = urlPattern.matcher(dburl);
25
26         if (!urlMatcher.matches())
27             throw new Exception JavaDoc("Please verify that the database url is correct " + dburl);
28
29         String JavaDoc dbType = urlMatcher.group(1);
30         String JavaDoc dbName = urlMatcher.group(6);
31         Integer JavaDoc port = new Integer JavaDoc(urlMatcher.group(5) == null ? "0" : urlMatcher.group(5));
32         String JavaDoc hostName = urlMatcher.group(3);
33
34         dbType = dbType.substring(0, 1).toUpperCase() + dbType.substring(1);
35
36         Class JavaDoc dumperClazz = Class.forName(MysqlDbDumper.class.getPackage().getName() + "." + dbType + "DbDumper");
37
38         Object JavaDoc[] valueParams = new Object JavaDoc[] { dbName, hostName, port, password, username };
39         Class JavaDoc[] classParams = new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class, Integer JavaDoc.class, String JavaDoc.class, String JavaDoc.class };
40
41         AbstractDbDumper dbDumper = (AbstractDbDumper) dumperClazz.getConstructor(classParams).newInstance(valueParams);
42
43         return dbDumper;
44     }
45 }
Popular Tags