it.openprj.jValidator.spring.EventTriggerValidator.java Source code

Java tutorial

Introduction

Here is the source code for it.openprj.jValidator.spring.EventTriggerValidator.java

Source

/*
 jValidator is a Data Quality middleware for managing data streams and Open Data.
 jValidator is a cutting edge technology Data Quality Firewall, Data QualityMonitor and ETL tool.
 Copyright (C) 2010-2013 OpenPRJ srl
 All rights reserved
    
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.
    
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    
 Go to jValidator site, at http://www.jvalidator.com, for further information.
 Installation, customization, training and support services are available with OpenPRJ srl
 Site: http://www.openprj.it
 Contact:  info@openprj.it
 */

package it.openprj.jValidator.spring;

import it.openprj.jValidator.eventtrigger.DynamicClassLoader;
import it.openprj.jValidator.eventtrigger.EventTrigger;
import it.openprj.jValidator.jpa.ReadList;
import it.openprj.jValidator.jpa.dao.DaoSet;
import it.openprj.jValidator.utils.generic.I18n;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class EventTriggerValidator implements Controller, DaoSet {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String addReq = request.getParameter("addReq");

        String result = null;

        ObjectMapper mapper = new ObjectMapper();
        ServletOutputStream out = null;
        response.setContentType("application/json");
        out = response.getOutputStream();

        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(name)) {
            result = I18n.getMessage("error.trigger.invaliddata");//"Failed. Reason:Invalid Data";
        } else {
            name = name.trim();
            if (addReq.equalsIgnoreCase("true")) {
                ReadList list = eventTriggerDao.findTriggersByName(name);
                if (list != null && CollectionUtils.isNotEmpty(list.getResults())) {
                    result = I18n.getMessage("error.trigger.name.alreadyexist");//"Failed. Reason:Name alredy exist";
                    out.write(mapper.writeValueAsBytes(result));
                    out.flush();
                    out.close();
                    return null;
                }
            }
            try {
                File sourceDir = new File(System.getProperty("java.io.tmpdir"), "jValidator/src");
                sourceDir.mkdirs();
                String classNamePack = name.replace('.', File.separatorChar);
                String srcFilePath = sourceDir + "" + File.separatorChar + classNamePack + ".java";
                File sourceFile = new File(srcFilePath);
                if (sourceFile.exists()) {
                    sourceFile.delete();
                }
                FileUtils.writeStringToFile(new File(srcFilePath), code);
                DynamicClassLoader dynacode = DynamicClassLoader.getInstance();
                dynacode.addSourceDir(sourceDir);
                EventTrigger eventTrigger = (EventTrigger) dynacode.newProxyInstance(EventTrigger.class, name);
                boolean isValid = false;
                if (eventTrigger != null) {
                    Class clazz = dynacode.getLoadedClass(name);
                    if (clazz != null) {
                        Class[] interfaces = clazz.getInterfaces();
                        if (ArrayUtils.isNotEmpty(interfaces)) {
                            for (Class clz : interfaces) {
                                if (clz.getName()
                                        .equalsIgnoreCase("it.openprj.jValidator.eventtrigger.EventTrigger")) {
                                    isValid = true;
                                }
                            }
                        } else if (clazz.getSuperclass() != null && clazz.getSuperclass().getName()
                                .equalsIgnoreCase("it.openprj.jValidator.eventtrigger.EventTriggerImpl")) {
                            isValid = true;
                        }
                    }
                }
                if (isValid) {
                    result = "Success";
                } else {
                    result = I18n.getMessage("error.trigger.wrongimpl");//"Failed. Reason: Custom code should implement it.openprj.jValidator.eventtrigger.EventTrigger Interface";
                }
            } catch (Exception e) {
                result = "Failed. Reason:" + e.getMessage();
            }
        }
        out.write(mapper.writeValueAsBytes(result));
        out.flush();
        out.close();
        return null;
    }
}