View Javadoc

1   /*
2    * $Id: MuleService.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.mbeans;
12  
13  import org.mule.MuleManager;
14  import org.mule.umo.UMOException;
15  import org.mule.util.IOUtils;
16  import org.mule.util.StringMessageUtils;
17  
18  import java.io.IOException;
19  import java.net.InetAddress;
20  import java.net.UnknownHostException;
21  import java.util.Date;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * <code>MuleService</code> exposes certain Mule server functions for management
28   * 
29   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30   * @version $Revision: 7976 $
31   */
32  public class MuleService implements MuleServiceMBean
33  {
34      /**
35       * logger used by this class
36       */
37      protected transient Log logger = LogFactory.getLog(getClass());
38  
39      private String version;
40      private String vendor;
41      private String jdk;
42      private String host;
43      private String ip;
44      private String os;
45      private String buildDate;
46      // TODO
47      private String copyright = "Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com";
48      private String license;
49  
50      public MuleService()
51      {
52          String patch = System.getProperty("sun.os.patch.level", null);
53          jdk = System.getProperty("java.version") + " (" + System.getProperty("java.vm.info") + ")";
54          os = System.getProperty("os.name");
55          if (patch != null && !"unknown".equalsIgnoreCase(patch))
56          {
57              os += " - " + patch;
58          }
59          os += " (" + System.getProperty("os.version") + ", " + System.getProperty("os.arch") + ")";
60  
61          buildDate = MuleManager.getConfiguration().getBuildDate();
62          try
63          {
64              InetAddress iad = InetAddress.getLocalHost();
65              host = iad.getCanonicalHostName();
66              ip = iad.getHostAddress();
67          }
68          catch (UnknownHostException e)
69          {
70              // ignore
71          }
72      }
73  
74      public boolean isInstanciated()
75      {
76          return MuleManager.isInstanciated();
77      }
78  
79      public boolean isInitialised()
80      {
81          return isInstanciated() && MuleManager.getInstance().isInitialised();
82      }
83  
84      public boolean isStopped()
85      {
86          return isInstanciated() && !MuleManager.getInstance().isStarted();
87      }
88  
89      public Date getStartTime()
90      {
91          if (!isStopped())
92          {
93              return new Date(MuleManager.getInstance().getStartDate());
94          }
95          else
96          {
97              return null;
98          }
99      }
100 
101     public String getVersion()
102     {
103         if (version == null)
104         {
105             version = MuleManager.getConfiguration().getProductVersion();
106             if (version == null)
107             {
108                 version = "Mule Version Info Not Set";
109             }
110         }
111         return version;
112     }
113 
114     public String getVendor()
115     {
116         if (vendor == null)
117         {
118             vendor = MuleManager.getConfiguration().getVendorName();
119             if (vendor == null)
120             {
121                 vendor = "Mule Vendor Info Not Set";
122             }
123         }
124         return vendor;
125     }
126 
127     public void start() throws UMOException
128     {
129         MuleManager.getInstance().start();
130     }
131 
132     public void stop() throws UMOException
133     {
134         MuleManager.getInstance().stop();
135     }
136 
137     public void dispose() throws UMOException
138     {
139         MuleManager.getInstance().dispose();
140     }
141 
142     public long getFreeMemory()
143     {
144         return Runtime.getRuntime().freeMemory();
145     }
146 
147     public long getMaxMemory()
148     {
149         return Runtime.getRuntime().maxMemory();
150     }
151 
152     public long getTotalMemory()
153     {
154         return Runtime.getRuntime().totalMemory();
155     }
156 
157     public String getServerId()
158     {
159         return MuleManager.getInstance().getId();
160     }
161 
162     public String getHostname()
163     {
164         return host;
165     }
166 
167     public String getHostIp()
168     {
169         return ip;
170     }
171 
172     public String getOsVersion()
173     {
174         return os;
175     }
176 
177     public String getJdkVersion()
178     {
179         return jdk;
180     }
181 
182     public String getCopyright()
183     {
184         return copyright;
185     }
186 
187     public String getLicense()
188     {
189         if (license == null)
190         {
191             try
192             {
193                 license = IOUtils.getResourceAsString("MULE_LICENSE.txt", getClass());
194                 license = StringMessageUtils.getBoilerPlate(license, ' ', 80);
195             }
196             catch (IOException e)
197             {
198                 logger.warn("Failed to load LICENSE.txt", e);
199             }
200             if (license == null)
201             {
202                 license = "Failed to load license";
203             }
204         }
205         return license;
206     }
207 
208     public String getBuildDate()
209     {
210         return buildDate;
211     }
212 
213     public String getInstanceId()
214     {
215         return MuleManager.getInstance().getId();
216     }
217 }