Search in sources :

Example 1 with UndeclaredThrowableException

use of freemarker.template.utility.UndeclaredThrowableException in project freemarker by apache.

the class DebuggerClient method getDebugger.

/**
 * Connects to the FreeMarker debugger service running on a specific host
 * and port. The Java VM to which the connection is made must have defined
 * the system property <tt>freemarker.debug.password</tt> in order to enable
 * the debugger service. Additionally, the <tt>freemarker.debug.port</tt>
 * system property can be set to specify the port where the debugger service
 * is listening. When not specified, it defaults to
 * {@link Debugger#DEFAULT_PORT}.
 * @param host the host address of the machine where the debugger service is
 * running.
 * @param port the port of the debugger service
 * @param password the password required to connect to the debugger service
 * @return Debugger a debugger object. null is returned in case incorrect
 * password was supplied.
 * @throws IOException if an exception occurs.
 */
public static Debugger getDebugger(InetAddress host, int port, String password) throws IOException {
    try {
        Socket s = new Socket(host, port);
        try {
            ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
            ObjectInputStream in = new ObjectInputStream(s.getInputStream());
            int protocolVersion = in.readInt();
            if (protocolVersion > 220) {
                throw new IOException("Incompatible protocol version " + protocolVersion + ". At most 220 was expected.");
            }
            byte[] challenge = (byte[]) in.readObject();
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(password.getBytes("UTF-8"));
            md.update(challenge);
            out.writeObject(md.digest());
            return new LocalDebuggerProxy((Debugger) in.readObject());
        // return (Debugger)in.readObject();
        } finally {
            s.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new UndeclaredThrowableException(e);
    }
}
Also used : UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) MessageDigest(java.security.MessageDigest) Socket(java.net.Socket) IOException(java.io.IOException) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) RemoteException(java.rmi.RemoteException) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with UndeclaredThrowableException

use of freemarker.template.utility.UndeclaredThrowableException in project freemarker by apache.

the class Environment method visit.

void visit(final TemplateElement[] childBuffer, TemplateDirectiveModel directiveModel, Map args, final List bodyParameterNames) throws TemplateException, IOException {
    TemplateDirectiveBody nested;
    if (childBuffer == null) {
        nested = null;
    } else {
        nested = new NestedElementTemplateDirectiveBody(childBuffer);
    }
    final TemplateModel[] outArgs;
    if (bodyParameterNames == null || bodyParameterNames.isEmpty()) {
        outArgs = NO_OUT_ARGS;
    } else {
        outArgs = new TemplateModel[bodyParameterNames.size()];
    }
    if (outArgs.length > 0) {
        pushLocalContext(new LocalContext() {

            public TemplateModel getLocalVariable(String name) {
                int index = bodyParameterNames.indexOf(name);
                return index != -1 ? outArgs[index] : null;
            }

            public Collection getLocalVariableNames() {
                return bodyParameterNames;
            }
        });
    }
    try {
        directiveModel.execute(this, args, outArgs, nested);
    } catch (FlowControlException e) {
        throw e;
    } catch (TemplateException e) {
        throw e;
    } catch (IOException e) {
        // For backward compatibility, we assume that this is because the output Writer has thrown it.
        throw e;
    } catch (Exception e) {
        if (EvalUtil.shouldWrapUncheckedException(e, this)) {
            throw new _MiscTemplateException(e, this, "Directive has thrown an unchecked exception; see the cause exception.");
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new UndeclaredThrowableException(e);
        }
    } finally {
        if (outArgs.length > 0) {
            localContextStack.pop();
        }
    }
}
Also used : TemplateException(freemarker.template.TemplateException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) MalformedTemplateNameException(freemarker.template.MalformedTemplateNameException) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) TemplateDirectiveBody(freemarker.template.TemplateDirectiveBody) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) Collection(java.util.Collection)

Example 3 with UndeclaredThrowableException

use of freemarker.template.utility.UndeclaredThrowableException in project freemarker by apache.

the class Environment method visitAndTransform.

/**
 * "Visit" the template element, passing the output through a TemplateTransformModel
 *
 * @param elementBuffer
 *            the element to visit through a transform; might contains trailing {@code null}-s
 * @param transform
 *            the transform to pass the element output through
 * @param args
 *            optional arguments fed to the transform
 */
