Search in sources :

Example 1 with ResourceHandler

use of org.apache.ofbiz.base.config.ResourceHandler in project ofbiz-framework by apache.

the class DispatchContext method getGlobalServiceMap.

private Map<String, ModelService> getGlobalServiceMap() {
    Map<String, ModelService> serviceMap = modelServiceMapByModel.get(this.model);
    if (serviceMap == null) {
        serviceMap = new HashMap<>();
        List<Future<Map<String, ModelService>>> futures = new LinkedList<>();
        List<GlobalServices> globalServicesList = null;
        try {
            globalServicesList = ServiceConfigUtil.getServiceEngine().getGlobalServices();
        } catch (GenericConfigException e) {
            // FIXME: Refactor API so exceptions can be thrown and caught.
            Debug.logError(e, module);
            throw new RuntimeException(e.getMessage());
        }
        for (GlobalServices globalServices : globalServicesList) {
            ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.getServiceEngineXmlFileName(), globalServices.getLoader(), globalServices.getLocation());
            futures.add(ExecutionPool.GLOBAL_FORK_JOIN.submit(createServiceReaderCallable(handler)));
        }
        // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file
        for (ComponentConfig.ServiceResourceInfo componentResourceInfo : ComponentConfig.getAllServiceResourceInfos("model")) {
            futures.add(ExecutionPool.GLOBAL_FORK_JOIN.submit(createServiceReaderCallable(componentResourceInfo.createResourceHandler())));
        }
        for (Map<String, ModelService> servicesMap : ExecutionPool.getAllFutures(futures)) {
            if (servicesMap != null) {
                serviceMap.putAll(servicesMap);
            }
        }
        Map<String, ModelService> cachedServiceMap = modelServiceMapByModel.putIfAbsentAndGet(this.model, serviceMap);
        if (cachedServiceMap == serviceMap) {
            // same object: this means that the object created by this thread was actually added to the cache
            ServiceEcaUtil.reloadConfig();
        }
    }
    return serviceMap;
}
Also used : ResourceHandler(org.apache.ofbiz.base.config.ResourceHandler) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) LinkedList(java.util.LinkedList) GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) ComponentConfig(org.apache.ofbiz.base.component.ComponentConfig) Future(java.util.concurrent.Future) GlobalServices(org.apache.ofbiz.service.config.model.GlobalServices)

Example 2 with ResourceHandler

use of org.apache.ofbiz.base.config.ResourceHandler 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 3 with ResourceHandler

use of org.apache.ofbiz.base.config.ResourceHandler in project ofbiz-framework by apache.

the class ModelFieldTypeReader method getModelFieldTypeReader.

public static ModelFieldTypeReader getModelFieldTypeReader(String helperName) {
    Datasource datasourceInfo = EntityConfig.getDatasource(helperName);
    if (datasourceInfo == null) {
        throw new IllegalArgumentException("Could not find a datasource/helper with the name " + helperName);
    }
    String tempModelName = datasourceInfo.getFieldTypeName();
    ModelFieldTypeReader reader = readers.get(tempModelName);
    while (reader == null) {
        FieldType fieldTypeInfo = null;
        try {
            fieldTypeInfo = EntityConfig.getInstance().getFieldType(tempModelName);
        } catch (GenericEntityConfException e) {
            Debug.logWarning(e, "Exception thrown while getting field type config: ", module);
        }
        if (fieldTypeInfo == null) {
            throw new IllegalArgumentException("Could not find a field-type definition with name \"" + tempModelName + "\"");
        }
        ResourceHandler fieldTypeResourceHandler = new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, fieldTypeInfo.getLoader(), fieldTypeInfo.getLocation());
        UtilTimer utilTimer = new UtilTimer();
        utilTimer.timerString("[ModelFieldTypeReader.getModelFieldTypeReader] Reading field types from " + fieldTypeResourceHandler.getLocation());
        Document document = null;
        try {
            document = fieldTypeResourceHandler.getDocument();
        } catch (GenericConfigException e) {
            Debug.logError(e, module);
            throw new IllegalStateException("Error loading field type file " + fieldTypeResourceHandler.getLocation());
        }
        Map<String, ModelFieldType> fieldTypeMap = createFieldTypeCache(document.getDocumentElement(), fieldTypeResourceHandler.getLocation());
        reader = readers.putIfAbsentAndGet(tempModelName, new ModelFieldTypeReader(fieldTypeMap));
        utilTimer.timerString("[ModelFieldTypeReader.getModelFieldTypeReader] Read " + fieldTypeMap.size() + " field types");
    }
    return reader;
}
Also used : Datasource(org.apache.ofbiz.entity.config.model.Datasource) GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) UtilTimer(org.apache.ofbiz.base.util.UtilTimer) ResourceHandler(org.apache.ofbiz.base.config.ResourceHandler) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) Document(org.w3c.dom.Document) FieldType(org.apache.ofbiz.entity.config.model.FieldType) GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler)

Example 4 with ResourceHandler

use of org.apache.ofbiz.base.config.ResourceHandler in project ofbiz-framework by apache.

the class ServiceEcaUtil method readConfig.

