| 
001 /*002  * Copyright 2008-2015 the original author or authors.
 003  *
 004  * Licensed under the Apache License, Version 2.0 (the "License");
 005  * you may not use this file except in compliance with the License.
 006  * You may obtain a copy of the License at
 007  *
 008  *     http://www.apache.org/licenses/LICENSE-2.0
 009  *
 010  * Unless required by applicable law or agreed to in writing, software
 011  * distributed under the License is distributed on an "AS IS" BASIS,
 012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 013  * See the License for the specific language governing permissions and
 014  * limitations under the License.
 015  */
 016 package org.codehaus.griffon.runtime.swing;
 017
 018 import griffon.core.CallableWithArgs;
 019 import griffon.core.GriffonApplication;
 020 import griffon.core.GriffonExceptionHandler;
 021 import org.codehaus.griffon.runtime.core.addon.AbstractGriffonAddon;
 022 import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
 023 import org.jdesktop.swinghelper.debug.EventDispatchThreadHangMonitor;
 024 import sun.awt.AppContext;
 025
 026 import javax.annotation.Nonnull;
 027 import javax.annotation.Nullable;
 028 import javax.inject.Named;
 029 import javax.swing.RepaintManager;
 030
 031 /**
 032  * @author Andres Almiray
 033  * @since 2.0.0
 034  */
 035 @Named("swing")
 036 public class SwingAddon extends AbstractGriffonAddon {
 037     private static final String SWING_EDT_VIOLATIONS_KEY = "griffon.swing.edt.violations.check";
 038     private static final String SWING_EDT_HANG_MONITOR_KEY = "griffon.swing.edt.hang.monitor";
 039     private static final String SWING_EDT_HANG_MONITOR_TIMEOUT_KEY = "griffon.swing.edt.hang.monitor.timeout";
 040
 041     private static final String[] EXCLUDED_PACKAGES =
 042         System.getProperty("groovy.sanitized.stacktraces",
 043             "groovy.," +
 044                 "org.codehaus.groovy.," +
 045                 "java.," +
 046                 "javax.," +
 047                 "sun.," +
 048                 "gjdk.groovy.," +
 049                 CheckThreadViolationRepaintManager.class.getPackage().getName()
 050         ).split("(\\s|,)+");
 051
 052     public void init(@Nonnull GriffonApplication application) {
 053         String value = System.getProperty(SWING_EDT_VIOLATIONS_KEY);
 054         if (value != null && Boolean.parseBoolean(value)) {
 055             if (getLog().isInfoEnabled()) {
 056                 getLog().info("EDT violations check enabled.");
 057             }
 058             RepaintManager currentRepaintManager = getCurrentRepaintManager();
 059             if (null == currentRepaintManager) {
 060                 currentRepaintManager = new RepaintManager();
 061             }
 062             if( currentRepaintManager instanceof CheckThreadViolationRepaintManager) {
 063                 return;
 064             }
 065             RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager(currentRepaintManager));
 066
 067             GriffonExceptionHandler.addClassTest(new CallableWithArgs<Boolean>() {
 068                 @Override
 069                 @Nullable
 070                 public Boolean call(@Nullable Object... args) {
 071                     String className = (String) args[0];
 072                     for (String groovyPackage : EXCLUDED_PACKAGES) {
 073                         if (className.startsWith(groovyPackage)) {
 074                             return false;
 075                         }
 076                     }
 077                     return true;
 078                 }
 079             });
 080         }
 081
 082         value = System.getProperty(SWING_EDT_HANG_MONITOR_KEY);
 083         if (value != null && Boolean.parseBoolean(value)) {
 084             if (getLog().isInfoEnabled()) {
 085                 getLog().info("EDT hang monitor enabled.");
 086             }
 087             EventDispatchThreadHangMonitor.initMonitoring();
 088             value = System.getProperty(SWING_EDT_HANG_MONITOR_TIMEOUT_KEY);
 089             if (value != null) {
 090                 try {
 091                     EventDispatchThreadHangMonitor.getInstance().setTimeout(Long.parseLong(value));
 092                 } catch (NumberFormatException e) {
 093                     // ignore
 094                 }
 095             }
 096         }
 097     }
 098
 099     private RepaintManager getCurrentRepaintManager() {
 100         return (RepaintManager) AppContext.getAppContext().get(RepaintManager.class);
 101     }
 102 }
 |