1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.concurrent.impl.thread; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.geronimo.concurrent.impl.GBeanBuilder; 22 import org.apache.geronimo.gbean.AbstractName; 23 import org.apache.geronimo.gbean.GBeanData; 24 import org.apache.geronimo.gbean.GBeanInfo; 25 import org.apache.geronimo.gbean.GBeanInfoBuilder; 26 import org.apache.geronimo.gbean.GBeanLifecycle; 27 import org.apache.geronimo.kernel.Kernel; 28 import org.apache.geronimo.management.ManagedConstants; 29 30 /** 31 * Deploys ManagedThreadFactoryGBean under the right JMX object name. 32 */ 33 public class ManagedThreadFactoryWrapperGBean 34 extends GBeanBuilder 35 implements GBeanLifecycle, 36 GeronimoManagedThreadFactorySource { 37 38 private final static Log LOG = LogFactory.getLog(ManagedThreadFactoryWrapperGBean.class); 39 40 public static final GBeanInfo GBEAN_INFO; 41 42 private AbstractName name; 43 44 private String groupName; 45 private boolean daemonThread; 46 private int threadPriority; 47 private long hungTaskThreshold; 48 private long hungTaskMonitorFrequency; 49 private String[] contextHandlerClasses; 50 51 private AbstractName threadFactoryName; 52 private ManagedThreadFactoryGBean threadFactoryGBean; 53 54 public ManagedThreadFactoryWrapperGBean(Kernel kernel, 55 ClassLoader classLoader, 56 AbstractName name, 57 String[] contextHandlerClasses, 58 String groupName, 59 int threadPriority, 60 boolean daemonThread, 61 long hungTaskThreshold, 62 long hungTaskMonitorFrequency) { 63 super(kernel, classLoader); 64 this.name = name; 65 66 this.contextHandlerClasses = contextHandlerClasses; 67 this.groupName = groupName; 68 this.daemonThread = daemonThread; 69 this.threadPriority = threadPriority; 70 this.hungTaskThreshold = hungTaskThreshold; 71 this.hungTaskMonitorFrequency = hungTaskMonitorFrequency; 72 } 73 74 public GeronimoManagedThreadFactory getManagedThreadFactory() { 75 return (this.threadFactoryGBean == null) ? null : this.threadFactoryGBean.getManagedThreadFactory(); 76 } 77 78 public void doStart() throws Exception { 79 String threadName = (String)this.name.getName().get("name"); 80 this.threadFactoryName = kernel.getNaming().createRootName(name.getArtifact(), threadName, ManagedConstants.MANAGED_THREAD_FACTORY); 81 GBeanData threadFactoryData = new GBeanData(this.threadFactoryName, ManagedThreadFactoryGBean.getGBeanInfo()); 82 threadFactoryData.setAttribute("contextHandlers", this.contextHandlerClasses); 83 threadFactoryData.setAttribute("groupName", this.groupName); 84 threadFactoryData.setAttribute("priority", this.threadPriority); 85 threadFactoryData.setAttribute("daemon", this.daemonThread); 86 threadFactoryData.setAttribute("hungTaskThreshold", this.hungTaskThreshold); 87 threadFactoryData.setAttribute("hungTaskMonitorFrequency", this.hungTaskMonitorFrequency); 88 89 addGBeanKernel(this.threadFactoryName, threadFactoryData); 90 91 this.threadFactoryGBean = (ManagedThreadFactoryGBean)kernel.getGBean(this.threadFactoryName); 92 this.threadFactoryGBean.verifyObjectName(); 93 } 94 95 public void doFail() { 96 try { 97 doStop(); 98 } catch (Exception e) { 99 // ignore 100 } 101 } 102 103 public void doStop() throws Exception { 104 this.threadFactoryGBean = null; 105 if (this.threadFactoryName != null) { 106 removeGBeanKernel(this.threadFactoryName); 107 } 108 } 109 110 static { 111 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ManagedThreadFactoryWrapperGBean.class, ManagedConstants.MANAGED_THREAD_FACTORY + "Builder"); 112 113 infoFactory.addAttribute("classLoader", ClassLoader.class, false, false); 114 infoFactory.addAttribute("abstractName", AbstractName.class, false, false); 115 infoFactory.addAttribute("kernel", Kernel.class, false, false); 116 117 infoFactory.addAttribute("contextHandlers", String[].class, true, false); 118 119 infoFactory.addAttribute("groupName", String.class, true); 120 infoFactory.addAttribute("priority", int.class, true); 121 infoFactory.addAttribute("daemon", boolean.class, true); 122 infoFactory.addAttribute("hungTaskThreshold", long.class, true); 123 infoFactory.addAttribute("hungTaskMonitorFrequency", long.class, true); 124 125 infoFactory.addInterface(GeronimoManagedThreadFactorySource.class); 126 127 infoFactory.setConstructor(new String[]{"kernel", 128 "classLoader", 129 "abstractName", 130 "contextHandlers", 131 "groupName", 132 "priority", 133 "daemon", 134 "hungTaskThreshold", 135 "hungTaskMonitorFrequency"}); 136 137 GBEAN_INFO = infoFactory.getBeanInfo(); 138 } 139 140 public static GBeanInfo getGBeanInfo() { 141 return GBEAN_INFO; 142 } 143 144 }