KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > main > StartupMonitorUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.main;
18
19 import java.io.PrintStream JavaDoc;
20 import java.net.InetSocketAddress JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.geronimo.gbean.AbstractName;
32 import org.apache.geronimo.gbean.AbstractNameQuery;
33 import org.apache.geronimo.gbean.GAttributeInfo;
34 import org.apache.geronimo.gbean.GBeanData;
35 import org.apache.geronimo.gbean.GBeanInfo;
36 import org.apache.geronimo.kernel.Kernel;
37 import org.apache.geronimo.kernel.management.State;
38
39 /**
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  */

42 public class StartupMonitorUtil {
43     private final static Log log = LogFactory.getLog(StartupMonitor.class.getName());
44
45     public static synchronized void wrapUp(PrintStream JavaDoc out, Kernel kernel) {
46         List JavaDoc apps = new ArrayList JavaDoc(); // type = String (message)
47
List JavaDoc webs = new ArrayList JavaDoc(); // type = WebAppInfo
48
List JavaDoc ports = new ArrayList JavaDoc(); // type = AddressHolder
49
Map JavaDoc failed = new HashMap JavaDoc(); // key = AbstractName, value = String (message)
50
String JavaDoc serverInfo = null;
51         try {
52             Set JavaDoc gbeans = kernel.listGBeans((AbstractNameQuery) null);
53             Map JavaDoc beanInfos = new HashMap JavaDoc(); // key = GBeanInfo, value = List (of attribute names)
54
for (Iterator JavaDoc it = gbeans.iterator(); it.hasNext();) {
55                 AbstractName name = (AbstractName) it.next();
56                 if (isApplicationModule(name)) {
57                     apps.add(" " + decodeModule(name.getNameProperty("j2eeType")) + ": " + name.getNameProperty("name"));
58                 }
59                 if (isWebModule(name)) {
60                     webs.add(kernel.getAttribute(name, "URLFor").toString());
61                 }
62
63                 int stateValue = kernel.getGBeanState(name);
64                 if (stateValue != State.RUNNING_INDEX) {
65                     GBeanData data = kernel.getGBeanData(name);
66                     State state = State.fromInt(stateValue);
67                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
68                     buf.append("(").append(state.getName());
69                     // Since it's not unusual for a failure to be caused by a port binding failure
70
// we'll see if there's a likely looking port attribute in the config data
71
// for the GBean. It's a long shot, but hey.
72
if (data != null && data.getAttributes() != null) {
73                         Map JavaDoc map = data.getAttributes();
74                         for (Iterator JavaDoc it2 = map.keySet().iterator(); it2.hasNext();) {
75                             String JavaDoc att = (String JavaDoc) it2.next();
76                             if (att.equals("port") || att.indexOf("Port") > -1) {
77                                 buf.append(",").append(att).append("=").append(map.get(att));
78                             }
79                         }
80                     }
81                     buf.append(")");
82                     failed.put(name, buf.toString());
83                     continue;
84                 }
85
86                 // Check if this is ServerInfo
87
GBeanInfo info = kernel.getGBeanInfo(name);
88                 if (info.getClassName().equals("org.apache.geronimo.system.serverinfo.ServerInfo")) {
89                     serverInfo = (String JavaDoc) kernel.getAttribute(name, "version");
90                 }
91
92                 // Look for any SocketAddress properties
93
List JavaDoc list = (List JavaDoc) beanInfos.get(info);
94                 if (list == null) {
95                     list = new ArrayList JavaDoc(3);
96                     beanInfos.put(info, list);
97                     Set JavaDoc atts = info.getAttributes();
98                     for (Iterator JavaDoc it2 = atts.iterator(); it2.hasNext();) {
99                         GAttributeInfo att = (GAttributeInfo) it2.next();
100                         if (att.getType().equals("java.net.InetSocketAddress")) {
101                             list.add(att);
102                         }
103                     }
104                 }
105                 for (int i = 0; i < list.size(); i++) {
106                     GAttributeInfo att = (GAttributeInfo) list.get(i);
107                     try {
108                         InetSocketAddress JavaDoc addr = (InetSocketAddress JavaDoc) kernel.getAttribute(name, att.getName());
109                         if (addr == null) {
110                             log.debug("No value for GBean " + name + " attribute " + att.getName());
111                             continue;
112                         } else if (addr.getAddress() == null || addr.getAddress().getHostAddress() == null) {
113                             log.debug("Null address or host for GBean " + name + " " + att.getName() + ": " + addr.getAddress());
114                         }
115                         String JavaDoc attName = info.getName();
116                         if (list.size() > 1) {
117                             attName += " " + decamelize(att.getName());
118                         } else if (info.getAttribute("name") != null) {
119                             attName += " " + kernel.getAttribute(name, "name");
120                         }
121                         ports.add(new AddressHolder(attName, addr));
122                     } catch (IllegalStateException JavaDoc e) {
123                         // We weren't able to load a port for this service -- that's a bummer
124
}
125                 }
126             }
127         } catch (Exception JavaDoc e) {
128             e.printStackTrace();
129         }
130
131         Collections.sort(apps);
132
133         // Helpful output: list of ports we listen on
134
if (ports.size() > 0) {
135             Collections.sort(ports);
136             System.out.println(" Listening on Ports:");
137             int max = 0;
138             for (int i = 0; i < ports.size(); i++) {
139                 AddressHolder holder = (AddressHolder) ports.get(i);
140                 if (holder.getAddress().getAddress() != null && holder.getAddress().getAddress().getHostAddress() != null)
141                 {
142                     max = Math.max(max, holder.getAddress().getAddress().getHostAddress().length());
143                 }
144             }
145             for (int i = 0; i < ports.size(); i++) {
146                 AddressHolder holder = (AddressHolder) ports.get(i);
147                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148                 buf.append(" ");
149                 if (holder.getAddress().getPort() < 10) {
150                     buf.append(' ');
151                 }
152                 if (holder.getAddress().getPort() < 100) {
153                     buf.append(' ');
154                 }
155                 if (holder.getAddress().getPort() < 1000) {
156                     buf.append(' ');
157                 }
158                 if (holder.getAddress().getPort() < 10000) {
159                     buf.append(' ');
160                 }
161                 buf.append(holder.getAddress().getPort()).append(' ');
162                 String JavaDoc address = holder.getAddress().getAddress() == null || holder.getAddress().getAddress().getHostAddress() == null ? "" :
163                         holder.getAddress().getAddress().getHostAddress();
164                 buf.append(address);
165                 for (int j = address.length(); j <= max; j++) {
166                     buf.append(' ');
167                 }
168                 buf.append(holder.getName());
169                 out.println(buf.toString());
170             }
171             out.println();
172         }
173         // Helpful output: list of applications started
174
if (apps.size() > 0) {
175             out.println(" Started Application Modules:");
176             for (int i = 0; i < apps.size(); i++) {
177                 out.println((String JavaDoc) apps.get(i));
178             }
179             out.println();
180         }
181         // Helpful output: Web URLs
182
if (webs.size() > 0) {
183             Collections.sort(webs);
184             out.println(" Web Applications:");
185             for (int i = 0; i < webs.size(); i++) {
186                 out.println(" " + webs.get(i));
187             }
188             out.println();
189         }
190
191         // Helpful output: list of GBeans that did not start
192
if (failed.size() > 0) {
193             out.println(" WARNING: Some GBeans were not started successfully:");
194             for (Iterator JavaDoc it = failed.keySet().iterator(); it.hasNext();) {
195                 AbstractName name = (AbstractName) it.next();
196                 String JavaDoc state = (String JavaDoc) failed.get(name);
197                 if (name.getNameProperty("name") != null) {
198                     log.debug("Unable to start " + name + " " + state);
199                     out.println(" " + name.getNameProperty("name") + " " + state);
200                 } else {
201                     out.println(" " + name + " " + state);
202                 }
203             }
204             out.println();
205         }
206
207         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
208         msg.append("Geronimo Application Server started");
209         if (serverInfo != null) {
210             msg.append(" (version ").append(serverInfo).append(")");
211         }
212         out.println(msg.toString());
213         out.flush();
214     }
215
216     private static boolean isApplicationModule(AbstractName abstractName) {
217         String JavaDoc type = abstractName.getNameProperty("j2eeType");
218         String JavaDoc app = abstractName.getNameProperty("J2EEApplication");
219         String JavaDoc name = abstractName.getNameProperty("name");
220         if (type != null && (app == null || app.equals("null"))) {
221             return (type.equals("WebModule") || type.equals("J2EEApplication") || type.equals("EJBModule") || type.equals("AppClientModule") || type.equals("ResourceAdapterModule")) && !name.startsWith("geronimo/system");
222         }
223         return false;
224     }
225
226     private static boolean isWebModule(AbstractName abstractName) {
227         String JavaDoc type = abstractName.getNameProperty("j2eeType");
228         return type != null && type.equals("WebModule");
229     }
230
231     private static String JavaDoc decodeModule(String JavaDoc value) {
232         if (value.equals("WebModule")) {
233             return "WAR";
234         } else if (value.equals("J2EEApplication")) {
235             return "EAR";
236         } else if (value.equals("EJBModule")) {
237             return "JAR";
238         } else if (value.equals("AppClientModule")) {
239             return "CAR";
240         } else if (value.equals("ResourceAdapterModule")) {
241             return "RAR";
242         } else {
243             return "UNK";
244         }
245     }
246
247     private static String JavaDoc decamelize(String JavaDoc s) {
248         if (s == null || s.equals("")) {
249             return s;
250         }
251         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
252         buf.append(Character.toUpperCase(s.charAt(0)));
253         for (int i = 1; i < s.length(); i++) {
254             if (Character.isUpperCase(s.charAt(i))) {
255                 if (s.length() > i + 1 && Character.isLowerCase(s.charAt(i + 1))) {
256                     buf.append(" ");
257                 }
258             }
259             buf.append(s.charAt(i));
260         }
261         return buf.toString();
262     }
263
264     private static class AddressHolder implements Comparable JavaDoc {
265         private String JavaDoc name;
266         private InetSocketAddress JavaDoc address;
267
268         public AddressHolder(String JavaDoc name, InetSocketAddress JavaDoc address) {
269             this.name = name;
270             this.address = address;
271         }
272
273         public String JavaDoc getName() {
274             return name;
275         }
276
277         public void setName(String JavaDoc name) {
278             this.name = name;
279         }
280
281         public InetSocketAddress JavaDoc getAddress() {
282             return address;
283         }
284
285         public void setAddress(InetSocketAddress JavaDoc address) {
286             this.address = address;
287         }
288
289         public int compareTo(Object JavaDoc o) {
290             AddressHolder other = (AddressHolder) o;
291             int value = address.getPort() - other.address.getPort();
292             return value == 0 ? address.getAddress().toString().compareTo(other.address.getAddress().toString()) : value;
293         }
294     }
295 }
296
Popular Tags