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.camel.management.mbean;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.api.management.ManagedResource;
021import org.apache.camel.api.management.mbean.ManagedStreamCachingStrategyMBean;
022import org.apache.camel.spi.StreamCachingStrategy;
023
024@ManagedResource(description = "Managed StreamCachingStrategy")
025public class ManagedStreamCachingStrategy extends ManagedService implements ManagedStreamCachingStrategyMBean {
026
027    private final CamelContext camelContext;
028    private final StreamCachingStrategy streamCachingStrategy;
029    private final String[] allowClasses;
030    private final String[] denyClasses;
031
032    public ManagedStreamCachingStrategy(CamelContext camelContext, StreamCachingStrategy streamCachingStrategy) {
033        super(camelContext, streamCachingStrategy);
034        this.camelContext = camelContext;
035        this.streamCachingStrategy = streamCachingStrategy;
036        if (streamCachingStrategy.getAllowClasses() != null) {
037            this.allowClasses = streamCachingStrategy.getAllowClasses().toArray(new String[0]);
038        } else {
039            this.allowClasses = null;
040        }
041        if (streamCachingStrategy.getDenyClasses() != null) {
042            this.denyClasses = streamCachingStrategy.getDenyClasses().toArray(new String[0]);
043        } else {
044            this.denyClasses = null;
045        }
046    }
047
048    public CamelContext getCamelContext() {
049        return camelContext;
050    }
051
052    public StreamCachingStrategy getStreamCachingStrategy() {
053        return streamCachingStrategy;
054    }
055
056    @Override
057    public boolean isEnabled() {
058        return streamCachingStrategy.isEnabled();
059    }
060
061    @Override
062    public String[] getAllowClasses() {
063        return allowClasses;
064    }
065
066    @Override
067    public String[] getDenyClasses() {
068        return denyClasses;
069    }
070
071    @Override
072    public boolean isSpoolEnabled() {
073        return streamCachingStrategy.isSpoolEnabled();
074    }
075
076    @Override
077    public String getSpoolDirectory() {
078        if (streamCachingStrategy.getSpoolDirectory() != null) {
079            return streamCachingStrategy.getSpoolDirectory().getPath();
080        } else {
081            return null;
082        }
083    }
084
085    @Override
086    public String getSpoolCipher() {
087        return streamCachingStrategy.getSpoolCipher();
088    }
089
090    @Override
091    public void setSpoolThreshold(long threshold) {
092        streamCachingStrategy.setSpoolThreshold(threshold);
093    }
094
095    @Override
096    public long getSpoolThreshold() {
097        return streamCachingStrategy.getSpoolThreshold();
098    }
099
100    @Override
101    public void setSpoolUsedHeapMemoryThreshold(int percentage) {
102        streamCachingStrategy.setSpoolUsedHeapMemoryThreshold(percentage);
103    }
104
105    @Override
106    public int getSpoolUsedHeapMemoryThreshold() {
107        return streamCachingStrategy.getSpoolUsedHeapMemoryThreshold();
108    }
109
110    @Override
111    public void setSpoolUsedHeapMemoryLimit(SpoolUsedHeapMemoryLimit limit) {
112        StreamCachingStrategy.SpoolUsedHeapMemoryLimit l;
113        if (limit == null) {
114            l = null;
115        } else {
116            switch (limit) {
117                case Committed:
118                    l = StreamCachingStrategy.SpoolUsedHeapMemoryLimit.Committed;
119                    break;
120                case Max:
121                    l = StreamCachingStrategy.SpoolUsedHeapMemoryLimit.Max;
122                    break;
123                default:
124                    throw new IllegalStateException();
125            }
126        }
127        streamCachingStrategy.setSpoolUsedHeapMemoryLimit(l);
128    }
129
130    @Override
131    public SpoolUsedHeapMemoryLimit getSpoolUsedHeapMemoryLimit() {
132        StreamCachingStrategy.SpoolUsedHeapMemoryLimit l = streamCachingStrategy.getSpoolUsedHeapMemoryLimit();
133        if (l == null) {
134            return null;
135        } else {
136            switch (l) {
137                case Committed:
138                    return SpoolUsedHeapMemoryLimit.Committed;
139                case Max:
140                    return SpoolUsedHeapMemoryLimit.Max;
141                default:
142                    throw new IllegalStateException();
143            }
144        }
145    }
146
147    @Override
148    public void setBufferSize(int bufferSize) {
149        streamCachingStrategy.setBufferSize(bufferSize);
150    }
151
152    @Override
153    public int getBufferSize() {
154        return streamCachingStrategy.getBufferSize();
155    }
156
157    @Override
158    public void setRemoveSpoolDirectoryWhenStopping(boolean remove) {
159        streamCachingStrategy.setRemoveSpoolDirectoryWhenStopping(remove);
160    }
161
162    @Override
163    public boolean isRemoveSpoolDirectoryWhenStopping() {
164        return streamCachingStrategy.isRemoveSpoolDirectoryWhenStopping();
165    }
166
167    @Override
168    public void setAnySpoolRules(boolean any) {
169        streamCachingStrategy.setAnySpoolRules(any);
170    }
171
172    @Override
173    public boolean isAnySpoolRules() {
174        return streamCachingStrategy.isAnySpoolRules();
175    }
176
177    @Override
178    public long getCacheMemoryCounter() {
179        return streamCachingStrategy.getStatistics().getCacheMemoryCounter();
180    }
181
182    @Override
183    public long getCacheMemorySize() {
184        return streamCachingStrategy.getStatistics().getCacheMemorySize();
185    }
186
187    @Override
188    public long getCacheMemoryAverageSize() {
189        return streamCachingStrategy.getStatistics().getCacheMemoryAverageSize();
190    }
191
192    @Override
193    public long getCacheSpoolCounter() {
194        return streamCachingStrategy.getStatistics().getCacheSpoolCounter();
195    }
196
197    @Override
198    public long getCacheSpoolSize() {
199        return streamCachingStrategy.getStatistics().getCacheSpoolSize();
200    }
201
202    @Override
203    public long getCacheSpoolAverageSize() {
204        return streamCachingStrategy.getStatistics().getCacheSpoolAverageSize();
205    }
206
207    @Override
208    public boolean isStatisticsEnabled() {
209        return streamCachingStrategy.getStatistics().isStatisticsEnabled();
210    }
211
212    @Override
213    public void setStatisticsEnabled(boolean enabled) {
214        streamCachingStrategy.getStatistics().setStatisticsEnabled(enabled);
215    }
216
217    @Override
218    public void resetStatistics() {
219        streamCachingStrategy.getStatistics().reset();
220    }
221
222}