View Javadoc

1   /*
2    * $Id: MuleService.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.module.management.mbean;
12  
13  import org.mule.MuleServer;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleException;
16  import org.mule.config.MuleManifest;
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) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com";
48      private String license;
49  
50      private MuleContext muleContext;
51  
52      public MuleService(MuleContext muleContext)
53      {
54          this.muleContext = muleContext;
55          String patch = System.getProperty("sun.os.patch.level", null);
56          jdk = System.getProperty("java.version") + " (" + System.getProperty("java.vm.info") + ")";
57          os = System.getProperty("os.name");
58          if (patch != null && !"unknown".equalsIgnoreCase(patch))
59          {
60              os += " - " + patch;
61          }
62          os += " (" + System.getProperty("os.version") + ", " + System.getProperty("os.arch") + ")";
63  
64          buildNumber = MuleManifest.getBuildNumber();
65          buildDate = MuleManifest.getBuildDate();
66          try
67          {
68              InetAddress iad = InetAddress.getLocalHost();
69              host = iad.getCanonicalHostName();
70              ip = iad.getHostAddress();
71          }
72          catch (UnknownHostException e)
73          {
74              // ignore
75          }
76      }
77  
78      public boolean isInitialised()
79      {
80          return muleContext!=null && muleContext.isInitialised();
81      }
82  
83      public boolean isStopped()
84      {
85          return muleContext!=null && !muleContext.isStarted();
86      }
87  
88      public Date getStartTime()
89      {
90          if (!isStopped())
91          {
92              return new Date(muleContext.getStartDate());
93          }
94          else
95          {
96              return null;
97          }
98      }
99  
100     public String getVersion()
101     {
102         if (version == null)
103         {
104             version = MuleManifest.getProductVersion();
105             if (version == null)
106             {
107                 version = "Mule Version Info Not Set";
108             }
109         }
110         return version;
111     }
112 
113     public String getVendor()
114     {
115         if (vendor == null)
116         {
117             vendor = MuleManifest.getVendorName();
118             if (vendor == null)
119             {
120                 vendor = "Mule Vendor Info Not Set";
121             }
122         }
123         return vendor;
124     }
125 
126     public void start() throws MuleException
127     {
128         muleContext.start();
129     }
130 
131     public void stop() throws MuleException
132     {
133         muleContext.stop();
134     }
135 
136     public void dispose() throws MuleException
137     {
138         muleContext.dispose();
139     }
140 
141     public long getFreeMemory()
142     {
143         return Runtime.getRuntime().freeMemory();
144     }
145 
146     public long getMaxMemory()
147     {
148         return Runtime.getRuntime().maxMemory();
149     }
150 
151     public long getTotalMemory()
152     {
153         return Runtime.getRuntime().totalMemory();
154     }
155 
156     public String getServerId()
157     {
158         return muleContext.getConfiguration().getId();
159     }
160 
161     public String getHostname()
162     {
163         return host;
164     }
165 
166     public String getHostIp()
167     {
168         return ip;
169     }
170 
171     public String getOsVersion()
172     {
173         return os;
174     }
175 
176     public String getJdkVersion()
177     {
178         return jdk;
179     }
180 
181     public String getCopyright()
182     {
183         return copyright;
184     }
185 
186     public String getLicense()
187     {
188         if (license == null)
189         {
190             loadEnterpriseLicense();
191             if (license == null)
192             {
193                 loadCommunityLicense();
194             }
195             
196             if (license == null)
197             {
198                 license = "Failed to load license";
199             }
200         }
201         return license;
202     }
203     
204     private void loadEnterpriseLicense()
205     {        
206         try
207         {
208             loadLicense("MULE_EE_LICENSE.txt");
209         }
210         catch (IOException e)
211         {
212             // this will happen if running in a CE distribution and is not an error per se
213         }        
214     }
215     
216     private void loadCommunityLicense()
217     {
218         try
219         {
220             loadLicense("MULE_LICENSE.txt");
221         }
222         catch (IOException e)
223         {
224             logger.warn("Failed to load MULE_LICENSE.txt", e);
225         }        
226     }
227 
228     private void loadLicense(String licenseFile) throws IOException
229     {
230         license = IOUtils.getResourceAsString(licenseFile, getClass());
231         license = StringMessageUtils.getBoilerPlate(license, ' ', 80);
232     }
233     
234     /**
235      * @deprecated use getBuildNumber() instead
236      */
237     public String getBuildDate()
238     {
239         return buildDate;
240     }
241 
242     public String getBuildNumber()
243     {
244         return buildNumber;
245     }
246 
247     public String getInstanceId()
248     {
249         return muleContext.getConfiguration().getId();
250     }
251 
252     public String getConfigBuilderClassName()
253     {
254         return MuleServer.getConfigBuilderClassName();
255     }
256 }