Search in sources :

Example 1 with Reply

use of com.ibm.tools.attach.target.Reply in project openj9 by eclipse.

the class OpenJ9VirtualMachine method tryAttachTarget.

private void tryAttachTarget(int timeout) throws IOException, AttachNotSupportedException {
    Reply replyFile = null;
    AttachHandler.waitForAttachApiInitialization();
    /* ignore result: we can still attach to another target if API is disabled */
    // $NON-NLS-1$
    IPC.logMessage("VirtualMachineImpl.tryAttachtarget");
    Object myIn = AttachHandler.getMainHandler().getIgnoreNotification();
    synchronized (myIn) {
        int numberOfTargets = 0;
        try {
            CommonDirectory.obtainAttachLock();
            List<VirtualMachineDescriptor> vmds = myProvider.listVirtualMachines();
            if (null == vmds) {
                return;
            }
            targetServer = new ServerSocket(0);
            /* select a free port */
            portNumber = Integer.valueOf(targetServer.getLocalPort());
            String key = Integer.toHexString((IPC.getRandomNumber()));
            replyFile = new Reply(portNumber, key, TargetDirectory.getTargetDirectoryPath(descriptor.id()), descriptor.getUid());
            try {
                replyFile.writeReply();
            } catch (IOException e) {
                /*
										 * target shut down while we were trying
										 * to attach
										 */
                /*[MSG "K0457", "Target no longer available"]*/
                // $NON-NLS-1$
                AttachNotSupportedException exc = new AttachNotSupportedException(getString("K0457"));
                exc.initCause(e);
                throw exc;
            }
            if (descriptor.id().equals(AttachHandler.getVmId())) {
                String allowAttachSelf_Value = AttachHandler.allowAttachSelf;
                // $NON-NLS-1$
                boolean selfAttachAllowed = "".equals(allowAttachSelf_Value) || Boolean.parseBoolean(allowAttachSelf_Value);
                if (!selfAttachAllowed) {
                    // $NON-NLS-1$
                    throw new IOException(getString("K0646"));
                }
                /* I am connecting to myself: bypass the notification and launch the attachment thread directly */
                if (AttachHandler.isAttachApiInitialized()) {
                    AttachHandler.getMainHandler().connectToAttacher();
                } else {
                    // $NON-NLS-1$
                    throw new AttachNotSupportedException(getString("K0558"));
                }
            } else {
                lockAllAttachNotificationSyncFiles(vmds);
                numberOfTargets = CommonDirectory.countTargetDirectories();
                int status = CommonDirectory.notifyVm(numberOfTargets);
                /*[MSG "K0532", "status={0}"]*/
                if ((IPC.JNI_OK != status) && (CommonDirectory.J9PORT_INFO_SHSEM_OPENED_STALE != status)) {
                    // $NON-NLS-1$
                    throw new AttachNotSupportedException(getString("K0532", status));
                }
            }
            try {
                // $NON-NLS-1$ //$NON-NLS-2$
                IPC.logMessage("attachTarget " + targetId + " on port " + portNumber);
                targetServer.setSoTimeout(timeout);
                targetSocket = targetServer.accept();
            } catch (SocketTimeoutException e) {
                targetServer.close();
                // $NON-NLS-1$ //$NON-NLS-2$
                IPC.logMessage("attachTarget SocketTimeoutException on " + portNumber + " to " + targetId);
                /*[MSG "K0539","acknowledgement timeout from {0} on port {1}"]*/
                // $NON-NLS-1$
                AttachNotSupportedException exc = new AttachNotSupportedException(getString("K0539", targetId, portNumber));
                exc.initCause(e);
                throw exc;
            }
            commandStream = targetSocket.getOutputStream();
            targetSocket.setSoTimeout(COMMAND_TIMEOUT);
            responseStream = targetSocket.getInputStream();
            /* 
				 * Limit data until the target is verified. 
				 */
            String response = AttachmentConnection.streamReceiveString(responseStream, ATTACH_CONNECTED_MESSAGE_LENGTH_LIMIT);
            /*[MSG "K0533", "key error: {0}"]*/
            if (!response.contains(' ' + key + ' ')) {
                // $NON-NLS-1$
                throw new AttachNotSupportedException(getString("K0533", response));
            }
            // $NON-NLS-1$
            IPC.logMessage("attachTarget connected on ", portNumber.toString());
            targetAttached = true;
        } finally {
            if (null != replyFile) {
                replyFile.deleteReply();
            }
            if (numberOfTargets > 0) {
                /*[PR 48044] if number of targets is 0, then the VM is attaching to itself  and the semaphore was not involved */
                unlockAllAttachNotificationSyncFiles();
                CommonDirectory.cancelNotify(numberOfTargets);
                if (numberOfTargets > 2) {
                    try {
                        int delayTime = 100 * ((numberOfTargets > 10) ? 10 : numberOfTargets);
                        // $NON-NLS-1$
                        IPC.logMessage("attachTarget sleep for ", delayTime);
                        Thread.sleep(delayTime);
                    } catch (InterruptedException e) {
                        // $NON-NLS-1$
                        IPC.logMessage("attachTarget sleep interrupted");
                    }
                }
            }
            CommonDirectory.releaseAttachLock();
        }
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) Reply(com.ibm.tools.attach.target.Reply) ServerSocket(java.net.ServerSocket) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) Msg.getString(com.ibm.oti.util.Msg.getString) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException)

Aggregations

Msg.getString (com.ibm.oti.util.Msg.getString)1 Reply (com.ibm.tools.attach.target.Reply)1 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)1 VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)1 IOException (java.io.IOException)1 ServerSocket (java.net.ServerSocket)1 SocketTimeoutException (java.net.SocketTimeoutException)1