More work on ServiceConfigUtil.java - based on a patch from Christoph Neuroth with modifications by me. The static helper methods now use the models instead of parsing the DOM tree. This commit makes a small API change - the helper methods now throw an exception if there is a problem with the serviceengine.xml file. The original code would just hide the errors, so client code didn't know anything was wrong.

https://issues.apache.org/jira/browse/OFBIZ-5204


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1490004 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian Crum
2013-06-05 18:59:51 +00:00
parent 7cfd3f4a3d
commit 06aea1b98a
8 changed files with 169 additions and 252 deletions
@@ -20,15 +20,19 @@
package org.ofbiz.service;
import org.ofbiz.service.config.ServiceConfigUtil;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilValidate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javolution.util.FastMap;
import org.ofbiz.service.config.model.NotificationGroup;
import org.ofbiz.service.config.model.Notify;
/**
* ModelNotification
@@ -100,31 +104,34 @@ public class ModelNotification {
}
public String buildTo() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
List<String> addr = group.getAddress("to");
if (UtilValidate.isNotEmpty(addr)) {
return StringUtil.join(addr, ",");
}
}
return null;
return getCommaSeparatedAddressList("to");
}
public String buildCc() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
List<String> addr = group.getAddress("cc");
if (addr != null) {
return StringUtil.join(addr, ",");
}
}
return null;
return getCommaSeparatedAddressList("cc");
}
public String buildBcc() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
List<String> addr = group.getAddress("bcc");
return getCommaSeparatedAddressList("bcc");
}
public String buildFrom() {
return getCommaSeparatedAddressList("from");
}
private String getCommaSeparatedAddressList(String notifyType) {
try {
NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
return getCommaSeparatedAddressList(group, notifyType);
} catch (GenericConfigException e) {
Debug.logWarning(e, "Exception thrown while getting service configuration: ", module);
return null;
}
}
private String getCommaSeparatedAddressList(NotificationGroup notificationGroup, String notifyType) {
if (notificationGroup != null) {
List<String> addr = getAddressesByType(notificationGroup, notifyType);
if (UtilValidate.isNotEmpty(addr)) {
return StringUtil.join(addr, ",");
}
@@ -132,38 +139,49 @@ public class ModelNotification {
return null;
}
public String buildFrom() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
List<String> addr = group.getAddress("from");
if (UtilValidate.isNotEmpty(addr)) {
return addr.get(0);
private List<String> getAddressesByType(NotificationGroup group, String type) {
List<String> l = new ArrayList<String>();
for (Notify n : group.getNotifyList()) {
if (n.getType().equals(type)) {
l.add(n.getContent());
}
}
return null;
return l;
}
public String getSubject() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
return group.getSubject();
try {
NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
return group.getNotification().getSubject();
}
} catch (GenericConfigException e) {
Debug.logWarning(e, "Exception thrown while getting service configuration: ", module);
}
return null;
}
public String getScreen() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
return group.getScreen();
try {
NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
return group.getNotification().getScreen();
}
} catch (GenericConfigException e) {
Debug.logWarning(e, "Exception thrown while getting service configuration: ", module);
}
return null;
}
public String getService() {
ServiceConfigUtil.NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
// only service supported at this time
return "sendMailFromScreen";
try {
NotificationGroup group = ServiceConfigUtil.getNotificationGroup(notificationGroupName);
if (group != null) {
// only service supported at this time
return "sendMailFromScreen";
}
} catch (GenericConfigException e) {
Debug.logWarning(e, "Exception thrown while getting service configuration: ", module);
}
return null;
}
@@ -985,7 +985,11 @@ public class ServiceDispatcher {
String delayStr = ss.getAttribute("runtime-delay");
String sendToPool = ss.getAttribute("run-in-pool");
if (UtilValidate.isEmpty(sendToPool)) {
sendToPool = ServiceConfigUtil.getSendPool();
try {
sendToPool = ServiceConfigUtil.getSendPool();
} catch (GenericConfigException e) {
Debug.logError(e, "Unable to get send pool in service [" + serviceName + "]: ", module);
}
}
long runtimeDelay;
@@ -32,6 +32,7 @@ import javax.transaction.Transaction;
import javolution.util.FastList;
import javolution.util.FastMap;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilGenerics;
@@ -372,14 +373,18 @@ public class ServiceUtil {
public static Map<String, Object> purgeOldJobs(DispatchContext dctx, Map<String, ? extends Object> context) {
Debug.logWarning("WARNING: purgeOldJobs service invoked. This service is obsolete - the Job Scheduler will purge old jobs automatically.", module);
String sendPool = ServiceConfigUtil.getSendPool();
int daysToKeep = ServiceConfigUtil.getPurgeJobDays();
String sendPool = null;
Calendar cal = Calendar.getInstance();
try {
sendPool = ServiceConfigUtil.getSendPool();
int daysToKeep = ServiceConfigUtil.getPurgeJobDays();
cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
} catch (GenericConfigException e) {
Debug.logWarning(e, "Exception thrown while getting service configuration: ", module);
return returnError("Exception thrown while getting service configuration: " + e);
}
Delegator delegator = dctx.getDelegator();
Timestamp now = UtilDateTime.nowTimestamp();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(now.getTime());
cal.add(Calendar.DAY_OF_YEAR, daysToKeep * -1);
Timestamp purgeTime = new Timestamp(cal.getTimeInMillis());
// create the conditions to query
@@ -18,15 +18,11 @@
*******************************************************************************/
package org.ofbiz.service.config;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import javolution.util.FastList;
import javolution.util.FastMap;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.config.ResourceLoader;
import org.ofbiz.base.util.Assert;
@@ -34,12 +30,13 @@ import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilURL;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.base.util.cache.UtilCache;
import org.ofbiz.service.config.model.Engine;
import org.ofbiz.service.config.model.NotificationGroup;
import org.ofbiz.service.config.model.RunFromPool;
import org.ofbiz.service.config.model.ServiceConfig;
import org.ofbiz.service.config.model.ServiceEngine;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Misc. utility method for dealing with the serviceengine.xml file
@@ -49,12 +46,18 @@ public final class ServiceConfigUtil {
public static final String module = ServiceConfigUtil.class.getName();
public static final String engine = "default";
public static final String SERVICE_ENGINE_XML_FILENAME = "serviceengine.xml";
// Remove this cache in the new implementation.
private static final UtilCache<String, Map<String, NotificationGroup>> notificationGroupCache = UtilCache.createUtilCache("service.NotificationGroups", 0, 0, false);
// Keep the ServiceConfig instance in a cache - so the configuration can be reloaded at run-time. There will be only one ServiceConfig instance in the cache.
private static final UtilCache<String, ServiceConfig> serviceConfigCache = UtilCache.createUtilCache("service.ServiceConfig", 0, 0, false);
private static final List<ServiceConfigListener> configListeners = new CopyOnWriteArrayList<ServiceConfigListener>();
/**
* Returns the service engine configuration (currently always the default one).
* @throws GenericConfigException
*/
private static ServiceEngine getServiceEngine() throws GenericConfigException {
return getServiceConfig().getServiceEngine(engine);
}
/**
* Returns the <code>ServiceConfig</code> instance.
* @throws GenericConfigException
@@ -80,7 +83,7 @@ public final class ServiceConfigUtil {
private static Document getXmlDocument() throws GenericConfigException {
URL confUrl = UtilURL.fromResource(SERVICE_ENGINE_XML_FILENAME);
if (confUrl == null) {
throw new GenericConfigException("Could not find the " + SERVICE_ENGINE_XML_FILENAME + " file on the classpath");
throw new GenericConfigException("Could not find the " + SERVICE_ENGINE_XML_FILENAME + " file");
}
try {
return UtilXml.readXmlDocument(confUrl, true, true);
@@ -99,16 +102,64 @@ public final class ServiceConfigUtil {
return getServiceConfig().getServiceEngine(name);
}
public static Element getXmlRootElement() throws GenericConfigException {
Element root = ResourceLoader.getXmlRootElement(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME);
return UtilXml.firstChildElement(root, "service-engine"); // only look at the first one for now
}
public static void registerServiceConfigListener(ServiceConfigListener listener) {
Assert.notNull("listener", listener);
configListeners.add(listener);
}
public static String getAuthorizationServiceName() throws GenericConfigException {
return getServiceEngine().getAuthorization().getServiceName();
}
public static boolean getPollEnabled() throws GenericConfigException {
return getServiceEngine().getThreadPool().getPollEnabled();
}
public static String getSendPool() throws GenericConfigException {
return getServiceEngine().getThreadPool().getSendToPool();
}
public static List<String> getRunPools() throws GenericConfigException {
List<RunFromPool> runFromPools = getServiceEngine().getThreadPool().getRunFromPools();
List<String> readPools = new ArrayList<String>(runFromPools.size());
for (RunFromPool runFromPool : runFromPools) {
readPools.add(runFromPool.getName());
}
return readPools;
}
public static int getPurgeJobDays() throws GenericConfigException {
return getServiceEngine().getThreadPool().getPurgeJobDays();
}
public static int getFailedRetryMin() throws GenericConfigException {
return getServiceEngine().getThreadPool().getFailedRetryMin();
}
public static NotificationGroup getNotificationGroup(String group) throws GenericConfigException {
List<NotificationGroup> notificationGroups;
notificationGroups = getServiceEngine().getNotificationGroups();
for (NotificationGroup notificationGroup : notificationGroups) {
if (notificationGroup.getName().equals(group)) {
return notificationGroup;
}
}
return null;
}
public static String getEngineParameter(String engineName, String parameterName) throws GenericConfigException {
Engine engine = getServiceEngine().getEngine(engineName);
if (engine != null) {
return engine.getParameterValue(parameterName);
}
return null;
}
public static Element getXmlRootElement() throws GenericConfigException {
Element root = ResourceLoader.getXmlRootElement(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME);
return UtilXml.firstChildElement(root, "service-engine"); // only look at the first one for now
}
public static Element getElement(String elementName) {
Element rootElement = null;
@@ -126,192 +177,4 @@ public final class ServiceConfigUtil {
if (element == null) return null;
return element.getAttribute(attrName);
}
public static String getSendPool() {
return getElementAttr("thread-pool", "send-to-pool");
}
public static List<String> getRunPools() {
List<String> readPools = null;
Element threadPool = getElement("thread-pool");
List<? extends Element> readPoolElements = UtilXml.childElementList(threadPool, "run-from-pool");
if (readPoolElements != null) {
readPools = FastList.newInstance();
for (Element e: readPoolElements) {
readPools.add(e.getAttribute("name"));
}
}
return readPools;
}
public static int getPurgeJobDays() {
String days = getElementAttr("thread-pool", "purge-job-days");
int purgeDays;
try {
purgeDays = Integer.parseInt(days);
} catch (NumberFormatException e) {
Debug.logError(e, "Cannot read the number of days to keep jobs; not purging", module);
purgeDays = 0;
}
return purgeDays;
}
public static int getFailedRetryMin() {
String minString = getElementAttr("thread-pool", "failed-retry-min");
int retryMin;
try {
retryMin = Integer.parseInt(minString);
} catch (NumberFormatException e) {
Debug.logError(e, "Unable to parse retry minutes; using default of 30", module);
retryMin = 30;
}
return retryMin;
}
public static NotificationGroup getNotificationGroup(String group) {
Map<String, NotificationGroup> engineNotifyMap = notificationGroupCache.get(engine);
if (engineNotifyMap == null) {
//
Element rootElement = null;
try {
rootElement = ServiceConfigUtil.getXmlRootElement();
} catch (GenericConfigException e) {
Debug.logError(e, "Error getting Service Engine XML root element", module);
}
engineNotifyMap = FastMap.newInstance();
for (Element e: UtilXml.childElementList(rootElement, "notification-group")) {
NotificationGroup ng = new NotificationGroup(e);
engineNotifyMap.put(ng.getName(), ng);
}
engineNotifyMap = notificationGroupCache.putIfAbsentAndGet(engine, engineNotifyMap);
}
if (engineNotifyMap != null) {
return engineNotifyMap.get(group);
}
return null;
}
public static String getEngineParameter(String engineName, String name) throws GenericConfigException {
Element root = ServiceConfigUtil.getXmlRootElement();
Node node = root.getFirstChild();
if (node != null) {
do {
if (node.getNodeType() == Node.ELEMENT_NODE && "engine".equals(node.getNodeName())) {
Element engine = (Element) node;
if (engineName.equals(engine.getAttribute("name"))) {
NodeList params = engine.getElementsByTagName("parameter");
if (params.getLength() > 0) {
for (int index = 0; index < params.getLength(); index++) {
Element param = (Element) params.item(index);
if (param != null && name.equals(param.getAttribute("name"))) {
return param.getAttribute("value");
}
}
}
}
}
} while ((node = node.getNextSibling()) != null);
}
return null;
}
// Replace these classes with the new implementation
public static class NotificationGroup implements Serializable {
protected Notification notification;
protected List<Notify> notify;
protected String name;
protected NotificationGroup(Element e) {
name = e.getAttribute("name");
notify = FastList.newInstance();
notification = new Notification(UtilXml.firstChildElement(e, "notification"));
for (Element e2: UtilXml.childElementList(e, "notify")) {
notify.add(new Notify(e2));
}
}
public String getName() {
return name;
}
public Notification getNotification() {
return notification;
}
public List<Notify> getNotify() {
return notify;
}
public String getService() {
return notification.getService();
}
public String getSubject() {
return notification.getSubject();
}
public String getScreen() {
return notification.getScreen();
}
public List<String> getAddress(String type) {
List<String> l = FastList.newInstance();
for (Notify n: notify) {
if (n.getType().equals(type)) {
l.add(n.getValue());
}
}
return l;
}
class Notification implements Serializable {
protected String subject = null;
protected String screen = null;
protected String service = null;
public Notification(Element e) {
service = e.getAttribute("service");
subject = e.getAttribute("subject");
screen = e.getAttribute("screen");
}
public String getScreen() {
return screen;
}
public String getSubject() {
return subject;
}
public String getService() {
return service;
}
}
class Notify implements Serializable {
protected String type;
protected String value;
public Notify(Element e) {
type = e.getAttribute("type");
value = UtilXml.elementValue(e);
}
public String getType() {
return type;
}
public String getValue() {
return value;
}
}
}
private ServiceConfigUtil() {}
}
@@ -22,6 +22,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
@@ -125,6 +126,8 @@ public abstract class GenericAsyncEngine extends AbstractEngine {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (IOException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
} catch (GenericConfigException e) {
throw new GenericServiceException("Problem serializing service attributes", e);
}
// make sure we stored okay
@@ -27,6 +27,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.RejectedExecutionException;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.Assert;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
@@ -155,9 +156,15 @@ public final class JobManager {
EntityCondition.makeCondition("cancelDateTime", EntityOperator.EQUALS, null),
EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null));
// limit to just defined pools
List<String> pools = ServiceConfigUtil.getRunPools();
List<String> pools = null;
try {
pools = ServiceConfigUtil.getRunPools();
} catch (GenericConfigException e) {
Debug.logWarning(e, "Unable to get run pools - not running job: ", module);
return poll;
}
List<EntityExpr> poolsExpr = UtilMisc.toList(EntityCondition.makeCondition("poolId", EntityOperator.EQUALS, null));
if (pools != null) {
if (!pools.isEmpty()) {
for (String poolName : pools) {
poolsExpr.add(EntityCondition.makeCondition("poolId", EntityOperator.EQUALS, poolName));
}
@@ -213,9 +220,14 @@ public final class JobManager {
}
if (poll.isEmpty()) {
// No jobs to run, see if there are any jobs to purge
int daysToKeep = ServiceConfigUtil.getPurgeJobDays();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
try {
int daysToKeep = ServiceConfigUtil.getPurgeJobDays();
cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
} catch (GenericConfigException e) {
Debug.logWarning(e, "Unable to get purge job days: ", module);
return poll;
}
Timestamp purgeTime = new Timestamp(cal.getTimeInMillis());
List<EntityExpr> finExp = UtilMisc.toList(EntityCondition.makeCondition("finishDateTime", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("finishDateTime", EntityOperator.LESS_THAN, purgeTime));
List<EntityExpr> canExp = UtilMisc.toList(EntityCondition.makeCondition("cancelDateTime", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("cancelDateTime", EntityOperator.LESS_THAN, purgeTime));
@@ -523,7 +535,11 @@ public final class JobManager {
if (UtilValidate.isNotEmpty(poolName)) {
jFields.put("poolId", poolName);
} else {
jFields.put("poolId", ServiceConfigUtil.getSendPool());
try {
jFields.put("poolId", ServiceConfigUtil.getSendPool());
} catch (GenericConfigException e) {
throw new JobManagerException(e.getMessage(), e);
}
}
// set the loader name
jFields.put("loaderName", delegator.getDelegatorName());
@@ -28,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException;
import javolution.util.FastMap;
import org.apache.commons.lang.StringUtils;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilGenerics;
@@ -244,7 +245,11 @@ public class PersistedServiceJob extends GenericServiceJob {
if (this.canRetry()) {
// create a recurrence
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, ServiceConfigUtil.getFailedRetryMin());
try {
cal.add(Calendar.MINUTE, ServiceConfigUtil.getFailedRetryMin());
} catch (GenericConfigException e) {
Debug.logWarning(e, "Unable to get retry minutes for job [" + getJobId() + "], defaulting to now: ", module);
}
long next = cal.getTimeInMillis();
try {
createRecurrence(next, true);
@@ -32,6 +32,7 @@ import javax.servlet.http.HttpServletRequest;
import javolution.util.FastMap;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilGenerics;
@@ -341,6 +342,8 @@ public class EbayStoreHelper {
return ServiceUtil.returnError(e.getMessage());
}catch (RecurrenceInfoException e) {
return ServiceUtil.returnError(e.getMessage());
} catch (GenericConfigException e) {
return ServiceUtil.returnError(e.getMessage());
}
return result;
}