View Javadoc

1   /*
2    * $Id: MuleService.java 10003 2007-12-05 18:00:51Z aperepel $
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.MuleServer;
15  import org.mule.config.MuleManifest;
16  import org.mule.umo.UMOException;
17  import org.mule.util.IOUtils;
18  import org.mule.util.StringMessageUtils;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  import java.util.Date;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * <code>MuleService</code> exposes certain Mule server functions for management
30   */
31  public class MuleService implements MuleServiceMBean
32  {
33      /**
34       * logger used by this class
35       */
36      protected transient Log logger = LogFactory.getLog(getClass());
37  
38      private String version;
39      private String vendor;
40      private String jdk;
41      private String host;
42      private String ip;
43      private String os;
44      private String buildNumber;
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  
51      public MuleService()
52      {
53          String patch = System.getProperty("sun.os.patch.level", null);
54          jdk = System.getProperty("java.version") + " (" + System.getProperty("java.vm.info") + ")";
55          os = System.getProperty("os.name");
56          if (patch != null && !"unknown".equalsIgnoreCase(patch))
57          {
58              os += " - " + patch;
59          }
60          os += " (" + System.getProperty("os.version") + ", " + System.getProperty("os.arch") + ")";
61  
62          buildNumber = MuleManifest.getBuildNumber();
63          buildDate = MuleManifest.getBuildDate();
64          try
65          {
66              InetAddress iad = InetAddress.getLocalHost();
67              host = iad.getCanonicalHostName();
68              ip = iad.getHostAddress();
69          }
70          catch (UnknownHostException e)
71          {
72              // ignore
73          }
74      }
75  
76      public boolean isInstanciated()
77      {
78          return MuleManager.isInstanciated();
79      }
80  
81      public boolean isInitialised()
82      {
83          return isInstanciated() && MuleManager.getInstance().isInitialised();
84      }
85  
86      public boolean isStopped()
87      {
88          return isInstanciated() && !MuleManager.getInstance().isStarted();
89      }
90  
91      public Date getStartTime()
92      {
93          if (!isStopped())
94          {
95              return new Date(MuleManager.getInstance().getStartDate());
96          }
97          else
98          {
99              return null;
100         }
101     }
102 
103     public String getVersion()
104     {
105         if (version == null)
106         {
107             version = MuleManifest.getProductVersion();
108             if (version == null)
109             {
110                 version = "Mule Version Info Not Set";
111             }
112         }
113         return version;
114     }
115 
116     public String getVendor()
117     {
118         if (vendor == null)
119         {
120             vendor = MuleManifest.getVendorName();
121             if (vendor == null)
122             {
123                 vendor = "Mule Vendor Info Not Set";
124             }
125         }
126         return vendor;
127     }
128 
129     public void start() throws UMOException
130     {
131         MuleManager.getInstance().start();
132     }
133 
134     public void stop() throws UMOException
135     {
136         MuleManager.getInstance().stop();
137     }
138 
139     public void dispose() throws UMOException
140     {
141         MuleManager.getInstance().dispose();
142     }
143 
144     public long getFreeMemory()
145     {
146         return Runtime.getRuntime().freeMemory();
147     }
148 
149     public long getMaxMemory()
150     {
151         return Runtime.getRuntime().maxMemory();
152     }
153 
154     public long getTotalMemory()
155     {
156         return Runtime.getRuntime().totalMemory();
157     }
158 
159     public String getServerId()
160     {
161         return MuleManager.getInstance().getId();
162     }
163 
164     public String getHostname()
165     {
166         return host;
167     }
168 
169     public String getHostIp()
170     {
171         return ip;
172     }
173 
174     public String getOsVersion()
175     {
176         return os;
177     }
178 
179     public String getJdkVersion()
180     {
181         return jdk;
182     }
183 
184     public String getCopyright()
185     {
186         return copyright;
187     }
188 
189     public String getLicense()
190     {
191         if (license == null)
192         {
193             try
194             {
195                 license = IOUtils.getResourceAsString("MULE_LICENSE.txt", getClass());
196                 license = StringMessageUtils.getBoilerPlate(license, ' ', 80);
197             }
198             catch (IOException e)
199             {
200                 logger.warn("Failed to load MULE_LICENSE.txt", e);
201             }
202             if (license == null)
203             {
204                 license = "Failed to load license";
205             }
206         }
207         return license;
208     }
209 
210     public String getBuildDate()
211     {
212         return buildDate;
213     }
214     
215     public String getBuildNumber()
216     {
217         return buildNumber;
218     }
219 
220     public String getInstanceId()
221     {
222         return MuleManager.getInstance().getId();
223     }
224 
225     public String getConfigBuilderClassName()
226     {
227         return MuleServer.getConfigBuilderClassName();
228     }
229 }