001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.configuration;
034:
035: import java.io.Serializable;
036: import java.util.regex.Pattern;
037:
038: /**
039: * Container for division data read from the global configuration.
040: *
041: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
042: */
043:
044: public class DivisionData implements Serializable {
045: private static final long serialVersionUID = -7804463451172463152L;
046: /** Division to be used for automated tests */
047: public static final int DIVISION_TEST = -2;
048: /** Global configuration division */
049: public static final int DIVISION_GLOBAL = 0;
050:
051: protected int id = -1;
052: protected boolean available;
053: protected String dataSource = null;
054: protected String domainRegEx = null;
055: protected Pattern domainPattern = null;
056: protected DBVendor dbVendor = null;
057: protected String dbVersion = null;
058:
059: /**
060: * Constructor.
061: * @param id the division ID
062: * @param available if division is available
063: * @param dataSource datasource JNDI path
064: * @param domainRegEx domain name matcher
065: * @param dbVendor database vendor
066: * @param dbVersion database version
067: */
068: public DivisionData(int id, boolean available, String dataSource,
069: String domainRegEx, DBVendor dbVendor, String dbVersion) {
070: this .id = id;
071: this .available = available;
072: this .dataSource = dataSource;
073: this .domainRegEx = domainRegEx;
074: this .dbVendor = dbVendor;
075: this .dbVersion = dbVersion;
076: try {
077: this .domainPattern = Pattern.compile(domainRegEx);
078: } catch (Exception e) {
079: this .domainPattern = null;
080: }
081: }
082:
083: /**
084: * Returns an editable division data object initialized with this instance. Note that the
085: * edit object does not influence the original division data object, so it is safe to create
086: * edit objects from system division data entries without cloning them first.
087: *
088: * @return an editable division data object initialized with this instance
089: */
090: public DivisionDataEdit asEditable() {
091: return new DivisionDataEdit(this );
092: }
093:
094: /**
095: * Returns the JNDI path to this division's datasource.
096: * @return the JNDI path to this division's datasource.
097: */
098: public String getDataSource() {
099: return dataSource;
100: }
101:
102: /**
103: * The regular expression describing matching domains for this divison.
104: * @return the regular expression describing matching domains for this divison.
105: */
106: public String getDomainRegEx() {
107: return domainRegEx;
108: }
109:
110: /**
111: * Returns true when the given domain matches, false otherwise.
112: *
113: * @param domain the domain to be checked
114: * @return true when the given domain matches, false otherwise.
115: */
116: public boolean isMatchingDomain(String domain) {
117: return domainPattern != null
118: && domainPattern.matcher(domain).lookingAt();
119: }
120:
121: /**
122: * Get the DB Vendor
123: *
124: * @return DB Vendor
125: */
126: public DBVendor getDbVendor() {
127: return dbVendor;
128: }
129:
130: /**
131: * Get the DB Version
132: *
133: * @return DB Version
134: */
135: public String getDbVersion() {
136: return dbVersion;
137: }
138:
139: /**
140: * Is this division available (ie can a connection be retrieved)
141: *
142: * @return available
143: */
144: public boolean isAvailable() {
145: return available;
146: }
147:
148: /**
149: * Return the unique ID of this division.
150: * @return the unique ID of this division.
151: */
152: public int getId() {
153: return id;
154: }
155:
156: @Override
157: public boolean equals(Object o) {
158: if (this == o)
159: return true;
160: if (o == null || getClass() != o.getClass())
161: return false;
162:
163: DivisionData that = (DivisionData) o;
164:
165: if (id != that.id)
166: return false;
167: if (!dataSource.equals(that.dataSource))
168: return false;
169: if (!domainRegEx.equals(that.domainRegEx))
170: return false;
171:
172: return true;
173: }
174:
175: @Override
176: public int hashCode() {
177: int result;
178: result = id;
179: result = 31 * result + dataSource.hashCode();
180: result = 31 * result + domainRegEx.hashCode();
181: return result;
182: }
183:
184: /**
185: * Checks if the given division ID is valid (i.e. if the division
186: * parameter has been set). This method does not check if the given division
187: * is available, i.e. if corresponding entries exist in the global configuration.
188: *
189: * @param divisionId the division ID to be checked
190: * @return true for valid division IDs
191: */
192: public static boolean isValidDivisionId(int divisionId) {
193: return divisionId > 0 || divisionId == DIVISION_TEST
194: || divisionId == DIVISION_GLOBAL;
195: }
196: }
|