| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.edl.impl; |
| 18 | |
|
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.kuali.rice.core.util.XmlHelper; |
| 21 | |
import org.kuali.rice.core.util.XmlJotter; |
| 22 | |
import org.kuali.rice.edl.impl.bo.EDocLiteAssociation; |
| 23 | |
import org.kuali.rice.edl.impl.service.EDocLiteService; |
| 24 | |
import org.kuali.rice.edl.impl.service.EdlServiceLocator; |
| 25 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
| 26 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
| 27 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 28 | |
import org.w3c.dom.Document; |
| 29 | |
import org.w3c.dom.Element; |
| 30 | |
import org.w3c.dom.Node; |
| 31 | |
import org.w3c.dom.NodeList; |
| 32 | |
|
| 33 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 34 | |
import javax.xml.transform.Templates; |
| 35 | |
import java.util.Iterator; |
| 36 | |
import java.util.LinkedHashMap; |
| 37 | |
import java.util.Map; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public class EDLControllerFactory { |
| 48 | |
|
| 49 | 0 | private static final Logger LOG = Logger.getLogger(EDLControllerFactory.class); |
| 50 | |
|
| 51 | |
private static final String CONFIG_CACHE_GROUP_NAME = "EDLConfig"; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static EDLController createEDLController(EDocLiteAssociation edlAssociation, EDLGlobalConfig edlGlobalConfig) { |
| 56 | 0 | EDLController edlController = new EDLController(); |
| 57 | 0 | edlController.setEdocLiteAssociation(edlAssociation); |
| 58 | |
|
| 59 | |
try { |
| 60 | 0 | edlController.setEdlGlobalConfig(edlGlobalConfig); |
| 61 | 0 | edlController.setDefaultDOM(getDefaultDOM(edlAssociation)); |
| 62 | 0 | loadConfigProcessors(edlController, edlGlobalConfig); |
| 63 | 0 | loadPreProcessors(edlController, edlGlobalConfig); |
| 64 | 0 | loadPostProcessor(edlController, edlGlobalConfig); |
| 65 | 0 | loadStateComponents(edlController, edlGlobalConfig); |
| 66 | 0 | loadStyle(edlController); |
| 67 | |
|
| 68 | 0 | } catch (Exception e) { |
| 69 | 0 | String edl = null; |
| 70 | 0 | if (edlAssociation != null) { |
| 71 | 0 | edl = edlAssociation.getEdlName(); |
| 72 | |
} |
| 73 | 0 | String message = "Error creating controller for EDL" + (edl == null ? "" : ": " + edl); |
| 74 | 0 | LOG.error(message, e); |
| 75 | 0 | throw new WorkflowRuntimeException("Problems creating controller for EDL: " + edl, e); |
| 76 | 0 | } |
| 77 | |
|
| 78 | 0 | return edlController; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public static EDLController createEDLController(EDocLiteAssociation edlAssociation, EDLGlobalConfig edlGlobalConfig, DocumentRouteHeaderValue document) { |
| 82 | 0 | EDLController edlController = createEDLController(edlAssociation, edlGlobalConfig); |
| 83 | |
try { |
| 84 | 0 | Document defaultDom = edlController.getDefaultDOM(); |
| 85 | 0 | Document documentDom = XmlHelper.readXml(document.getDocContent()); |
| 86 | |
|
| 87 | 0 | Element documentData = (Element) documentDom.getElementsByTagName(EDLXmlUtils.DATA_E).item(0); |
| 88 | 0 | if (documentData != null) { |
| 89 | 0 | Element defaultDomEDL = EDLXmlUtils.getEDLContent(defaultDom, false); |
| 90 | 0 | Element defaultDomData = (Element) defaultDomEDL.getElementsByTagName(EDLXmlUtils.DATA_E).item(0); |
| 91 | 0 | defaultDomEDL.replaceChild(defaultDom.importNode(documentData, true), defaultDomData); |
| 92 | |
} |
| 93 | 0 | if (LOG.isDebugEnabled()) { |
| 94 | 0 | LOG.debug("Created default Node from document id " + document.getRouteHeaderId() + " content " + XmlJotter.jotNode(defaultDom)); |
| 95 | |
} |
| 96 | 0 | } catch (Exception e) { |
| 97 | 0 | throw new WorkflowRuntimeException("Problems creating controller for EDL " + edlAssociation.getEdlName() + " document " + document.getRouteHeaderId(), e); |
| 98 | 0 | } |
| 99 | 0 | return edlController; |
| 100 | |
} |
| 101 | |
|
| 102 | |
private synchronized static void loadStyle(EDLController edlController) throws Exception { |
| 103 | 0 | EDocLiteService edlService = getEDLService(); |
| 104 | 0 | Templates styleSheet = null; |
| 105 | 0 | styleSheet = edlService.getStyleAsTranslet(edlController.getEdocLiteAssociation().getStyle()); |
| 106 | 0 | edlController.setStyle(styleSheet); |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
private synchronized static void loadPreProcessors(EDLController edlController, EDLGlobalConfig edlGlobalConfig) { |
| 110 | 0 | edlController.setPreProcessors(cloneConfigMap(edlGlobalConfig.getPreProcessors(), edlController.getDefaultDOM())); |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
private synchronized static void loadPostProcessor(EDLController edlController, EDLGlobalConfig edlGlobalConfig) { |
| 114 | 0 | edlController.setPostProcessors(cloneConfigMap(edlGlobalConfig.getPostProcessors(), edlController.getDefaultDOM())); |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
private synchronized static void loadStateComponents(EDLController edlController, EDLGlobalConfig edlGlobalConfig) { |
| 118 | 0 | edlController.setStateComponents(cloneConfigMap(edlGlobalConfig.getStateComponents(), edlController.getDefaultDOM())); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
private synchronized static void loadConfigProcessors(EDLController edlController, EDLGlobalConfig edlGlobalConfig) throws Exception { |
| 122 | 0 | EDocLiteAssociation edlAssociation = edlController.getEdocLiteAssociation(); |
| 123 | 0 | Map configProcessorMappings = fetchConfigFromCache(edlAssociation.getDefinition()); |
| 124 | 0 | if (configProcessorMappings != null) { |
| 125 | 0 | edlController.setConfigProcessors(cloneConfigMap(configProcessorMappings, edlController.getDefaultDOM())); |
| 126 | |
} else { |
| 127 | |
|
| 128 | 0 | Document document = getEDLService().getDefinitionXml(edlAssociation); |
| 129 | 0 | Element definitionElement = (Element) document.getFirstChild(); |
| 130 | |
|
| 131 | 0 | configProcessorMappings = new LinkedHashMap(); |
| 132 | 0 | edlController.setEdlGlobalConfig(edlGlobalConfig); |
| 133 | 0 | NodeList edlDefinitionNodes = definitionElement.getChildNodes(); |
| 134 | 0 | for (int i = 0; i < edlDefinitionNodes.getLength(); i++) { |
| 135 | 0 | Node definitionNode = edlDefinitionNodes.item(i); |
| 136 | 0 | Class configProcessorClass = edlGlobalConfig.getConfigProcessor(definitionNode); |
| 137 | 0 | if (configProcessorClass != null) { |
| 138 | 0 | configProcessorMappings.put(definitionNode, configProcessorClass); |
| 139 | |
} |
| 140 | |
} |
| 141 | 0 | putConfigInCache(edlAssociation.getDefinition(), configProcessorMappings); |
| 142 | |
|
| 143 | 0 | loadConfigProcessors(edlController, edlGlobalConfig); |
| 144 | |
} |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
protected synchronized static Map fetchConfigFromCache(String definition) { |
| 148 | 0 | return (Map)KEWServiceLocator.getCacheAdministrator().getFromCache(getConfigCacheKey(definition)); |
| 149 | |
} |
| 150 | |
|
| 151 | |
private synchronized static void putConfigInCache(String definition, Map configProcessorMappings) { |
| 152 | 0 | KEWServiceLocator.getCacheAdministrator().putInCache(getConfigCacheKey(definition), configProcessorMappings, CONFIG_CACHE_GROUP_NAME); |
| 153 | 0 | } |
| 154 | |
|
| 155 | |
private static String getConfigCacheKey(String definition) { |
| 156 | 0 | return CONFIG_CACHE_GROUP_NAME + ":" + definition; |
| 157 | |
} |
| 158 | |
|
| 159 | |
private synchronized static Map cloneConfigMap(Map configMap, Document defaultDom) { |
| 160 | 0 | Map tempConfigProcessors = new LinkedHashMap(); |
| 161 | 0 | for (Iterator iter = configMap.entrySet().iterator(); iter.hasNext();) { |
| 162 | 0 | Map.Entry configProcessorMapping = (Map.Entry) iter.next(); |
| 163 | 0 | tempConfigProcessors.put(defaultDom.importNode((Node)configProcessorMapping.getKey(), true), configProcessorMapping.getValue()); |
| 164 | 0 | } |
| 165 | 0 | return tempConfigProcessors; |
| 166 | |
} |
| 167 | |
|
| 168 | |
private static EDocLiteService getEDLService() { |
| 169 | 0 | return EdlServiceLocator.getEDocLiteService(); |
| 170 | |
} |
| 171 | |
|
| 172 | |
private static Document getDefaultDOM(EDocLiteAssociation edlAssociation) throws Exception { |
| 173 | 0 | Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); |
| 174 | 0 | Element rootElement = dom.createElement("documentContent"); |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 0 | dom.appendChild(rootElement); |
| 182 | 0 | Element edlContentElement = EDLXmlUtils.getEDLContent(dom, true); |
| 183 | 0 | EDLXmlUtils.getDataFromEDLDocument(edlContentElement, true); |
| 184 | |
|
| 185 | |
|
| 186 | 0 | Element edlData = EDLXmlUtils.getChildElement(edlContentElement, EDLXmlUtils.DATA_E); |
| 187 | |
|
| 188 | 0 | edlData.setAttribute("edlName", edlAssociation.getEdlName()); |
| 189 | |
|
| 190 | 0 | return dom; |
| 191 | |
} |
| 192 | |
|
| 193 | |
public static void flushDefinitionFromConfigCache(String definition) { |
| 194 | 0 | KEWServiceLocator.getCacheAdministrator().flushEntry(getConfigCacheKey(definition)); |
| 195 | |
|
| 196 | 0 | } |
| 197 | |
|
| 198 | |
public static void flushDefinitionCache() { |
| 199 | 0 | KEWServiceLocator.getCacheAdministrator().flushGroup(CONFIG_CACHE_GROUP_NAME); |
| 200 | 0 | } |
| 201 | |
} |