piątek, stycznia 16, 2015

Make more friendly Tibco BW cmdline Validator

Override class name in validateproject.tra

package com.tibco.ae.tools.designer.cmdline;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import com.tibco.ae.designerapi.AEResource;
import com.tibco.ae.designerapi.DesignerError;
import com.tibco.ae.designerapi.DesignerFolder;
import com.tibco.ae.tools.designer.AEApplication;
import com.tibco.ae.tools.designer.AEDocument;
import com.tibco.ae.tools.designer.actions.ValidateProjectAction;
import com.tibco.ae.tools.vfileresourcestore.FileSystemVFileFactoryResourceStore;
import com.tibco.objectrepo.vfile.VFile;
import com.tibco.util.ListHashMap;
import com.tibco.util.ResourceManager;

public class ValidateBwProject extends DesignerCommandLineApp {

private TempAliases aliases = null;
private int errorCount = 0;
private int warningCount =0;

public void execute(String projectLocation, String aliasFileLocation) throws Exception {
initResources();
initializeApp();
TempAliases temp = new TempAliases() {
public void updateAliases(File file) throws FileNotFoundException, IOException {
int start = "tibco.alias.".length();
Properties p = new Properties();
p.load(new FileInputStream(file));
Properties modified = new Properties();
for (Iterator<Object> i = p.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String value = p.getProperty(key);
if (key.length() != 0 && value.length() != 0) {
if (key.startsWith("tibco.alias."))
key = key.substring(start);
modified.setProperty(key, value);
value = value.replace("\\", "/");
File f = new File(value);
boolean fileOK = false;
try {
f = f.getCanonicalFile();
fileOK = f.exists();
}
catch (Exception exc) {}
if (!fileOK)
System.err.println("The file '"+f.getPath()+"' specified for alias '"+key+"' "+
" was not found.");
}
}
AEApplication.getAEApplication().setFileAliases(modified);
}
};
System.out.println("**** Processing aliases ****");
temp.updateAliases(new File(aliasFileLocation));
aliases = temp;
Properties props = createDocumentPropertiesForFile(projectLocation);
props.put("ae.forimport", "true");
props.put("ae.silent", "true");
System.out.println("**** Creating ActiveEnterprise document (serviceagents below may contain Java Instance instead of WebService with WSDL document) ****");
FileSystemVFileFactoryResourceStore storeFileFactory = new FileSystemVFileFactoryResourceStore(props) {
@Override
public void loadResourceFrom(DesignerFolder parent, VFile file)
throws Exception {
try {
String uri = file.getFullURI();
AEResource res = (AEResource) provider.getObject(uri, AEResource.class, true);
res.initializeNameFrom(file.getName());
parent.addResource(res);
}
catch (Exception e) { /* ignore unknown files */}
}
};
doc = new AEDocument(storeFileFactory, false);
doc.getResourceStore().insureFoldersAreInitialized();
ValidateProjectAction action = new ValidateProjectAction(doc);
System.out.println("**** Validating (you may see output from Java static initializers here) ****");
DesignerError errors[] = action.runValidation(false);

if (errors != null && errors.length > 0) {
printStatistics(errors);

ListHashMap errorTypeToErrorList = new ListHashMap();
int i = 0;
for (int max = errors.length; i < max; i++) {
String type = errors[i].getType();
if (type == null)
type = "ae.error.default.type";
errorTypeToErrorList.put(type, errors[i]);
}

for (@SuppressWarnings("rawtypes") Iterator iter = errorTypeToErrorList.keySet().iterator(); iter.hasNext(); ) {
String type = (String) iter.next();
@SuppressWarnings("unchecked")
List<DesignerError> errList = errorTypeToErrorList.getList(type);
StringBuffer sb = new StringBuffer();

if (!type.equals("ae.error.unused.gvardefn.type")) {
boolean ignoreXpathInGV = type.equals("ae.error.missing.gvardefn.type");
String categName = ResourceManager.manager.getString(type+".displayName");
sb.append("\n**** ["+type+"] "+categName+" ****\n\n");

int hits = 0;
for (int k=0; k < errList.size(); k++) {
DesignerError err = (DesignerError) errList.get(k);
String summ = err.getSummary();
String msg = err.getMessage();

if (ignoreXpathInGV && (msg.contains("[") || msg.contains("*")))
continue;
if (summ.contains("The resource") && summ.contains("is unknown"))
continue; /* Ignore not BW artifact files inside repository */
String uri = err.getAssociatedResource()!=null ? err.getAssociatedResource().getURI() : null;
if (uri!=null && uri.toLowerCase().startsWith("/test"))
continue; /* Ignore not valid unit tests */

sb.append("  "+summ+": "+msg+"\n");
if (uri != null)
sb.append("      <"+uri+">\r\n");
sb.append("\r\n");

if (type.contains("warning"))
warningCount++;
else
errorCount++;
hits++;
}
if (hits>0) {
System.err.println(sb);
}
}
}
if (errorCount==0)
System.out.println(ResourceManager.manager.getString("ae.validate.project.noerrors"));
}
cleanup();
}

@Override
public void cleanup() {
if (aliases != null)
aliases.resetAliases();
if (doc != null) {
try {
doc.close();
}
catch (Exception exp) {}
}
}

@Override
public void checkForErrors() {
if (DesignerError.getErrorCount() != 0) {
DesignerError errs[] = DesignerError.getErrorLog();
int i = 0;
for (int max = errs.length; i < max; i++) {
DesignerError err = errs[i];
System.err.println("[REPO ERROR] "+err.getSummary());
System.err.println((new StringBuilder()).append("\t")
.append(err.getMessage()).toString());
}
cleanup();
System.exit(1);
}
}

private void printStatistics(DesignerError errors[]) {
String s = ResourceManager.manager.getString(
"ae.validate.project.dialog.summary.status.text",
String.valueOf(errorCount), String.valueOf(warningCount));
System.out.println("**** Validation stats *****\n"+s);
}

public final static void main(String[] args) throws Exception {
String projectPath = null;
String aliasPath = null;
String[] cmdline = new String[args.length+3];
for (int k = 0; k < args.length; k++)
cmdline[k] = args[k];
for (int i=0; i < args.length; i++) {
if (args[i].equals("-a")) {
aliasPath = args[i+1];
if (projectPath==null)
projectPath = args[i+2];
}
else
projectPath = args[i];
}
if (projectPath==null || aliasPath==null ||
projectPath.trim().length()==0 || aliasPath.trim().length()==0) {
System.out.println("Usage: projectPath -a aliases.txt");
System.exit(0);
}
new ValidateBwProject().execute(projectPath, aliasPath);
System.exit(0);
}
}

0 komentarze: