public static String getBundledPath(String path) { Bundle bundle = Activator.getDefault().getBundle(); if (bundle == null) { LogUtil.info("getBundlePath(" + path + " bundle is null"); return ""; } try { URL location = FileLocator.toFileURL(bundle.getEntry("/")); File file = new File(location.getPath(), path); LogUtil.info("BundledPath: " + file.getAbsolutePath()); return file.getAbsolutePath(); } catch (Exception ex) { LogUtil.error(ex); return ""; } }
public int tryRecall(WareHouse inhouse) { try { if (!checkRecall()) setRecall(true); } catch (RecallException re) { LogUtil.info("[RecallException]", "[tryRecall]" + inhouse, re); return -1; } return 0; }
public boolean releaseLock() { if (!this.exists() || flk == null) return false; try { flk.release(); } catch (Exception e) { LogUtil.info("[ReleaseLock]", "[No effect]", e); return false; } return true; }
public void close() { try { if (fc != null) fc.close(); if (raf != null) raf.close(); closeBuffer(mbread); closeBuffer(mbwrite); } catch (Exception e) { LogUtil.info("[FileAdapter]", "[close]", e.getMessage()); } }
// ---------------- private void initWrite() { try { if (raf == null) { if (!this.exists()) // fl createFile(this.getPath()); // fl raf = new RandomAccessFile(this, "rw"); // fl fc = raf.getChannel(); } } catch (Exception e) { LogUtil.info("[FileAdapter]", "[WriteAdapter]", e.getMessage()); } }
/** @desc 调用set方法 */ public Object getMethodValue(String property) { Object value = null; Method m = getMethods.get(property.toLowerCase()); if (m != null) { try { /** 调用obj类的setter函数 */ value = m.invoke(obj, new Object[] {}); } catch (Exception ex) { LogUtil.info("invoke getter on " + property + " error: " + ex.toString()); } } return value; }
/** @desc 调用set方法 */ public boolean setMethodValue(String property, Object object) { Method m = setMethods.get(property.toLowerCase()); if (m != null) { try { // 调用目标类的setter函数 m.invoke(obj, object); return true; } catch (Exception ex) { LogUtil.info("invoke getter on " + property + " error: " + ex.toString()); return false; } } return false; }
boolean setLock(String rw, long beginIndex, long bytesNum, boolean s) { if (!this.exists()) return false; try { raf = new RandomAccessFile(this, rw); fc = raf.getChannel(); flk = fc.lock(beginIndex, bytesNum, s); } catch (OverlappingFileLockException oe) { LogUtil.warn("[SetLock]", "[No effect]", "the region has already been locked"); return false; } catch (Exception e) { LogUtil.info("[SetLock]", "[No effect]", e); return false; } return true; }
/** * Write a stacktrace to a file * * @param e The exception * @return The file where the crash was saved * @throws IOException If it failed to write the crash */ public File writeError(Exception e) throws IOException { File file; int number = 0; while ((file = new File(dir, "crash-" + number + ".txt")).exists()) number++; LogUtil.info("writing-crash", file.getAbsolutePath()); file.getParentFile().mkdirs(); FileWriter fw = new FileWriter(file); fw.write(makeCrashReport(name, e)); fw.close(); return file; }
public File createFile(String fileUrl, boolean fileflag) { File newFile = new File(fileUrl); try { if (fileflag) { String parentStr = newFile.getParent(); String fileName = newFile.getName(); File newDir = parentStr != null ? createFile(parentStr, false) : new File(""); File theFile = new File(newDir, fileName); theFile.createNewFile(); /*if(!theFile.createNewFile()) System.out.println(fileUrl+" already existed!");*/ } else { if (!newFile.exists()) newFile.mkdirs(); } } catch (Exception e) { LogUtil.info("[FileAdapter]", "[createFile]", e.getMessage() + ":" + fileUrl); } return newFile; }
private boolean initRead(long beginIndex, long bytesNum) { if (beginIndex == Long.MIN_VALUE && bytesNum == Long.MAX_VALUE) return true; try { if (raf == null) { raf = new RandomAccessFile(this, "r"); // fl fc = raf.getChannel(); } long readnum = Math.min(this.length() - beginIndex, bytesNum); // fl // if(readnum==0)return false; mbread = fc.map(MapMode.READ_ONLY, beginIndex, readnum); } catch (Exception e) { // System.out.println(e); LogUtil.info("[FileAdapter]", "[ReadAdapter]", e.getMessage()); return false; } return true; }
public String generate(Object argument) { final StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(TEXT_1); class BasicLogUtil { protected String cid = ""; protected org.talend.core.model.process.INode node = null; protected boolean log4jEnabled = false; private String logID = ""; private BasicLogUtil() {} public BasicLogUtil(org.talend.core.model.process.INode node) { this.node = node; String cidx = this.node.getUniqueName(); if (cidx.matches("^.*?tAmazonAuroraOutput_\\d+_out$")) { cidx = cidx.substring(0, cidx.length() - 4); // 4 ==> "_out".length(); } this.cid = cidx; this.log4jEnabled = ("true") .equals( org.talend.core.model.process.ElementParameterParser.getValue( this.node.getProcess(), "__LOG4J_ACTIVATE__")); this.log4jEnabled = this.log4jEnabled && this.node.getComponent().isLog4JEnabled() && !"JOBLET".equals(node.getComponent().getComponentType().toString()); this.logID = this.cid; } public String var(String varName) { return varName + "_" + this.cid; } public String str(String content) { return "\"" + content + "\""; } public void info(String... message) { log4j("info", message); } public void debug(String... message) { log4j("debug", message); } public void warn(String... message) { log4j("warn", message); } public void error(String... message) { log4j("error", message); } public void fatal(String... message) { log4j("fatal", message); } public void trace(String... message) { log4j("trace", message); } java.util.List<String> checkableList = java.util.Arrays.asList(new String[] {"info", "debug", "trace"}); public void log4j(String level, String... messages) { if (this.log4jEnabled) { if (checkableList.contains(level)) { stringBuffer.append(TEXT_2); stringBuffer.append(level.substring(0, 1).toUpperCase() + level.substring(1)); stringBuffer.append(TEXT_3); } stringBuffer.append(TEXT_4); stringBuffer.append(level); stringBuffer.append(TEXT_5); stringBuffer.append(logID); stringBuffer.append(TEXT_6); for (String message : messages) { stringBuffer.append(TEXT_7); stringBuffer.append(message); stringBuffer.append(TEXT_8); } stringBuffer.append(TEXT_9); } } public boolean isActive() { return this.log4jEnabled; } } class LogUtil extends BasicLogUtil { private LogUtil() {} public LogUtil(org.talend.core.model.process.INode node) { super(node); } public void startWork() { info(str("Start to work.")); } public void endWork() { info(str("Done.")); } public void logIgnoredException(String exception) { warn(exception); } public void logPrintedException(String exception) { error(exception); } public void logException(String exception) { fatal(exception); } public void logCompSetting() { if (log4jEnabled) { stringBuffer.append(TEXT_10); stringBuffer.append(var("log4jParamters")); stringBuffer.append(TEXT_11); stringBuffer.append(TEXT_12); stringBuffer.append(var("log4jParamters")); stringBuffer.append(TEXT_13); java.util.Set<org.talend.core.model.process.EParameterFieldType> ignoredParamsTypes = new java.util.HashSet<org.talend.core.model.process.EParameterFieldType>(); ignoredParamsTypes.addAll( java.util.Arrays.asList( org.talend.core.model.process.EParameterFieldType.SCHEMA_TYPE, org.talend.core.model.process.EParameterFieldType.LABEL, org.talend.core.model.process.EParameterFieldType.EXTERNAL, org.talend.core.model.process.EParameterFieldType.MAPPING_TYPE, org.talend.core.model.process.EParameterFieldType.IMAGE, org.talend.core.model.process.EParameterFieldType.TNS_EDITOR, org.talend.core.model.process.EParameterFieldType.WSDL2JAVA, org.talend.core.model.process.EParameterFieldType.GENERATEGRAMMARCONTROLLER, org.talend.core.model.process.EParameterFieldType .GENERATE_SURVIVORSHIP_RULES_CONTROLLER, org.talend.core.model.process.EParameterFieldType.REFRESH_REPORTS, org.talend.core.model.process.EParameterFieldType.BROWSE_REPORTS, org.talend.core.model.process.EParameterFieldType.PALO_DIM_SELECTION, org.talend.core.model.process.EParameterFieldType.GUESS_SCHEMA, org.talend.core.model.process.EParameterFieldType.MATCH_RULE_IMEX_CONTROLLER, org.talend.core.model.process.EParameterFieldType.MEMO_PERL, org.talend.core.model.process.EParameterFieldType.DBTYPE_LIST, org.talend.core.model.process.EParameterFieldType.VERSION, org.talend.core.model.process.EParameterFieldType.TECHNICAL, org.talend.core.model.process.EParameterFieldType.ICON_SELECTION, org.talend.core.model.process.EParameterFieldType.JAVA_COMMAND, org.talend.core.model.process.EParameterFieldType.TREE_TABLE, org.talend.core.model.process.EParameterFieldType.VALIDATION_RULE_TYPE, org.talend.core.model.process.EParameterFieldType.DCSCHEMA, org.talend.core.model.process.EParameterFieldType.SURVIVOR_RELATION, org.talend.core.model.process.EParameterFieldType.REST_RESPONSE_SCHEMA_TYPE)); for (org.talend.core.model.process.IElementParameter ep : org.talend.core.model.utils.NodeUtil.getDisplayedParameters(node)) { if (!ep.isLog4JEnabled() || ignoredParamsTypes.contains(ep.getFieldType())) { continue; } String name = ep.getName(); if (org.talend.core.model.process.EParameterFieldType.PASSWORD.equals( ep.getFieldType())) { String epName = "__" + name + "__"; String password = ""; if (org.talend.core.model.process.ElementParameterParser.canEncrypt(node, epName)) { password = org.talend.core.model.process.ElementParameterParser.getEncryptedValue( node, epName); } else { String passwordValue = org.talend.core.model.process.ElementParameterParser.getValue(node, epName); if (passwordValue == null || "".equals(passwordValue.trim())) { // for the value which empty passwordValue = "\"\""; } password = "******" + passwordValue + ")"; } stringBuffer.append(TEXT_14); stringBuffer.append(var("log4jParamters")); stringBuffer.append(TEXT_15); stringBuffer.append(name); stringBuffer.append(TEXT_16); stringBuffer.append(password); stringBuffer.append(TEXT_17); } else { String value = org.talend.core.model.utils.NodeUtil.getNormalizeParameterValue(node, ep); stringBuffer.append(TEXT_18); stringBuffer.append(var("log4jParamters")); stringBuffer.append(TEXT_19); stringBuffer.append(name); stringBuffer.append(TEXT_20); stringBuffer.append(value); stringBuffer.append(TEXT_21); } stringBuffer.append(TEXT_22); stringBuffer.append(var("log4jParamters")); stringBuffer.append(TEXT_23); } } debug(var("log4jParamters")); } // no use for now, because we log the data by rowStruct public void traceData( String rowStruct, java.util.List<org.talend.core.model.metadata.IMetadataColumn> columnList, String nbline) { if (log4jEnabled) { stringBuffer.append(TEXT_24); stringBuffer.append(var("log4jSb")); stringBuffer.append(TEXT_25); for (org.talend.core.model.metadata.IMetadataColumn column : columnList) { org.talend.core.model.metadata.types.JavaType javaType = org.talend.core.model.metadata.types.JavaTypesManager.getJavaTypeFromId( column.getTalendType()); String columnName = column.getLabel(); boolean isPrimit = org.talend.core.model.metadata.types.JavaTypesManager.isJavaPrimitiveType( column.getTalendType(), column.isNullable()); if (isPrimit) { stringBuffer.append(TEXT_26); stringBuffer.append(var("log4jSb")); stringBuffer.append(TEXT_27); stringBuffer.append(rowStruct); stringBuffer.append(TEXT_28); stringBuffer.append(columnName); stringBuffer.append(TEXT_29); } else { stringBuffer.append(TEXT_30); stringBuffer.append(rowStruct); stringBuffer.append(TEXT_31); stringBuffer.append(columnName); stringBuffer.append(TEXT_32); stringBuffer.append(TEXT_33); stringBuffer.append(var("log4jSb")); stringBuffer.append(TEXT_34); stringBuffer.append(TEXT_35); stringBuffer.append(var("log4jSb")); stringBuffer.append(TEXT_36); stringBuffer.append(rowStruct); stringBuffer.append(TEXT_37); stringBuffer.append(columnName); stringBuffer.append(TEXT_38); } stringBuffer.append(TEXT_39); stringBuffer.append(var("log4jSb")); stringBuffer.append(TEXT_40); } } trace(str("Content of the record "), nbline, str(": "), var("log4jSb")); } } class LogHelper { java.util.Map<String, String> pastDict = null; public LogHelper() { pastDict = new java.util.HashMap<String, String>(); pastDict.put("insert", "inserted"); pastDict.put("update", "updated"); pastDict.put("delete", "deleted"); pastDict.put("upsert", "upserted"); } public String upperFirstChar(String data) { return data.substring(0, 1).toUpperCase() + data.substring(1); } public String toPastTense(String data) { return pastDict.get(data); } } LogHelper logHelper = new LogHelper(); LogUtil log = null; CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument; INode node = (INode) codeGenArgument.getArgument(); String cid = node.getUniqueName(); log = new LogUtil(node); stringBuffer.append(TEXT_41); stringBuffer.append(cid); stringBuffer.append(TEXT_42); stringBuffer.append(cid); stringBuffer.append(TEXT_43); stringBuffer.append(cid); stringBuffer.append(TEXT_44); stringBuffer.append(cid); stringBuffer.append(TEXT_45); stringBuffer.append(cid); stringBuffer.append(TEXT_46); log.info(log.str("Sent messages count: "), log.var("nbline"), log.str(".")); stringBuffer.append(TEXT_47); stringBuffer.append(TEXT_48); return stringBuffer.toString(); }