void visitAndTransform(TemplateElement[] elementBuffer, TemplateTransformModel transform, Map args) throws TemplateException, IOException {
    try {
        Writer tw = transform.getWriter(out, args);
        if (tw == null)
            tw = EMPTY_BODY_WRITER;
        TransformControl tc = tw instanceof TransformControl ? (TransformControl) tw : null;
        Writer prevOut = out;
        out = tw;
        try {
            if (tc == null || tc.onStart() != TransformControl.SKIP_BODY) {
                do {
                    visit(elementBuffer);
                } while (tc != null && tc.afterBody() == TransformControl.REPEAT_EVALUATION);
            }
        } catch (Throwable t) {
            try {
                if (tc != null && !(t instanceof FlowControlException && getConfiguration().getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_27)) {
                    tc.onError(t);
                } else {
                    throw t;
                }
            } catch (TemplateException e) {
                throw e;
            } catch (IOException e) {
                throw e;
            } catch (Error e) {
                throw e;
            } catch (Throwable e) {
                if (EvalUtil.shouldWrapUncheckedException(e, this)) {
                    throw new _MiscTemplateException(e, this, "Transform has thrown an unchecked exception; see the cause exception.");
                } else if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw new UndeclaredThrowableException(e);
                }
            }
        } finally {
            out = prevOut;
            if (prevOut != tw) {
                tw.close();
            }
        }
    } catch (TemplateException te) {
        handleTemplateException(te);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) TransformControl(freemarker.template.TransformControl) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) NullWriter(freemarker.template.utility.NullWriter) StringWriter(java.io.StringWriter)

Example 4 with UndeclaredThrowableException

use of freemarker.template.utility.UndeclaredThrowableException in project freemarker by apache.

the class SoftCacheStorage method processQueue.

private void processQueue() {
    for (; ; ) {
        SoftValueReference ref = (SoftValueReference) queue.poll();
        if (ref == null) {
            return;
        }
        Object key = ref.getKey();
        if (concurrent) {
            try {
                atomicRemove.invoke(map, new Object[] { key, ref });
            } catch (IllegalAccessException e) {
                throw new UndeclaredThrowableException(e);
            } catch (InvocationTargetException e) {
                throw new UndeclaredThrowableException(e);
            }
        } else if (map.get(key) == ref) {
            map.remove(key);
        }
    }
}
Also used : UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with UndeclaredThrowableException

use of freemarker.template.utility.UndeclaredThrowableException in project freemarker by apache.

the class JaxenXPathSupport method executeQuery.

public TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
    try {
        BaseXPath xpath;
        Map<String, BaseXPath> xpathCache = (Map<String, BaseXPath>) XPATH_CACHE_ATTR.get();
        synchronized (xpathCache) {
            xpath = xpathCache.get(xpathQuery);
            if (xpath == null) {
                xpath = new BaseXPath(xpathQuery, FM_DOM_NAVIGATOR);
                xpath.setNamespaceContext(customNamespaceContext);
                xpath.setFunctionContext(FM_FUNCTION_CONTEXT);
                xpath.setVariableContext(FM_VARIABLE_CONTEXT);
                xpathCache.put(xpathQuery, xpath);
            }
        }
        List result = xpath.selectNodes(context != null ? context : EMPTY_ARRAYLIST);
        if (result.size() == 1) {
            // [2.4] Use the proper object wrapper (argument in 2.4)
            return ObjectWrapper.DEFAULT_WRAPPER.wrap(result.get(0));
        }
        NodeListModel nlm = new NodeListModel(result, null);
        nlm.xpathSupport = this;
        return nlm;
    } catch (UndeclaredThrowableException e) {
        Throwable t = e.getUndeclaredThrowable();
        if (t instanceof TemplateModelException) {
            throw (TemplateModelException) t;
        }
        throw e;
    } catch (JaxenException je) {
        throw new TemplateModelException(je);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BaseXPath(org.jaxen.BaseXPath) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) JaxenException(org.jaxen.JaxenException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UndeclaredThrowableException (freemarker.template.utility.UndeclaredThrowableException)7 IOException (java.io.IOException)4 TemplateException (freemarker.template.TemplateException)3 TemplateModel (freemarker.template.TemplateModel)2 TemplateModelException (freemarker.template.TemplateModelException)2 TransformControl (freemarker.template.TransformControl)2 NullWriter (freemarker.template.utility.NullWriter)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Environment (freemarker.core.Environment)1 MalformedTemplateNameException (freemarker.template.MalformedTemplateNameException)1 TemplateDirectiveBody (freemarker.template.TemplateDirectiveBody)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Socket (java.net.Socket)1 RemoteException (java.rmi.RemoteException)1 MessageDigest (java.security.MessageDigest)1 ArrayList (java.util.ArrayList)1