001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Emmanuel Cecchet.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.controller.backup.backupers;
021:
022: import java.util.regex.Matcher;
023: import java.util.regex.Pattern;
024:
025: /**
026: * This class defines a BasicUrlParser, which provides a default implementation
027: * of the JdbcUrlParser interface.
028: *
029: * @author <a href="mailto:robert.hodges@continuent.com">Robert Hodges</a>
030: * @version 1.0
031: */
032: public class BasicUrlParser implements JdbcUrlParser {
033: private String dbType;
034: private String host;
035: private String port;
036: private String dbName;
037:
038: // Pattern and defaults for parsing mysql URLs.
039: private static final String MYSQL_PREFIX = "jdbc:mysql";
040: private static Pattern mysqlPattern = Pattern
041: .compile("jdbc:mysql://([a-zA-Z0-9_\\-.]+|)((:(\\d+))|)/([a-zA-Z][a-zA-Z0-9_\\-]*)(\\?.*)?");
042:
043: // Pattern and defaults for parsing PostgreSQL URLs.
044: private static final String POSTGRESQL_PREFIX = "jdbc:postgresql";
045: private static Pattern postgresqlPattern = Pattern
046: .compile("jdbc:postgresql:((//([a-zA-Z0-9_\\-.]+|\\[[a-fA-F0-9:]+])((:(\\d+))|))/|)([^\\s?]*).*$");
047:
048: /**
049: * Creates a new <code>BasicUrlParser</code> object.
050: */
051: public BasicUrlParser() {
052: }
053:
054: /**
055: * {@inheritDoc}
056: * @see org.continuent.sequoia.controller.backup.backupers.JdbcUrlParser#setUrl(java.lang.String)
057: */
058: public void setUrl(String url) {
059: parse(url);
060: }
061:
062: /**
063: * Parses the URL using available patterns.
064: *
065: * @param url The URL to parse.
066: */
067: protected void parse(String url) {
068: // Assign defaults.
069: dbType = null;
070: host = null;
071: port = null;
072: dbName = null;
073:
074: if (url.startsWith(MYSQL_PREFIX)) {
075: dbType = "mysql";
076: host = "localhost";
077: port = "3306";
078:
079: Matcher matcher = mysqlPattern.matcher(url);
080: if (matcher.matches()) {
081: if (matcher.group(1) != null
082: && matcher.group(1).length() > 0)
083: host = matcher.group(1);
084: if (matcher.group(4) != null
085: && matcher.group(4).length() > 0)
086: port = matcher.group(4);
087: dbName = matcher.group(5);
088: }
089: } else if (url.startsWith(POSTGRESQL_PREFIX)) {
090: dbType = "postgresql";
091: host = "localhost";
092: port = "5432";
093:
094: Matcher matcher = postgresqlPattern.matcher(url);
095: if (matcher.matches()) {
096: if (matcher.group(3) != null)
097: host = matcher.group(3);
098: if (matcher.group(6) != null)
099: port = matcher.group(6);
100: dbName = matcher.group(7);
101: }
102: } else {
103: }
104: }
105:
106: /**
107: * {@inheritDoc}
108: * @see org.continuent.sequoia.controller.backup.backupers.JdbcUrlParser#getDbName()
109: */
110: public String getDbName() {
111: return dbName;
112: }
113:
114: /**
115: * {@inheritDoc}
116: * @see org.continuent.sequoia.controller.backup.backupers.JdbcUrlParser#getDbType()
117: */
118: public String getDbType() {
119: return dbType;
120: }
121:
122: /**
123: * {@inheritDoc}
124: * @see org.continuent.sequoia.controller.backup.backupers.JdbcUrlParser#getHost()
125: */
126: public String getHost() {
127: return host;
128: }
129:
130: /**
131: * {@inheritDoc}
132: * @see org.continuent.sequoia.controller.backup.backupers.JdbcUrlParser#getPort()
133: */
134: public String getPort() {
135: return port;
136: }
137: }
|