日期:2014-05-20  浏览次数:20648 次

优化一段代码,希望高手来解决一下!
Java code

    public final void run() {
        UserContextAssociator.getInstance().associate(userContext);
        final Scope systemInfoUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.INFO);
        final boolean hasInterestInSystemInfoScope = EventMediator.hasInterest(systemInfoUserScope);
        final long starttime = System.currentTimeMillis();
        while (!hasFinished()) {
            final Command current_command = getCommand();
            final Object current_target = getTarget();
            ((ServiceCommand) current_command).setMessage(this);
            final long time = System.currentTimeMillis();
            try {
                current_command.execute(current_target);
                commandSucceeded();
            } catch (final RequestProcessingException e) {
                // TODO this isn't very elegant but Impala requires these error pages NOT to show up in the logs..
                if (!(e.getCause() instanceof ErrorPageException)) {
                    final Throwable detail = e.getDetail();
                    if (detail instanceof AuthenticationExpiredException || detail instanceof UserDisabledException) {
                        final Scope systemDebugUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.DEBUG);
                        if (EventMediator.hasInterest(systemDebugUserScope)) {
                            EventMediator.mediate(EventFactory.createEvent(systemDebugUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Execute command '").append(current_command.getClass().getName()).append("' failed. ").append(e.getMessage()).toString())));
                        }
                    } else {
                        final Scope systemErrorUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.ERROR);
                        if (EventMediator.hasInterest(systemErrorUserScope)) {
                            EventMediator.mediate(EventFactory.createEvent(systemErrorUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Execute command '").append(current_command.getClass().getName()).append("' failed.").toString(), e)));
                        }
                    }
                }
                commandFailed(e);
            } catch (final Throwable e) {
                final Scope systemErrorUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.ERROR);
                if (EventMediator.hasInterest(systemErrorUserScope)) {
                    EventMediator.mediate(EventFactory.createEvent(systemErrorUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Execute command '").append(current_command.getClass().getName()).append("' failed. Uncaught handler exception.").toString(), e)));
                }
                final RequestProcessingException re;
                if (e instanceof Exception) {
                    re = new RequestProcessingException(ErrorCode.SYSTEM, FrameworkErrorDetail.UNCAUGHTHANDLEREXCEPTION, current_target, (Exception) e);
                } else {
                    re = new RequestProcessingException(ErrorCode.SYSTEM, FrameworkErrorDetail.UNCAUGHTHANDLEREXCEPTION, current_target, new Exception(e.getMessage()));
                }
                commandFailed(re);
            } finally {
                if (hasInterestInSystemInfoScope) {
                    EventMediator.mediate(EventFactory.createEvent(systemInfoUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Executed command '").append(current_command.getClass().getName()).append("' on '" + current_target.getClass().getName()).append("', duration: ").append((System.currentTimeMillis() - time)).append("ms.").toString())));
                }
            }
        }
        if (hasInterestInSystemInfoScope) {
            EventMediator.mediate(EventFactory.createEvent(systemInfoUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Processed message '").append(getClass().getName()).append("', duration: ").append(System.currentTimeMillis() - starttime).append("ms.").toString())));
        }
    }