001    /*
002    @license.text@
003     */
004    package biz.hammurapi.util;
005    
006    import org.w3c.dom.Element;
007    import org.w3c.dom.Node;
008    
009    import biz.hammurapi.config.Component;
010    import biz.hammurapi.config.ConfigurationException;
011    import biz.hammurapi.config.Context;
012    import biz.hammurapi.config.DomConfigurable;
013    import biz.hammurapi.config.Wrapper;
014    
015    public class ThreadPoolComponentWrapper implements Wrapper, Component, DomConfigurable {
016            private ThreadPool master;
017            private int numberOfThreads=20;
018            private int priority=Thread.NORM_PRIORITY;
019            private int maxQueue=1000;
020            private String exceptionSinkName;
021            private Object owner;
022            private String name;
023            
024            public Object getMaster() {
025                    return master;
026            }
027    
028            public void start() throws ConfigurationException {
029                    ExceptionSink exceptionSink=null;
030                    if (exceptionSinkName!=null) {
031                            if (!(owner instanceof Context)) {
032                                    throw new ConfigurationException("Cannot lookup exception sink - owner doesn't implement Context");
033                            }
034                            
035                            exceptionSink=(ExceptionSink) ((Context) owner).get(exceptionSinkName);
036                            
037                            if (exceptionSink==null) {
038                                    throw new ConfigurationException("Lookup failed for exception sink '"+exceptionSinkName+"'");
039                            }
040                    }
041                    
042                    master=new ThreadPool(numberOfThreads, priority, maxQueue, exceptionSink, name);
043                    master.start();
044            }
045    
046            public void stop() throws ConfigurationException {
047                    if (master!=null) {
048                            master.stop();
049                    }
050            }
051    
052            public void setOwner(Object owner) {
053                    this.owner=owner;
054            }
055    
056            public void configure(Node configNode, Context context) throws ConfigurationException {
057                    Element e=(Element) configNode;
058                    if (e.hasAttribute("threads")) {
059                            numberOfThreads=Integer.parseInt(e.getAttribute("threads"));
060                            if (numberOfThreads<1) {
061                                    throw new ConfigurationException("Number of threads shall be >=1");
062                            }
063                    }
064                    
065                    if (e.hasAttribute("max-queue")) {
066                            maxQueue=Integer.parseInt(e.getAttribute("max-queue"));
067                    }
068                    
069                    if (e.hasAttribute("exception-sink")) {
070                            exceptionSinkName=e.getAttribute("exception-sink");
071                    }
072    
073                    if (e.hasAttribute("pool-name")) {
074                            name=e.getAttribute("pool-name");
075                    }
076                    
077                    if (e.hasAttribute("priority")) {
078                            String pStr=e.getAttribute("priority");
079                            if ("min".equalsIgnoreCase(pStr)) {
080                                    priority=Thread.MIN_PRIORITY;
081                            } else if ("max".equalsIgnoreCase(pStr)) {
082                                    priority=Thread.MAX_PRIORITY;
083                            } else if ("normal".equalsIgnoreCase(pStr)) {
084                                    priority=Thread.NORM_PRIORITY;
085                            } else {
086                                    throw new ConfigurationException("Invalid priority value: "+pStr);
087                            }
088                    }
089            }
090    
091    }