Package org.jboss.mx.remote

Source Code of org.jboss.mx.remote.InstanceID

/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/

package org.jboss.mx.remote;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.*;
import java.rmi.dgc.VMID;

/**
* InstanceID is used to uniquely identify the same MX server between reboots of the
* JVM. If MX is running inside of JBoss, the attribute <tt>ServerDataDir</tt> will be retrieved
* from the MBean, <tt>jboss.system:type=ServerConfig</tt>.  If this MBean doesn't exist, the
* System property <tt>jboss.mx.instanceid.dir</tt> will be used to determine the appropriate
* directory to write the instance id, using the current directory as the default location. <P>
*
* The file, <tt>jboss-mx.instanceid</tt>, will be created in this directory with the value of the
* InstanceID and will be read in the first time the <tt>getID</tt> method is called.  In the event this
* file does not exist, it will be created.  This will allow persistence of the same ID between reboots. <P>
*
* The system property <tt>jboss.mx.instanceid</tt> can also be set to specify the InstanceID.
*
*
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
* @version $Revision: 1.4 $
*/
public class InstanceID
{
    static String id;

    public static final String getID(MBeanServer server)
    {
        if (id==null)
        {
            id = loadID(server);
        }
        return id;
    }
    private synchronized static final String loadID (MBeanServer server)
    {
        String id=null;
        File file=null;
        try
        {
            // FIRST TRY THE JBOSS guy to determine our data directory
            ObjectName obj=new ObjectName("jboss.system:type=ServerConfig");
            File dir = (File)server.getAttribute(obj,"ServerDataDir");
            if (dir!=null)
            {
                if (dir.exists()==false)
                {
                    dir.mkdirs();
                }
                file = new File(dir,"jboss-mx.instanceid");
            }
        }
        catch (Exception ex)
        {
        }
        if (file==null)
        {
           // we can set as a system property
           String myid = System.getProperty("jboss.mx.instanceid");
           if (myid!=null)
           {
               id = myid;
               return id;
           }
           // we may not have that mbean, which is OK
           String fl = System.getProperty("jboss.mx.instanceid.dir",".");
           File dir=new File(fl);
           if (dir.exists()==false)
           {
              dir.mkdirs();
           }
           file = new File(dir,"jboss-mx.instanceid");
        }
        if (file.exists() && file.canRead())
        {
            InputStream is=null;
            try
            {
                is=new FileInputStream(file);
                byte buf[]=new byte[800];
                int c=is.read(buf);
                id = new String(buf,0,c);
            }
            catch (Exception ex)
            {
                throw new RuntimeException("Error loading instanceid: "+ex.toString());
            }
            finally
            {
                if (is!=null)
                {
                    try { is.close(); } catch (Exception ig) { }
                }
            }
        }
        else
        {
            OutputStream out=null;
            try
            {
                id = createID();
                if (file.exists()==false)
                {
                    file.createNewFile();
                }
                out=new FileOutputStream(file);
                out.write(id.getBytes());
            }
            catch (Exception ex)
            {
                throw new RuntimeException("Error creating Instance ID: "+ex.toString());
            }
            finally
            {
                if (out!=null)
                {
                    try { out.flush(); out.close(); } catch (Exception ig) { }
                }
            }
        }
        return id;
    }
    private static final String createID()
    {
        String id = new VMID().toString();
        // colons don't work in JMX
        return id.replace(':','x');
    }
}
TOP

Related Classes of org.jboss.mx.remote.InstanceID

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.