DataSourceConnectionSource.java (apache-log4j-2.12.3-src) | : | DataSourceConnectionSource.java (apache-log4j-2.12.4-src) | ||
---|---|---|---|---|
skipping to change at line 21 | skipping to change at line 21 | |||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the license for the specific language governing permissions and | * See the license for the specific language governing permissions and | |||
* limitations under the license. | * limitations under the license. | |||
*/ | */ | |||
package org.apache.logging.log4j.core.appender.db.jdbc; | package org.apache.logging.log4j.core.appender.db.jdbc; | |||
import java.sql.Connection; | import java.sql.Connection; | |||
import java.sql.SQLException; | import java.sql.SQLException; | |||
import java.util.Objects; | ||||
import javax.naming.InitialContext; | ||||
import javax.naming.NamingException; | import javax.naming.NamingException; | |||
import javax.sql.DataSource; | import javax.sql.DataSource; | |||
import org.apache.logging.log4j.Logger; | import org.apache.logging.log4j.Logger; | |||
import org.apache.logging.log4j.core.Core; | import org.apache.logging.log4j.core.Core; | |||
import org.apache.logging.log4j.core.config.plugins.Plugin; | import org.apache.logging.log4j.core.config.plugins.Plugin; | |||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute; | import org.apache.logging.log4j.core.config.plugins.PluginAttribute; | |||
import org.apache.logging.log4j.core.config.plugins.PluginFactory; | import org.apache.logging.log4j.core.config.plugins.PluginFactory; | |||
import org.apache.logging.log4j.core.net.JndiManager; | ||||
import org.apache.logging.log4j.status.StatusLogger; | import org.apache.logging.log4j.status.StatusLogger; | |||
import org.apache.logging.log4j.util.Strings; | import org.apache.logging.log4j.util.Strings; | |||
/** | /** | |||
* A {@link JdbcAppender} connection source that uses a {@link DataSource} to co nnect to the database. | * A {@link JdbcAppender} connection source that uses a {@link DataSource} to co nnect to the database. | |||
*/ | */ | |||
@Plugin(name = "DataSource", category = Core.CATEGORY_NAME, elementType = "conne ctionSource", printObject = true) | @Plugin(name = "DataSource", category = Core.CATEGORY_NAME, elementType = "conne ctionSource", printObject = true) | |||
public final class DataSourceConnectionSource extends AbstractConnectionSource { | public final class DataSourceConnectionSource extends AbstractConnectionSource { | |||
private static final Logger LOGGER = StatusLogger.getLogger(); | private static final Logger LOGGER = StatusLogger.getLogger(); | |||
private final DataSource dataSource; | private final DataSource dataSource; | |||
private final String description; | private final String description; | |||
private DataSourceConnectionSource(final String dataSourceName, final DataSo urce dataSource) { | private DataSourceConnectionSource(final String dataSourceName, final DataSo urce dataSource) { | |||
this.dataSource = dataSource; | this.dataSource = Objects.requireNonNull(dataSource, "dataSource"); | |||
this.description = "dataSource{ name=" + dataSourceName + ", value=" + d ataSource + " }"; | this.description = "dataSource{ name=" + dataSourceName + ", value=" + d ataSource + " }"; | |||
} | } | |||
@Override | @Override | |||
public Connection getConnection() throws SQLException { | public Connection getConnection() throws SQLException { | |||
return this.dataSource.getConnection(); | return this.dataSource.getConnection(); | |||
} | } | |||
@Override | @Override | |||
public String toString() { | public String toString() { | |||
return this.description; | return this.description; | |||
} | } | |||
/** | /** | |||
* Factory method for creating a connection source within the plugin manager . | * Factory method for creating a connection source within the plugin manager . | |||
* | * | |||
* @param jndiName The full JNDI path where the data source is bound. Should | * @param jndiName The full JNDI path where the data source is bound. Must s | |||
start with java:/comp/env or | tart with java:/comp/env or environment-equivalent. | |||
* environment-equivalent. | ||||
* @return the created connection source. | * @return the created connection source. | |||
*/ | */ | |||
@PluginFactory | @PluginFactory | |||
public static DataSourceConnectionSource createConnectionSource(@PluginAttri bute("jndiName") final String jndiName) { | public static DataSourceConnectionSource createConnectionSource(@PluginAttri bute("jndiName") final String jndiName) { | |||
if (!JndiManager.isJndiJdbcEnabled()) { | ||||
LOGGER.error("JNDI must be enabled by setting log4j2.enableJndiJdbc= | ||||
true"); | ||||
return null; | ||||
} | ||||
if (Strings.isEmpty(jndiName)) { | if (Strings.isEmpty(jndiName)) { | |||
LOGGER.error("No JNDI name provided."); | LOGGER.error("No JNDI name provided."); | |||
return null; | return null; | |||
} | } | |||
try { | try { | |||
final InitialContext context = new InitialContext(); | @SuppressWarnings("resource") | |||
final DataSource dataSource = (DataSource) context.lookup(jndiName); | final DataSource dataSource = JndiManager.getDefaultManager(DataSour | |||
ceConnectionSource.class.getCanonicalName()).lookup(jndiName); | ||||
if (dataSource == null) { | if (dataSource == null) { | |||
LOGGER.error("No data source found with JNDI name [" + jndiName + "]."); | LOGGER.error("No DataSource found with JNDI name [" + jndiName + "]."); | |||
return null; | return null; | |||
} | } | |||
return new DataSourceConnectionSource(jndiName, dataSource); | return new DataSourceConnectionSource(jndiName, dataSource); | |||
} catch (final NamingException e) { | } catch (final NamingException e) { | |||
LOGGER.error(e.getMessage(), e); | LOGGER.error(e.getMessage(), e); | |||
return null; | return null; | |||
} | } | |||
} | } | |||
} | } | |||
End of changes. 10 change blocks. | ||||
10 lines changed or deleted | 14 lines changed or added |