Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2006-2013 Bitronix Software (http://www.bitronix.be)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bitronix.tm.resource;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.internal.LogDebugCheck;
import bitronix.tm.resource.common.XAResourceProducer;
import bitronix.tm.utils.ClassLoaderUtils;
import bitronix.tm.utils.InitializationException;
import bitronix.tm.utils.PropertyUtils;
import bitronix.tm.utils.Service;
import jakarta.jms.XAConnectionFactory;
import javax.sql.XADataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.logging.Level;
/**
* XA resources pools configurator & loader.
*
{@link ResourceLoader} relies on the optional bitronix.tm.resource.configuration propery to load the
* JDBC datasources ({@link bitronix.tm.resource.jdbc.PoolingDataSource}) and JMS connection factories
* ({@link bitronix.tm.resource.jms.PoolingConnectionFactory}) configuration file and create the resources.
*
When bitronix.tm.resource.configuration is not specified, ResourceLoader is disabled and resources
* should be manually created.
*
* @author Ludovic Orban
*/
public class ResourceLoader
implements Service
{
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(ResourceLoader.class.toString());
private static final String JDBC_RESOURCE_CLASSNAME = "bitronix.tm.resource.jdbc.PoolingDataSource";
private static final String JMS_RESOURCE_CLASSNAME = "bitronix.tm.resource.jms.PoolingConnectionFactory";
private final Map resourcesByUniqueName = new HashMap<>();
/**
* Constructor ResourceLoader creates a new ResourceLoader instance.
*/
public ResourceLoader()
{
//No config required
}
/**
* Get a Map with the configured uniqueName as key and {@link XAResourceProducer} as value.
*
* @return a Map using the uniqueName as key and {@link XAResourceProducer} as value.
*/
public Map getResources()
{
return resourcesByUniqueName;
}
/**
* Initialize the ResourceLoader and load the resources configuration file specified in
* bitronix.tm.resource.configuration property.
*
* @return the number of resources which failed to initialize.
*/
public int init()
{
String filename = TransactionManagerServices.getConfiguration()
.getResourceConfigurationFilename();
if (filename != null)
{
if (!new File(filename).exists())
{
throw new ResourceConfigurationException(
"cannot find resources configuration file '" + filename + "', missing or invalid value of property 'bitronix.tm.resource.configuration'");
}
log.info("reading resources configuration from " + filename);
return init(filename);
}
else
{
if (LogDebugCheck.isDebugEnabled())
{
log.finer("no resource configuration file specified");
}
return 0;
}
}
/**
* Read the resources properties file and create {@link XAResourceProducer} accordingly.
*
* @param propertiesFilename
* the name of the properties file to load.
*
* @return the number of resources which failed to initialize.
*/
private int init(String propertiesFilename)
{
try (FileInputStream fis = new FileInputStream(propertiesFilename))
{
Properties properties;
properties = new Properties();
properties.load(fis);
return initXAResourceProducers(properties);
}
catch (IOException ex)
{
throw new InitializationException("cannot create resource loader", ex);
}
}
/*
* Internal impl.
*/
/**
* Initialize {@link XAResourceProducer}s given a set of properties.
*
* @param properties
* the properties to use for initialization.
*
* @return the number of resources which failed to initialize.
*/
int initXAResourceProducers(Properties properties)
{
Map> entries = buildConfigurationEntriesMap(properties);
int errorCount = 0;
for (Map.Entry> entry : entries.entrySet())
{
String uniqueName = entry.getKey();
List propertyPairs = entry.getValue();
XAResourceProducer producer = buildXAResourceProducer(uniqueName, propertyPairs);
if (ResourceRegistrar.get(producer.getUniqueName()) != null)
{
if (LogDebugCheck.isDebugEnabled())
{
log.finer("resource already registered, skipping it:" + producer.getUniqueName());
}
continue;
}
if (LogDebugCheck.isDebugEnabled())
{
log.finer("creating resource " + producer);
}
try
{
producer.init();
}
catch (ResourceConfigurationException ex)
{
log.log(Level.WARNING, "unable to create resource with unique name " + producer.getUniqueName(), ex);
producer.close();
errorCount++;
}
resourcesByUniqueName.put(producer.getUniqueName(), producer);
}
return errorCount;
}
/**
* Create a map using the configured resource name as the key and a List of PropertyPair objects as the value.
*
* @param properties
* object to analyze.
*
* @return the built map.
*/
private Map> buildConfigurationEntriesMap(Properties properties)
{
Map> entries = new HashMap<>();
for (Map.Entry