001: package org.makumba.providers;
002:
003: import java.util.Enumeration;
004: import java.util.Properties;
005:
006: import org.makumba.Transaction;
007: import org.makumba.commons.ClassResource;
008: import org.makumba.commons.NamedResourceFactory;
009: import org.makumba.commons.NamedResources;
010: import org.makumba.commons.RuntimeWrappedException;
011:
012: /**
013: * This class is a facade for creating different kinds of TransactionProviders. Its constructor knows from a
014: * Configuration (or in the future maybe through other means) which implementation to use, and provides this
015: * implementation methods to its client, without revealing the implementation used.
016: *
017: * TODO this is not the best way of doing things, needs refactoring (TransactionProvider as superclass for the other guys)
018:
019: * @author Manuel Gay
020: * @version $Id: TransactionProvider.java,v 1.1 28.09.2007 15:49:55 Manuel Exp $
021: */
022: public class TransactionProvider implements
023: TransactionProviderInterface {
024:
025: private TransactionProviderInterface transactionProviderImplementation;
026:
027: public TransactionProvider() {
028: this (new Configuration());
029: }
030:
031: public TransactionProvider(TransactionProviderInterface tpi) {
032: this .transactionProviderImplementation = tpi;
033: }
034:
035: public TransactionProvider(Configuration config) {
036: try {
037: this .transactionProviderImplementation = (TransactionProviderInterface) Class
038: .forName(config.getTransactionProviderClass())
039: .newInstance();
040: } catch (InstantiationException e) {
041: // TODO Auto-generated catch block
042: e.printStackTrace();
043: } catch (IllegalAccessException e) {
044: // TODO Auto-generated catch block
045: e.printStackTrace();
046: } catch (ClassNotFoundException e) {
047: // TODO Auto-generated catch block
048: e.printStackTrace();
049: }
050: }
051:
052: public Transaction getConnectionTo(String name) {
053: return transactionProviderImplementation.getConnectionTo(name);
054: }
055:
056: public String getDefaultDataSourceName() {
057: return transactionProviderImplementation
058: .getDefaultDataSourceName();
059: }
060:
061: public String getDatabaseProperty(String name, String propName) {
062: return transactionProviderImplementation.getDatabaseProperty(
063: name, propName);
064: }
065:
066: public void _copy(String sourceDB, String destinationDB,
067: String[] typeNames, boolean ignoreDbsv) {
068: transactionProviderImplementation._copy(sourceDB,
069: destinationDB, typeNames, ignoreDbsv);
070: }
071:
072: public void _delete(String whereDB, String provenienceDB,
073: String[] typeNames, boolean ignoreDbsv) {
074: transactionProviderImplementation._delete(whereDB,
075: provenienceDB, typeNames, ignoreDbsv);
076: }
077:
078: public String getDataSourceName(String lookupFile) {
079: return transactionProviderImplementation
080: .getDataSourceName(lookupFile);
081: }
082:
083: public boolean supportsUTF8() {
084: return transactionProviderImplementation.supportsUTF8();
085: }
086:
087: public CRUDOperationProvider getCRUD() {
088: return transactionProviderImplementation.getCRUD();
089: }
090:
091: public static String findInHostProperties(Properties p, String str) {
092: for (Enumeration e = p.keys(); e.hasMoreElements();) {
093: String s = (String) e.nextElement();
094: int i = s.indexOf('#');
095: try {
096: if ((i == -1 || java.net.InetAddress.getByName(
097: s.substring(0, i)).equals(
098: java.net.InetAddress.getLocalHost()))
099: && str.endsWith(s.substring(i + 1)))
100: return p.getProperty(s);
101: } catch (java.net.UnknownHostException uhe) {
102: }
103: }
104: return null;
105: }
106:
107: /**
108: * finds the database name of the server according to the host name and current directory. If none is specified, a
109: * default is used, if available
110: */
111: public static String findDatabaseName(Properties p) {
112: String userDir = System.getProperty("user.dir");
113: String n;
114: java.net.URL u = ClassResource.get("/");
115: String wbp = u != null ? u.toString() : null;
116:
117: if ((n = TransactionProvider.findInHostProperties(p, userDir)) != null
118: || wbp != null
119: && ((n = TransactionProvider.findInHostProperties(p,
120: wbp)) != null)
121: || (n = TransactionProvider.findInHostProperties(p,
122: "default")) != null)
123:
124: return n;
125:
126: return p.getProperty("default");
127: }
128:
129: public static String findDatabaseName(String s) {
130: try {
131: return TransactionProvider
132: .findDatabaseName((Properties) NamedResources
133: .getStaticCache(TransactionProvider.dbsel)
134: .getResource(s));
135: } catch (RuntimeWrappedException e) {
136: if (e.getCause() instanceof org.makumba.MakumbaError)
137: throw (org.makumba.MakumbaError) e.getCause();
138: throw e;
139: }
140: }
141:
142: public static int dbsel = NamedResources.makeStaticCache(
143: "Database selection files", new NamedResourceFactory() {
144:
145: private static final long serialVersionUID = 1L;
146:
147: protected Object makeResource(Object nm) {
148: Properties p = new Properties();
149: try {
150: java.io.InputStream input = org.makumba.commons.ClassResource
151: .get((String) nm).openStream();
152: p.load(input);
153: input.close();
154: } catch (Exception e) {
155: throw new org.makumba.ConfigFileError(
156: (String) nm);
157: }
158: return p;
159: }
160: });
161:
162: }
|