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 */ 017package org.apache.activemq.usage; 018 019import java.io.File; 020 021import org.apache.activemq.store.PersistenceAdapter; 022import org.apache.activemq.util.StoreUtil; 023 024/** 025 * Used to keep track of how much of something is being used so that a 026 * productive working set usage can be controlled. Main use case is manage 027 * memory usage. 028 * 029 * @org.apache.xbean.XBean 030 * 031 */ 032public class StoreUsage extends PercentLimitUsage<StoreUsage> { 033 034 private PersistenceAdapter store; 035 036 public StoreUsage() { 037 super(null, null, 1.0f); 038 } 039 040 public StoreUsage(String name, PersistenceAdapter store) { 041 super(null, name, 1.0f); 042 this.store = store; 043 updateLimitBasedOnPercent(); 044 } 045 046 public StoreUsage(StoreUsage parent, String name) { 047 super(parent, name, 1.0f); 048 this.store = parent.store; 049 updateLimitBasedOnPercent(); 050 } 051 052 @Override 053 protected long retrieveUsage() { 054 if (store == null) 055 return 0; 056 return store.size(); 057 } 058 059 public PersistenceAdapter getStore() { 060 return store; 061 } 062 063 public void setStore(PersistenceAdapter store) { 064 this.store = store; 065 if (percentLimit > 0 && store != null) { 066 //will trigger onLimitChange 067 updateLimitBasedOnPercent(); 068 } else { 069 onLimitChange(); 070 } 071 } 072 073 @Override 074 public int getPercentUsage() { 075 usageLock.writeLock().lock(); 076 try { 077 percentUsage = caclPercentUsage(); 078 return super.getPercentUsage(); 079 } finally { 080 usageLock.writeLock().unlock(); 081 } 082 } 083 084 @Override 085 public boolean waitForSpace(long timeout, int highWaterMark) throws InterruptedException { 086 if (parent != null) { 087 if (parent.waitForSpace(timeout, highWaterMark)) { 088 return true; 089 } 090 } 091 092 return super.waitForSpace(timeout, highWaterMark); 093 } 094 095 @Override 096 protected void updateLimitBasedOnPercent() { 097 usageLock.writeLock().lock(); 098 try { 099 100 if (percentLimit > 0 && store != null) { 101 File dir = StoreUtil.findParentDirectory(store.getDirectory()); 102 103 if (dir != null) { 104 this.setLimit(dir.getTotalSpace() * percentLimit / 100); 105 } 106 } 107 } finally { 108 usageLock.writeLock().unlock(); 109 } 110 } 111}