1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package com.sun.star.comp.sdbc;
22 
23 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.CompHelper;
24 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.ResourceBasedEventLogger;
25 import org.apache.openoffice.comp.sdbc.dbtools.util.Resources;
26 import org.apache.openoffice.comp.sdbc.dbtools.util.SharedResources;
27 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState;
28 
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.lang.XServiceInfo;
31 import com.sun.star.lang.XSingleComponentFactory;
32 import com.sun.star.lib.uno.helper.ComponentBase;
33 import com.sun.star.lib.uno.helper.Factory;
34 import com.sun.star.logging.LogLevel;
35 import com.sun.star.sdbc.DriverPropertyInfo;
36 import com.sun.star.sdbc.SQLException;
37 import com.sun.star.sdbc.XConnection;
38 import com.sun.star.sdbc.XDriver;
39 import com.sun.star.uno.Any;
40 import com.sun.star.uno.XComponentContext;
41 
42 public class JDBCDriver extends ComponentBase implements XServiceInfo, XDriver {
43     private static String[] services = new String[] {
44             "com.sun.star.sdbc.Driver"
45     };
46     private XComponentContext context;
47     private final ResourceBasedEventLogger logger;
48 
__getComponentFactory(String implName)49     public static XSingleComponentFactory __getComponentFactory(String implName) {
50         XSingleComponentFactory xSingleComponentFactory = null;
51         if (implName.equals(getImplementationNameStatic())) {
52             xSingleComponentFactory = Factory.createComponentFactory(JDBCDriver.class,
53                     getImplementationNameStatic(), services);
54         }
55         return xSingleComponentFactory;
56     }
57 
JDBCDriver(XComponentContext componentContext)58     public JDBCDriver(XComponentContext componentContext) {
59         this.context = componentContext;
60         SharedResources.registerClient(componentContext);
61         logger = new ResourceBasedEventLogger(context, "sdbcl", "org.openoffice.sdbc.jdbcBridge");
62     }
63 
getContext()64     public XComponentContext getContext() {
65         return context;
66     }
67 
getLogger()68     public ResourceBasedEventLogger getLogger() {
69         return logger;
70     }
71 
getImplementationNameStatic()72     private static String getImplementationNameStatic() {
73         // this name is referenced in the configuration and in the jdbc.xml
74         // Please take care when changing it.
75         return JDBCDriver.class.getName();
76     }
77 
78     // XComponent:
79 
80     @Override
postDisposing()81     protected synchronized void postDisposing() {
82         context = null;
83         SharedResources.revokeClient();
84     }
85 
86     // XServiceInfo:
87 
88     @Override
getImplementationName()89     public String getImplementationName() {
90         return getImplementationNameStatic();
91     }
92 
93     @Override
getSupportedServiceNames()94     public String[] getSupportedServiceNames() {
95         return services.clone();
96     }
97 
98     @Override
supportsService(String serviceName)99     public boolean supportsService(String serviceName) {
100         for (String service : getSupportedServiceNames()) {
101             if (service.equals(serviceName)) {
102                 return true;
103             }
104         }
105         return false;
106     }
107 
108     // XDriver:
109 
110     @Override
acceptsURL(String url)111     public boolean acceptsURL(String url) throws SQLException {
112         // don't ask the real driver for the url
113         // I feel responsible for all jdbc url's
114         return url.startsWith("jdbc:");
115     }
116 
117     @Override
connect(String url, PropertyValue[] info)118     public synchronized XConnection connect(String url, PropertyValue[] info) throws SQLException {
119         checkDisposed();
120         logger.log(LogLevel.INFO, Resources.STR_LOG_DRIVER_CONNECTING_URL, url);
121         XConnection out = null;
122         if (acceptsURL(url)) {
123             JavaSQLConnection connection = new JavaSQLConnection(this);
124             out = connection;
125             if (!connection.construct(url, info)) {
126                 // an error occurred and the java driver didn't throw an exception
127                 CompHelper.disposeComponent(out);
128                 out = null;
129             } else {
130                 logger.log(LogLevel.INFO, Resources.STR_LOG_DRIVER_SUCCESS);
131             }
132         }
133         return out;
134     }
135 
136     @Override
getMajorVersion()137     public int getMajorVersion() {
138         return 1;
139     }
140 
141     @Override
getMinorVersion()142     public int getMinorVersion() {
143         return 0;
144     }
145 
146     @Override
getPropertyInfo(String url, PropertyValue[] info)147     public DriverPropertyInfo[] getPropertyInfo(String url, PropertyValue[] info) throws SQLException {
148         if (!acceptsURL(url)) {
149             String message = SharedResources.getInstance().getResourceString(Resources.STR_URI_SYNTAX_ERROR);
150             throw new SQLException(message, this, StandardSQLState.SQL_GENERAL_ERROR.text(), 0, Any.VOID);
151         }
152         String[] booleanValues = { "false", "true" };
153         return new DriverPropertyInfo [] {
154                 new DriverPropertyInfo(
155                         "JavaDriverClass", "The JDBC driver class name.",
156                         true, "", new String[0]),
157                 new DriverPropertyInfo(
158                         "JavaDriverClassPath", "The class path where to look for the JDBC driver.",
159                         true, "", new String[0]),
160                 new DriverPropertyInfo(
161                         "SystemProperties", "Additional properties to set at java.lang.System before loading the driver.",
162                         true, "", new String[0]),
163                 new DriverPropertyInfo(
164                         "ParameterNameSubstitution", "Change named parameters with '?'.",
165                         false, "false", booleanValues),
166                 new DriverPropertyInfo(
167                         "IgnoreDriverPrivileges", "Ignore the privileges from the database driver.",
168                         false, "false", booleanValues),
169                 new DriverPropertyInfo(
170                         "IsAutoRetrievingEnabled", "Retrieve generated values.",
171                         false, "false", booleanValues),
172                 new DriverPropertyInfo(
173                         "AutoRetrievingStatement", "Auto-increment statement.",
174                         false, "", new String[0]),
175                 new DriverPropertyInfo(
176                         "GenerateASBeforeCorrelationName", "Generate AS before table correlation names.",
177                         false, "true", booleanValues),
178                 new DriverPropertyInfo(
179                         "IgnoreCurrency", "Ignore the currency field from the ResultsetMetaData.",
180                         false, "false", booleanValues),
181                 new DriverPropertyInfo(
182                         "EscapeDateTime", "Escape date time format.",
183                         false, "true", booleanValues),
184                 new DriverPropertyInfo(
185                         "TypeInfoSettings", "Defines how the type info of the database metadata should be manipulated.",
186                         false, "", new String[0]),
187                 new DriverPropertyInfo(
188                         "ImplicitCatalogRestriction", "The catalog which should be used in getTables calls, when the caller passed NULL.",
189                         false, "", new String[0]),
190                 new DriverPropertyInfo(
191                         "ImplicitSchemaRestriction", "The schema which should be used in getTables calls, when the caller passed NULL.",
192                         false, "", new String[0])
193         };
194 
195     }
196 }