Search in sources :

Example 1 with Resource

use of org.apache.ofbiz.entity.config.model.Resource in project ofbiz-framework by apache.

the class EntityDataLoader method getUrlList.

public static <E> List<URL> getUrlList(String helperName, String componentName, List<E> readerNames) {
    String paths = getPathsString(helperName);
    List<URL> urlList = new LinkedList<URL>();
    // first get files from resources
    if (readerNames != null) {
        for (Object readerInfo : readerNames) {
            String readerName = null;
            if (readerInfo instanceof String) {
                readerName = (String) readerInfo;
            } else if (readerInfo instanceof ReadData) {
                readerName = ((ReadData) readerInfo).getReaderName();
            } else if (readerInfo instanceof Element) {
                readerName = ((Element) readerInfo).getAttribute("reader-name");
            } else {
                throw new IllegalArgumentException("Reader name list does not contain String(s) or Element(s)");
            }
            readerName = readerName.trim();
            // ignore the "tenant" reader if multitenant is disabled
            if ("tenant".equals(readerName) && !EntityUtil.isMultiTenantEnabled()) {
                continue;
            }
            // get all of the main resource model stuff, ie specified in the entityengine.xml file
            EntityDataReader entityDataReaderInfo = null;
            try {
                entityDataReaderInfo = EntityConfig.getInstance().getEntityDataReader(readerName);
                if (entityDataReaderInfo == null) {
                    // create a reader name defined at runtime
                    Debug.logInfo("Could not find entity-data-reader named: " + readerName + ". Creating a new reader with this name. ", module);
                    entityDataReaderInfo = new EntityDataReader(readerName);
                }
            } catch (GenericEntityConfException e) {
                Debug.logWarning(e, "Exception thrown while getting entity data reader config: ", module);
            }
            if (entityDataReaderInfo != null) {
                for (Resource resourceElement : entityDataReaderInfo.getResourceList()) {
                    ResourceHandler handler = new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, resourceElement.getLoader(), resourceElement.getLocation());
                    try {
                        urlList.add(handler.getURL());
                    } catch (GenericConfigException e) {
                        String errorMsg = "Could not get URL for Main ResourceHandler: " + e.toString();
                        Debug.logWarning(errorMsg, module);
                    }
                }
                // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file
                for (ComponentConfig.EntityResourceInfo componentResourceInfo : ComponentConfig.getAllEntityResourceInfos("data", componentName)) {
                    if (readerName.equals(componentResourceInfo.readerName)) {
                        ResourceHandler handler = componentResourceInfo.createResourceHandler();
                        try {
                            urlList.add(handler.getURL());
                        } catch (GenericConfigException e) {
                            String errorMsg = "Could not get URL for Component ResourceHandler: " + e.toString();
                            Debug.logWarning(errorMsg, module);
                        }
                    }
                }
            } else {
                String errorMsg = "Could not find entity-data-reader named: " + readerName;
                Debug.logWarning(errorMsg, module);
            }
        }
    } else {
        String errorMsg = "Could not find datasource named: " + helperName;
        Debug.logWarning(errorMsg, module);
    }
    // get files from the paths string
    if (UtilValidate.isNotEmpty(paths)) {
        StringTokenizer tokenizer = new StringTokenizer(paths, ";");
        while (tokenizer.hasMoreTokens()) {
            String path = tokenizer.nextToken().toLowerCase(Locale.getDefault());
            File loadDir = new File(path);
            if (loadDir.exists() && loadDir.isDirectory()) {
                File[] files = loadDir.listFiles();
                List<File> tempFileList = new LinkedList<File>();
                if (files != null) {
                    for (File file : files) {
                        if (file.getName().toLowerCase(Locale.getDefault()).endsWith(".xml")) {
                            tempFileList.add(file);
                        }
                    }
                }
                Collections.sort(tempFileList);
                for (File dataFile : tempFileList) {
                    if (dataFile.exists()) {
                        URL url = null;
                        try {
                            url = dataFile.toURI().toURL();
                            urlList.add(url);
                        } catch (java.net.MalformedURLException e) {
                            String xmlError = "Error loading XML file \"" + dataFile.getAbsolutePath() + "\"; Error was: " + e.getMessage();
                            Debug.logError(xmlError, module);
                        }
                    } else {
                        String errorMsg = "Could not find file: \"" + dataFile.getAbsolutePath() + "\"";
                        Debug.logError(errorMsg, module);
                    }
                }
            }
        }
    }
    return urlList;
}
Also used : GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) Element(org.w3c.dom.Element) Resource(org.apache.ofbiz.entity.config.model.Resource) ResourceHandler(org.apache.ofbiz.base.config.ResourceHandler) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) URL(java.net.URL) LinkedList(java.util.LinkedList) StringTokenizer(java.util.StringTokenizer) GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) ComponentConfig(org.apache.ofbiz.base.component.ComponentConfig) EntityDataReader(org.apache.ofbiz.entity.config.model.EntityDataReader) ReadData(org.apache.ofbiz.entity.config.model.ReadData) File(java.io.File)

