001/*
002 * Copyright 2011 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014package org.atteo.moonshine.jmx;
015
016import java.util.Map.Entry;
017
018import javax.inject.Inject;
019import javax.management.Attribute;
020import javax.management.AttributeList;
021import javax.management.AttributeNotFoundException;
022import javax.management.DynamicMBean;
023import javax.management.InvalidAttributeValueException;
024import javax.management.MBeanAttributeInfo;
025import javax.management.MBeanException;
026import javax.management.MBeanInfo;
027import javax.management.ReflectionException;
028
029import com.google.inject.Binding;
030import com.google.inject.Injector;
031import com.google.inject.Key;
032
033/**
034 * MBean which lists all registered Guice bindings.
035 */
036// TODO: also show private injectors content
037@MBean
038public class GuiceBindings implements DynamicMBean {
039    private Injector injector;
040
041    @Inject
042    private void setInjector(Injector injector) {
043        // Moonshine injects this service's private injector, we want to show the keys from the global
044        this.injector = injector.getParent();
045    }
046
047    @Override
048    public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException,
049            ReflectionException {
050
051        for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
052            Key<?> key = entry.getKey();
053            Binding<?> binding = entry.getValue();
054
055            if (attribute.equals(key.toString())) {
056                return binding.getProvider().toString() + " at " + binding.getSource().toString();
057            }
058        }
059        return null;
060    }
061
062    @Override
063    public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
064        throw new UnsupportedOperationException("Not supported yet.");
065    }
066
067    @Override
068    public AttributeList getAttributes(String[] attributes) {
069        AttributeList list = new AttributeList(injector.getAllBindings().size());
070
071        for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
072            Key<?> key = entry.getKey();
073
074            list.add(new Attribute(key.toString(), key));
075        }
076
077        return list;
078    }
079
080    @Override
081    public AttributeList setAttributes(AttributeList attributes) {
082        throw new UnsupportedOperationException("Not supported yet.");
083    }
084
085    @Override
086    public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
087        throw new UnsupportedOperationException("Not supported yet.");
088    }
089
090    @Override
091    public MBeanInfo getMBeanInfo() {
092        MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[injector.getAllBindings().size()];
093        int i = 0;
094        for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
095            Key<?> key = entry.getKey();
096            attributes[i] = new MBeanAttributeInfo(key.toString(), "java.lang.String",
097                    "Key " + key.toString(), true, false, false);
098            i++;
099        }
100
101        return new MBeanInfo(GuiceBindings.class.getName(), "Guice Bindings", attributes, null,
102                null, null);
103    }
104
105}