001    /*
002     * Copyright (c) 1998-2014 ChemAxon Ltd. All Rights Reserved.
003     *
004     * This software is the confidential and proprietary information of
005     * ChemAxon. You shall not disclose such Confidential Information
006     * and shall use it only in accordance with the terms of the agreements
007     * you entered into with ChemAxon.
008     *
009     */
010    package com.chemaxon.overlap.concurrent;
011    
012    /**
013     * Management bean implementation.
014     *
015     * <p>
016     * This class implements operations defined in {@link BppManager} interface. Some atomic operations are implemented
017     * using multiple operations on {@link ManagedBpp}. To ensure atomicity this class synchronizes on the exposed
018     * instance.</p>
019     *
020     * @author Gabor Imre
021     */
022    class BppManager implements BppManagerMBean {
023    
024        /**
025         * Exposed processor.
026         */
027        private final ManagedBpp<?, ?> processor;
028    
029        /**
030         * Construct new instance.
031         *
032         * @param processor Processor to expose
033         */
034        public BppManager(ManagedBpp<?, ?> processor) {
035            this.processor = processor;
036        }
037    
038        @Override
039        public int getMaxQueueSize() {
040            return this.processor.getMaxQueueSize();
041        }
042    
043        @Override
044        public int getQueueSize() {
045            return this.processor.getCurrentQueueSize();
046        }
047    
048        @Override
049        public void setMaxQueueSize(int queueSize) {
050            this.processor.setMaxQueueSize(queueSize);
051        }
052    
053        @Override
054        public void setPageSize(int size) {
055            this.processor.setPageSize(size);
056        }
057    
058        @Override
059        public int getPageSize() {
060            return this.processor.getPagesize();
061        }
062    
063        @Override
064        public long getInputCount() {
065            return this.processor.getInputCount();
066        }
067    
068        @Override
069        public long getOutputCount() {
070            return this.processor.getOutputCount();
071        }
072    
073        @Override
074        public int getNonFinishedCount() {
075            return this.processor.getCurrentNonFinishedCount();
076        }
077    
078        @Override
079        public String[] getFirstPageSources() {
080            return this.processor.firstPageToStrings();
081        }
082    
083        @Override
084        public void killAllAndFreeze() {
085            this.processor.kill(false);
086            this.processor.setFrozen(true);
087        }
088    
089        @Override
090        public void unfreeze() {
091            this.processor.setFrozen(false);
092        }
093    
094        @Override
095        public void killFirstPage() {
096            this.processor.kill(true);
097        }
098    
099        @Override
100        public void freeze() {
101            this.processor.setFrozen(true);
102    
103        }
104    
105        @Override
106        public boolean isFrozen() {
107            return this.processor.isFrozen();
108        }
109    
110    
111        @Override
112        public void killFirstPageIfBlocking() {
113            synchronized (this.processor) {
114                if (this.processor.isEmpty()) {
115                    return;
116                }
117                if (this.processor.isFirstPageDone()) {
118                    return;
119                }
120                if (this.processor.getCurrentNonFinishedCount() == 1) {
121                    this.processor.kill(true);
122                }
123            }
124        }
125    
126    }