001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.scripting.ruby;
016    
017    import com.liferay.portal.kernel.scripting.ScriptingContainer;
018    import com.liferay.portal.kernel.scripting.ScriptingException;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import org.jruby.RubyArray;
022    import org.jruby.RubyException;
023    import org.jruby.exceptions.RaiseException;
024    import org.jruby.runtime.builtin.IRubyObject;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class RubyScriptingContainer
030            implements ScriptingContainer<org.jruby.embed.ScriptingContainer> {
031    
032            public RubyScriptingContainer(
033                    org.jruby.embed.ScriptingContainer scriptingContainer) {
034    
035                    _scriptingContainer = scriptingContainer;
036            }
037    
038            @Override
039            public <T> T callMethod(
040                            Object scriptObject, String methodName, Object[] arguments,
041                            Class<T> returnClass)
042                    throws ScriptingException {
043    
044                    try {
045                            return _scriptingContainer.callMethod(
046                                    scriptObject, methodName, arguments, returnClass);
047                    }
048                    catch (RaiseException re) {
049                            RubyException rubyException = re.getException();
050    
051                            IRubyObject iRubyObject = rubyException.getBacktrace();
052    
053                            RubyArray rubyArray = (RubyArray)iRubyObject.toJava(
054                                    RubyArray.class);
055    
056                            StringBuilder sb = new StringBuilder(2 * rubyArray.size() + 2);
057    
058                            sb.append(
059                                    String.valueOf(rubyException.message.toJava(String.class)));
060                            sb.append(StringPool.NEW_LINE);
061    
062                            for (int i = 0; i < rubyArray.size(); i++) {
063                                    Object object = rubyArray.get(i);
064    
065                                    sb.append(String.valueOf(object));
066                                    sb.append(StringPool.NEW_LINE);
067                            }
068    
069                            throw new ScriptingException(sb.toString(), re);
070                    }
071            }
072    
073            @Override
074            public void destroy() {
075                    _scriptingContainer.terminate();
076            }
077    
078            @Override
079            public org.jruby.embed.ScriptingContainer getWrappedScriptingContainer() {
080                    return _scriptingContainer;
081            }
082    
083            @Override
084            public Object runScriptlet(String scriptlet) {
085                    return _scriptingContainer.runScriptlet(scriptlet);
086            }
087    
088            @Override
089            public void setCurrentDirName(String currentDirName) {
090                    _scriptingContainer.setCurrentDirectory(currentDirName);
091            }
092    
093            private final org.jruby.embed.ScriptingContainer _scriptingContainer;
094    
095    }