View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.management.mbean;
8   
9   import org.mule.MuleServer;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleException;
12  import org.mule.config.MuleManifest;
13  import org.mule.util.IOUtils;
14  import org.mule.util.StringMessageUtils;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.UnknownHostException;
19  import java.util.Date;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * <code>MuleService</code> exposes certain Mule server functions for management
26   */
27  public class MuleService implements MuleServiceMBean
28  {
29      /**
30       * logger used by this class
31       */
32      protected transient Log logger = LogFactory.getLog(getClass());
33  
34      private String version;
35      private String vendor;
36      private String jdk;
37      private String host;
38      private String ip;
39      private String os;
40      private String buildNumber;
41      private String buildDate;
42      // TODO
43      private String copyright = "Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com";
44      private String license;
45  
46      private MuleContext muleContext;
47  
48      public MuleService(MuleContext muleContext)
49      {
50          this.muleContext = muleContext;
51          String patch = System.getProperty("sun.os.patch.level", null);
52          jdk = System.getProperty("java.version") + " (" + System.getProperty("java.vm.info") + ")";
53          os = System.getProperty("os.name");
54          if (patch != null && !"unknown".equalsIgnoreCase(patch))
55          {
56              os += " - " + patch;
57          }
58          os += " (" + System.getProperty("os.version") + ", " + System.getProperty("os.arch") + ")";
59  
60          buildNumber = MuleManifest.getBuildNumber();
61          buildDate = MuleManifest.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 isInitialised()
75      {
76          return muleContext!=null && muleContext.isInitialised();
77      }
78  
79      public boolean isStopped()
80      {
81          return muleContext!=null && !muleContext.isStarted();
82      }
83  
84      public Date getStartTime()
85      {
86          if (!isStopped())
87          {
88              return new Date(muleContext.getStartDate());
89          }
90          else
91          {
92              return null;
93          }
94      }
95  
96      public String getVersion()
97      {
98          if (version == null)
99          {
100             version = MuleManifest.getProductVersion();
101             if (version == null)
102             {
103                 version = "Mule Version Info Not Set";
104             }
105         }
106         return version;
107     }
108 
109     public String getVendor()
110     {
111         if (vendor == null)
112         {
113             vendor = MuleManifest.getVendorName();
114             if (vendor == null)
115             {
116                 vendor = "Mule Vendor Info Not Set";
117             }
118         }
119         return vendor;
120     }
121 
122     public void start() throws MuleException
123     {
124         muleContext.start();
125     }
126 
127     public void stop() throws MuleException
128     {
129         muleContext.stop();
130     }
131 
132     public void dispose() throws MuleException
133     {
134         muleContext.dispose();
135     }
136 
137     public long getFreeMemory()
138     {
139         return Runtime.getRuntime().freeMemory();
140     }
141 
142     public long getMaxMemory()
143     {
144         return Runtime.getRuntime().maxMemory();
145     }
146 
147     public long getTotalMemory()
148     {
149         return Runtime.getRuntime().totalMemory();
150     }
151 
152     public String getServerId()
153     {
154         return muleContext.getConfiguration().getId();
155     }
156 
157     public String getHostname()
158     {
159         return host;
160     }
161 
162     public String getHostIp()
163     {
164         return ip;
165     }
166 
167     public String getOsVersion()
168     {
169         return os;
170     }
171 
172     public String getJdkVersion()
173     {
174         return jdk;
175     }
176 
177     public String getCopyright()
178     {
179         return copyright;
180     }
181 
182     public String getLicense()
183     {
184         if (license == null)
185         {
186             loadEnterpriseLicense();
187             if (license == null)
188             {
189                 loadCommunityLicense();
190             }
191             
192             if (license == null)
193             {
194                 license = "Failed to load license";
195             }
196         }
197         return license;
198     }
199     
200     private void loadEnterpriseLicense()
201     {        
202         try
203         {
204             loadLicense("MULE_EE_LICENSE.txt");
205         }
206         catch (IOException e)
207         {
208             // this will happen if running in a CE distribution and is not an error per se
209         }        
210     }
211     
212     private void loadCommunityLicense()
213     {
214         try
215         {
216             loadLicense("MULE_LICENSE.txt");
217         }
218         catch (IOException e)
219         {
220             logger.warn("Failed to load MULE_LICENSE.txt", e);
221         }        
222     }
223 
224     private void loadLicense(String licenseFile) throws IOException
225     {
226         license = IOUtils.getResourceAsString(licenseFile, getClass());
227         license = StringMessageUtils.getBoilerPlate(license, ' ', 80);
228     }
229     
230     /**
231      * @deprecated use getBuildNumber() instead
232      */
233     public String getBuildDate()
234     {
235         return buildDate;
236     }
237 
238     public String getBuildNumber()
239     {
240         return buildNumber;
241     }
242 
243     public String getInstanceId()
244     {
245         return muleContext.getConfiguration().getId();
246     }
247 
248     public String getConfigBuilderClassName()
249     {
250         return MuleServer.getConfigBuilderClassName();
251     }
252 }