001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.usage;
018    
019    import java.util.List;
020    import java.util.concurrent.CopyOnWriteArrayList;
021    import org.apache.activemq.Service;
022    import org.apache.activemq.kaha.Store;
023    import org.apache.activemq.store.PersistenceAdapter;
024    
025    /**
026     * Holder for Usage instances for memory, store and temp files Main use case is
027     * manage memory usage.
028     * 
029     * @org.apache.xbean.XBean
030     * @version $Revision: 1.3 $
031     */
032    public class SystemUsage implements Service {
033    
034        private SystemUsage parent;
035        private String name;
036        private MemoryUsage memoryUsage;
037        private StoreUsage storeUsage;
038        private TempUsage tempUsage;
039    
040        /**
041         * True if someone called setSendFailIfNoSpace() on this particular usage
042         * manager
043         */
044        private boolean sendFailIfNoSpaceExplicitySet;
045        private boolean sendFailIfNoSpace;
046    
047        private boolean sendFailIfNoSpaceAfterTimeoutExplicitySet;
048        private long sendFailIfNoSpaceAfterTimeout = 0;
049        
050        private List<SystemUsage> children = new CopyOnWriteArrayList<SystemUsage>();
051    
052        public SystemUsage() {
053            this("default", null, null);
054        }
055    
056        public SystemUsage(String name, PersistenceAdapter adapter, Store tempStore) {
057            this.parent = null;
058            this.name = name;
059            this.memoryUsage = new MemoryUsage(name + ":memory");
060            this.storeUsage = new StoreUsage(name + ":store", adapter);
061            this.tempUsage = new TempUsage(name + ":temp", tempStore);
062        }
063    
064        public SystemUsage(SystemUsage parent, String name) {
065            this.parent = parent;
066            this.name = name;
067            this.memoryUsage = new MemoryUsage(parent.memoryUsage, name + ":memory");
068            this.storeUsage = new StoreUsage(parent.storeUsage, name + ":store");
069            this.tempUsage = new TempUsage(parent.tempUsage, name + ":temp");
070        }
071    
072        public String getName() {
073            return name;
074        }
075    
076        /**
077         * @return the memoryUsage
078         */
079        public MemoryUsage getMemoryUsage() {
080            return this.memoryUsage;
081        }
082    
083        /**
084         * @return the storeUsage
085         */
086        public StoreUsage getStoreUsage() {
087            return this.storeUsage;
088        }
089    
090        /**
091         * @return the tempDiskUsage
092         */
093        public TempUsage getTempUsage() {
094            return this.tempUsage;
095        }
096    
097        public String toString() {
098            return "UsageManager(" + getName() + ")";
099        }
100    
101        public void start() {
102            if (parent != null) {
103                parent.addChild(this);
104            }
105            this.memoryUsage.start();
106            this.storeUsage.start();
107            this.tempUsage.start();
108        }
109    
110        public void stop() {
111            if (parent != null) {
112                parent.removeChild(this);
113            }
114            this.memoryUsage.stop();
115            this.storeUsage.stop();
116            this.tempUsage.stop();
117        }
118    
119        /**
120         * Sets whether or not a send() should fail if there is no space free. The
121         * default value is false which means to block the send() method until space
122         * becomes available
123         */
124        public void setSendFailIfNoSpace(boolean failProducerIfNoSpace) {
125            sendFailIfNoSpaceExplicitySet = true;
126            this.sendFailIfNoSpace = failProducerIfNoSpace;
127        }
128    
129        public boolean isSendFailIfNoSpace() {
130            if (sendFailIfNoSpaceExplicitySet || parent == null) {
131                return sendFailIfNoSpace;
132            } else {
133                return parent.isSendFailIfNoSpace();
134            }
135        }
136    
137        private void addChild(SystemUsage child) {
138            children.add(child);
139        }
140    
141        private void removeChild(SystemUsage child) {
142            children.remove(child);
143        }
144    
145        public SystemUsage getParent() {
146            return parent;
147        }
148    
149        public void setParent(SystemUsage parent) {
150            this.parent = parent;
151        }
152    
153        public boolean isSendFailIfNoSpaceExplicitySet() {
154            return sendFailIfNoSpaceExplicitySet;
155        }
156    
157        public void setSendFailIfNoSpaceExplicitySet(boolean sendFailIfNoSpaceExplicitySet) {
158            this.sendFailIfNoSpaceExplicitySet = sendFailIfNoSpaceExplicitySet;
159        }
160    
161        public long getSendFailIfNoSpaceAfterTimeout() {
162            if (sendFailIfNoSpaceAfterTimeoutExplicitySet || parent == null) {
163                return sendFailIfNoSpaceAfterTimeout;
164            } else {
165                return parent.getSendFailIfNoSpaceAfterTimeout();
166            }
167        }
168    
169        public void setSendFailIfNoSpaceAfterTimeout(long sendFailIfNoSpaceAfterTimeout) {
170            this.sendFailIfNoSpaceAfterTimeoutExplicitySet = true;
171            this.sendFailIfNoSpaceAfterTimeout = sendFailIfNoSpaceAfterTimeout;
172        }
173    
174        public void setName(String name) {
175            this.name = name;
176            this.memoryUsage.setName(name + ":memory");
177            this.storeUsage.setName(name + ":store");
178            this.tempUsage.setName(name + ":temp");
179        }
180    
181        public void setMemoryUsage(MemoryUsage memoryUsage) {
182            if (memoryUsage.getName() == null) {
183                memoryUsage.setName(this.memoryUsage.getName());
184            }
185            if (parent != null) {
186                memoryUsage.setParent(parent.memoryUsage);
187            }
188            this.memoryUsage = memoryUsage;
189        }
190    
191        public void setStoreUsage(StoreUsage storeUsage) {
192            if (storeUsage.getStore() == null) {
193                storeUsage.setStore(this.storeUsage.getStore());
194            }
195            if (storeUsage.getName() == null) {
196                storeUsage.setName(this.storeUsage.getName());
197            }
198            if (parent != null) {
199                storeUsage.setParent(parent.storeUsage);
200            }
201            this.storeUsage = storeUsage;
202    
203        }
204    
205        public void setTempUsage(TempUsage tempDiskUsage) {
206            if (tempDiskUsage.getStore() == null) {
207                tempDiskUsage.setStore(this.tempUsage.getStore());
208            }
209            if (tempDiskUsage.getName() == null) {
210                tempDiskUsage.setName(this.tempUsage.getName());
211            }
212            if (parent != null) {
213                tempDiskUsage.setParent(parent.tempUsage);
214            }
215            this.tempUsage = tempDiskUsage;
216        }
217    }