Source Code Cross Referenced for AbstractDataSourceFactory.java in  » Database-ORM » Torque » org » apache » torque » dsfactory » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » Torque » org.apache.torque.dsfactory 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.torque.dsfactory;
002:
003:        /*
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *   http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:
022:        import java.util.Iterator;
023:
024:        import javax.sql.ConnectionPoolDataSource;
025:        import javax.sql.DataSource;
026:
027:        import org.apache.commons.beanutils.ConvertUtils;
028:        import org.apache.commons.beanutils.MappedPropertyDescriptor;
029:        import org.apache.commons.beanutils.PropertyUtils;
030:        import org.apache.commons.configuration.Configuration;
031:        import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
032:        import org.apache.commons.lang.StringUtils;
033:        import org.apache.commons.logging.Log;
034:        import org.apache.commons.logging.LogFactory;
035:        import org.apache.torque.Torque;
036:        import org.apache.torque.TorqueException;
037:        import org.apache.torque.TorqueRuntimeException;
038:
039:        /**
040:         * A class that contains common functionality of the factories in this
041:         * package.
042:         *
043:         * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
044:         * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
045:         * @version $Id: AbstractDataSourceFactory.java 473821 2006-11-11 22:37:25Z tv $
046:         */
047:        public abstract class AbstractDataSourceFactory implements 
048:                DataSourceFactory {
049:            /** "pool" Key for the configuration */
050:            public static final String POOL_KEY = "pool";
051:
052:            /** "connection" Key for the configuration */
053:            public static final String CONNECTION_KEY = "connection";
054:
055:            /** "schema" Key for the configuration */
056:            public static final String SCHEMA_KEY = "schema";
057:
058:            /** "defaults" Key for the configuration */
059:            public static final String DEFAULTS_KEY = "defaults";
060:
061:            /** "defaults.pool" Key for the configuration */
062:            public static final String DEFAULT_POOL_KEY = DEFAULTS_KEY + "."
063:                    + POOL_KEY;
064:
065:            /** "defaults.connection" Key for the configuration */
066:            public static final String DEFAULT_CONNECTION_KEY = DEFAULTS_KEY
067:                    + "." + CONNECTION_KEY;
068:
069:            /** default schema name for the configuration */
070:            public static final String DEFAULT_SCHEMA_KEY = DEFAULTS_KEY + "."
071:                    + SCHEMA_KEY;
072:
073:            /** The log */
074:            private static Log log = LogFactory
075:                    .getLog(AbstractDataSourceFactory.class);
076:
077:            /** Internal Marker for the Schema name of this database connection */
078:            private String schema = null;
079:
080:            /**
081:             * Encapsulates setting configuration properties on
082:             * <code>DataSource</code> objects.
083:             *
084:             * @param property the property to read from the configuration
085:             * @param c the configuration to read the property from
086:             * @param ds the <code>DataSource</code> instance to write the property to
087:             * @throws Exception if anything goes wrong
088:             */
089:            protected void setProperty(String property, Configuration c,
090:                    Object ds) throws Exception {
091:                if (c == null || c.isEmpty()) {
092:                    return;
093:                }
094:
095:                String key = property;
096:                Class dsClass = ds.getClass();
097:                int dot = property.indexOf('.');
098:                try {
099:                    if (dot > 0) {
100:                        property = property.substring(0, dot);
101:
102:                        MappedPropertyDescriptor mappedPD = new MappedPropertyDescriptor(
103:                                property, dsClass);
104:                        Class propertyType = mappedPD.getMappedPropertyType();
105:                        Configuration subProps = c.subset(property);
106:                        // use reflection to set properties
107:                        Iterator j = subProps.getKeys();
108:                        while (j.hasNext()) {
109:                            String subProp = (String) j.next();
110:                            String propVal = subProps.getString(subProp);
111:                            Object value = ConvertUtils.convert(propVal,
112:                                    propertyType);
113:                            PropertyUtils.setMappedProperty(ds, property,
114:                                    subProp, value);
115:
116:                            if (log.isDebugEnabled()) {
117:                                log.debug("setMappedProperty(" + ds + ", "
118:                                        + property + ", " + subProp + ", "
119:                                        + value + ")");
120:                            }
121:                        }
122:                    } else {
123:                        if ("password".equals(key)) {
124:                            // do not log value of password
125:                            // for this, ConvertUtils.convert cannot be used
126:                            // as it also logs the value of the converted property
127:                            // so it is assumed here that the password is a String
128:                            String value = c.getString(property);
129:                            PropertyUtils
130:                                    .setSimpleProperty(ds, property, value);
131:                            if (log.isDebugEnabled()) {
132:                                log.debug("setSimpleProperty(" + ds + ", "
133:                                        + property + ", "
134:                                        + " (value not logged)" + ")");
135:                            }
136:                        } else {
137:                            Class propertyType = PropertyUtils.getPropertyType(
138:                                    ds, property);
139:                            Object value = ConvertUtils.convert(c
140:                                    .getString(property), propertyType);
141:                            PropertyUtils
142:                                    .setSimpleProperty(ds, property, value);
143:
144:                            if (log.isDebugEnabled()) {
145:                                log.debug("setSimpleProperty(" + ds + ", "
146:                                        + property + ", " + value + ")");
147:                            }
148:                        }
149:                    }
150:                } catch (RuntimeException e) {
151:                    throw new TorqueRuntimeException(
152:                            "Runtime error setting property " + property, e);
153:                } catch (Exception e) {
154:                    log.error("Property: " + property + " value: "
155:                            + c.getString(key)
156:                            + " is not supported by DataSource: "
157:                            + ds.getClass().getName());
158:                }
159:            }
160:
161:            /**
162:             * Iterate over a Configuration subset and apply all
163:             * properties to a passed object which must contain Bean
164:             * setter and getter
165:             *
166:             * @param c The configuration subset
167:             * @param o The object to apply the properties to
168:             * @throws TorqueException if a property set fails
169:             */
170:            protected void applyConfiguration(Configuration c, Object o)
171:                    throws TorqueException {
172:                log.debug("applyConfiguration(" + c + ", " + o + ")");
173:
174:                if (c != null) {
175:                    try {
176:                        for (Iterator i = c.getKeys(); i.hasNext();) {
177:                            String key = (String) i.next();
178:                            setProperty(key, c, o);
179:                        }
180:                    } catch (Exception e) {
181:                        log.error(e);
182:                        throw new TorqueException(e);
183:                    }
184:                }
185:            }
186:
187:            /**
188:             * Initializes the ConnectionPoolDataSource.
189:             *
190:             * @param configuration where to read the settings from
191:             * @throws TorqueException if a property set fails
192:             * @return a configured <code>ConnectionPoolDataSource</code>
193:             */
194:            protected ConnectionPoolDataSource initCPDS(
195:                    Configuration configuration) throws TorqueException {
196:                log.debug("Starting initCPDS");
197:                ConnectionPoolDataSource cpds = new DriverAdapterCPDS();
198:                Configuration c = Torque.getConfiguration();
199:
200:                if (c == null || c.isEmpty()) {
201:                    log
202:                            .warn("Global Configuration not set,"
203:                                    + " no Default connection pool data source configured!");
204:                } else {
205:                    Configuration conf = c.subset(DEFAULT_CONNECTION_KEY);
206:                    applyConfiguration(conf, cpds);
207:                }
208:
209:                Configuration conf = configuration.subset(CONNECTION_KEY);
210:                applyConfiguration(conf, cpds);
211:
212:                return cpds;
213:            }
214:
215:            /**
216:             * Sets the current schema for the database connection
217:             *
218:             * @param schema The current schema name
219:             */
220:            public void setSchema(String schema) {
221:                this .schema = schema;
222:            }
223:
224:            /**
225:             * This method returns the current schema for the database connection
226:             *
227:             * @return The current schema name. Null means, no schema has been set.
228:             * @throws TorqueException Any exceptions caught during processing will be
229:             *         rethrown wrapped into a TorqueException.
230:             * @deprecated use DatabaseInfo.setSchema() instead. Will be removed
231:             *             in a future version of Torque.
232:             */
233:            public String getSchema() {
234:                return schema;
235:            }
236:
237:            /**
238:             * @return the <code>DataSource</code> configured by the factory.
239:             * @throws TorqueException if the source can't be returned
240:             */
241:            public abstract DataSource getDataSource() throws TorqueException;
242:
243:            /**
244:             * Initialize the factory.
245:             *
246:             * @param configuration where to load the factory settings from
247:             * @throws TorqueException Any exceptions caught during processing will be
248:             *         rethrown wrapped into a TorqueException.
249:             */
250:            public void initialize(Configuration configuration)
251:                    throws TorqueException {
252:                if (configuration == null) {
253:                    throw new TorqueException(
254:                            "Torque cannot be initialized without "
255:                                    + "a valid configuration. Please check the log files "
256:                                    + "for further details.");
257:                }
258:
259:                schema = configuration.getString(SCHEMA_KEY, null);
260:
261:                if (StringUtils.isEmpty(schema)) {
262:                    Configuration conf = Torque.getConfiguration();
263:                    schema = conf.getString(DEFAULT_SCHEMA_KEY, null);
264:                }
265:            }
266:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.