Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
* ]]
*/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.xmlbeans.XmlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -47,8 +45,8 @@
* @author dberry
*/
public final class CpoAdapterFactoryManager extends CpoAdapterFactoryCache {

private static final Logger logger = LoggerFactory.getLogger(CpoAdapterFactoryManager.class);
private static final ReentrantLock lock = new ReentrantLock();
public static final String CPO_CONFIG = "CPO_CONFIG";
private static final String CPO_CONFIG_XML = "/cpoConfig.xml";
private static String defaultContext = null;
Expand Down Expand Up @@ -113,105 +111,80 @@ public static void loadAdapters() {
*
* @param cpoConfig
*/
private static synchronized void loadAdapters(String cpoConfig) {
InputStream is = null;
var errBuilder = new StringBuilder();

// See if the file is a uri
try {
URL cpoConfigUrl = new URL(cpoConfig);
is = cpoConfigUrl.openStream();
} catch (IOException e) {
errBuilder.append("Uri Not Found: ").append(cpoConfig).append("\n");
}

// See if the file is a resource in the jar
if (is == null) is = CpoClassLoader.getResourceAsStream(cpoConfig);

if (is == null) {
errBuilder.append("Resource Not Found: ").append(cpoConfig).append("\n");
try {
// See if the file is a local file on the server
is = new FileInputStream(cpoConfig);
} catch (FileNotFoundException fnfe) {
errBuilder.append("File Not Found: ").append(cpoConfig).append("\n");
is = null;
}
}

private static void loadAdapters(String cpoConfig) {
lock.lock();
try {
CpoConfigDocument configDoc;
if (is == null) {
// See if the config is sent in as a string
try {
configDoc = CpoConfigDocument.Factory.parse(cpoConfig);
} catch (XmlException e) {
throw new CpoException(errBuilder.toString(), e);
}
} else {
configDoc = CpoConfigDocument.Factory.parse(is);
}

String errMsg = XmlBeansHelper.validateXml(configDoc);
if (errMsg != null) {
logger.error("Invalid CPO Config file: " + cpoConfig + ":" + errMsg);
} else {
logger.info("Processing Config File: " + cpoConfig);
// Moving the clear to here to make sure we get a good file before we just blow away all the
// adapters.
// We are doing a load clear all the caches first, in case the load gets called more than
// once.
CpoMetaDescriptor.clearAllInstances();
clearCpoAdapterFactoryCache();

CtCpoConfig ctCpoConfig = configDoc.getCpoConfig();

// Set the default context.
if (ctCpoConfig.isSetDefaultConfig()) {
defaultContext = ctCpoConfig.getDefaultConfig();
var errBuilder = new StringBuilder();
try (InputStream is = XmlBeansHelper.loadXmlStream(cpoConfig, errBuilder)) {
CpoConfigDocument configDoc;
if (is == null) {
// See if the config is sent in as a string
try {
configDoc = CpoConfigDocument.Factory.parse(cpoConfig);
} catch (XmlException e) {
throw new CpoException(errBuilder.toString(), e);
}
} else {
// make the first listed config the default.
defaultContext = ctCpoConfig.getDataConfigArray(0).getName();
configDoc = CpoConfigDocument.Factory.parse(is);
}

for (CtMetaDescriptor metaDescriptor : ctCpoConfig.getMetaConfigArray()) {
boolean caseSensitive = true;
if (metaDescriptor.isSetCaseSensitive()) {
caseSensitive = metaDescriptor.getCaseSensitive();
String errMsg = XmlBeansHelper.validateXml(configDoc);
if (errMsg != null) {
logger.error("Invalid CPO Config file: " + cpoConfig + ":" + errMsg);
} else {
logger.info("Processing Config File: " + cpoConfig);
// Moving the clear to here to make sure we get a good file before we just blow away all
// the
// adapters.
// We are doing a load clear all the caches first, in case the load gets called more than
// once.
CpoMetaDescriptor.clearAllInstances();
clearCpoAdapterFactoryCache();

CtCpoConfig ctCpoConfig = configDoc.getCpoConfig();

// Set the default context.
if (ctCpoConfig.isSetDefaultConfig()) {
defaultContext = ctCpoConfig.getDefaultConfig();
} else {
// make the first listed config the default.
defaultContext = ctCpoConfig.getDataConfigArray(0).getName();
}

// this will create and cache, so we don't need the return
CpoMetaDescriptor.getInstance(
metaDescriptor.getName(), metaDescriptor.getMetaXmlArray(), caseSensitive);
}
for (CtMetaDescriptor metaDescriptor : ctCpoConfig.getMetaConfigArray()) {
boolean caseSensitive = true;
if (metaDescriptor.isSetCaseSensitive()) {
caseSensitive = metaDescriptor.getCaseSensitive();
}

// now lets loop through all the adapters and get them cached.
for (CtDataSourceConfig dataSourceConfig : ctCpoConfig.getDataConfigArray()) {
CpoAdapterFactory cpoAdapterFactory = makeCpoAdapterFactory(dataSourceConfig);
if (cpoAdapterFactory != null) {
addCpoAdapterFactory(dataSourceConfig.getName(), cpoAdapterFactory);
// this will create and cache, so we don't need the return
CpoMetaDescriptor.getInstance(
metaDescriptor.getName(), metaDescriptor.getMetaXmlArray(), caseSensitive);
}

// now lets loop through all the adapters and get them cached.
for (CtDataSourceConfig dataSourceConfig : ctCpoConfig.getDataConfigArray()) {
CpoAdapterFactory cpoAdapterFactory = makeCpoAdapterFactory(dataSourceConfig);
if (cpoAdapterFactory != null) {
addCpoAdapterFactory(dataSourceConfig.getName(), cpoAdapterFactory);
}
}
}
} catch (IOException ioe) {
logger.error("Error reading " + cpoConfig + ": ", ioe);
} catch (XmlException xe) {
logger.error("Error processing " + cpoConfig + ": Invalid XML", xe);
} catch (CpoException ce) {
logger.error("Error processing " + cpoConfig + ": ", ce);
}
} catch (IOException ioe) {
logger.error("Error reading " + cpoConfig + ": ", ioe);
} catch (XmlException xe) {
logger.error("Error processing " + cpoConfig + ": Invalid XML", xe);
} catch (CpoException ce) {
logger.error("Error processing " + cpoConfig + ": ", ce);
} finally {
if (is != null)
try {
is.close();
} catch (Exception e) {
logger.error("Error processing " + cpoConfig + ": ", e);
}
lock.unlock();
}
}

public static CpoAdapterFactory makeCpoAdapterFactory(CtDataSourceConfig dataSourceConfig)
throws CpoException {
CpoAdapterFactory cpoAdapterFactory;
CpoAdapterFactory cpoAdapterFactory = null;

// make the CpoAdapter
try {
Expand Down Expand Up @@ -251,7 +224,10 @@ public static CpoAdapterFactory makeCpoAdapterFactory(CtDataSourceConfig dataSou
logger.error(msg);
throw new CpoException(msg);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
String msg =
"Could not invoke the constructor for CpoConfigProcessor: "
+ dataSourceConfig.getCpoConfigProcessor();
logger.error(msg);
}

return cpoAdapterFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
* ]]
*/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
Expand Down Expand Up @@ -57,4 +62,31 @@ public static XmlOptions getXmlOptions() {
xo.setUseDefaultNamespace();
return xo;
}

public static InputStream loadXmlStream(String xmlStr, StringBuilder errorBuilder) {
InputStream is = null;
// See if the file is a uri
try {
URL cpoConfigUrl = new URL(xmlStr);
is = cpoConfigUrl.openStream();
} catch (IOException e) {
errorBuilder.append("Uri Not Found: ").append(xmlStr).append("\n");
}

// See if the file is a resource in the jar
if (is == null) is = CpoClassLoader.getResourceAsStream(xmlStr);

if (is == null) {
errorBuilder.append("Resource Not Found: ").append(xmlStr).append("\n");
try {
// See if the file is a local file on the server
is = new FileInputStream(xmlStr);
} catch (FileNotFoundException fnfe) {
errorBuilder.append("File Not Found: ").append(xmlStr).append("\n");
is = null;
}
}

return is;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -157,7 +156,7 @@ public void refreshDescriptorMeta(List<String> metaXmls, boolean overwrite) thro

protected static CpoMetaDescriptor createUpdateInstance(
String name, List<String> metaXmls, boolean caseSensitive) throws CpoException {
return createUpdateInstance(name, metaXmls.toArray(new String[metaXmls.size()]), caseSensitive);
return createUpdateInstance(name, metaXmls.toArray(new String[0]), caseSensitive);
}

protected static CpoMetaDescriptor createUpdateInstance(
Expand All @@ -167,30 +166,8 @@ protected static CpoMetaDescriptor createUpdateInstance(
var errBuilder = new StringBuilder();

for (String metaXml : metaXmls) {
InputStream is = null;

// See if the file is a uri
try {
URL cpoConfigUrl = new URL(metaXml);
is = cpoConfigUrl.openStream();
} catch (IOException e) {
errBuilder.append("Uri Not Found: ").append(metaXml).append("\n");
}

// See if the file is a resource in the jar
if (is == null) is = CpoClassLoader.getResourceAsStream(metaXml);

if (is == null) {
errBuilder.append("Resource Not Found: ").append(metaXml).append("\n");
try {
// See if the file is a local file on the server
is = new FileInputStream(metaXml);
} catch (FileNotFoundException fnfe) {
errBuilder.append("File Not Found: ").append(metaXml).append("\n");
is = null;
}
}
try {
try (InputStream is = XmlBeansHelper.loadXmlStream(metaXml, errBuilder)) {
CpoMetaDataDocument metaDataDoc;
if (is == null) {
// See if the config is sent in as a string
Expand All @@ -214,9 +191,6 @@ protected static CpoMetaDescriptor createUpdateInstance(
logger.debug("Getting the Class");
Class<?> clazz = CpoClassLoader.forName(metaDescriptorClassName);
logger.debug("Getting the Constructor");
if (clazz == null) {
logger.debug("clazz==null");
}
Constructor<?> cons = clazz.getConstructor(String.class, boolean.class);
logger.debug("Creating the instance");
metaDescriptor = (CpoMetaDescriptor) cons.newInstance(name, caseSensitive);
Expand Down Expand Up @@ -293,16 +267,6 @@ protected static CpoMetaDescriptor createUpdateInstance(
+ metaDescriptorClassName
+ ":"
+ ExceptionHelper.getLocalizedMessage(cce));
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
if (logger.isTraceEnabled()) {
logger.trace(e.getLocalizedMessage());
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.synchronoss.cpo.CpoException;
Expand All @@ -43,6 +44,8 @@ public abstract class CpoClass extends CpoClassBean
private static final long serialVersionUID = 1L;

private static final Logger logger = LoggerFactory.getLogger(CpoClass.class);
private final ReentrantLock lock = new ReentrantLock();

private Class<?> metaClass = null;

/** javaMap contains a Map of CpoAttribute Objects the key is the javaName of the attribute */
Expand Down Expand Up @@ -177,24 +180,29 @@ public void acceptMetaDFVisitor(MetaVisitor visitor) {
}
}

public synchronized void loadRunTimeInfo(CpoMetaDescriptor metaDescriptor) throws CpoException {
if (metaClass == null) {
Class<?> tmpMetaClass = null;
public void loadRunTimeInfo(CpoMetaDescriptor metaDescriptor) throws CpoException {
lock.lock();
try {
if (metaClass == null) {
Class<?> tmpMetaClass = null;

try {
logger.debug("Loading runtimeinfo for " + getName());
tmpMetaClass = CpoClassLoader.forName(getName());
} catch (ClassNotFoundException cnfe) {
throw new CpoException(
"Class not found: " + getName() + ": " + ExceptionHelper.getLocalizedMessage(cnfe));
}
try {
logger.debug("Loading runtimeinfo for " + getName());
tmpMetaClass = CpoClassLoader.forName(getName());
} catch (ClassNotFoundException cnfe) {
throw new CpoException(
"Class not found: " + getName() + ": " + ExceptionHelper.getLocalizedMessage(cnfe));
}

for (CpoAttribute attribute : javaMap.values()) {
attribute.loadRunTimeInfo(metaDescriptor, tmpMetaClass);
}
logger.debug("Loaded runtimeinfo for " + getName());
for (CpoAttribute attribute : javaMap.values()) {
attribute.loadRunTimeInfo(metaDescriptor, tmpMetaClass);
}
logger.debug("Loaded runtimeinfo for " + getName());

metaClass = tmpMetaClass;
metaClass = tmpMetaClass;
}
} finally {
lock.unlock();
}
}

Expand Down