⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelservicereader.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* 
 * $Id: ModelServiceReader.java,v 1.8 2004/02/11 16:49:36 ajzeneski Exp $
 *
 * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.ofbiz.service;

import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;

import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.config.ResourceHandler;
import org.ofbiz.entity.*;
import org.ofbiz.entity.model.*;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.OrderedMap;
import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilTimer;
import org.ofbiz.base.util.UtilXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Generic Service - Service Definition Reader
 *
 * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
 * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
 * @version    $Revision: 1.8 $
 * @since      2.0
 */

public class ModelServiceReader {

    public static final String module = ModelServiceReader.class.getName();

    protected static UtilCache readersUrl = new UtilCache("service.ModelServiceReader.ByURL", 0, 0);
    protected static UtilCache readersLoader = new UtilCache("service.ModelServiceReader.ByResourceLoader", 0, 0);

    /** is either from a URL or from a ResourceLoader (through the ResourceHandler) */
    protected boolean isFromURL;
    protected URL readerURL = null;
    protected ResourceHandler handler = null;
    protected Map modelServices = null;
    protected DispatchContext dctx = null;

    public static ModelServiceReader getModelServiceReader(URL readerURL, DispatchContext dctx) {
        ModelServiceReader reader = null;

        // if ( readersUrl.containsKey(readerURL) ) <-- this is unnecessary as it will return null below if not found
        reader = (ModelServiceReader) readersUrl.get(readerURL);
        if (reader == null) { // don't want to block here
            synchronized (ModelServiceReader.class) {
                // must check if null again as one of the blocked threads can still enter
                reader = (ModelServiceReader) readersUrl.get(readerURL);
                if (reader == null) {
                    // if (Debug.infoOn()) Debug.logInfo("[Creating reader]: " + readerURL.toExternalForm(), module);
                    reader = new ModelServiceReader(readerURL, dctx);
                    readersUrl.put(readerURL, reader);
                }
            }
        }
        return reader;
    }

    public static ModelServiceReader getModelServiceReader(ResourceHandler handler, DispatchContext dctx) {
        ModelServiceReader reader = null;

        reader = (ModelServiceReader) readersLoader.get(handler);
        if (reader == null) { // don't want to block here
            synchronized (ModelServiceReader.class) {
                // must check if null again as one of the blocked threads can still enter
                reader = (ModelServiceReader) readersLoader.get(handler);
                if (reader == null) {
                    // if (Debug.infoOn()) Debug.logInfo("[Creating reader]: " + handler, module);
                    reader = new ModelServiceReader(handler, dctx);
                    readersLoader.put(handler, reader);
                }
            }
        }
        return reader;
    }

    protected ModelServiceReader(URL readerURL, DispatchContext dctx) {
        this.isFromURL = true;
        this.readerURL = readerURL;
        this.handler = null;
        this.dctx = dctx;
        // preload models...
        getModelServices();
    }

    protected ModelServiceReader(ResourceHandler handler, DispatchContext dctx) {
        this.isFromURL = false;
        this.readerURL = null;
        this.handler = handler;
        this.dctx = dctx;
        // preload models...
        getModelServices();
    }

    public Map getModelServices() {
        if (modelServices == null) { // don't want to block here
            synchronized (ModelServiceReader.class) {
                // must check if null again as one of the blocked threads can still enter
                if (modelServices == null) { // now it's safe
                    modelServices = new HashMap();

                    UtilTimer utilTimer = new UtilTimer();

                    Document document = null;

                    if (this.isFromURL) {
                        // utilTimer.timerString("Before getDocument in file " + readerURL);
                        document = getDocument(readerURL);

                        if (document == null) {
                            modelServices = null;
                            return null;
                        }
                    } else {
                        // utilTimer.timerString("Before getDocument in " + handler);
                        try {
                            document = handler.getDocument();
                        } catch (GenericConfigException e) {
                            Debug.logError(e, "Error getting XML document from resource", module);
                            return null;
                        }
                    }

                    if (this.isFromURL) {// utilTimer.timerString("Before getDocumentElement in file " + readerURL);
                    } else {// utilTimer.timerString("Before getDocumentElement in " + handler);
                    }

                    Element docElement = document.getDocumentElement();
                    if (docElement == null) {
                        modelServices = null;
                        return null;
                    }

                    docElement.normalize();

                    int i = 0;
                    Node curChild = docElement.getFirstChild();
                    if (curChild != null) {
                        if (this.isFromURL) {
                            utilTimer.timerString("Before start of service loop in file " + readerURL);
                        } else {
                            utilTimer.timerString("Before start of service loop in " + handler);
                        }
                        int servicesLoaded = 0;

                        do {
                            if (curChild.getNodeType() == Node.ELEMENT_NODE && "service".equals(curChild.getNodeName())) {
                                i++;
                                Element curService = (Element) curChild;
                                String serviceName = UtilXml.checkEmpty(curService.getAttribute("name"));

                                // check to see if service with same name has already been read
                                if (modelServices.containsKey(serviceName)) {
                                    Debug.logWarning("WARNING: Service " + serviceName + " is defined more than once, " +
                                        "most recent will over-write previous definition(s)", module);
                                }

                                // utilTimer.timerString("  After serviceName -- " + i + " --");
                                ModelService service = createModelService(curService);

                                // utilTimer.timerString("  After createModelService -- " + i + " --");
                                if (service != null) {
                                    modelServices.put(serviceName, service);
                                    // utilTimer.timerString("  After modelServices.put -- " + i + " --");
                                    /*
                                    int reqIn = service.getParameterNames(ModelService.IN_PARAM, false).size();
                                    int optIn = service.getParameterNames(ModelService.IN_PARAM, true).size() - reqIn;
                                    int reqOut = service.getParameterNames(ModelService.OUT_PARAM, false).size();
                                    int optOut = service.getParameterNames(ModelService.OUT_PARAM, true).size() - reqOut;

                                    if (Debug.verboseOn()) {
                                        String msg = "-- getModelService: # " + i + " Loaded service: " + serviceName +
                                            " (IN) " + reqIn + "/" + optIn + " (OUT) " + reqOut + "/" + optOut;

                                        Debug.logVerbose(msg, module);                                        
                                    }
                                    */
                                } else {
                                    Debug.logWarning(
                                        "-- -- SERVICE ERROR:getModelService: Could not create service for serviceName: " +
                                        serviceName, module);
                                }

                            }
                        } while ((curChild = curChild.getNextSibling()) != null);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -