001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.q2.qbean;
020
021import org.jpos.q2.Q2;
022import org.jpos.q2.QBeanSupport;
023import org.jpos.space.LocalSpace;
024import org.jpos.space.Space;
025import org.jpos.space.SpaceFactory;
026
027import javax.management.ObjectName;
028import java.util.Set;
029
030/**
031 * Space Adaptor
032 *
033 * @author Alejandro Revilla
034 * @version $Revision$ $Date$
035 */
036
037public class SpaceAdaptor 
038    extends QBeanSupport
039    implements SpaceAdaptorMBean
040{
041    private Space sp = null;
042    private String spaceName = null;
043    private ObjectName objectName = null;
044
045    /** Default constructor. */
046    public SpaceAdaptor () {
047        super ();
048    }
049
050    protected void startService () throws Exception {
051        if (spaceName == null) 
052            sp = SpaceFactory.getSpace ();
053        else 
054            sp = SpaceFactory.getSpace (spaceName);
055
056        objectName = new ObjectName (Q2.QBEAN_NAME + 
057            getName() + ",space=" +
058            (spaceName != null ? spaceName : "default")
059        );
060        getServer().getMBeanServer().registerMBean (sp, objectName);
061    }
062
063    protected void stopService () throws Exception {
064        getServer().getMBeanServer().unregisterMBean (objectName);
065    }
066
067    public synchronized void setSpaceName (String spaceName) {
068        this.spaceName = spaceName;
069        setAttr (getAttrs (), "spaceName", spaceName);
070        setModified (true);
071    }
072
073    public String getSpaceName () {
074        return spaceName;
075    }
076    
077    public Set getKeys () {
078        if (sp instanceof LocalSpace) 
079            return ((LocalSpace)sp).getKeySet ();
080        return null;
081    }
082}
083