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.wicket.util.instrument;
018
019import java.io.IOException;
020import java.io.ObjectOutputStream;
021import java.io.OutputStream;
022import java.io.Serializable;
023import java.lang.instrument.Instrumentation;
024
025import org.apache.wicket.core.util.lang.WicketObjects.IObjectSizeOfStrategy;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * Object size of strategy that is based on instrumentation.
031 * 
032 * @author eelcohillenius
033 */
034public class InstrumentationObjectSizeOfStrategy implements IObjectSizeOfStrategy
035{
036
037        private static final Logger LOG = LoggerFactory.getLogger(InstrumentationObjectSizeOfStrategy.class);
038
039        /**
040         * Records the size of an object and it's dependents as if they were serialized but using the
041         * instrumentation API to calculate.
042         */
043        private final class SizeRecodingOutputStream extends ObjectOutputStream
044        {
045
046                private long totalSize = 0;
047
048                /**
049                 * Construct.
050                 * 
051                 * @throws IOException
052                 */
053                public SizeRecodingOutputStream() throws IOException
054                {
055                        super(new OutputStream()
056                        {
057
058                                @Override
059                                public void write(int b) throws IOException
060                                {
061                                }
062                        });
063                        enableReplaceObject(true);
064                }
065
066                /**
067                 * Gets the calculated size.
068                 * 
069                 * @return
070                 */
071                public long getTotalSize()
072                {
073                        return totalSize;
074                }
075
076                @Override
077                protected Object replaceObject(Object obj) throws IOException
078                {
079
080                        if (obj != null)
081                        {
082                                totalSize += instrumentation.getObjectSize(obj);
083                        }
084
085                        return obj;
086                }
087        }
088
089        /**
090         * Instrumentation instance.
091         */
092        private final Instrumentation instrumentation;
093
094        /**
095         * Construct.
096         * 
097         * @param instrumentation
098         */
099        public InstrumentationObjectSizeOfStrategy(Instrumentation instrumentation)
100        {
101                this.instrumentation = instrumentation;
102        }
103
104        /**
105         * Calculates full size of object iterating over its hierarchy graph.
106         * 
107         * @param obj
108         *            object to calculate size of
109         * @return object size
110         * 
111         * @see org.apache.wicket.core.util.lang.WicketObjects.IObjectSizeOfStrategy#sizeOf(java.io.Serializable)
112         */
113        @Override
114        public long sizeOf(Serializable obj)
115        {
116                if (obj == null)
117                {
118                        return 0;
119                }
120                try
121                {
122                        SizeRecodingOutputStream recorder = new SizeRecodingOutputStream();
123                        recorder.writeObject(obj);
124                        return recorder.getTotalSize();
125                }
126                catch (IOException e)
127                {
128                        LOG.error("An error occurred while calculating the size of object: " + obj, e);
129                        return -1;
130                }
131
132        }
133}