Search in sources :

Example 1 with HeadlessException

use of java.awt.HeadlessException in project GNS by MobilityFirst.

the class Sudo method main.

/**
   *
   * @param arg
   */
public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();
        Session session = SSHClient.authenticateWithKey(jsch, null, null, null);
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        String command = JOptionPane.showInputDialog("Enter command, execed with sudo", "printenv SUDO_USER");
        String sudo_pass = null;
        {
            JTextField passwordField = new JPasswordField(8);
            Object[] ob = { passwordField };
            int result = JOptionPane.showConfirmDialog(null, ob, "Enter password for sudo", JOptionPane.OK_CANCEL_OPTION);
            if (result != JOptionPane.OK_OPTION) {
                System.exit(-1);
            }
            sudo_pass = passwordField.getText();
        }
        Channel channel = session.openChannel("exec");
        // man sudo
        //   -S  The -S (stdin) option causes sudo to read the password from the
        //       standard input instead of the terminal device.
        //   -p  The -p (prompt) option allows you to override the default
        //       password prompt and use a custom one.
        ((ChannelExec) channel).setCommand("sudo -S -p '' " + command);
        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);
        ((ChannelExec) channel).setPty(true);
        channel.connect();
        out.write((sudo_pass + "\n").getBytes());
        out.flush();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                System.out.print(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
            }
        }
        channel.disconnect();
        session.disconnect();
    } catch (JSchException | HeadlessException | IOException e) {
        System.out.println(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfoPrompted(edu.umass.cs.aws.networktools.UserInfoPrompted) HeadlessException(java.awt.HeadlessException) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) JTextField(javax.swing.JTextField) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) HeadlessException(java.awt.HeadlessException) JSchException(com.jcraft.jsch.JSchException) JPasswordField(javax.swing.JPasswordField) Session(com.jcraft.jsch.Session)

Example 2 with HeadlessException

use of java.awt.HeadlessException in project GNS by MobilityFirst.

the class ThroughputAsynchMultiClientTest method main.

/**
   * The main routine run from the command line.
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    try {
        CommandLine parser = initializeOptions(args);
        if (parser.hasOption("help")) {
            printUsage();
            System.exit(1);
        }
        String alias = parser.getOptionValue("alias");
        String host = parser.getOptionValue("host");
        String port = parser.getOptionValue("port");
        boolean disableSSL = parser.hasOption("disableSSL");
        if (parser.hasOption("op") && ("update".equals(parser.getOptionValue("op")) || "write".equals(parser.getOptionValue("op")))) {
            doingReads = false;
            System.out.println("Testing updates");
        } else {
            doingReads = true;
            System.out.println("Testing reads");
        }
        int rate = (parser.hasOption("rate")) ? Integer.parseInt(parser.getOptionValue("rate")) : 1;
        int increment = (parser.hasOption("inc")) ? Integer.parseInt(parser.getOptionValue("inc")) : 10;
        numberOfClients = (parser.hasOption("clients")) ? Integer.parseInt(parser.getOptionValue("clients")) : DEFAULT_NUMBER_OF_CLIENTS;
        int requestsPerClient = (parser.hasOption("requests")) ? Integer.parseInt(parser.getOptionValue("requests")) : DEFAULT_NUMBER_REQUESTS_PER_CLIENT;
        numberOfGuids = (parser.hasOption("guids")) ? Integer.parseInt(parser.getOptionValue("guids")) : 1;
        updateAlias = parser.hasOption("updateAlias") ? parser.getOptionValue("updateAlias") : null;
        updateField = parser.hasOption("updateField") ? parser.getOptionValue("updateField") : DEFAULT_FIELD;
        updateValue = parser.hasOption("updateValue") ? parser.getOptionValue("updateValue") : DEFAULT_VALUE;
        ThroughputAsynchMultiClientTest test = new ThroughputAsynchMultiClientTest(alias);
        test.createSubGuidsAndWriteValue(parser.hasOption("useExistingGuids"));
        // prebuild a packet for each client
        for (int clientIndex = 0; clientIndex < numberOfClients; clientIndex++) {
            for (int i = 0; i < numberOfGuids; i++) {
                if (doingReads) {
                    commmandPackets[i][clientIndex] = createReadCommandPacket(clients[clientIndex], subGuids[i], updateField, masterGuid);
                } else {
                    JSONObject json = new JSONObject();
                    json.put(updateField, updateValue);
                    commmandPackets[i][clientIndex] = createUpdateCommandPacket(clients[clientIndex], subGuids[i], json, masterGuid);
                }
            }
        }
        if (parser.hasOption("inc")) {
            test.ramp(rate, increment, requestsPerClient);
        } else if (parser.hasOption("rate")) {
            test.ramp(rate, 0, requestsPerClient);
        } else {
            // should really do this earlier
            printUsage();
            System.exit(1);
        }
        // cleanup
        test.removeSubGuid();
        for (int i = 0; i < numberOfClients; i++) {
            clients[i].close();
        }
        System.exit(0);
    } catch (HeadlessException e) {
        System.out.println("When running headless you'll need to specify the host and port on the command line");
        printUsage();
        System.exit(1);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) JSONObject(org.json.JSONObject) HeadlessException(java.awt.HeadlessException) RandomString(edu.umass.cs.gnscommon.utils.RandomString)

Example 3 with HeadlessException

use of java.awt.HeadlessException in project bigbluebutton by bigbluebutton.

the class MouseLocationTaker method getMouseLocation.

private Point getMouseLocation() {
    PointerInfo pInfo;
    Point pointerLocation = new Point(0, 0);
    try {
        pInfo = MouseInfo.getPointerInfo();
    } catch (HeadlessException e) {
        pInfo = null;
    } catch (SecurityException e) {
        pInfo = null;
    }
    if (pInfo == null)
        return pointerLocation;
    return pInfo.getLocation();
}
Also used : PointerInfo(java.awt.PointerInfo) HeadlessException(java.awt.HeadlessException) Point(java.awt.Point)

Example 4 with HeadlessException

use of java.awt.HeadlessException in project jgnash by ccavanaugh.

the class CursorUtils method getCursor.

/**
     * Returns a custom cursor object of the type specified.
     *
     * @param type the type of the custom cursor as defined in this class
     * @return the specified custom cursor
     * @throws IllegalArgumentException if the specified cursor type is
     *                                  invalid
     */
