001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.segment.io; 020 021import java.io.IOException; 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.codehaus.groovy.runtime.InvokerHelper; 031import org.nuxeo.common.xmap.annotation.XNode; 032import org.nuxeo.common.xmap.annotation.XNodeList; 033import org.nuxeo.common.xmap.annotation.XNodeMap; 034import org.nuxeo.common.xmap.annotation.XObject; 035 036import groovy.lang.Binding; 037import groovy.lang.GroovyClassLoader; 038import groovy.lang.Script; 039 040@XObject("mapper") 041public class SegmentIOMapper { 042 043 private static Log log = LogFactory.getLog(SegmentIOMapper.class); 044 045 @XNode("@name") 046 String name; 047 048 @XNode("@targetAPI") 049 String target = "track"; 050 051 @XNodeList(value = "events/event", type = ArrayList.class, componentType = String.class) 052 List<String> events; 053 054 @XNode("groovy") 055 String groovyMapping; 056 057 @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class) 058 Map<String, String> parameters = new HashMap<String, String>(); 059 060 @Override 061 public boolean equals(Object obj) { 062 if (obj instanceof SegmentIOMapper) { 063 return name.equals(((SegmentIOMapper) obj).name); 064 } 065 return super.equals(obj); 066 } 067 068 public boolean isIdentify() { 069 return "identify".equalsIgnoreCase(target); 070 } 071 072 public Map<String, Serializable> getMappedData(Map<String, Object> context) { 073 074 Map<String, Serializable> mapping = new HashMap<String, Serializable>(); 075 context.put("mapping", mapping); 076 077 StringBuffer sb = new StringBuffer(); 078 for (String key : parameters.keySet()) { 079 sb.append("mapping.put(\""); 080 sb.append(key); 081 sb.append("\", "); 082 sb.append(parameters.get(key)); 083 sb.append(");\n"); 084 } 085 086 if (groovyMapping != null && !groovyMapping.isEmpty()) { 087 sb.append(groovyMapping); 088 } 089 090 Binding binding = new Binding(context); 091 try (GroovyClassLoader loader = new GroovyClassLoader(this.getClass().getClassLoader())) { 092 Class<?> klass = loader.parseClass(sb.toString()); 093 Script script = InvokerHelper.createScript(klass, binding); 094 script.run(); 095 } catch (IOException e) { 096 log.error(String.format("Error during Groovy script execution for the '%s' segmentIO mapper", name), e); 097 } 098 return mapping; 099 } 100 101 public String getName() { 102 return name; 103 } 104 105 public String getTarget() { 106 return target; 107 } 108 109}