BSJavaTemplateProcessorImpl.java :  » UML » MetaBoss » com » metaboss » sdlctools » services » jdktools » impl » Java Open Source

Java Open Source » UML » MetaBoss 
MetaBoss » com » metaboss » sdlctools » services » jdktools » impl » BSJavaTemplateProcessorImpl.java
// THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
// OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Copyright 2000-2005  Softaris Pty.Ltd. All Rights Reserved.
package com.metaboss.sdlctools.services.jdktools.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.metaboss.enterprise.bs.BSException;
import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;
import com.metaboss.javatemplate.JavaTemplate;
import com.metaboss.javatemplate.JavaTemplateContext;
import com.metaboss.javatemplate.JavaTemplateContextMapImpl;
import com.metaboss.javatemplate.JavaTemplateException;
import com.metaboss.sdlctools.services.jdktools.BSJavaCompiler;
import com.metaboss.sdlctools.services.jdktools.BSJavaTemplateProcessor;
import com.metaboss.sdlctools.services.jdktools.CompilationResult;
import com.metaboss.sdlctools.services.jdktools.MergeResult;
import com.metaboss.sdlctools.services.jdktools.SourceType;
import com.metaboss.util.JarClassLoader;

/** This class implements template merging functionality based on Java template */
public class BSJavaTemplateProcessorImpl implements BSJavaTemplateProcessor
{
    /** Thread safe store of template classes used before */
    private Map mClassLoaders = new HashMap(); // Clas loades with already compiled templates


    /* Merges given template with given set of properties. */
    public MergeResult mergeTemplate(String pSourceTemplate, String pSourceTemplateName, String pTemplateJavaClassName, java.util.Map pContextMap) throws BSException
    {
      // Find out if we have already compiled and loaded this template before
    JarClassLoader lClassLoaderForTemplate = (JarClassLoader)mClassLoaders.get(pSourceTemplateName);
        if (lClassLoaderForTemplate == null)
        {
            try
            {
                // Compile and store compiled class in the class loader
                Context ctx = new InitialContext();
                BSJavaCompiler lJavaCompiler = (BSJavaCompiler)ctx.lookup(BSJavaCompiler.COMPONENT_URL);
                CompilationResult lRes = lJavaCompiler.compileSource(pTemplateJavaClassName, pSourceTemplate, SourceType.METABOSS_DEVTIME);
                if (!lRes.isSuccessful())
                    throw new BSException("Java template source compilation error. Template class name : " + pTemplateJavaClassName + ";\r\n" + lRes.getCompilerPrintout());
                // Note. We need to maje sure that MetaBossCore.jar is available, because it has
                // JavaTemplate stuff in it. Strictly speaking it should be done by loading the Jar or
                // better still nominating the URL to it (had JarClassLoader allowed for it to happen)
                // At the moment we have no choice, but create the class loader with the JavaTemplate loader being the parent loader.     
        lClassLoaderForTemplate = new JarClassLoader(JavaTemplate.class.getClassLoader());
        lClassLoaderForTemplate.addJar(lRes.getResultJar());
        mClassLoaders.put(pSourceTemplateName,lClassLoaderForTemplate);    
            }
            catch(IOException e)
            {
                throw new BSException("Java template source processing error. Template class name : " + pTemplateJavaClassName, e);
            }
            catch(NamingException e)
            {
                throw new BSNamingAndDirectoryServiceInvocationException(e);
            }
        }

        try
        {
            JavaTemplate lTemplateInstance = (JavaTemplate)lClassLoaderForTemplate.loadClass(pTemplateJavaClassName).newInstance();
            StringWriter lStringWriter = null;
            PrintWriter lPrintWriter = null;
            try
            {
        JavaTemplateContext lContext = new JavaTemplateContextMapImpl(pContextMap);
                lStringWriter = new StringWriter();
                lPrintWriter = new PrintWriter(lStringWriter);
                lTemplateInstance.mergeTemplate(lPrintWriter,lContext);
                lPrintWriter.flush();
                return MergeResult.createMergeSuccess(lStringWriter.toString());
            }
            catch(JavaTemplateException e)
            {
                return MergeResult.createMergeFailure(e.toString());
            }
            finally
            {
                lPrintWriter.close();
                try
                {
                    lStringWriter.close();
                }
                catch(IOException e)
                {
                    // Ignore
                }
            }
        }
        catch(ClassNotFoundException e)
        {
            throw new BSException("Java template loading error. Template name : " + pSourceTemplateName + ". Template Java class name : " + pTemplateJavaClassName, e);
        }
        catch(IllegalAccessException e)
        {
            throw new BSException("Java template loading error. Template name : " + pSourceTemplateName + ". Template Java class name : " + pTemplateJavaClassName, e);
        }
        catch(InstantiationException e)
        {
            throw new BSException("Java template loading error. Template name : " + pSourceTemplateName + ". Template Java class name : " + pTemplateJavaClassName, e);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.