Search in sources :

Example 96 with EngineException

use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.

the class RequestableObject method run.

/**
 * Initializes all common things for transaction and launch
 * the execution of the core transaction.
 *
 * @param requester the calling requester.
 * @param context the associated context.
 *
 * @return the resulting DOM of the transaction.
 */
public Document run(Requester requester, Context context) throws EngineException {
    fireRequestableEvent(RequestableObject.EVENT_REQUESTABLE_STARTED);
    try {
        this.requester = requester;
        this.context = context;
        this.score = 0;
        context.cacheControl = this.isClientCachable() ? "true" : "false";
        context.outputDocument = requester.createDOM(getEncodingCharSet());
        Element outputDocumentRootElement = context.outputDocument.createElement("document");
        context.outputDocument.appendChild(outputDocumentRootElement);
        outputDocumentRootElement.setAttribute("project", context.projectName);
        outputDocumentRootElement.setAttribute("sequence", context.sequenceName);
        outputDocumentRootElement.setAttribute("connector", context.connectorName);
        outputDocumentRootElement.setAttribute("transaction", context.transactionName);
        if (context.lang != null && context.lang.length() != 0) {
            outputDocumentRootElement.setAttribute("lang", context.lang);
        }
        int maxNbCurrentWorkerThreads = Integer.parseInt(EnginePropertiesManager.getProperty(PropertyName.DOCUMENT_THREADING_MAX_WORKER_THREADS));
        if (nbCurrentWorkerThreads >= maxNbCurrentWorkerThreads)
            throw new EngineException("No more available worker thread (" + maxNbCurrentWorkerThreads + ")");
        Engine.logContext.debug("(RequestableObject) Start of the thread for the requested object '" + getName() + "' (" + context.contextID + ")");
        workerThreadCreationStatistic = context.statistics.start(EngineStatistics.WORKER_THREAD_START);
        runningThread = new RequestableThread();
        runningThread.start();
        try {
            synchronized (Engine.theApp) {
                // modification of the worker threads counter
                if (runningThread.engineId == Engine.startStopDate)
                    nbCurrentWorkerThreads++;
            }
            boolean hasBeenInterrupted = false;
            do {
                try {
                    synchronized (runningThread) {
                        long lTime = System.currentTimeMillis();
                        long haveToWait;
                        while (runningThread.bContinue) {
                            if (getResponseTimeout() <= 0) {
                                Engine.logContext.trace("(RequestableObject) Waiting for requested object response during 60s...");
                                haveToWait = 60000;
                                runningThread.wait(60000);
                            } else if (Engine.isStudioMode()) {
                                // Studio context => ~infinite responseTimeout
                                Engine.logContext.trace("(RequestableObject) Waiting for requested object response during infinite timeout (24 hours) because of Studio context execution...");
                                haveToWait = 1000 * 60 * 60 * 24;
                                runningThread.wait(1000 * 60 * 60 * 24);
                            } else {
                                Engine.logContext.trace("(RequestableObject) Waiting for requested object response during " + getResponseTimeout() + "s");
                                haveToWait = getResponseTimeout() * 1000;
                                runningThread.wait(getResponseTimeout() * 1000);
                            }
                            Engine.logContext.trace("(RequestableObject) End of wait(), runningThread.bContinue=" + runningThread.bContinue);
                            if (runningThread.bContinue) {
                                if (System.currentTimeMillis() - lTime > haveToWait) {
                                    // Cleans up before stopping
                                    cleanup();
                                    // Stops thread
                                    if (EnginePropertiesManager.getProperty(PropertyName.DOCUMENT_THREADING_USE_STOP_METHOD).equalsIgnoreCase("true")) {
                                        ThreadUtils.stopThread(runningThread);
                                        Engine.logContext.error("(RequestableObject) Stopping the thread for the requested object '" + getName() + "' because of timeout expiration");
                                    } else {
                                        runningThread.bContinue = false;
                                        Engine.logContext.error("(RequestableObject) Request for stopping the thread for the requested object '" + getName() + "' because of timeout expiration");
                                    }
                                    TransactionTimeoutException e = new TransactionTimeoutException("The requested object '" + getName() + "' has been interrupted because it did not terminate quickly enough.");
                                    throw e;
                                } else
                                    Engine.logContext.trace("(RequestableObject) Spurious wakup , keep waiting");
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    Engine.logContext.warn("(RequestableObject) InterruptedException while waiting for requested object response; retrying wait()");
                    hasBeenInterrupted = true;
                }
            } while (hasBeenInterrupted && runningThread.bContinue);
        } finally {
            synchronized (Engine.theApp) {
                // modification of the worker threads counter
                if (runningThread.engineId == Engine.startStopDate)
                    nbCurrentWorkerThreads--;
            }
        }
        // An exception has been thrown ?
        if (runningThread.exception != null)
            if (runningThread.exception instanceof EngineException)
                throw (EngineException) runningThread.exception;
            else
                throw new EngineException("An unexpected error has occured while the execution of the requested object '" + getName() + "'.", runningThread.exception);
        outputDocumentRootElement.setAttribute("generated", Calendar.getInstance(Locale.getDefault()).getTime().toString());
        Engine.logContext.debug("(RequestableObject) End of the thread for the requested object '" + getName() + "'");
    /* 
             * If we have a status node , this means that we are in async mode.
             * As the outputDocument has been used to return the status, the outputdocument will be appended to the status.
             * 
             *  In this case, We have to : 
             *  1) delete JOB tags in status
             *  2) add as child node to outputDocumentRootElement the follwing sibling (the real data)
             *  3) replace status with outputDocumentRootElement.
             *
            if (context.isAsync) {
	            NodeList nl = context.outputDocument.getElementsByTagName("status");
	            if (nl.getLength() != 0) {
	            	nl.item(0).removeChild(nl.item(0).getFirstChild()); // remove "job" tag, now firstChild is the real data
	            	Node node = outputDocumentRootElement.getOwnerDocument().importNode(nl.item(0).getFirstChild(), true); 
	            	outputDocumentRootElement.appendChild(node);
	            	node = context.outputDocument.importNode(outputDocumentRootElement, true);
	            	context.outputDocument.removeChild(nl.item(0));
	            	context.outputDocument.appendChild(node);
	            }
            }
            */
    } finally {
        fireRequestableEvent(RequestableObject.EVENT_REQUESTABLE_FINISHED);
        this.context = null;
        this.requester = null;
        this.runningThread = null;
        this.scope = null;
    }
    return context.outputDocument;
}
Also used : Element(org.w3c.dom.Element) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 97 with EngineException

use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.

the class RequestableObject method configure.

@Override
public void configure(Element element) throws Exception {
    super.configure(element);
    String version = element.getAttribute("version");
    if (version == null) {
        String s = XMLUtils.prettyPrintDOM(element);
        EngineException ee = new EngineException("Unable to find version number for the database object \"" + getName() + "\".\nXML data: " + s);
        throw ee;
    }
    try {
        NodeList childNodes = element.getElementsByTagName("wsdltype");
        int len = childNodes.getLength();
        if (len > 0) {
            Node childNode = childNodes.item(0);
            Node cdata = XMLUtils.findChildNode(childNode, Node.CDATA_SECTION_NODE);
            if (cdata != null) {
                wsdlType = cdata.getNodeValue();
                Engine.logBeans.trace("(Requestable) Requestable.configure() : wsdltype has been successfully set");
            } else {
                Engine.logBeans.trace("(Requestable) Requestable.configure() : wsdltype is empty");
            }
        }
    } catch (Exception e) {
        throw new EngineException("Unable to configure the WSDL types of the requestable \"" + getName() + "\".", e);
    }
    try {
        // Convert the publicMethod property to new semantic (accessibility)
        if (VersionUtils.compare(version, "6.1.2") < 0) {
            boolean publicMethod = (Boolean) XMLUtils.findPropertyValue(element, "publicMethod");
            if (publicMethod) {
                setAccessibility(Accessibility.Public);
            } else {
                setAccessibility(Accessibility.Hidden);
            }
            hasChanged = true;
            Engine.logBeans.warn("[RequestableObject] The object \"" + getName() + "\" has been updated to version 6.1.2; publicMethod=" + publicMethod + "; accessibility=" + accessibility);
        }
    } catch (Exception e) {
        throw new EngineException("Unable to migrate the accessibility for requestable \"" + getName() + "\".", e);
    }
    if (VersionUtils.compare(version, "4.6.0") < 0) {
        // Backup wsdlTypes to file
        try {
            backupWsdlTypes(element);
            if (!wsdlType.equals("")) {
                wsdlType = "";
                hasChanged = true;
                Engine.logBeans.warn("[RequestableObject] Successfully backup wsdlTypes for requestable \"" + getName() + "\" (v 4.6.0)");
            } else {
                Engine.logBeans.warn("[RequestableObject] Empty wsdlTypes for requestable \"" + getName() + "\", none backup done (v 4.6.0)");
            }
        } catch (Exception e) {
            Engine.logBeans.error("[RequestableObject] Could not backup wsdlTypes for requestable \"" + getName() + "\" (v 4.6.0)", e);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EngineException(com.twinsoft.convertigo.engine.EngineException) NoSuchElementException(java.util.NoSuchElementException) StepException(com.twinsoft.convertigo.beans.steps.StepException) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 98 with EngineException

use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.

the class RequestableStep method stepExecute.

@Override
protected boolean stepExecute(Context javascriptContext, Scriptable scope) throws EngineException {
    if (isEnabled()) {
        if (super.stepExecute(javascriptContext, scope)) {
            try {
                request = new HashMap<String, Object>();
                try {
                    prepareForRequestable(javascriptContext, scope);
                } catch (Exception e) {
                    Engine.logBeans.error("An error occured while preparing transaction step \"" + RequestableStep.this.getName() + "\"", e);
                    xmlHttpDocument = ConvertigoError.get(e).buildErrorDocument(sequence.getRequester(), sequence.context, false);
                    flushDocument();
                    return true;
                }
                if (bInternalInvoke) {
                    Engine.logBeans.debug("(RequestableStep) Internal invoke requested");
                    InternalRequester internalRequester = new InternalRequester(request, sequence.context.httpServletRequest);
                    LogParameters logParameters = sequence.context.logParameters;
                    Object result = internalRequester.processRequest();
                    // MDC log parameters must return to their original values, because
                    // the internal requester has been executed on the same thread as us.
                    Log4jHelper.mdcSet(logParameters);
                    if (result != null) {
                        xmlHttpDocument = (Document) result;
                        // if (Engine.isStudioMode()) {
                        // ((Sequence)sequence.getOriginal()).fireDataChanged(new SequenceEvent(this, result));
                        // }
                        // else {
                        // sequence.fireDataChanged(new SequenceEvent(this, result));
                        // }
                        ((Sequence) sequence.getOriginal()).fireDataChanged(new SequenceEvent(this, result));
                        flushDocument();
                    }
                } else {
                    Engine.logBeans.debug("(RequestableStep) requesting : " + method.getURI());
                    byte[] result = executeMethod();
                    Engine.logBeans.debug("(RequestableStep) Total read bytes: " + ((result != null) ? result.length : 0));
                    if (result != null) {
                        makeDocument(result);
                        if (Engine.isStudioMode()) {
                            ((Sequence) sequence.getOriginal()).fireDataChanged(new SequenceEvent(this, result));
                        } else {
                            sequence.fireDataChanged(new SequenceEvent(this, result));
                        }
                        flushDocument();
                    }
                }
            } catch (Exception e) {
                setErrorStatus(true);
                Engine.logBeans.error("An error occured while invoking transaction step \"" + RequestableStep.this.getName() + "\"", e);
            } finally {
                if (!bInternalInvoke && (method != null))
                    method.releaseConnection();
            }
            return true;
        }
    }
    return false;
}
Also used : LogParameters(com.twinsoft.convertigo.engine.LogParameters) InternalRequester(com.twinsoft.convertigo.engine.requesters.InternalRequester) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) URIException(org.apache.commons.httpclient.URIException) EngineException(com.twinsoft.convertigo.engine.EngineException) TransformerException(javax.xml.transform.TransformerException) ConnectionException(com.twinsoft.convertigo.beans.connectors.ConnectionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 99 with EngineException

use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.

the class ScreenClass method getOrder.

/**
 * Get representation of order for quick sort of a given database object.
 */
@Override
public Object getOrder(Object object) throws EngineException {
    if (object instanceof Criteria) {
        List<Long> ordered = orderedCriterias.get(0);
        long time = ((Criteria) object).priority;
        if (ordered.contains(time))
            return (long) ordered.indexOf(time);
        else
            throw new EngineException("Corrupted criterias for screenclass \"" + getName() + "\". Criteria \"" + ((Criteria) object).getName() + "\" with priority \"" + time + "\" isn't referenced anymore.");
    } else if (object instanceof ExtractionRule) {
        List<Long> ordered = orderedExtractionRules.get(0);
        long time = ((ExtractionRule) object).priority;
        if (ordered.contains(time))
            return (long) ordered.indexOf(time);
        else
            throw new EngineException("Corrupted extraction rules for screenclass \"" + getName() + "\". Extraction rule \"" + ((ExtractionRule) object).getName() + "\" with priority \"" + time + "\" isn't referenced anymore.");
    } else
        return super.getOrder(object);
}
Also used : EngineException(com.twinsoft.convertigo.engine.EngineException) List(java.util.List) LinkedList(java.util.LinkedList)

Example 100 with EngineException

use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.

the class Sequence method createDOM.

public Document createDOM() throws EngineException {
    Document doc = null;
    try {
        if (requester == null)
            doc = new DefaultRequester().createDomWithNoXMLDeclaration(getEncodingCharSet());
        else
            doc = ((GenericRequester) requester).createDomWithNoXMLDeclaration(getEncodingCharSet());
        Element rootElement = doc.createElement("document");
        doc.appendChild(rootElement);
    } catch (Exception e) {
    }
    return doc;
}
Also used : GenericRequester(com.twinsoft.convertigo.engine.requesters.GenericRequester) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) DefaultRequester(com.twinsoft.convertigo.engine.requesters.DefaultRequester) EngineException(com.twinsoft.convertigo.engine.EngineException)

Aggregations

EngineException (com.twinsoft.convertigo.engine.EngineException)426 IOException (java.io.IOException)155 File (java.io.File)117 Element (org.w3c.dom.Element)84 NodeList (org.w3c.dom.NodeList)64 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)62 Document (org.w3c.dom.Document)43 JSONObject (org.codehaus.jettison.json.JSONObject)41 Node (org.w3c.dom.Node)40 Project (com.twinsoft.convertigo.beans.core.Project)35 ArrayList (java.util.ArrayList)35 JSONException (org.codehaus.jettison.json.JSONException)33 Sequence (com.twinsoft.convertigo.beans.core.Sequence)31 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)29 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)27 DatabaseObjectTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject)25 Connector (com.twinsoft.convertigo.beans.core.Connector)24 HashMap (java.util.HashMap)23 Transaction (com.twinsoft.convertigo.beans.core.Transaction)21 ObjectWithSameNameException (com.twinsoft.convertigo.engine.ObjectWithSameNameException)20