
java.fedora.client.console.ConsoleCommandInvoker Maven / Gradle / Ivy
Show all versions of fcrepo-client Show documentation
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.client.console;
import java.awt.BorderLayout;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
/**
*
* Title: ConsoleCommandInvoker.java
* Description:
*
* @author [email protected]
* @version $Id: ConsoleCommandInvoker.java 5162 2006-10-25 00:49:06Z eddie $
*/
public class ConsoleCommandInvoker
extends JPanel {
private static final long serialVersionUID = 1L;
private ConsoleCommand m_command;
private Console m_console;
private InputPanel[] m_inputPanels;
public ConsoleCommandInvoker(ConsoleCommand command, Console console) {
m_command=command;
m_console=console;
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JLabel commandNameLabel=new JLabel("Command: " + m_command.getName());
JPanel jeez=new JPanel();
jeez.setLayout(new BorderLayout());
jeez.add(commandNameLabel, BorderLayout.WEST);
add(jeez);
Class types[]=command.getParameterTypes();
String names[]=command.getParameterNames();
m_inputPanels=new InputPanel[types.length];
for (int i=0; i0) {
for (int i=0; i");
} else {
m_console.print(stringify(paramValue));
}
m_console.print("\n");
}
}
long startms=new Date().getTime();
Object returned=m_command.invoke(
m_console.getInvocationTarget(m_command), parameters);
long endms=new Date().getTime();
long totalms=endms-startms;
if (returned!=null) {
m_console.print("Returned: " + stringify(returned) + "\n");
} else {
if (m_command.getReturnType()==null) {
m_console.print("Returned.\n");
} else {
m_console.print("Returned: \n");
}
}
String duration;
if (totalms==0) {
duration="< 0.001 seconds.";
} else {
double secs=totalms/1000.0;
duration=secs+" seconds.";
}
m_console.print("Roundtrip time: ");
m_console.print(duration);
m_console.print("\n");
} catch (InvocationTargetException ite) {
m_console.print("ERROR (" + ite.getTargetException().getClass().getName() + ") : "
+ ite.getTargetException().getMessage() + "\n");
} catch (Throwable th) {
m_console.print("ERROR (" + th.getClass().getName() + ") : "
+ th.getMessage() + "\n");
StringWriter sw=new StringWriter();
th.printStackTrace(new PrintWriter(sw));
m_console.print(sw.toString());
} finally {
m_console.setBusy(false);
}
}
private String stringify(Object obj) {
if (obj==null) { return ""; }
String nm=obj.getClass().getName();
if (nm.startsWith("[L")) {
// print array
StringBuffer buf=new StringBuffer();
buf.append("{");
for (int i=0; i0) {
buf.append(",");
}
buf.append(Array.get(obj, i).toString());
}
buf.append("}");
return buf.toString();
}
return obj.toString();
}
}