Example 2 with Resource

use of org.apache.ofbiz.entity.config.model.Resource in project ofbiz-framework by apache.

the class EntityEcaUtil method readConfig.

private static void readConfig(String entityEcaReaderName, Map<String, Map<String, List<EntityEcaRule>>> ecaCache) {
    EntityEcaReader entityEcaReaderInfo = null;
    try {
        entityEcaReaderInfo = EntityConfig.getInstance().getEntityEcaReader(entityEcaReaderName);
    } catch (GenericEntityConfException e) {
        Debug.logError(e, "Exception thrown while getting entity-eca-reader config with name: " + entityEcaReaderName, module);
    }
    if (entityEcaReaderInfo == null) {
        Debug.logError("BAD ERROR: Could not find entity-eca-reader config with name: " + entityEcaReaderName, module);
        return;
    }
    List<Future<List<EntityEcaRule>>> futures = new LinkedList<Future<List<EntityEcaRule>>>();
    for (Resource eecaResourceElement : entityEcaReaderInfo.getResourceList()) {
        ResourceHandler handler = new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, eecaResourceElement.getLoader(), eecaResourceElement.getLocation());
        futures.add(ExecutionPool.GLOBAL_FORK_JOIN.submit(createEcaLoaderCallable(handler)));
    }
    // get all of the component resource eca stuff, ie specified in each ofbiz-component.xml file
    for (ComponentConfig.EntityResourceInfo componentResourceInfo : ComponentConfig.getAllEntityResourceInfos("eca")) {
        if (entityEcaReaderName.equals(componentResourceInfo.readerName)) {
            futures.add(ExecutionPool.GLOBAL_FORK_JOIN.submit(createEcaLoaderCallable(componentResourceInfo.createResourceHandler())));
        }
    }
    for (List<EntityEcaRule> oneFileRules : ExecutionPool.getAllFutures(futures)) {
        for (EntityEcaRule rule : oneFileRules) {
            String entityName = rule.getEntityName();
            String eventName = rule.getEventName();
            Map<String, List<EntityEcaRule>> eventMap = ecaCache.get(entityName);
            List<EntityEcaRule> rules = null;
            if (eventMap == null) {
                eventMap = new HashMap<String, List<EntityEcaRule>>();
                rules = new LinkedList<EntityEcaRule>();
                ecaCache.put(entityName, eventMap);
                eventMap.put(eventName, rules);
            } else {
                rules = eventMap.get(eventName);
                if (rules == null) {
                    rules = new LinkedList<EntityEcaRule>();
                    eventMap.put(eventName, rules);
                }
            }
            // This will prevent duplicate rule execution along with enabled/disabled eca workflow
            if (rules.remove(rule)) {
                Debug.logWarning("Duplicate Entity ECA [" + entityName + "]" + "for operation [ " + rule.getOperationName() + "] " + "on [" + eventName + "] ", module);
            }
            rules.add(rule);
        }
    }
}
Also used : GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) Resource(org.apache.ofbiz.entity.config.model.Resource) ResourceHandler(org.apache.ofbiz.base.config.ResourceHandler) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) LinkedList(java.util.LinkedList) EntityEcaReader(org.apache.ofbiz.entity.config.model.EntityEcaReader) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) ComponentConfig(org.apache.ofbiz.base.component.ComponentConfig) Future(java.util.concurrent.Future) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)2 ComponentConfig (org.apache.ofbiz.base.component.ComponentConfig)2 MainResourceHandler (org.apache.ofbiz.base.config.MainResourceHandler)2 ResourceHandler (org.apache.ofbiz.base.config.ResourceHandler)2 GenericEntityConfException (org.apache.ofbiz.entity.GenericEntityConfException)2 Resource (org.apache.ofbiz.entity.config.model.Resource)2 File (java.io.File)1 URL (java.net.URL)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 Future (java.util.concurrent.Future)1 GenericConfigException (org.apache.ofbiz.base.config.GenericConfigException)1 EntityDataReader (org.apache.ofbiz.entity.config.model.EntityDataReader)1 EntityEcaReader (org.apache.ofbiz.entity.config.model.EntityEcaReader)1 ReadData (org.apache.ofbiz.entity.config.model.ReadData)1 Element (org.w3c.dom.Element)1