public static Cursor getCursor(int type) {
    if (type < ZOOM_IN || type > ZOOM_OUT) {
        throw new IllegalArgumentException("illegal cursor type");
    }
    if (predefined[type] == null) {
        try {
            // See comment above the static variable.
            final Object[] props = cursorProperties[type];
            final String name = (String) props[0];
            final String resource = (String) props[1];
            final int[] spot = (int[]) props[2];
            final Point point = new Point(spot[0], spot[1]);
            final Toolkit tk = Toolkit.getDefaultToolkit();
            Image image = IconUtils.getImage(resource);
            predefined[type] = tk.createCustomCursor(image, point, name);
        } catch (IndexOutOfBoundsException | HeadlessException e) {
            // this would be an error in the properties
            predefined[type] = Cursor.getDefaultCursor();
            throw new RuntimeException(e);
        }
    }
    return predefined[type];
}
Also used : HeadlessException(java.awt.HeadlessException) Toolkit(java.awt.Toolkit) Point(java.awt.Point) Image(java.awt.Image)

Example 5 with HeadlessException

use of java.awt.HeadlessException in project jdk8u_jdk by JetBrains.

the class WPrinterJob method pageDialog.

/* Instance Methods */
/**
     * Display a dialog to the user allowing the modification of a
     * PageFormat instance.
     * The <code>page</code> argument is used to initialize controls
     * in the page setup dialog.
     * If the user cancels the dialog, then the method returns the
     * original <code>page</code> object unmodified.
     * If the user okays the dialog then the method returns a new
     * PageFormat object with the indicated changes.
     * In either case the original <code>page</code> object will
     * not be modified.
     * @param     page    the default PageFormat presented to the user
     *                    for modification
     * @return    the original <code>page</code> object if the dialog
     *            is cancelled, or a new PageFormat object containing
     *            the format indicated by the user if the dialog is
     *            acknowledged
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
     * returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     * @since     JDK1.2
     */
@Override
public PageFormat pageDialog(PageFormat page) throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    if (!(getPrintService() instanceof Win32PrintService)) {
        return super.pageDialog(page);
    }
    PageFormat pageClone = (PageFormat) page.clone();
    boolean result = false;
    /*
         * Fix for 4507585: show the native modal dialog the same way printDialog() does so
         * that it won't block event dispatching when called on EventDispatchThread.
         */
    WPageDialog dialog = new WPageDialog((Frame) null, this, pageClone, null);
    dialog.setRetVal(false);
    dialog.setVisible(true);
    result = dialog.getRetVal();
    dialog.dispose();
    // myService => current PrintService
    if (result && (myService != null)) {
        // It's possible that current printer is changed through
        // the "Printer..." button so we query again from native.
        String printerName = getNativePrintService();
        if (!myService.getName().equals(printerName)) {
            // we update the current PrintService
            try {
                setPrintService(Win32PrintServiceLookup.getWin32PrintLUS().getPrintServiceByName(printerName));
            } catch (PrinterException e) {
            }
        }
        // Update attributes, this will preserve the page settings.
        //  - same code as in RasterPrinterJob.java
        updatePageAttributes(myService, pageClone);
        return pageClone;
    } else {
        return page;
    }
}
Also used : Win32PrintService(sun.print.Win32PrintService) PageFormat(java.awt.print.PageFormat) HeadlessException(java.awt.HeadlessException) PrinterException(java.awt.print.PrinterException)

Aggregations

HeadlessException (java.awt.HeadlessException)61 File (java.io.File)17 IOException (java.io.IOException)17 Point (java.awt.Point)8 GraphicsConfiguration (java.awt.GraphicsConfiguration)7 BufferedImage (java.awt.image.BufferedImage)7 Dimension (java.awt.Dimension)6 Rectangle (java.awt.Rectangle)6 ActionEvent (java.awt.event.ActionEvent)5 PrintService (javax.print.PrintService)5 StreamPrintService (javax.print.StreamPrintService)5 JFrame (javax.swing.JFrame)5 GraphicsDevice (java.awt.GraphicsDevice)4 GraphicsEnvironment (java.awt.GraphicsEnvironment)4 PrinterException (java.awt.print.PrinterException)4 DialogTypeSelection (javax.print.attribute.standard.DialogTypeSelection)4 ImageIcon (javax.swing.ImageIcon)4 JFileChooser (javax.swing.JFileChooser)4 Frame (java.awt.Frame)3 Graphics (java.awt.Graphics)3