public static void readConfig() {
    // Only proceed if the cache hasn't already been populated, caller should be using reloadConfig() in that situation
    if (UtilValidate.isNotEmpty(ecaCache)) {
        return;
    }
    List<Future<List<ServiceEcaRule>>> futures = new LinkedList<Future<List<ServiceEcaRule>>>();
    List<ServiceEcas> serviceEcasList = null;
    try {
        serviceEcasList = ServiceConfigUtil.getServiceEngine().getServiceEcas();
    } catch (GenericConfigException e) {
        // FIXME: Refactor API so exceptions can be thrown and caught.
        Debug.logError(e, module);
        throw new RuntimeException(e.getMessage());
    }
    for (ServiceEcas serviceEcas : serviceEcasList) {
        ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.getServiceEngineXmlFileName(), serviceEcas.getLoader(), serviceEcas.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.ServiceResourceInfo componentResourceInfo : ComponentConfig.getAllServiceResourceInfos("eca")) {
        futures.add(ExecutionPool.GLOBAL_FORK_JOIN.submit(createEcaLoaderCallable(componentResourceInfo.createResourceHandler())));
    }
    for (List<ServiceEcaRule> handlerRules : ExecutionPool.getAllFutures(futures)) {
        mergeEcaDefinitions(handlerRules);
    }
}
Also used : ResourceHandler(org.apache.ofbiz.base.config.ResourceHandler) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) LinkedList(java.util.LinkedList) GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) ServiceEcas(org.apache.ofbiz.service.config.model.ServiceEcas) 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)

Example 5 with ResourceHandler

use of org.apache.ofbiz.base.config.ResourceHandler in project ofbiz-framework by apache.

the class ModelGroupReader method getGroupCache.

public Map<String, String> getGroupCache(String delegatorName) {
    if (this.groupCache == null) {
        // don't want to block here
        synchronized (ModelGroupReader.class) {
            // must check if null again as one of the blocked threads can still enter
            if (this.groupCache == null) {
                // now it's safe
                this.groupCache = new HashMap<>();
                this.groupNames = new TreeSet<>();
                UtilTimer utilTimer = new UtilTimer();
                // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocument");
                int i = 0;
                for (ResourceHandler entityGroupResourceHandler : this.entityGroupResourceHandlers) {
                    Document document = null;
                    try {
                        document = entityGroupResourceHandler.getDocument();
                    } catch (GenericConfigException e) {
                        Debug.logError(e, "Error loading entity group model", module);
                    }
                    if (document == null) {
                        this.groupCache = null;
                        return null;
                    }
                    // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocumentElement");
                    Element docElement = document.getDocumentElement();
                    if (docElement == null) {
                        continue;
                    }
                    docElement.normalize();
                    Node curChild = docElement.getFirstChild();
                    if (curChild != null) {
                        utilTimer.timerString("[ModelGroupReader.getGroupCache] Before start of entity loop");
                        do {
                            if (curChild.getNodeType() == Node.ELEMENT_NODE && "entity-group".equals(curChild.getNodeName())) {
                                Element curEntity = (Element) curChild;
                                String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity")).intern();
                                String groupName = UtilXml.checkEmpty(curEntity.getAttribute("group")).intern();
                                try {
                                    if (null == EntityConfig.getInstance().getDelegator(delegatorName).getGroupDataSource(groupName)) {
                                        Debug.logError("The declared group name " + groupName + " has no corresponding group-map in entityengine.xml: ", module);
                                    }
                                } catch (GenericEntityConfException e) {
                                    Debug.logWarning(e, "Exception thrown while getting group name: ", module);
                                }
                                this.groupNames.add(groupName);
                                this.groupCache.put(entityName, groupName);
                                // utilTimer.timerString("  After entityEntityName -- " + i + " --");
                                i++;
                            }
                        } while ((curChild = curChild.getNextSibling()) != null);
                    } else {
                        Debug.logWarning("[ModelGroupReader.getGroupCache] No child nodes found.", module);
                    }
                }
                utilTimer.timerString("[ModelGroupReader.getGroupCache] FINISHED - Total Entity-Groups: " + i + " FINISHED");
            }
        }
    }
    return this.groupCache;
}
Also used : GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) DelegatorElement(org.apache.ofbiz.entity.config.model.DelegatorElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) UtilTimer(org.apache.ofbiz.base.util.UtilTimer) ResourceHandler(org.apache.ofbiz.base.config.ResourceHandler) MainResourceHandler(org.apache.ofbiz.base.config.MainResourceHandler) Document(org.w3c.dom.Document)

Aggregations

MainResourceHandler (org.apache.ofbiz.base.config.MainResourceHandler)8 ResourceHandler (org.apache.ofbiz.base.config.ResourceHandler)8 GenericConfigException (org.apache.ofbiz.base.config.GenericConfigException)7 LinkedList (java.util.LinkedList)5 ComponentConfig (org.apache.ofbiz.base.component.ComponentConfig)5 GenericEntityConfException (org.apache.ofbiz.entity.GenericEntityConfException)5 Future (java.util.concurrent.Future)3 UtilTimer (org.apache.ofbiz.base.util.UtilTimer)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)3 List (java.util.List)2 DelegatorElement (org.apache.ofbiz.entity.config.model.DelegatorElement)2 Resource (org.apache.ofbiz.entity.config.model.Resource)2 Node (org.w3c.dom.Node)2 File (java.io.File)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 TreeSet (java.util.TreeSet)1