SwingAddon.java
001 /*
002  * Copyright 2008-2016 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
037     SwingAddon extends AbstractGriffonAddon {
038     private static final String SWING_EDT_VIOLATIONS_KEY = "griffon.swing.edt.violations.check";
039     private static final String SWING_EDT_HANG_MONITOR_KEY = "griffon.swing.edt.hang.monitor";
040     private static final String SWING_EDT_HANG_MONITOR_TIMEOUT_KEY = "griffon.swing.edt.hang.monitor.timeout";
041 
042     private static final String[] EXCLUDED_PACKAGES =
043         System.getProperty("groovy.sanitized.stacktraces",
044             "groovy.," +
045                 "org.codehaus.groovy.," +
046                 "java.," +
047                 "javax.," +
048                 "sun.," +
049                 "gjdk.groovy.," +
050                 CheckThreadViolationRepaintManager.class.getPackage().getName()
051         ).split("(\\s|,)+");
052 
053     public void init(@Nonnull GriffonApplication application) {
054         String value = System.getProperty(SWING_EDT_VIOLATIONS_KEY);
055         if (value != null && Boolean.parseBoolean(value)) {
056             if (getLog().isInfoEnabled()) {
057                 getLog().info("EDT violations check enabled.");
058             }
059             RepaintManager currentRepaintManager = getCurrentRepaintManager();
060             if (null == currentRepaintManager) {
061                 currentRepaintManager = new RepaintManager();
062             }
063             ifcurrentRepaintManager instanceof CheckThreadViolationRepaintManager) {
064                 return;
065             }
066             RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager(currentRepaintManager));
067 
068             GriffonExceptionHandler.addClassTest(new CallableWithArgs<Boolean>() {
069                 @Override
070                 @Nullable
071                 public Boolean call(@Nullable Object... args) {
072                     String className = (Stringargs[0];
073                     for (String groovyPackage : EXCLUDED_PACKAGES) {
074                         if (className.startsWith(groovyPackage)) {
075                             return false;
076                         }
077                     }
078                     return true;
079                 }
080             });
081         }
082 
083         value = System.getProperty(SWING_EDT_HANG_MONITOR_KEY);
084         if (value != null && Boolean.parseBoolean(value)) {
085             if (getLog().isInfoEnabled()) {
086                 getLog().info("EDT hang monitor enabled.");
087             }
088             EventDispatchThreadHangMonitor.initMonitoring();
089             value = System.getProperty(SWING_EDT_HANG_MONITOR_TIMEOUT_KEY);
090             if (value != null) {
091                 try {
092                     EventDispatchThreadHangMonitor.getInstance().setTimeout(Long.parseLong(value));
093                 catch (NumberFormatException e) {
094                     // ignore
095                 }
096             }
097         }
098     }
099 
100     private RepaintManager getCurrentRepaintManager() {
101         return (RepaintManagerAppContext.getAppContext().get(RepaintManager.class);
102     }
103 }