Search in sources :

Example 1 with AcsLogServiceOperations

use of alma.Logging.AcsLogServiceOperations in project ACS by ACS-Community.

the class ClientLogManager method internalInitRemoteLogging.

/**
     * {@link #initRemoteLogging(ORB, Manager, int, boolean)} and {@link #initRemoteLoggingForService(ORB, boolean)}
     * delegate to this method for the initialization of the remote logging.
     * <P>
     * If the passed manager is <code>null</code>, <code>internalInitRemoteLogging</code> gets
     * the log service reference from the naming service; if the manager is not <code>null</code>,
     * it asked the manager for the reference to the log service.
     * 
     * @param orb The not <code>null</code> ORB
     * @param manager the ACS manager (it can be <code>null</code>)
     * @param managerHandle the handle assigned by the ACS Manager for this client;
     *                      ignored if the manager is <code>null</code>
     * @param retry if <code>true</code>, a failing connection to the log service will trigger up to 5 other attempts, every 10 seconds.
     * @return <code>true</code> if remote logging was initialized successfully
     */
private boolean internalInitRemoteLogging(ORB orb, Manager manager, int managerHandle, boolean retry) {
    if (logDispatcher != null) {
        System.out.println("Ignoring call to ClientLogManager#init: already initialized!");
        // todo: or is there a case where we want to retrieve the log service again? 
        return false;
    }
    if (orb == null) {
        System.out.println("Given ORB is null.");
        return false;
    }
    if (manager == null) {
        System.out.println("Will retrieve the log service reference from the naming service...");
    } else if (managerHandle <= 0) {
        System.out.println("can't connect to log service: invalid handle " + managerHandle);
        return false;
    }
    AcsLogServiceOperations logService = null;
    int count = 0;
    String errMsg;
    do {
        errMsg = null;
        count++;
        try {
            // normally there will be a remote handler and log queue already, which has captured all log records produced so far.
            // However, if suppressRemoteLogging was called, we need to set up remote logging from scratch. 
            prepareRemoteLogging();
            if (manager != null) {
                logService = getLogService(manager, managerHandle);
            } else {
                logService = getLogServiceFromNameSarvice(orb);
            }
            if (logService == null) {
                errMsg = "Failed to obtain central log service '" + logServiceName + "': reference is 'null'. ";
            } else {
                // we keep the above get_service call outside this locked section in order to not block shutdown() too long
                logQueueLock.lock();
                //System.out.println("ClientLogManager#initRemoteLogging got the logQueue lock");
                try {
                    if (logQueue == null) {
                        // this can happen if shutdown or suppressRemoteLogging is called concurrently
                        System.out.println("Will abort ClientLogManager#initRemoteLogging because remote logging seems no longer needed.");
                        return false;
                    }
                    if (count > 1) {
                        // all is fine, but we report the difficulty
                        m_internalLogger.info("Connected to central log service after initial failure. ");
                    }
                    // make log service available to our dispatcher, and flush the records collected so far
                    if (LOG_BIN_TYPE) {
                        logDispatcher = new RemoteLogDispatcher(orb, logService, new AcsBinLogFormatter());
                    } else {
                        logDispatcher = new RemoteLogDispatcher(orb, logService, new AcsXMLLogFormatter());
                    }
                    logQueue.setRemoteLogDispatcher(logDispatcher);
                    logQueue.flushAllAndWait();
                    logQueue.setPeriodicFlushing(flushPeriodSeconds * 1000);
                } finally {
                    logQueueLock.unlock();
                //	                	System.out.println("ClientLogManager#initRemoteLogging released the logQueue lock");
                }
            }
        } catch (Throwable thr) {
            errMsg = "Failed to connect to central log service with exception " + thr.toString();
            // as this message must go repeatedly to the command line output regardless of local log level, 
            // we don't want to print the multi-line exception stack, but just the original location.
            StackTraceElement[] trace = thr.getStackTrace();
            if (trace != null && trace.length > 0) {
                StackTraceElement traceOrigin = trace[0];
                errMsg += " in file " + traceOrigin.getFileName() + ", line " + traceOrigin.getLineNumber();
            }
            errMsg += ". ";
        }
        if (errMsg != null) {
            // can't use the loggers, so println is ok here
            if (retry) {
                System.out.println(errMsg + "Will try again in 10 seconds.");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println("Abandoning ClientLogManager#initRemoteLogging retries because of thread interruption.");
                    retry = false;
                }
            } else {
                System.out.println(errMsg);
            }
        }
    } while (retry && count <= 5 && errMsg != null);
    return (errMsg == null);
}
Also used : AcsLogServiceOperations(alma.Logging.AcsLogServiceOperations) AcsXMLLogFormatter(alma.acs.logging.formatters.AcsXMLLogFormatter) AcsBinLogFormatter(alma.acs.logging.formatters.AcsBinLogFormatter)

Aggregations

AcsLogServiceOperations (alma.Logging.AcsLogServiceOperations)1 AcsBinLogFormatter (alma.acs.logging.formatters.AcsBinLogFormatter)1 AcsXMLLogFormatter (alma.acs.logging.formatters.AcsXMLLogFormatter)1